mdaxfr/axfr.py

115 lines
4.5 KiB
Python
Raw Normal View History

2023-10-28 21:55:58 +00:00
#!/usr/bin/env python
# Mass DNS AXFR - developed by acidvegas in python (https://git.acid.vegas/mdaxfr)
2023-10-29 01:40:06 +00:00
import os
2023-10-28 21:55:58 +00:00
import urllib.request
2023-10-29 01:40:06 +00:00
import logging
2023-10-28 21:55:58 +00:00
try:
import dns.rdatatype
import dns.query
import dns.zone
import dns.resolver
except ImportError:
raise SystemExit('missing required \'dnspython\' module (pip install dnspython)')
2023-10-29 01:40:06 +00:00
def attempt_axfr(tld: str, nameserver: str, filename: str):
2023-10-28 21:55:58 +00:00
'''
Perform a DNS zone transfer on a target domain.
:param target: The target domain to perform the zone transfer on.
:param nameserver: The nameserver to perform the zone transfer on.
2023-10-29 01:40:06 +00:00
:param filename: The filename to store the zone transfer results in.
2023-10-28 21:55:58 +00:00
'''
2023-10-29 01:40:06 +00:00
temp_file = filename + '.temp'
try:
nameserver = resolve_nameserver(nameserver)
except Exception as ex:
logging.error(f'Failed to resolve nameserver {nameserver}: {ex}')
else:
try:
with open(temp_file, 'w') as file:
xfr = dns.query.xfr(nameserver, tld+'.', timeout=15)
for msg in xfr:
for rrset in msg.answer:
for rdata in rrset:
file.write(f'{rrset.name}.{tld} {rrset.ttl} {rdata}')
os.rename(temp_file, filename)
except Exception as ex:
if os.path.exists(temp_file):
os.remove(temp_file)
logging.error(f'Failed to perform zone transfer from {nameserver} for {tld}: {ex}')
2023-10-28 21:55:58 +00:00
2023-10-29 01:40:06 +00:00
def get_root_nameservers() -> list:
2023-10-28 21:55:58 +00:00
'''Generate a list of the root nameservers.'''
2023-10-29 01:40:06 +00:00
root_ns_records = dns.resolver.resolve('.', 'NS')
root_servers = [str(rr.target)[:-1] for rr in root_ns_records]
return root_servers
2023-10-28 21:55:58 +00:00
def get_root_tlds() -> list:
'''Get the root TLDs from IANA.'''
return urllib.request.urlopen('https://data.iana.org/TLD/tlds-alpha-by-domain.txt').read().decode('utf-8').lower().split('\n')[1:]
2023-10-29 01:40:06 +00:00
def get_tld_nameservers(tld: str) -> list:
2023-10-28 21:55:58 +00:00
'''Get the nameservers for a TLD.'''
return [nameserver for nameserver in dns.resolver.query(tld+'.', 'NS' )]
2023-10-29 01:40:06 +00:00
def resolve_nameserver(nameserver: str) -> str:
2023-10-28 21:55:58 +00:00
'''
Resolve a nameserver to its IP address.
:param nameserver: The nameserver to resolve.
'''
try:
ip_addresses = dns.resolver.resolve(nameserver, 'A', lifetime=15)
except:
ip_addresses = dns.resolver.resolve(nameserver, 'AAAA', lifetime=15)
return ip_addresses[0].address
if __name__ == '__main__':
2023-10-29 01:40:06 +00:00
import argparse
parser = argparse.ArgumentParser(description='Mass DNS AXFR')
parser.add_argument('-r', '--root', action='store_true', help='perform zone transfer on root nameservers')
parser.add_argument('-t', '--tld', help='perform zone transfer on a specific TLD')
parser.add_argument('-ts', '--tlds', help='perform zone transfer on all TLDs')
parser.add_argument('-o', '--output', default='axfrout', help='output directory')
args = parser.parse_args()
os.makedirs(args.output, exist_ok=True) # Create output directory if it doesn't exist
if args.root:
try:
for root in get_root_nameservers():
try:
attempt_axfr('', root+'.root-servers.net', os.path.join(args.output, root+'-root.txt'))
except Exception as e:
logging.error(f'Failed to perform zone transfer from the {root} root server: {e}')
except Exception as e:
logging.error(f'Failed to get root nameservers: {e}')
2023-10-28 21:55:58 +00:00
2023-10-29 01:40:06 +00:00
if args.tlds:
2023-10-28 21:55:58 +00:00
try:
2023-10-29 01:40:06 +00:00
for tld in get_root_tlds():
try:
for ns in get_tld_nameservers(tld):
try:
attempt_axfr(tld, ns, os.path.join(args.output, tld+'.txt'))
except Exception as e:
logging.error(f'Failed to perform zone transfer from {ns} for {tld}: {e}')
except Exception as e:
logging.error(f'Failed to get nameservers for {tld}: {e}')
2023-10-28 21:55:58 +00:00
except Exception as e:
2023-10-29 01:40:06 +00:00
logging.error(f'Failed to get root TLDs: {e}')
2023-10-28 21:55:58 +00:00
2023-10-29 01:40:06 +00:00
elif args.tld:
2023-10-28 21:55:58 +00:00
try:
2023-10-29 01:40:06 +00:00
for ns in get_tld_nameservers(args.tld):
try:
attempt_axfr(args.tld, ns, os.path.join(args.output, args.tld+'.txt'))
except Exception as e:
logging.error(f'Failed to perform zone transfer from {ns} for {args.tld}: {e}')
2023-10-28 21:55:58 +00:00
except Exception as e:
2023-10-29 01:40:06 +00:00
logging.error(f'Failed to get nameservers for {args.tld}: {e}')