DNS and DHCP
DNS translates names into IP addresses, and DHCP gives each machine its address, default gateway and DNS server. Nobody looks at either service until it breaks, and that’s exactly why they’re a goldmine: DNS records sketch an organisation’s infrastructure, /etc/hosts redirects traffic without touching the network, and a rogue DHCP server decides where an entire network’s traffic goes.
Concepts
Section titled “Concepts”The hierarchy
Section titled “The hierarchy”DNS is a distributed database shaped like a tree. No server holds every name: each level knows who to ask about the next one.
- Root (
.): knows where each TLD’s servers are. - TLD (
.com,.es,.org): knows which servers are authoritative for each domain. - Authoritative (
ns1.example.com): holds the domain’s actual records. - Recursive resolver: the server your machine uses (the router’s, the company’s, 1.1.1.1). It does the whole walk for you and caches the answers.
Resolution order
Section titled “Resolution order”flowchart TB
A["Program: IP of www.example.com?"] --> B["Local cache"]
B -->|"not there"| C["/etc/hosts"]
C -->|"not there"| D["Configured resolver (DHCP or /etc/resolv.conf)"]
D -->|"not in its cache"| E["Root server"]
E --> F[".com TLD servers"]
F --> G["example.com authoritative NS"]
G -->|"A 93.184.215.14, TTL 300"| D
D --> A
Every answer carries a TTL (seconds it may be cached). That’s why a DNS change doesn’t show up everywhere instantly.
On Linux, the order between files and DNS is set by the hosts: line in /etc/nsswitch.conf (usually files comes before dns).
Record types
Section titled “Record types”| Record | What it holds | Why you care |
|---|---|---|
| A · AAAA | Name → IPv4 · IPv6 | The real target. AAAA can reveal services exposed only over IPv6. |
| CNAME | Alias from one name to another | A CNAME pointing to a deprovisioned cloud service is a subdomain takeover. |
| MX | Mail servers | Mail provider, phishing targets. |
| NS | Authoritative servers | Who to ask (and who to try an AXFR against). |
| TXT | Free text | SPF, DKIM, DMARC, third-party service verifications. |
| SOA | Zone administrative data | Primary server and the admin’s email. |
| PTR | IP → name (reverse DNS) | Host names from a range of IPs. |
| SRV | Service → host and port | In Active Directory, how domain controllers are found. |
UDP and TCP
Section titled “UDP and TCP”Normal queries use UDP 53: one small question and one small answer. TCP 53 is used when the answer doesn’t fit and for zone transfers (AXFR), where a server hands over the whole zone. A server that allows AXFR to anyone gives you the domain’s full list of names.
/etc/hosts
Section titled “/etc/hosts”/etc/hosts (on Windows, C:\Windows\System32\drivers\etc\hosts) is checked before any DNS server. Two consequences:
- In labs you’ll use it constantly: machines answer to names (
domain.htb, vhosts) that don’t exist in any public DNS. - On a suspicious machine, an entry added to
hostsredirects traffic without touching the network: it’s one of the first things to check.
DHCP hands out network configuration automatically. A machine that has just connected has no IP, so everything starts with broadcast (UDP 67 on the server, 68 on the client):
sequenceDiagram
participant C as Client (no IP)
participant S as DHCP server
C->>S: DISCOVER (broadcast): is there a server?
S-->>C: OFFER: I can give you 192.168.1.50
C->>S: REQUEST (broadcast): I'll take 192.168.1.50
S-->>C: ACK: it's yours for 24 h
The server hands out IP, mask, default gateway, DNS servers and a lease with an expiry. If nobody answers, the machine self-assigns a 169.254.x.x (link-local) address: a clear sign that DHCP failed. See IP addressing.
In practice
Section titled “In practice”# Basic queries: +short returns just the datadig +short example.com Adig +short example.com MXdig +short example.com TXTdig +short -x 93.184.215.14 # reverse (PTR)
# Full answer: look at the ANSWER SECTION and the TTLdig example.com A# ;; ANSWER SECTION:# example.com. 300 IN A 93.184.215.14# ^ TTL in seconds
# Ask a specific server (compare with yours if you suspect it)dig @1.1.1.1 example.com A
# Zone transfer against a lab machine's DNSdig axfr domain.htb @10.10.10.5# Lab names that don't exist in any DNSecho '10.10.10.5 domain.htb admin.domain.htb' | sudo tee -a /etc/hostsgetent hosts admin.domain.htb # resolves the same way your tools will# Which DNS and which DHCP lease do I have?cat /etc/resolv.conf # nameserver ...resolvectl status 2>/dev/null # if you use systemd-resolvedip -4 a # "dynamic" next to the IP = it came from DHCPNext offensive step
Section titled “Next offensive step”- Find a real target’s subdomains: subdomain recon.
- Claim an orphaned CNAME: subdomain takeover.
- Attack the protocol (spoofing, poisoning, rebinding): DNS attacks.
Cheat sheet
Section titled “Cheat sheet”| Concept | What to remember |
|---|---|
| Order | Cache → /etc/hosts → resolver → root → TLD → authoritative. |
| TTL | How long an answer is cached. |
| A · CNAME · MX · TXT · NS · PTR · SRV | IP · alias · mail · text · authoritative · reverse · services (AD). |
| UDP · TCP 53 | Normal queries · large answers and AXFR. |
| Open AXFR | The whole zone. Always try it in labs. |
| /etc/hosts | Beats DNS. dig ignores it; getent doesn’t. |
| DHCP | DISCOVER → OFFER → REQUEST → ACK. IP, mask, gateway, DNS. |
| 169.254.x.x | DHCP didn’t answer. |
| No authentication | DNS without DNSSEC and DHCP: spoofable on the local network. |
Review questions
Section titled “Review questions”-
In what order does your machine look up the IP of a name?
Answer
Local cache,
/etc/hosts(pernsswitch.conf), then the configured resolver, which—if it doesn’t have it cached—asks the root, the TLD and the authoritative server. -
You add
10.10.10.5 domain.htbto/etc/hosts, butdig domain.htbreturns nothing. Why?Answer
Because
digqueries the DNS server directly and doesn’t read/etc/hosts. The browser,curlorgetent hostswill resolve it. -
Why does DNS switch from UDP to TCP for a zone transfer?
Answer
Because a full zone doesn’t fit in a single UDP datagram and needs reliable, ordered delivery.
-
A machine has
169.254.10.4. Which service failed?Answer
DHCP: it got no answer to its DISCOVER and self-assigned a link-local address.
-
Which four network settings does a DHCP server usually hand out?
Answer
IP address, mask, default gateway and DNS servers (plus the lease duration).
-
What does an SRV record do for you on a Windows network?
Answer
Machines locate domain controllers through SRV records (for example
_ldap._tcp.dc._msdcs.domain). Querying them tells you which machines are DCs.