Skip to content

Commit

Permalink
Improved request configuration (#88) (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
anna1492 committed Oct 26, 2023
1 parent 5a73d35 commit 305b482
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
4 changes: 2 additions & 2 deletions artemis/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ class Limits:
"E.g. when set to 100, Artemis will send no more than 100 port scanning packets per seconds per port scanner instance.",
] = get_config("SCANNING_PACKETS_PER_SECOND", default=100, cast=int)

SECONDS_PER_REQUEST: Annotated[
REQUEST_PER_SECOND: Annotated[
int,
"""
E.g. when set to 2, Artemis will make sure no HTTP/MySQL connect/... request takes less than 2 seconds, sleeping if needed.
""",
] = get_config("SECONDS_PER_REQUEST", default=0, cast=int)
] = get_config("REQUEST_PER_SECOND", default=1, cast=float)

class Miscellaneous:
BLOCKLIST_FILE: Annotated[
Expand Down
2 changes: 1 addition & 1 deletion artemis/modules/nuclei.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def run_multiple(self, tasks: List[Task]) -> None:
"-headless-bulk-size",
str(len(tasks_filtered)),
"-milliseconds-per-request",
str(int(Config.Limits.SECONDS_PER_REQUEST * 1000.0 / len(tasks_filtered))),
str(int((1 / Config.Limits.REQUEST_PER_SECOND) * 1000.0 / len(tasks_filtered))),
] + additional_configuration

targets = []
Expand Down
23 changes: 16 additions & 7 deletions artemis/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import math
import subprocess
import time
import urllib.parse
Expand Down Expand Up @@ -43,13 +44,21 @@ def is_directory_index(content: str) -> bool:


def throttle_request(f: Callable[[], Any]) -> Any:
time_start = time.time()
try:
return f()
finally:
time_elapsed = time.time() - time_start
if time_elapsed < Config.Limits.SECONDS_PER_REQUEST:
time.sleep(Config.Limits.SECONDS_PER_REQUEST - time_elapsed)
request_per_second = Config.Limits.REQUEST_PER_SECOND
if request_per_second >= 1:
average_time_per_request = 1 / request_per_second
f_start = time.time()
f()
func_time = time.time() - f_start
if func_time < average_time_per_request:
time.sleep(average_time_per_request - func_time)
elif request_per_second < 1:
seconds_for_req = math.floor(1 / request_per_second)
f_start = time.time()
f()
func_time = time.time() - f_start
if func_time < seconds_for_req:
time.sleep(func_time - f_start)


def get_host_from_url(url: str) -> str:
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.test-e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:

karton-bruter:
environment:
SECONDS_PER_REQUEST: 0
REQUEST_PER_SECOND: 10
redis:
volumes:
- data-test-redis:/data
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ services:
POSTMAN_MAIL_FROM: [email protected]
POSTMAN_MAIL_TO: [email protected]

SECONDS_PER_REQUEST: 0
REQUEST_PER_SECOND: 10
SCANNING_PACKETS_PER_SECOND: 5
CUSTOM_PORT_SCANNER_PORTS: 21,80,6379
NUCLEI_CHECK_TEMPLATE_LIST: False
Expand Down

0 comments on commit 305b482

Please sign in to comment.