DNS RESOLUTION
The distributed phone book of the internet
api.example.com has a TTL (how long resolvers may cache it) of 86400 seconds. At noon you change its A record and switch the old server off. A resolver cached the old record at 11:00. When does it start returning the new address?
A client asks a recursive resolver. On a miss, the resolver asks a root server, which refers it to the .com servers, which refer it to the domain’s authoritative server, which answers. The resolver caches each answer for the TTL the authority set and answers later clients from cache.
The cache is the point and the risk. A resolver keeps an old record for its full TTL, whatever you change upstream. It also accepts the first response that matches its outstanding query by transaction ID, port and name. If those are guessable, an off-path attacker can answer first, and the forgery is cached for everyone.
// resolver
const e = cache.get(name)
if (e) return e.addr // ignores expiry
send(query, { srcPort: 53, id: nextId++ }) // guessable
const r = await firstResponse() // accepts anyone's
cache.set(name, { addr: r.addr, expires: now + r.ttl })const e = cache.get(name)
if (e && now < e.expires) return e.addr // honour the TTL
const q = { id: rand16(), srcPort: randPort(), name } // ~2^32 to guess
send(q)
const r = await responses.find(r =>
r.id === q.id && r.dstPort === q.srcPort && r.name === q.name)
// DNSSEC: also verify r's RRSIG chain to the root
cache.set(name, { addr: r.addr, expires: now + r.ttl })
// operator, before a migration:
// lower the TTL, wait out the OLD TTL, then change the record Watch the RESOLVER box and the invariant strip. COLD → WARM CACHE makes 3 upstream queries, then 0, then 1 after the TTL expires, because the .com delegation is still cached. MIGRATION, TTL 1 DAY sends a client to the switched-off address 2600 s after the change. LOWER TTL FIRST makes the same change with no stale answer. POISONING lands a forgery on the first guess against port 53 and sequential IDs. RANDOM PORT + ID survives 100 forgeries that now have to guess both the port and the ID.
The record has a 3600 s TTL. Inject one fault so that a client is sent to an address that has been switched off for more than 5 minutes, or to a forged one.
GOAL: a client gets a dead or forged address
Make resolve honour the TTL (an entry is valid while now < expires) and accept only a response that matches the outstanding query: same ID, same port, same name. If nothing matches, return null and cache nothing.
Cutovers and failovers that “take a while to propagate”, dig +trace and the TTL column in dig output, CDN and load balancer records with 60 s TTLs, JVMs and other clients that cache DNS longer than the TTL, and DNSSEC validation in resolvers like Unbound and 1.1.1.1.
“What happens when you type a URL?” (the DNS part) and “we changed DNS and some users still hit the old server, why?” Walk root → TLD → authoritative, explain caching per TTL, give the lower-TTL-first migration plan, and describe cache poisoning: what has to be guessed, why randomization helps, and what DNSSEC adds.
USED IN
Chapters that build on this lesson.
- Network philosophies: simple, observable, and every change with a way backNetwork Engineer
- Troubleshooting a live faultNetwork Engineer
- Why network engineers exist when the cloud hides the networkNetwork Engineer