Check against every ipv4 and ipv6 address now

This commit is contained in:
Dionysus 2023-10-29 12:02:48 -04:00
parent 353f23fb0d
commit ddd54db172
Signed by: acidvegas
GPG Key ID: EF4B922DB85DC9DE

View File

@ -14,6 +14,7 @@ try:
except ImportError: except ImportError:
raise SystemExit('missing required \'dnspython\' module (pip install dnspython)') raise SystemExit('missing required \'dnspython\' module (pip install dnspython)')
def attempt_axfr(tld: str, nameserver: str, filename: str): def attempt_axfr(tld: str, nameserver: str, filename: str):
''' '''
Perform a DNS zone transfer on a target domain. Perform a DNS zone transfer on a target domain.
@ -23,14 +24,13 @@ def attempt_axfr(tld: str, nameserver: str, filename: str):
:param filename: The filename to store the zone transfer results in. :param filename: The filename to store the zone transfer results in.
''' '''
temp_file = filename + '.temp' temp_file = filename + '.temp'
try: if not (nameserver := resolve_nameserver(nameserver)):
nameserver = resolve_nameserver(nameserver)[0].address # Not sure why, but we need to do this...
except Exception as ex:
logging.error(f'Failed to resolve nameserver {nameserver}: {ex}') logging.error(f'Failed to resolve nameserver {nameserver}: {ex}')
else: else:
for ns in nameserver: # Let's try all the IP addresses for the nameserver
try: try:
with open(temp_file, 'w') as file: with open(temp_file, 'w') as file:
xfr = dns.query.xfr(nameserver, tld+'.', lifetime=300) xfr = dns.query.xfr(nameserver.address, tld+'.', lifetime=300)
for msg in xfr: for msg in xfr:
for rrset in msg.answer: for rrset in msg.answer:
for rdata in rrset: for rdata in rrset:
@ -39,7 +39,8 @@ def attempt_axfr(tld: str, nameserver: str, filename: str):
except Exception as ex: except Exception as ex:
if os.path.exists(temp_file): if os.path.exists(temp_file):
os.remove(temp_file) os.remove(temp_file)
logging.error(f'Failed to perform zone transfer from {nameserver} for {tld}: {ex}') logging.error(f'Failed to perform zone transfer from {nameserver.address} for {tld}: {ex}')
def get_root_nameservers() -> list: def get_root_nameservers() -> list:
'''Generate a list of the root nameservers.''' '''Generate a list of the root nameservers.'''
@ -47,32 +48,39 @@ def get_root_nameservers() -> list:
root_servers = [str(rr.target)[:-1] for rr in root_ns_records] root_servers = [str(rr.target)[:-1] for rr in root_ns_records]
return root_servers return root_servers
def get_root_tlds() -> list: def get_root_tlds() -> list:
'''Get the root TLDs from IANA.''' '''Get the root TLDs from IANA.'''
tlds = urllib.request.urlopen('https://data.iana.org/TLD/tlds-alpha-by-domain.txt').read().decode('utf-8').lower().split('\n')[1:] tlds = urllib.request.urlopen('https://data.iana.org/TLD/tlds-alpha-by-domain.txt').read().decode('utf-8').lower().split('\n')[1:]
random.shuffle(tlds) random.shuffle(tlds)
return tlds return tlds
def get_tld_nameservers(tld: str) -> list: def get_tld_nameservers(tld: str) -> list:
'''Get the nameservers for a TLD.''' '''Get the nameservers for a TLD.'''
try: try:
return [str(nameserver) for nameserver in dns.resolver.resolve(tld+'.', 'NS', lifetime=60)] # Increase lifetime return [str(nameserver) for nameserver in dns.resolver.resolve(tld+'.', 'NS', lifetime=60)]
except dns.exception.Timeout: except dns.exception.Timeout:
logging.warning(f"Timeout fetching nameservers for TLD: {tld}") logging.warning(f"Timeout fetching nameservers for TLD: {tld}")
except dns.resolver.NoNameservers: except dns.resolver.NoNameservers:
logging.warning(f"No nameservers found for TLD: {tld}") logging.warning(f"No nameservers found for TLD: {tld}")
return [] return []
def resolve_nameserver(nameserver: str) -> str: def resolve_nameserver(nameserver: str) -> str:
''' '''
Resolve a nameserver to its IP address. Resolve a nameserver to its IP address.
:param nameserver: The nameserver to resolve. :param nameserver: The nameserver to resolve.
''' '''
data = []
for version in ('A', 'AAAA'):
try: try:
return dns.resolver.resolve(nameserver, 'A', lifetime=60) data += [ip.address for ip in dns.resolver.resolve(nameserver, version, lifetime=60)]
except: except:
return dns.resolver.resolve(nameserver, 'AAAA', lifetime=60) pass
return data
if __name__ == '__main__': if __name__ == '__main__':