The recursive internet scanner for hackers. 🧡
Python asmattack-surface-managementautomationbugbountyclieasmhackingneo4josintosint-frameworkpentestingpythonreconrecursionscannersubdomain-enumerationsubdomain-scannersubdomainsthreat-intelligencethreatintel
Set url_extension on HTTP_RESPONSE events (#3406)
Paul Mueller440aacb4bcb1
diff --git a/bbot/core/event/base.py b/bbot/core/event/base.pyindex 0880180b6..f47144de2 100644--- a/bbot/core/event/base.py+++ b/bbot/core/event/base.py@@ -142,6 +142,9 @@ class BaseEvent:_discovery_context_regex = re.compile(r"\{(?:event|module)[^}]*\}")# Stats class for the status line — override in subclasses for custom formatting_stats_class = None+ # Whether this event is subject to `url_extension_special` distribution filtering.+ # False for events representing already-retrieved content, whose consumers analyze the body.+ _url_special_filterable = True# using __slots__ dramatically reduces memory usage in large scans__slots__ = [@@ -1168,18 +1171,24 @@ class DefaultEvent(BaseEvent):class DictEvent(BaseEvent):__slots__ = ["url_extension"]+ def _set_url_extension(self):+ """Extract the file extension from self.parsed_url and record it as an attribute and a tag."""+ parsed_url = getattr(self, "parsed_url", None)+ if parsed_url is None:+ return+ url_path = parsed_url.path+ if not url_path:+ return+ extension = get_file_extension(str(url_path).lower())+ if extension:+ self.url_extension = extension+ self.add_tag(f"extension-{extension}")+def sanitize_data(self, data):url = data.get("url", "")if url:self.parsed_url = self.validators.validate_url_parsed(url)- # extract url_extension from any dict event with a URL- url_path = self.parsed_url.path- if url_path:- parsed_path_lower = str(url_path).lower()- extension = get_file_extension(parsed_path_lower)- if extension:- self.url_extension = extension- self.add_tag(f"extension-{extension}")+ self._set_url_extension()return datadef _data_load(self, data):@@ -1450,15 +1459,7 @@ class URL_UNVERIFIED(DictHostEvent):self.parsed_url = self.validators.validate_url_parsed(url)data["url"] = self.parsed_url.geturl()- # special handling of URL extensions- if self.parsed_url is not None:- url_path = self.parsed_url.path- if url_path:- parsed_path_lower = str(url_path).lower()- extension = get_file_extension(parsed_path_lower)- if extension:- self.url_extension = extension- self.add_tag(f"extension-{extension}")+ self._set_url_extension()# tag as dir or endpointif str(self.parsed_url.path).endswith("/"):@@ -1707,6 +1708,10 @@ class EMAIL_ADDRESS(BaseEvent):class HTTP_RESPONSE(URL_UNVERIFIED):+ # the response body has already been retrieved, so content-analysis modules+ # should still receive it even for special extensions like .js+ _url_special_filterable = False+def __init__(self, *args, **kwargs):super().__init__(*args, **kwargs)# count number of consecutive redirects@@ -1783,6 +1788,7 @@ class HTTP_RESPONSE(URL_UNVERIFIED):url = data.get("url", "")self.parsed_url = self.validators.validate_url_parsed(url)data["url"] = self.parsed_url.geturl()+ self._set_url_extension()if not "raw_header" in data:raise ValueError("raw_header is required for HTTP_RESPONSE events")diff --git a/bbot/modules/base.py b/bbot/modules/base.pyindex 5a9912ef1..ae77a0cd0 100644--- a/bbot/modules/base.py+++ b/bbot/modules/base.py@@ -871,7 +871,7 @@ class BaseModule:return False, "it did not meet target_only filter criteria"# limit events with special URL extensions (e.g. .js) to modules that opt in- if not self.accept_url_special:+ if not self.accept_url_special and event._url_special_filterable:extension = getattr(event, "url_extension", "")if extension in self.scan.url_extension_special:return (diff --git a/bbot/test/test_step_1/test_events.py b/bbot/test/test_step_1/test_events.pyindex dbc219756..ed4010590 100644--- a/bbot/test/test_step_1/test_events.py+++ b/bbot/test/test_step_1/test_events.py@@ -175,6 +175,27 @@ async def test_events(events, helpers):)assert getattr(wp_no_ext, "url_extension", "NOT_SET") == "NOT_SET"+ # url_extension: HTTP_RESPONSE events+ # modules that filter on url_extension (e.g. paramminer, lightfuzz) rely on this being set+ def _http_response(url):+ return scan.make_event(+ {"url": url, "raw_header": "HTTP/1.1 200 OK\r\n\r\n"},+ "HTTP_RESPONSE",+ dummy=True,+ )++ hr_pdf = _http_response("https://evilcorp.com/files/document.pdf?foo=bar")+ assert getattr(hr_pdf, "url_extension", "") == "pdf"+ assert "extension-pdf" in hr_pdf.tags+ hr_no_ext = _http_response("https://evilcorp.com/search")+ assert getattr(hr_no_ext, "url_extension", "NOT_SET") == "NOT_SET"++ # special extensions (.js) must still reach modules that don't opt in to special URLs,+ # since the response body has already been retrieved. the distribution behavior+ # itself is pinned in test_modules_basic.py+ hr_js = _http_response("https://evilcorp.com/app.js")+ assert getattr(hr_js, "url_extension", "") == "js"+# http responseassert events.http_response.host == "example.com"assert events.http_response.port == 80diff --git a/bbot/test/test_step_1/test_modules_basic.py b/bbot/test/test_step_1/test_modules_basic.pyindex 60e408071..bd9cd360e 100644--- a/bbot/test/test_step_1/test_modules_basic.py+++ b/bbot/test/test_step_1/test_modules_basic.py@@ -118,6 +118,42 @@ async def test_modules_basic_checks(events, blasthttp_mock):assert result is Trueassert reason == "event is always emitted"+ # special URL extensions (.js) are withheld from modules that don't opt in.+ # HTTP_RESPONSE is exempt: its body is already retrieved, so consumers analyze+ # content rather than deciding whether to fetch. BaseModule defaults to+ # accept_url_special = False.+ special_module = BaseModule(scan)+ special_module._watched_events = None+ special_module.watched_events = ["*"]+ js_url = scan.make_event("http://127.0.0.1/app.js", "URL_UNVERIFIED", parent=scan.root_event)+ assert js_url.url_extension == "js"+ result, reason = special_module._event_precheck(js_url)+ assert result is False+ assert "special URL extension" in reason+ js_response = scan.make_event(+ {"url": "http://127.0.0.1/app.js", "method": "GET", "raw_header": "HTTP/1.1 200 OK\r\n\r\n"},+ "HTTP_RESPONSE",+ parent=scan.root_event,+ )+ assert js_response.url_extension == "js"+ result, reason = special_module._event_precheck(js_response)+ assert result is True, reason++ # blacklisted extensions are rejected outright at ingress, HTTP_RESPONSE included.+ # no module emits one with a blacklisted extension today, so this pins the intended+ # semantics rather than behavior anything currently depends on+ css_url = scan.make_event("http://127.0.0.1/style.css", "URL_UNVERIFIED", parent=scan.root_event)+ # compared as a tuple: handle_event returns None when it accepts, so an+ # exemption shows up as a clean mismatch rather than an unpack error+ assert await scan.ingress_module.handle_event(css_url) == (False, "event is blacklisted")+ css_response = scan.make_event(+ {"url": "http://127.0.0.1/style.css", "method": "GET", "raw_header": "HTTP/1.1 200 OK\r\n\r\n"},+ "HTTP_RESPONSE",+ parent=scan.root_event,+ )+ assert css_response.url_extension == "css"+ assert await scan.ingress_module.handle_event(css_response) == (False, "event is blacklisted")+# common event filtering testsfor module_class in (BaseModule, BaseOutputModule, BaseReportModule, BaseInternalModule):base_module = module_class(scan)diff --git a/bbot/test/test_step_2/module_tests/test_module_paramminer_getparams.py b/bbot/test/test_step_2/module_tests/test_module_paramminer_getparams.pyindex ec931afc9..90e0f1dd9 100644--- a/bbot/test/test_step_2/module_tests/test_module_paramminer_getparams.py+++ b/bbot/test/test_step_2/module_tests/test_module_paramminer_getparams.py@@ -288,3 +288,11 @@ class TestParamminer_Getparams_filter_static(TestParamminer_Getparams_finish):assert excavate_extracted_param, "Excavate should still extract the parameter from the HTML link"assert not paramminer_recycled_to_php, "Paramminer should not recycle words from static-URL WEB_PARAMETERs"assert not paramminer_bruted_pdf, "Paramminer should not brute-force parameters on static URLs"++ # paramminer's static filter also applies to HTTP_RESPONSE events, which requires+ # url_extension to be populated on them+ pdf_responses = [e for e in events if e.type == "HTTP_RESPONSE" and e.data["url"].endswith("test2.pdf")]+ assert pdf_responses, "expected an HTTP_RESPONSE for the .pdf URL"+ assert getattr(pdf_responses[0], "url_extension", None) == "pdf", (+ "HTTP_RESPONSE is missing url_extension, so paramminer's static filter is bypassed"+ )