Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 68 additions & 13 deletions bbot/modules/chaos.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,59 @@
import re
from collections import defaultdict

from bbot.modules.templates.subdomain_enum import subdomain_enum_apikey


PER_PARENT_CAP = 300
PTR_NOISE_PATTERNS = [
# leading dotted IPv4-style octets, e.g. 001.106.103.218.static.netvigator.com
re.compile(r"^\d{1,4}(\.\d{1,4}){2,}(\.|$)"),
# leading hyphenated IPv4-style octets, e.g. 000-1-246-220.static.netvigator.com
re.compile(r"^\d{1,4}(-\d{1,4}){2,}(\.|$|-)"),
]


def _collapse(subdomains, domain, query):
"""Filter PTR-style noise and collapse floods of sibling subdomains.

When chaos returns more than PER_PARENT_CAP subdomains sharing a single
immediate parent, drop the children and emit just the parent. Bbot's own
DNS resolver will then determine wildcard status through normal sampling
rather than us asserting it from chaos's evidence alone.
"""
prefixes = []
for s in subdomains:
s = str(s).lower().strip(".*")
if not s:
continue
if any(p.match(s) for p in PTR_NOISE_PATTERNS):
continue
prefixes.append(s)

suffix = f".{query}"
fqdns = []
for p in prefixes:
full = f"{p}.{domain}"
if full.endswith(suffix):
fqdns.append(full)

parent_children = defaultdict(list)
for name in fqdns:
_, parent = name.split(".", 1)
parent_children[parent].append(name)

out = set()
capped = []
for parent, kids in parent_children.items():
if len(kids) > PER_PARENT_CAP:
capped.append((parent, len(kids)))
# emit just the parent so bbot's DNS layer can decide wildcard status
out.add(parent)
else:
out.update(kids)
return out, capped


class chaos(subdomain_enum_apikey):
watched_events = ["DNS_NAME"]
produced_events = ["DNS_NAME"]
Expand All @@ -27,18 +80,20 @@ async def request_url(self, query):
return await self.api_request(url)

async def parse_results(self, r, query):
results = set()
j = r.json()
subdomains_set = set()
if isinstance(j, dict):
domain = j.get("domain", "")
if domain:
subdomains = j.get("subdomains", [])
for s in subdomains:
s = s.lower().strip(".*")
subdomains_set.add(s)
for s in subdomains_set:
full_subdomain = f"{s}.{domain}"
if full_subdomain and full_subdomain.endswith(f".{query}"):
results.add(full_subdomain)
if not isinstance(j, dict):
return set()
domain = j.get("domain", "")
if not domain:
return set()
subdomains = j.get("subdomains") or []
if not subdomains:
return set()
results, capped = await self.helpers.run_in_executor_cpu(_collapse, subdomains, domain, query)
for parent, n in capped:
self.verbose(
f"chaos returned {n:,} children of {parent}, "
f"above per-parent cap of {PER_PARENT_CAP}; "
f"emitting parent only and dropping children"
)
return results
75 changes: 75 additions & 0 deletions bbot/test/test_step_2/module_tests/test_module_chaos.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,78 @@

def check(self, module_test, events):
assert any(e.data == "asdf.cloud.blacklanternsecurity.com" for e in events), "Failed to detect subdomain"


class TestChaosFloodCollapse(ModuleTestBase):
"""Chaos sometimes returns hundreds of thousands of children under a single
parent (typically ISP PTR explosions). Verify two behaviors:

1. Names whose leading segments look like IPv4 octets (dotted or
hyphenated) are filtered out as obvious PTR garbage.
2. When a parent still has more children than the cap, the children are
dropped and the parent is emitted on its own — letting bbot's normal
DNS wildcard detection decide whether it's actually wildcard.
"""

modules_overrides = ["chaos"]
config_overrides = {"modules": {"chaos": {"api_key": "asdf"}}}

async def setup_before_prep(self, module_test):
flood = []
# PTR junk that should be dropped by the IP-octet regex before it ever
# contributes to a parent's child count.
ptr_junk = [
"001.106.103.218.static", # dotted leading octets, with leading zero
"192.168.1.1.static", # dotted leading octets
"203.0.113.7.customer", # dotted leading octets
"000-1-246-220.static", # hyphenated leading octets, leading zero
"10-20-30-40.static", # hyphenated leading octets
"42-98-224-32.cust", # hyphenated leading octets
]
flood.extend(ptr_junk)
# 400 fake siblings under one parent (above the cap=300) — should
# collapse to just the parent.
flood.extend(f"host{i}.bigparent" for i in range(400))
# A normal subdomain under a different parent — should pass through.
flood.append("realone.normalparent")

module_test.blasthttp_mock.add_response(
url="https://dns.projectdiscovery.io/dns/example.com",
match_headers={"Authorization": "asdf"},
json={"domain": "example.com", "subdomains": 65},
)
module_test.blasthttp_mock.add_response(
url="https://dns.projectdiscovery.io/dns/blacklanternsecurity.com/subdomains",
match_headers={"Authorization": "asdf"},
json={"domain": "blacklanternsecurity.com", "subdomains": flood},
)

def check(self, module_test, events):
emitted = [e.data for e in events if e.module and getattr(e.module, "name", "") == "chaos"]

# 400 flooded children -> dropped; parent emitted instead
assert "bigparent.blacklanternsecurity.com" in emitted, (
Comment thread
liquidsec marked this conversation as resolved.
Dismissed
f"Expected parent to be emitted when children exceed cap; got {emitted}"
)
assert not any(d.startswith("host") for d in emitted), (
f"Flooded children should have been dropped; got {emitted}"
)

# Normal sibling under an uncapped parent passes through
assert "realone.normalparent.blacklanternsecurity.com" in emitted, (
Comment thread
liquidsec marked this conversation as resolved.
Dismissed
f"Real subdomain under uncapped parent should be emitted; got {emitted}"
)

# Every PTR-style leading-IP-octet name is filtered before reaching the cap
ptr_fragments = [
"001.106.103.218",
"192.168.1.1",
"203.0.113.7",
"000-1-246-220",
"10-20-30-40",
"42-98-224-32",
]
for frag in ptr_fragments:
assert not any(frag in d for d in emitted), (
f"PTR-style name containing {frag!r} should have been filtered; got {emitted}"
)
Loading