Skip to content
Tutorials Center
My learning

Topics

Lesson 47 of 79 ยท Application Layer: Names and Web

DNS in Practice: Zones, Security and Privacy

Read and write DNS zone files, understand glue and propagation, and learn how DNSSEC, DoT and DoH protect DNS, how cache poisoning works, and DNS load balancing.

The previous lesson showed how a name is resolved. This lesson looks at DNS from the operator's side: how a domain's records are written down in a zone file, how changes reach the world, and how to answer three security questions. Can an answer be forged? Can anyone watch which names I look up? And can DNS be used to spread traffic over many servers?

DNS was designed in the 1980s for a friendly network, with no encryption and no authentication. Almost every protection you will read about here was added later and sits on top of the original design.

Zone files

A zone is the part of the DNS tree that one set of authoritative servers is responsible for. Its records are stored in a text format, the master file or zone file, defined in the original DNS standards and still used by BIND, NSD, Knot and others. (Many providers hide it behind a web form, but the records are the same.)

TEXT
$ORIGIN example.com.
$TTL 3600
@     IN SOA ns1 hostmaster ( 2026103101 7200 900 1209600 300 )
      IN NS  ns1
@     IN MX  10 mail
www   IN CNAME @
ns1   IN A   198.51.100.53

The rules that trip up newcomers:

  • $ORIGIN is the suffix appended to every name that does not end with a dot. So mail means mail.example.com., while mail.example.com (no final dot) would become mail.example.com.example.com., a very common mistake.
  • @ stands for the origin itself, the apex of the zone.
  • A line that starts with a space or tab reuses the previous record's owner name.
  • $TTL is the default TTL; a record may state its own TTL before the class (api 60 IN A ...).
  • ; starts a comment, and parentheses let one record span several lines. The SOA (start of authority) record uses that: it holds the primary server, the contact address (with the @ written as a dot), and timers. The serial number must grow whenever the zone changes; secondary servers compare serials to decide whether to copy the zone again. Dates in the form YYYYMMDDnn are a popular convention.

The parser below does exactly what a name server does when it loads a zone: it joins parenthesized lines, strips comments, applies $ORIGIN and $TTL, expands relative names and then checks the result. The zone contains two deliberate details: a name server inside the zone it serves, and a CNAME mistake.

Python Parse a zone file and check it

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The checker caught the mistake. A CNAME says "this name is an alias for another", which cannot be true if the same name also has a TXT record, so a name that owns a CNAME may have no other data. This is the same reason you cannot place a CNAME at the apex, where the SOA and NS records must live.

Delegation and glue

A zone is delegated to its name servers by NS records held in the parent zone. For example.com, the .com servers store NS records that say "for anything under example.com, ask ns1.example.com". That raises a chicken-and-egg problem: to find ns1.example.com you must already be able to resolve names in example.com.

The solution is glue: the parent also stores the address of the name server (ns1.example.com A 198.51.100.53) and hands it out in the referral, as you saw in the trace in the previous lesson. Glue is needed only when a name server's name lies inside the zone it serves; ns2.example.net needs no glue because it can be resolved separately.

You do not edit the parent zone yourself. You tell your registrar, who passes the NS records (and glue) to the registry that runs the TLD.

Propagation is just TTL

People speak of DNS changes "propagating" for 24 or 48 hours as if the new data were being pushed around the world. Nothing is pushed. When you change a record at your authoritative server, the new data is available immediately; what takes time is the expiry of old copies in caches. Each cache holds the old answer until its own TTL runs out, and each cached it at a different moment, so users switch over gradually.

Changing the NS records of a domain is slower because the parent zone (for example .com) publishes the delegation with its own long TTL, commonly 48 hours.

The first simulation models 1,000 resolvers, each of which cached the old record at a random moment. The second one computes how many forged replies an attacker would need, which leads into the next section.

Python How long do changes take, and how hard is forging?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the TTL 300 column: after five minutes every cache has the new data, while with a one-day TTL half of the world still sees the old address after 12 hours. This is why you lower the TTL before a migration. The second table is a preview of the next section.

Security: cache poisoning

A classic DNS exchange has almost no authentication. A resolver sends a UDP query and accepts the first reply that looks right: the right source address, the right destination port and the right 16-bit transaction ID. An attacker who can guess these can send forged replies and make the resolver cache a false record, which is cache poisoning. Every user of that resolver is then sent to the attacker's server until the TTL expires.

With only a 16-bit ID, the table above shows the problem: tens of thousands of forged packets give a coin-flip chance, which is nothing for an attacker. In 2008 Dan Kaminsky showed an even more efficient variant that repeatedly asks for non-existent names under a target domain to get unlimited attempts. The industry response was source port randomization: the query leaves from a random UDP port, so the attacker has to guess about 32 bits instead of 16, making the attack far more expensive. Other defenses:

  • 0x20 encoding. The resolver randomly changes the capitalization of the query name (ExAmPlE.cOm) and requires the reply to repeat it exactly. This adds entropy for free.
  • Bailiwick checking. A resolver ignores extra records in a reply that are outside the zone the server is responsible for.
  • DNS cookies. A small shared secret exchanged between client and server.
  • DNSSEC, which fixes the root cause: replies can be verified, not just guessed.

DNSSEC: signing the data

DNSSEC adds digital signatures to DNS. It authenticates answers (origin and integrity); it does not encrypt them. The new record types are:

