Skip to content

armgit / bbot

Public Imported from GitHub
0

The recursive internet scanner for hackers. 🧡

waf_bypass: only attempt bypasses against scan targets by default (#3460)

Paul Mueller4268e23bd8be
diff --git a/bbot/modules/waf_bypass.py b/bbot/modules/waf_bypass.py
index 0dce93900..a30debfc7 100644
--- a/bbot/modules/waf_bypass.py
+++ b/bbot/modules/waf_bypass.py
@@ -16,6 +16,8 @@ class waf_bypass(BaseModule):
In finish(), we test if WAF-protected content can be accessed directly via IPs from non-protected domains.
Optionally, it explores IP neighbors within the same ASN to find additional bypass candidates.
+
+ The `scan_targets_only` option narrows which protected hosts are attempted (candidate discovery is unaffected).
"""
watched_events = ["URL"]
@@ -36,6 +38,10 @@ class waf_bypass(BaseModule):
ge=1,
description="Maximum number of concurrent bypass-attempt HTTP checks in finish()",
)
+ scan_targets_only: bool = Field(
+ True,
+ description="Only attempt bypasses against WAF-protected hosts that were explicitly specified as scan targets",
+ )
meta = {
"description": "Detects potential WAF bypasses",
@@ -52,6 +58,17 @@ class waf_bypass(BaseModule):
self.search_ip_neighbors = self.config.get("search_ip_neighbors", True)
self.neighbor_cidr = int(self.config.get("neighbor_cidr", 24))
+ # When scan_targets_only is enabled (the default), only hosts named verbatim in the scan's targets/seeds are
+ # attempted. Strict scope means a target of "evilcorp.com" won't match "www.evilcorp.com".
+ self.explicit_targets = None
+ if self.config.get("scan_targets_only", True):
+ self.explicit_targets = self.helpers.make_target(
+ *self.scan.target.target.event_seeds,
+ *self.scan.target.seeds.event_seeds,
+ strict_scope=True,
+ )
+ self.verbose(f"Restricting bypass attempts to {len(self.explicit_targets)} explicit scan target(s)")
+
# Keep track of (protected_domain, ip) pairs we have already attempted to bypass
self.attempted_bypass_pairs = set()
# Keep track of any IPs that came from hosts that are "cloud-ips"
@@ -101,6 +118,11 @@ class waf_bypass(BaseModule):
self.protected_domains[domain] = event
self.debug(f"Found {provider_name}-protected domain: {domain}")
+ # ineligible hosts still count towards candidate discovery, they just aren't fingerprinted or attempted
+ if not self.bypass_eligible(domain):
+ self.debug(f"Skipping {domain}: not an explicit scan target")
+ return
+
response = await self.get_url_content(url)
if not response:
self.debug(f"Failed to get response from protected URL {url}")
@@ -119,6 +141,10 @@ class waf_bypass(BaseModule):
}
self.debug(f"Stored simhash of response from {url} (content length: {len(response.text)})")
+ def bypass_eligible(self, host):
+ """Whether a WAF-protected host is allowed to be attempted (all of them if scan_targets_only is disabled)"""
+ return self.explicit_targets is None or host in self.explicit_targets
+
async def get_url_content(self, url, ip=None):
"""Helper function to fetch content from a URL, optionally through specific IP"""
try:
@@ -174,6 +200,10 @@ class waf_bypass(BaseModule):
async def finish(self):
self.verbose(f"Found {len(self.protected_domains)} Protected Domains")
+ eligible_domains = {d: e for d, e in self.protected_domains.items() if self.bypass_eligible(d)}
+ if self.explicit_targets is not None:
+ self.verbose(f"{len(eligible_domains)} of them are explicit scan targets and will be attempted")
+
confirmed_bypasses = [] # [(protected_url, matching_ip, similarity)]
ip_bypass_candidates = {} # {ip: domain}
waf_ips = set()
@@ -239,7 +269,7 @@ class waf_bypass(BaseModule):
coros = []
new_pairs_count = 0
- for protected_domain, source_event in self.protected_domains.items():
+ for protected_domain, source_event in eligible_domains.items():
for ip, src in ip_bypass_candidates.items():
combo = (protected_domain, ip)
if combo in self.attempted_bypass_pairs:
diff --git a/bbot/test/test_step_2/module_tests/test_module_waf_bypass.py b/bbot/test/test_step_2/module_tests/test_module_waf_bypass.py
index 8140ebbcb..243855a8b 100644
--- a/bbot/test/test_step_2/module_tests/test_module_waf_bypass.py
+++ b/bbot/test/test_step_2/module_tests/test_module_waf_bypass.py
@@ -37,12 +37,13 @@ class TestWAFBypass(ModuleTestBase):
watched_events = ["DNS_NAME"]
_name = "dummy_module"
events_seen = []
+ protected_host = "protected.test"
async def handle_event(self, event):
if event.data == "protected.test":
await self.helpers.sleep(0.5)
self.events_seen.append(event.data)
- url = f"http://protected.test:{HTTPSERVER_PORT}/"
+ url = f"http://{self.protected_host}:{HTTPSERVER_PORT}/"
url_event = self.scan.make_event(
url, "URL", parent=self.scan.root_event, tags=["cloudflare", "in-scope", "status-200"]
)
@@ -59,14 +60,14 @@ class TestWAFBypass(ModuleTestBase):
if url_event is not None:
await self.emit_event(url_event)
+ dns_mock = {
+ "protected.test": {"A": [PROTECTED_IP]},
+ "direct.test": {"A": [DIRECT_IP]},
+ "": {"A": []},
+ }
+
async def setup_after_prep(self, module_test):
- await module_test.mock_dns(
- {
- "protected.test": {"A": [self.PROTECTED_IP]},
- "direct.test": {"A": [self.DIRECT_IP]},
- "": {"A": []},
- }
- )
+ await module_test.mock_dns(self.dns_mock)
self.module_test = module_test
@@ -138,6 +139,49 @@ class TestWAFBypass(ModuleTestBase):
assert correct_description, "Incorrect description"
+class TestWAFBypassScanTargetsOnlySkip(TestWAFBypass):
+ """By default, a protected host that isn't a scan target is discovered but never attempted"""
+
+ PROTECTED_HOST = "sub.protected.test"
+
+ dns_mock = {
+ "protected.test": {"A": [TestWAFBypass.PROTECTED_IP]},
+ PROTECTED_HOST: {"A": [TestWAFBypass.PROTECTED_IP]},
+ "direct.test": {"A": [TestWAFBypass.DIRECT_IP]},
+ "": {"A": []},
+ }
+
+ class DummyModule(TestWAFBypass.DummyModule):
+ protected_host = "sub.protected.test"
+
+ def check(self, module_test, events):
+ module = module_test.scan.modules["waf_bypass"]
+ # candidate discovery is unaffected -- the host is still tracked, so its IP stays out of the candidate pool
+ assert self.PROTECTED_HOST in module.protected_domains, "Protected host was not discovered"
+ assert not module.content_fingerprints, "Non-target protected host should not be fingerprinted"
+ assert not module.attempted_bypass_pairs, "No bypasses should have been attempted"
+ assert not [e for e in events if e.type == "FINDING"], "FINDING produced for a non-target host"
+
+
+class TestWAFBypassAllHosts(TestWAFBypassScanTargetsOnlySkip):
+ """With scan_targets_only disabled, a protected host that isn't a scan target is still attempted"""
+
+ config_overrides = {
+ "scope": {"report_distance": 2},
+ "modules": {
+ "waf_bypass": {"search_ip_neighbors": True, "neighbor_cidr": 30, "scan_targets_only": False},
+ },
+ }
+
+ def check(self, module_test, events):
+ assert any(
+ e.type == "FINDING"
+ and f"WAF Bypass Confirmed - Direct IPs: 127.0.0.1 for http://{self.PROTECTED_HOST}:{HTTPSERVER_PORT}/"
+ in e.data["description"]
+ for e in events
+ ), "No bypass FINDING for the non-target protected host"
+
+
class TestWAFBypassTagRecognition(ModuleTestBase):
"""Regression: waf_bypass keys off the flat cloud-provider tags cloudcheck emits
post-`Migrate cloudcheck to host_metadata` refactor. Both `cloudflare` and `imperva`
diff --git a/docs/scanning/configuration.md b/docs/scanning/configuration.md
index af42fc6be..a8605da66 100644
--- a/docs/scanning/configuration.md
+++ b/docs/scanning/configuration.md
@@ -589,6 +589,7 @@ In addition to the stated options for each module, the following universal optio
| modules.virtualhost.wordcloud_check | bool | Enable check using scan-wide wordcloud data on target host | False |
| modules.waf_bypass.max_concurrent_checks | int | Maximum number of concurrent bypass-attempt HTTP checks in finish() | 100 |
| modules.waf_bypass.neighbor_cidr | int | CIDR mask (24-31) used for neighbor enumeration when search_ip_neighbors is true | 28 |
+| modules.waf_bypass.scan_targets_only | bool | Only attempt bypasses against WAF-protected hosts that were explicitly specified as scan targets | True |
| modules.waf_bypass.search_ip_neighbors | bool | Also check IP neighbors of the target domain | True |
| modules.waf_bypass.similarity_threshold | float | Similarity threshold for content matching | 0.9 |
| modules.wafw00f.generic_detect | bool | When no specific WAF detections are made, try to perform a generic detect | True |