The recursive internet scanner for hackers. 🧡
Python asmattack-surface-managementautomationbugbountyclieasmhackingneo4josintosint-frameworkpentestingpythonreconrecursionscannersubdomain-enumerationsubdomain-scannersubdomainsthreat-intelligencethreatintel
Add origin check and depth limit to gitdumper directory listing (#3466)
Paul Muellerfd60ef53f2d0
diff --git a/bbot/modules/gitdumper.py b/bbot/modules/gitdumper.pyindex 791347fb9..aee6e984e 100644--- a/bbot/modules/gitdumper.py+++ b/bbot/modules/gitdumper.py@@ -146,7 +146,10 @@ class gitdumper(BaseModule):return responsereturn None- async def recursive_dir_list(self, dir_listing):+ async def recursive_dir_list(self, dir_listing, _depth=0, _max_depth=10):+ if _depth >= _max_depth:+ self.verbose(f"Maximum directory listing depth ({_max_depth}) reached at {dir_listing.url}")+ return []file_list = []soup = self.helpers.beautifulsoup(dir_listing.text, "html.parser")links = soup.find_all("a")@@ -156,12 +159,14 @@ class gitdumper(BaseModule):continueif href.endswith("/"):folder_url = self.helpers.urljoin(str(dir_listing.url), href)+ if not folder_url.startswith(str(dir_listing.url)):+ self.verbose(f"Skipping out-of-scope directory link: {folder_url}")+ continueresponse = await self.helpers.request(folder_url)if getattr(response, "status_code", 0) == 200:- file_list.extend(await self.recursive_dir_list(response))+ file_list.extend(await self.recursive_dir_list(response, _depth=_depth + 1, _max_depth=_max_depth))else:file_url = self.helpers.urljoin(str(dir_listing.url), href)- # Ensure the file is in the same domain as the directory listingif file_url.startswith(str(dir_listing.url)):url = self.helpers.urlparse(file_url)file_list.append(url)diff --git a/bbot/test/test_step_1/test_gitdumper_safeguards.py b/bbot/test/test_step_1/test_gitdumper_safeguards.pyindex 0ba36e849..ca79923bd 100644--- a/bbot/test/test_step_1/test_gitdumper_safeguards.py+++ b/bbot/test/test_step_1/test_gitdumper_safeguards.py@@ -6,6 +6,7 @@ recurse without bound on the unfixed code."""import pytest+from unittest.mock import MagicMockfrom ..bbot_fixtures import * # noqa: F401, F403@@ -139,3 +140,72 @@ async def test_download_object_detects_cycles(bbot_scanner, tmp_path, monkeypatc)finally:await scan._cleanup()+++async def test_recursive_dir_list_rejects_cross_origin_folders(bbot_scanner, monkeypatch):+ """Folder links pointing to a different origin must be skipped."""+ scan = bbot_scanner("evilcorp.com", modules=["gitdumper"])+ await scan._prep()+ try:+ gitdumper = scan.modules["gitdumper"]+ requested_urls = []++ async def tracking_request(url, **kwargs):+ requested_urls.append(str(url))+ return None++ monkeypatch.setattr(gitdumper.helpers, "request", tracking_request)++ listing_html = """<html><body>+ <a href="objects/">objects/</a>+ <a href="http://evil.test:9999/steal/">steal/</a>+ <a href="http://169.254.169.254/latest/meta-data/">meta/</a>+ <a href="HEAD">HEAD</a>+ </body></html>"""++ fake_response = MagicMock()+ fake_response.url = "http://example.com/.git/"+ fake_response.text = listing_html+ fake_response.status_code = 200++ await gitdumper.recursive_dir_list(fake_response)++ assert any("example.com/.git/objects/" in u for u in requested_urls), (+ "legitimate subdirectory was not followed"+ )+ assert not any("evil.test" in u for u in requested_urls), "cross-origin folder link was followed"+ assert not any("169.254" in u for u in requested_urls), "metadata endpoint was followed"+ finally:+ await scan._cleanup()+++async def test_recursive_dir_list_caps_depth(bbot_scanner, monkeypatch):+ """recursive_dir_list must stop at the configured max depth."""+ scan = bbot_scanner("evilcorp.com", modules=["gitdumper"])+ await scan._prep()+ try:+ gitdumper = scan.modules["gitdumper"]+ depth_counter = [0]++ async def infinite_listing(url, **kwargs):+ depth_counter[0] += 1+ resp = MagicMock()+ resp.url = str(url)+ resp.text = '<html><body><a href="deeper/">deeper/</a></body></html>'+ resp.status_code = 200+ return resp++ monkeypatch.setattr(gitdumper.helpers, "request", infinite_listing)++ fake_response = MagicMock()+ fake_response.url = "http://example.com/.git/"+ fake_response.text = '<html><body><a href="level1/">level1/</a></body></html>'+ fake_response.status_code = 200++ await gitdumper.recursive_dir_list(fake_response, _max_depth=5)++ assert depth_counter[0] <= 5, f"recursed {depth_counter[0]} levels, expected max 5"+ finally:+ await scan._cleanup()