Type Purpose
DNSKEY The zone's public key
RRSIG A signature over a set of records, made with the zone's private key
DS A hash of the child's key, stored in the parent zone
NSEC / NSEC3 Signed proof that a name does not exist

A validating resolver builds a chain of trust from a trust anchor, the root zone's public key, which is built into its software. The root signs the DS record for com, com signs the DS record for example.com, and example.com signs its own records. If any signature fails to verify, the resolver returns an error (SERVFAIL) instead of the data.

flowchart TB
    TA["Trust anchor<br/>root DNSKEY (built in)"] --> RS["Root zone<br/>DS for com + RRSIG"]
    RS --> CS["com zone<br/>DS for example.com + RRSIG"]
    CS --> ES["example.com zone<br/>DNSKEY, RRSIG on A, MX ..."]
    ES --> ANS["Verified answer"]

Each level vouches for the key of the level below, from the root down to the record you asked for.

DNSSEC is valuable but unevenly deployed: signing must be done correctly, expired signatures or a botched key rollover can take a whole domain offline, and answers get larger. It also protects only the answer. The path between you and your resolver is still unprotected unless you add encryption.

Security: privacy and encrypted DNS

Ordinary DNS queries are plain text over UDP port 53. Anybody on the path (your Wi-Fi operator, your ISP) sees every name you look up, even when the website itself uses HTTPS. They can also tamper with replies. Two standards encrypt the link between a client and its resolver:

  • DNS over TLS (DoT) wraps DNS messages in a TLS connection on a dedicated port, TCP 853. It is easy for a network administrator to identify and manage.
  • DNS over HTTPS (DoH) sends DNS messages as HTTPS requests on port 443. It looks like ordinary web traffic, which makes it hard to block and also hard to monitor. Browsers can use it with a resolver of their choice.
  • DNS over QUIC (DoQ) carries DNS inside QUIC with similar benefits.
TEXT
$ kdig @1.1.1.1 +tls www.example.com A      (DoT, port 853)
$ curl -H 'accept: application/dns-json' \
    'https://cloudflare-dns.com/dns-query?name=www.example.com&type=A'   (DoH)

Encryption hides the questions from the network but not from the resolver itself, which sees everything. So the privacy question moves to whom you trust as a resolver. Also, DoH and DoT protect only the hop to the resolver; the resolver's queries to authoritative servers are still plain unless other protocols are used. (The TLS handshake may also reveal the site name in its SNI field.) Encrypted DNS and DNSSEC solve different problems and work well together.

Load balancing and traffic steering with DNS

Because DNS decides which address a client receives, it is a cheap and powerful way to spread or steer traffic.

  • Round robin. Publish several A records for one name; the server rotates their order. Simple, but it has no idea whether a server is healthy or how much load each one carries.
  • Weighted and health-checked answers. Managed DNS services return only the servers that pass health checks, with weights such as 70/30.
  • Geographic (geo-DNS) and latency-based routing. The answer depends on where the resolver is located, sending users to the nearest data center or CDN edge.
  • Anycast. The same IP address is announced from many places with BGP; DNS itself is the main user of this for its root servers and big resolvers.

The limits of DNS balancing come from caching. A resolver that caches an answer sends all of its users to whichever server it received first, and clients, browsers and operating systems may ignore the order too. A low TTL (30 to 60 seconds) makes failover faster, at the price of more queries. The next program shows how shared caches skew a round robin.

Python Round-robin DNS and shared caches

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For real load balancing, DNS normally selects a region or a group of load balancers, and the load balancers do the fine-grained work per connection.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In a zone file for example.com., what does the owner name 'mail' (without a final dot) mean?

    Names without a trailing dot are relative and have $ORIGIN appended. Writing mail.example.com without the final dot would wrongly give mail.example.com.example.com.
  2. 2. When is glue required?

    If ns1.example.com serves example.com, resolvers cannot look up its address without already using it, so the parent supplies the address as glue.
  3. 3. You change the A record of a site with a TTL of 3600 seconds. What is the most accurate description of what happens?

    Propagation is cache expiry. Each resolver returns the old address until its cached copy reaches its TTL, so the worst case is about one TTL.
  4. 4. What does DNSSEC provide?

    DNSSEC signs records so a resolver can verify they are genuine and unmodified. It does not hide the queries. For that you need DoT, DoH or DoQ.
  5. 5. Why did source port randomization make cache poisoning harder?

    A forged reply must match the ID and the port the query was sent from. Randomizing the port multiplies the number of guesses needed by roughly 64,000.

Summary

  • A zone file holds a zone's records: $ORIGIN and $TTL set defaults, @ is the apex, relative names get the origin appended, and the SOA serial must grow with every change. A CNAME cannot share its name with other records.
  • Delegation is done with NS records in the parent zone; glue supplies addresses for name servers that live inside the zone they serve.
  • "Propagation" is the expiry of cached copies; plan changes by lowering the TTL in advance.
  • Cache poisoning exploits guessable query IDs; defenses are random ports and IDs, 0x20 encoding, bailiwick checks and DNSSEC.
  • DNSSEC authenticates answers with a chain of trust from the root. DoT and DoH encrypt the client-to-resolver link. They solve different problems.
  • DNS can balance load by round robin, health checks, geography or anycast, but caching makes it coarse.

Now that you can find a server by name, learn how to talk to it with HTTP Fundamentals.