Skip to content

Commit

Permalink
Handle loops in CNAME chains
Browse files Browse the repository at this point in the history
  • Loading branch information
m-appel committed Jan 18, 2025
1 parent 11736b6 commit b1e21b0
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions iyp/crawlers/openintel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ def recurse_chain(current_chain: list, chain_links: dict, records: dict, state:
state[link][record_type].update(ips)
if chain_tail in chain_links:
for link in chain_links[chain_tail]:
if link in current_chain:
# Prevent infinite recursion due to CNAME loops.
continue
current_chain.append(link)
OpenIntelCrawler.recurse_chain(current_chain, chain_links, records, state)
current_chain.pop()
Expand Down Expand Up @@ -430,10 +433,14 @@ def normalize_ipv6(address):
return address

@staticmethod
def recurse_cnames(source: str, cnames: set, ips: set, state: dict):
def recurse_cnames(source: str, cnames: dict, ips: set, state: dict, processed_cnames: set):
for target in cnames[source]:
if target in processed_cnames:
# Prevent infinite recursion due to CNAME loops.
continue
processed_cnames.add(target)
state[target].update(ips)
DnsgraphCrawler.recurse_cnames(target, cnames, ips, state)
DnsgraphCrawler.recurse_cnames(target, cnames, ips, state, processed_cnames)

def run(self):
# Extract current date for partitioning
Expand Down Expand Up @@ -554,7 +561,7 @@ def run(self):
# pointing to it.
cname_resolves = defaultdict(set)
for name, ips in resolves_to.items():
self.recurse_cnames(name, cnames, ips, cname_resolves)
self.recurse_cnames(name, cnames, ips, cname_resolves, {name})
for hostname, ips in cname_resolves.items():
host_qid = hosts_id[hostname]
for ip in ips:
Expand Down

0 comments on commit b1e21b0

Please sign in to comment.