Practice library / Python for security work
Lab · Medium · 1.5 hoursFree account
DNS Enumeration with Python
Build a DNS reconnaissance tool from scratch in Python.
On this page 12 sections
DNS records tell an attacker which servers run mail, which host names exist, and often which zones lack basic protection. A single open AXFR can leak an entire internal name list. Here you write a resolver with dnspython, test for open zone transfers, brute-force subdomains with wildcard detection, and tie the parts into one command-line tool.
What You'll Learn
- Explain what A, AAAA, MX, NS, SOA, TXT and CNAME records tell a tester about a domain
- Write a resolver in Python with
dnspythonthat handles NXDOMAIN, empty answers and time-outs - Test a name server for an open zone transfer (AXFR) and read the result
- Build a threaded subdomain brute-forcer that detects and ignores wildcard records
- Join the parts into one command-line tool
- Close the zone transfer and find your own scan in the name server query log
A domain name system (DNS) server holds a list of names and the addresses behind them. Before a tester sends a single packet to a web server, DNS already shows which servers exist, where the mail goes and which names the company forgot. A defender who knows this list closes the gaps first.
This lab builds that tool in Python. You write about 80 lines, and you must be able to explain each one. You run the tool against a name server that you own.
Prerequisites
- Python Fundamentals: functions, loops,
tryandexcept, and reading a file. - Networking Deep Dive: IP addresses, UDP and TCP, and how a client asks a resolver.
Related labs: Network Scanning with Nmap for the active scan that follows DNS work, and OSINT & Passive Reconnaissance for the passive side.
Lab Setup
You need two lab machines on the lab network 10.0.0.0/8:
| Machine | Address | Job |
|---|---|---|
| Kali | 10.0.0.5 | Runs your tool. |
lab-dns (Debian or Ubuntu VM) | 10.0.0.53 | Runs BIND 9 and hosts the zone corp.test. |
The name corp.test uses the reserved top-level domain .test. It does not exist on the Internet, so nothing you do can reach a third party.
Run the tool only against your own lab-dns server. Do not point it at a company domain without written permission. The scope of a real test must name the domain and the name servers in writing. Brute force and zone transfer requests are visible in the logs of the owner.
A public option exists for practice: the domain zonetransfer.me is run by its owner for DNS training and allows zone transfers. Use it only for one transfer and a short word list, and read the owner's terms first. This lab does not need it.
Step 1: Build a target zone
Set up BIND and the zone corp.test
A zone is the file that holds the records of one domain. You write a small zone with known contents. This way you can check later that your tool finds everything that exists, and nothing that does not.
Work on lab-dns. Install BIND and its test tools.
sudo apt install -y bind9 bind9-utils bind9-dnsutilsWrite the zone file. Look at the record build: it is a name that no word list will contain.
sudo tee /etc/bind/db.corp.test >/dev/null <<'EOF'
$TTL 300
@ IN SOA ns1.corp.test. admin.corp.test. ( 2026111901 3600 600 86400 300 )
@ IN NS ns1.corp.test.
@ IN A 10.0.0.10
@ IN MX 10 mail.corp.test.
@ IN TXT "v=spf1 ip4:10.0.0.25 -all"
ns1 IN A 10.0.0.53
www IN A 10.0.0.10
mail IN A 10.0.0.25
vpn IN A 10.0.0.30
dev IN A 10.0.0.41
staging IN CNAME dev
admin IN A 10.0.0.60
build IN A 10.0.0.77
*.wild IN A 10.0.0.99
EOFTell BIND about the zone. Lab only: allow-transfer { any; }; is the mistake that you will find and fix in Step 7.
sudo tee -a /etc/bind/named.conf.local >/dev/null <<'EOF'
zone "corp.test" {
type master;
file "/etc/bind/db.corp.test";
allow-transfer { any; };
};
EOF
sudo named-checkzone corp.test /etc/bind/db.corp.test
sudo systemctl restart namedExample output:
zone corp.test/IN: loaded serial 2026111901
OKNow work on Kali. Install the library in a virtual environment and create the word list.
python3 -m venv ~/dnsenv && . ~/dnsenv/bin/activate
pip install dnspython
python3 -c "import dns.version; print(dns.version.version)"
printf '%s\n' www mail ftp vpn dev staging admin test api intranet ns1 ns2 smtp > ~/words.txt
dig @10.0.0.53 corp.test MX +shortExample output (your version number can differ):
2.8.0
10 mail.corp.test.The dig answer proves that lab-dns is reachable from Kali and answers for corp.test.
Step 2: Write the resolver
Look up record types with dnspython
Each record type answers a different question. A: which IPv4 address? AAAA: which IPv6 address? MX: which server takes the mail? NS: which servers are authoritative? SOA: who runs the zone and which serial number does it have? TXT: free text, often SPF rules and verification tokens. CNAME: an alias for another name.
Create ~/dnsenum.py on Kali. This first part holds the imports for the whole lab and three functions.
#!/usr/bin/env python3
"""dnsenum.py: a small DNS enumeration tool. Use only on authorised targets."""
import argparse
import secrets
from concurrent.futures import ThreadPoolExecutor
import dns.exception
import dns.query
import dns.rdatatype
import dns.resolver
DNS_ERRORS = (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer, dns.resolver.NoNameservers, dns.exception.Timeout)
TYPES = ["A", "AAAA", "MX", "NS", "SOA", "TXT"]
def make_resolver(server, timeout=3.0):
"""A resolver that asks one server only, not the system resolver."""
resolver = dns.resolver.Resolver(configure=False)
resolver.nameservers = [server]
resolver.lifetime = timeout
return resolver
def lookup(resolver, name, rtype):
"""Answers for one name and type, as text. An empty list means no answer."""
try:
answer = resolver.resolve(name, rtype)
except DNS_ERRORS:
return []
return [rdata.to_text() for rdata in answer]
def records(resolver, domain):
return [(domain, rtype, value) for rtype in TYPES for value in lookup(resolver, domain, rtype)]
def fmt(name, rtype, data):
return f"{name:<20} {rtype:<6} {data}"Three errors matter. NXDOMAIN means the name does not exist. NoAnswer means the name exists but has no record of this type. A Timeout means the server did not answer in time. The tool must treat all three as "nothing found" and carry on, or one bad name stops a long scan.
Run the function from the shell.
python3 -c "import dnsenum as d; r = d.make_resolver('10.0.0.53'); [print(d.fmt(*row)) for row in d.records(r, 'corp.test')]"Example output:
corp.test A 10.0.0.10
corp.test MX 10 mail.corp.test.
corp.test NS ns1.corp.test.
corp.test SOA ns1.corp.test. admin.corp.test. 2026111901 3600 600 86400 300
corp.test TXT "v=spf1 ip4:10.0.0.25 -all"The AAAA type printed nothing: the zone has no IPv6 address. Look at the TXT line. The SPF rule names 10.0.0.25 as the only host that may send mail. A tester learns the mail server address here without a scan. Look at the SOA line. The serial 2026111901 shows when the zone last changed.
Sign in to continue
8 more sections are for signed-in members. It is free: sign in with Google.
Related
This lab is extra practice for the course Python for security work. Do the lessons first.Open the course