Skip to content

armgit / bbot

Public Imported from GitHub
0

The recursive internet scanner for hackers. 🧡

Fix nested wildcard detection in is_wildcard (#3536)

Paul Muellerfea99f57ff96
diff --git a/bbot/core/helpers/dns/dns.py b/bbot/core/helpers/dns/dns.py
index c9b59e6cc..b95cfb22e 100644
--- a/bbot/core/helpers/dns/dns.py
+++ b/bbot/core/helpers/dns/dns.py
@@ -229,34 +229,26 @@ class DNSHelper:
if not raw_dns_records:
return result
- rdtypes_to_check = set(raw_dns_records)
-
- # walk parent domains shortest-first, comparing baseline against any wildcard pool
- parents = list(domain_parents(query))
- for parent in parents[::-1]:
- wildcard_results = await self.is_wildcard_domain(parent, rdtypes_to_check)
-
- for rdtype in list(baseline_raw):
- if rdtype in result:
- continue
-
- _baseline = baseline.get(rdtype, set())
- _baseline_raw = baseline_raw.get(rdtype, set())
-
- wildcard_rdtypes = wildcard_results.get(parent, {})
- wildcards = wildcard_rdtypes.get(rdtype)
- if wildcards is None:
+ # walk parent domains shortest-first, stopping at the first wildcard pool that contains the baseline.
+ # a non-matching pool may belong to an outer zone with a nested wildcard below it, so keep descending.
+ possible = {}
+ for parent in list(domain_parents(query))[::-1]:
+ rdtypes_to_check = [rdtype for rdtype in baseline_raw if rdtype not in result]
+ if not rdtypes_to_check:
+ break
+ zones = await asyncio.gather(*[self._is_wildcard_zone(parent, rdtype) for rdtype in rdtypes_to_check])
+ for rdtype, (wildcards, wildcard_raw) in zip(rdtypes_to_check, zones):
+ if not wildcard_raw:
continue
- wildcards, wildcard_raw = wildcards
+ is_wc = any(r in wildcards for r in baseline.get(rdtype, set()))
+ is_wc_raw = any(r in wildcard_raw for r in baseline_raw[rdtype])
+ if is_wc or is_wc_raw:
+ result[rdtype] = (True, parent)
+ else:
+ possible.setdefault(rdtype, ("POSSIBLE", parent))
- if wildcard_raw:
- rdtypes_to_check.discard(rdtype)
- is_wc = any(r in wildcards for r in _baseline)
- is_wc_raw = any(r in wildcard_raw for r in _baseline_raw)
- if is_wc or is_wc_raw:
- result[rdtype] = (True, parent)
- else:
- result[rdtype] = ("POSSIBLE", parent)
+ for rdtype, verdict in possible.items():
+ result.setdefault(rdtype, verdict)
for rdtype, answers in baseline_raw.items():
if answers and rdtype not in result:
@@ -269,7 +261,7 @@ class DNSHelper:
key=lambda domain, rdtypes: (domain, tuple(sorted(rdtypes))),
)
async def is_wildcard_domain(self, domain, rdtypes):
- """For each parent of ``domain``, return the wildcard pool per rdtype.
+ """For each rdtype, return the wildcard pool of the shallowest wildcard parent of ``domain``.
Returns ``{parent: {rdtype: (hosts_set, raw_text_set)}}``.
"""
diff --git a/bbot/test/test_step_1/test_dns.py b/bbot/test/test_step_1/test_dns.py
index 55a964e25..3d7329bd8 100644
--- a/bbot/test/test_step_1/test_dns.py
+++ b/bbot/test/test_step_1/test_dns.py
@@ -825,6 +825,62 @@ async def test_wildcard_deduplication(bbot_scanner):
assert sorted(e.data for e in dns_name_events) == ["_wildcard.evilcorp.com", "evilcorp.com"]
+# *.evilcorp.com and *.nested.evilcorp.com are both wildcards, but serve different pools.
+# The regexes don't overlap: wildcard probes are 10 random lowercase letters.
+NESTED_WILDCARD_DNS = {
+ "evilcorp.com": {"A": ["127.0.0.1"]},
+ r"regex:^[a-z]{10}\.evilcorp\.com$": {"A": ["127.0.0.2"]},
+ r"regex:.*\.nested\.evilcorp\.com$": {"A": ["127.0.0.3", "127.0.0.4"]},
+ "real.evilcorp.com": {"A": ["127.0.0.5"]},
+}
+
+
+async def test_nested_wildcards(bbot_scanner):
+ """A wildcard nested under a wildcard with a different pool is detected as the nested zone."""
+ scan = bbot_scanner("evilcorp.com", config={"dns": {"wildcard_ignore": []}})
+ await scan._prep()
+ await scan.helpers.dns._mock_dns(NESTED_WILDCARD_DNS)
+
+ # every depth under the nested zone must report the same parent, so the names dedupe
+ for query in ("a.nested.evilcorp.com", "a.b.nested.evilcorp.com", "a.b.c.d.e.nested.evilcorp.com"):
+ wildcard_rdtypes = await scan.helpers.dns.is_wildcard(query, ["A"])
+ assert wildcard_rdtypes.get("A") == (True, "nested.evilcorp.com"), query
+
+ # a name under a wildcard that matches no wildcard pool may be real
+ wildcard_rdtypes = await scan.helpers.dns.is_wildcard("real.evilcorp.com", ["A"])
+ assert wildcard_rdtypes.get("A") == ("POSSIBLE", "evilcorp.com")
+
+ await scan._cleanup()
+
+
+async def test_nested_wildcard_deduplication(bbot_scanner):
+ """Names at any depth under a nested wildcard collapse into one _wildcard.* event."""
+ from bbot.modules.base import BaseModule
+
+ class DummyModule(BaseModule):
+ watched_events = ["DNS_NAME"]
+ per_domain_only = True
+
+ async def handle_event(self, event):
+ for host in ("a.nested.evilcorp.com", "a.b.nested.evilcorp.com", "a.b.c.d.e.nested.evilcorp.com"):
+ await self.emit_event(host, "DNS_NAME", parent=event)
+
+ scan = bbot_scanner(
+ "evilcorp.com",
+ config={"dns": {"minimal": False, "wildcard_ignore": []}, "omit_event_types": []},
+ )
+ await scan._prep()
+ await scan.helpers.dns._mock_dns(NESTED_WILDCARD_DNS)
+ dummy_module = DummyModule(scan)
+ scan.modules["dummy_module"] = dummy_module
+ events = [e async for e in scan.async_start()]
+
+ dns_names = sorted(e.data for e in events if e.type == "DNS_NAME")
+ assert dns_names == ["_wildcard.nested.evilcorp.com", "evilcorp.com"]
+
+
@pytest.mark.asyncio
async def test_dns_graph_structure(bbot_scanner):
scan = bbot_scanner("https://evilcorp.com", config={"dns": {"search_distance": 1, "minimal": False}})