The recursive internet scanner for hackers. 🧡
Python asmattack-surface-managementautomationbugbountyclieasmhackingneo4josintosint-frameworkpentestingpythonreconrecursionscannersubdomain-enumerationsubdomain-scannersubdomainsthreat-intelligencethreatintel
Reject symlinks in unarchive output after extraction (#3560)
Paul Muellera572aa3a038d
diff --git a/bbot/modules/internal/unarchive.py b/bbot/modules/internal/unarchive.pyindex eb1d200a9..4935c7668 100644--- a/bbot/modules/internal/unarchive.py+++ b/bbot/modules/internal/unarchive.py@@ -87,6 +87,11 @@ class unarchive(BaseInternalModule):command = [s.format(filename=path, extract_dir=output_dir) for s in cmd_list]try:await self.run_process(command, check=True)+ # the listing check above depends on 7-Zip's output format, so verify what was actually written+ if any(p.is_symlink() for p in output_dir.rglob("*")):+ self.helpers.rm_rf(output_dir)+ self.warning(f"Archive {path} extracted a symlink, removing {output_dir}")+ return Falseextracted_size = sum(f.stat().st_size for f in output_dir.rglob("*") if f.is_file())budget[0] -= extracted_sizeif budget[0] < 0:diff --git a/bbot/test/test_step_2/module_tests/test_module_unarchive.py b/bbot/test/test_step_2/module_tests/test_module_unarchive.pyindex 30e6ef039..e2b5e5d5b 100644--- a/bbot/test/test_step_2/module_tests/test_module_unarchive.py+++ b/bbot/test/test_step_2/module_tests/test_module_unarchive.py@@ -355,3 +355,59 @@ class TestUnarchiveTraversalCheck(ModuleTestBase):# (mainline 7-Zip always does, which is exactly where the zip case is a no-op)if self.results["symlink_7z_is_link"]:assert not self.results["symlink_7z"], "Symlink 7z was not rejected"+++class TestUnarchiveSymlinkExtraction(ModuleTestBase):+ """Symlinks are rejected after extraction, regardless of how 7-Zip lists them."""++ targets = ["blacklanternsecurity.com"]+ modules_overrides = ["unarchive"]++ async def setup_after_prep(self, module_test):+ m = module_test.scan.modules["unarchive"]+ temp_path = Path(BBOT_TEST_DIR) / "unarchive_symlink"+ module_test.scan.helpers.rm_rf(temp_path, ignore_errors=True)+ temp_path.mkdir(parents=True)++ def make_zip(name, create_system=None):+ path = temp_path / f"{name}.zip"+ with zipfile.ZipFile(path, "w") as zf:+ zf.writestr("normal.txt", "safe content")+ if create_system is not None:+ info = zipfile.ZipInfo("evil_link")+ info.create_system = create_system+ info.external_attr = 0o120777 << 16 # S_IFLNK | 0o777+ zf.writestr(info, "normal.txt")+ return path++ safe_zip = make_zip("safe")+ # FAT/DOS host with a unix symlink mode: newer 7-Zip lists this as a hex+ # value with no "l" mode character, but still extracts it as a symlink+ fat_symlink_zip = make_zip("fat_symlink", create_system=0)+ unix_symlink_zip = make_zip("unix_symlink", create_system=3)++ def has_symlink(output_dir):+ return any(p.is_symlink() for p in output_dir.rglob("*"))++ self.results = {}+ for name, path in (("safe", safe_zip), ("fat_symlink", fat_symlink_zip)):+ output_dir = temp_path / f"{name}_out"+ self.results[name] = (await m.extract_file(path, output_dir), has_symlink(output_dir))++ # bypass the listing check so the post-extraction check is exercised on any 7-Zip version+ async def listing_check_passes(*args, **kwargs):+ return True++ m._check_archive_safe = listing_check_passes+ output_dir = temp_path / "unix_symlink_out"+ self.results["unix_symlink"] = (+ await m.extract_file(unix_symlink_zip, output_dir),+ has_symlink(output_dir),+ )++ def check(self, module_test, events):+ assert self.results["safe"] == (True, False), "Safe zip was not extracted"+ for name in ("fat_symlink", "unix_symlink"):+ extracted, symlink_left = self.results[name]+ assert not extracted, f"{name} zip was not rejected"+ assert not symlink_left, f"{name} zip left a symlink on disk"