~/service-dns

DNS

CPTS Notes8 min read

TL;DR

DNS (Domain Name System). Resolves names to addresses. UDP and TCP 53. UDP serves normal queries. TCP serves zone transfers and any response over 512 bytes. A zone transfer (AXFR) is how a secondary server syncs the full zone from the primary, which is why a misconfigured one hands the whole zone to anyone who asks.

First checks: a zone transfer, the records, and subdomains. DNS enumeration needs a domain name. On a box you often guess it (bank.htb) or read it from a certificate or another service, then set $DOMAIN and add it to /etc/hosts.

Protocol background is in DNS Resolution.

Records

Record Returns
A / AAAA IPv4 / IPv6 address.
NS The zone’s nameservers.
MX Mail servers.
TXT Free text: SPF, DKIM, domain-verification tokens, sometimes secrets.
CNAME Alias to another name.
SOA Zone info and the admin email (the first . stands for @).
PTR Reverse lookup, IP to name.
SRV Service locations (_ldap, _kerberos).

The records live in a zone file, which an AXFR returns in full:

$ORIGIN example.com. ; the zone root, written @ below
$TTL 3600 ; default record cache time, in seconds
@ IN SOA ns1.example.com. admin.example.com. ( ; authority + contact [email protected]
2026101901 ; serial, bumped on every edit
7200 ; refresh, how often a secondary re-syncs
900 ; retry, wait after a failed refresh
1209600 ; expire, drop the zone if the primary stays unreachable
3600 ) ; negative-cache TTL, how long an NXDOMAIN is cached
@ IN NS ns1.example.com. ; authoritative nameserver
@ IN NS ns2.example.com. ; second nameserver, for redundancy
@ IN A 93.184.215.14 ; IPv4 of example.com
@ IN AAAA 2606:2800:21f:cb07:6820:80da:af6b:8b2c ; IPv6 of example.com
@ IN MX 10 mail.example.com. ; mail server, lowest number wins
mail IN A 93.184.215.20 ; address the MX name resolves to
www IN CNAME example.com. ; alias, www points to the apex
ftp IN A 93.184.215.21 ; a plain host record
@ IN TXT "v=spf1 mx -all" ; SPF, only the MX may send mail for the domain
@ IN TXT "google-site-verification=abc123" ; domain-ownership token
_ldap._tcp IN SRV 0 100 389 dc01.example.com. ; service record, LDAP on dc01 port 389
eu IN NS ns.eu-team.example.net. ; delegation, eu.example.com is its own zone
; reverse zone: IP back to name
$ORIGIN 215.184.93.in-addr.arpa. ; the 93.184.215.0/24 reverse zone
14 IN PTR example.com. ; 93.184.215.14 resolves back to this name

Enumeration

Workflow: query records with dig, try a zone transfer, then brute subdomains.

Query records.

dig any $DOMAIN @$TARGET # every record type, many servers refuse it
dig ns $DOMAIN @$TARGET # nameservers
dig mx $DOMAIN @$TARGET # mail servers
dig txt $DOMAIN @$TARGET # text records
dig soa $DOMAIN @$TARGET # admin contact and primary nameserver
dig ch txt version.bind @$TARGET # BIND version, when the server exposes it

@$TARGET sends the query to that server. ch txt version.bind asks for a CHAOS-class record.

Zone transfer (AXFR). The jackpot. A misconfigured server dumps every record:

dig axfr $DOMAIN @$TARGET # full zone transfer
fierce --domain $DOMAIN # find each nameserver and try AXFR on all of them

The reply holds internal hostnames, IPs, and subdomains.

All-in-one. dnsrecon and dnsenum run records, an AXFR attempt, and a brute in one command. On a lab, point them at the box, since the system resolver does not know a .htb zone:

dnsrecon -d $DOMAIN -n $TARGET -t std # records plus an automatic zone-transfer attempt
dnsenum --dnsserver $TARGET $DOMAIN # same idea, also sweeps reverse IP ranges

dnsrecon is the maintained one, with NSEC zone walking and JSON output (--json). Convenient, but the dedicated tools below find more.

Brute subdomains when AXFR fails. On a lab the box is the only resolver that knows the zone, so point the brute at it. subfinder is passive OSINT and needs a public domain, so it is secondary.

gobuster dns --domain $DOMAIN --resolver $TARGET -w <wordlist> # preinstalled on Kali
echo $TARGET > resolvers.txt # puredns reads resolvers from a file
puredns bruteforce <wordlist> $DOMAIN -r resolvers.txt # faster, needs go install plus massdns
subfinder -d $DOMAIN -silent | dnsx -silent # public domain: passive names, resolved

gobuster dns ships with Kali and takes the resolver inline, the easy lab default. puredns is faster for big runs. dnsx -r also takes a resolver inline (-r $TARGET).

Wordlists come from SecLists, under /usr/share/seclists/Discovery/DNS/ (sudo apt install seclists if the directory is missing):

