Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e7920e1

Browse files
authoredMar 12, 2025
Per-host speed limit (#1598)
1 parent 557c5aa commit e7920e1

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed
 

‎artemis/config.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import os
2-
from typing import Annotated, Any, List, get_type_hints
2+
from typing import Annotated, Any, List, Optional, get_type_hints
33

44
import decouple
55

@@ -166,6 +166,11 @@ class Limits:
166166
"Default request timeout (for all protocols).",
167167
] = get_config("REQUEST_TIMEOUT_SECONDS", default=5, cast=int)
168168

169+
SCAN_SPEED_OVERRIDES_FILE: Annotated[
170+
Optional[str],
171+
"A JSON file with a dictionary mapping from IP to scan speed - use if you want to slow down scanning of particular hosts.",
172+
] = get_config("SCAN_SPEED_OVERRIDES_FILE", default="", cast=str)
173+
169174
REQUESTS_PER_SECOND: Annotated[
170175
float,
171176
"""

‎artemis/module_base.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
22
import fcntl
3+
import json
34
import logging
45
import random
56
import shutil
@@ -8,7 +9,7 @@
89
import traceback
910
import urllib.parse
1011
from ipaddress import ip_address
11-
from typing import Any, Callable, List, Optional, Tuple
12+
from typing import Any, Callable, Dict, List, Optional, Tuple
1213

1314
import timeout_decorator
1415
from karton.core import Karton, Task
@@ -89,6 +90,12 @@ def __init__(self, db: Optional[DB] = None, *args, **kwargs) -> None: # type: i
8990
hosts_file.write(additional_data_file.read())
9091
fcntl.flock(hosts_file.fileno(), fcntl.LOCK_UN)
9192

93+
if Config.Limits.SCAN_SPEED_OVERRIDES_FILE:
94+
with open(Config.Limits.SCAN_SPEED_OVERRIDES_FILE, "r") as f:
95+
self._scan_speed_overrides: Dict[str, float] = json.load(f)
96+
else:
97+
self._scan_speed_overrides = {}
98+
9299
if Config.Miscellaneous.BLOCKLIST_FILE:
93100
self._blocklist = load_blocklist(Config.Miscellaneous.BLOCKLIST_FILE)
94101
else:
@@ -288,12 +295,17 @@ def _single_iteration(self) -> int:
288295
if "requests_per_second_override" in task.payload_persistent
289296
]
290297

298+
for task in tasks:
299+
destination = self._get_scan_destination(task)
300+
if destination in self._scan_speed_overrides:
301+
requests_per_second_overrides.append(self._scan_speed_overrides[destination])
302+
291303
self.requests_per_second_for_current_tasks = min( # type: ignore
292304
requests_per_second_overrides if requests_per_second_overrides else [Config.Limits.REQUESTS_PER_SECOND]
293305
)
294306

295307
if requests_per_second_overrides:
296-
self.log.info("Overriding requests per second to %f", self.requests_per_second_for_current_tasks)
308+
self.log.info("Setting requests per second to %f", self.requests_per_second_for_current_tasks)
297309

298310
if len(tasks):
299311
time_start = time.time()

‎artemis/modules/port_scanner.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def _scan(self, target_ips: List[str]) -> Dict[str, Dict[str, Dict[str, Any]]]:
125125
str(Config.Modules.PortScanner.PORT_SCANNER_TIMEOUT_MILLISECONDS),
126126
]
127127
+ (
128-
["-rate", str(int(self.requests_per_second_for_current_tasks) * len(new_target_ips))]
128+
["-rate", str(max(1, int(self.requests_per_second_for_current_tasks)) * len(new_target_ips))]
129129
if int(self.requests_per_second_for_current_tasks)
130130
else []
131131
),

0 commit comments

Comments
 (0)
Failed to load comments.