Wordlist Names Use
subdomains-top1million-5000.txt 5k fast first pass
subdomains-top1million-20000.txt 20k deeper
bitquark-subdomains-top100000.txt 100k thorough
dns-Jhaddix.txt ~2M exhaustive, slow

Triage the results. A brute gives three kinds of result, and the errors are leads. Triage all three:

  • A clean resolve: a normal record, a host or vhost. Add it to /etc/hosts and enumerate it.
  • NXDOMAIN: nothing there. Ignore.
  • An error that is neither (SERVFAIL, “server misbehaving”, REFUSED): something answers oddly, often a delegated subzone. Investigate it with dig.

A subdomain with its own NS record is a separate zone, and the parent transfer never held its records. Transfer it against the box, since a subzone often allows AXFR where the parent refuses:

dig ns <name>.$DOMAIN @$TARGET # NS records here mean a delegated zone
dig axfr <name>.$DOMAIN @$TARGET # transfer the subzone

Internal zones (internal., hr., dev.) hold records the parent zone hides.

nmap.

# recursion, nameserver ID, and zone transfer
sudo nmap -p53 -sV --script dns-recursion,dns-nsid,dns-zone-transfer --script-args dns-zone-transfer.domain=$DOMAIN -oA scans/dns $TARGET

Common misconfigurations

  • Zone transfer (AXFR) allowed to any client.
  • Internal records answered to external queries (split-horizon leak).
  • Dynamic updates allowed without authentication.
  • DNSSEC NSEC records let an attacker walk the whole zone.
  • Open recursion, which enables cache poisoning and amplification.
  • A dangling CNAME to an unclaimed third-party host, open to subdomain takeover.

Attack playbook

DNS is reconnaissance. Search it to map the environment.

  1. Zone transfer. dig axfr first. One misconfig dumps the whole internal namespace. Transfer any internal zones it reveals too. They leak domain controllers, VPN, and workstation hosts.
    dig axfr internal.$DOMAIN @$TARGET # transfer an internal zone
  2. Brute subdomains. When AXFR fails, brute and resolve. New names are vhosts, dev and staging hosts, and internal services. Add each to /etc/hosts, then enumerate the web and other services on it.
  3. Harvest records. MX names the mail server. TXT leaks SPF entries, keys, and verification tokens. SRV locates services (_ldap, _kerberos point to Active Directory). NS names the authoritative servers.
  4. Zone walking. DNSSEC NSEC chains leak every name even without AXFR:
    ldns-walk @$TARGET $DOMAIN # walk the NSEC chain
  5. Cache poisoning on the LAN. Situational, and needs a man-in-the-middle position. Bettercap answers chosen domains with your IP.
    # arp.spoof gets between the victim and the gateway, dns.spoof forges the replies
    sudo bettercap -iface eth0 -eval "set dns.spoof.domains $DOMAIN; set dns.spoof.address <you>; arp.spoof on; dns.spoof on"
    The same steps run one at a time inside the bettercap prompt:
    sudo bettercap -iface eth0
    # at the bettercap prompt:
    set dns.spoof.domains $DOMAIN,*.$DOMAIN
    set dns.spoof.address <you>
    set arp.spoof.targets <victim>
    arp.spoof on
    dns.spoof on
    dns.spoof.domains is what to forge (the wildcard also catches subdomains), dns.spoof.address is the IP handed back, and arp.spoof.targets is the victim. Without targets, arp.spoof poisons the whole subnet. Covered deeper in the MITM material.

Subdomain takeover

A subdomain takeover happens when a subdomain’s CNAME points to a third-party host that no longer exists. The organization once pointed shop.example.com at a provider such as an S3 bucket, GitHub Pages, or an Azure site, then deleted the resource but left the DNS record. The name still resolves to the provider, but nothing there answers for it. Register that resource yourself and you serve content on shop.example.com.

The impact is high because users and apps trust the name. You can phish under the real domain and read cookies scoped to the parent domain.

Find the danglers. Put the subdomains from enumeration in subs.txt, one host per line, then read their CNAME targets:

dig cname shop.$DOMAIN @$TARGET # show where the alias points

A target at a known provider, with a service error on the page, is the tell. For S3 it is NoSuchBucket. Scan subs.txt for these fingerprints:

subzy run --targets subs.txt # flag takeover-able subdomains
nuclei -l subs.txt -t http/takeovers/ # same, nuclei templates

Claim it. Match the fingerprint against can-i-take-over-xyz, which lists each provider and whether it is exploitable. Register the same resource name at that provider, then host your content.

Practice boxes

Box Difficulty DNS’s role
Cronos Easy Zone transfer reveals admin.cronos.htb, the foothold.
FriendZone Easy Zone transfer reveals subdomains leading to an LFI-to-RCE panel.
Bank Easy Zone transfer reveals the bank.htb vhost to enumerate.

DNS is almost always recon, not the foothold itself. The win is the hostnames and subdomains it leaks, which point you at the real target.