Sub topic 16
Encrypted DNS: DoT and DoH
This chapter assumes you know how a stub resolver talks to a recursive resolver, how DNS messages travel over UDP and TCP on port 53, and what DNSSEC does. It also assumes you can read basic dig output.
The problem encrypted DNS solves
Classic DNS sends queries and answers in plain text. Anyone on the path between your device and your recursive resolver can read the names you look up. They can also modify the answers, unless DNSSEC signatures let you detect that.
DNSSEC covers integrity of the data. It does not provide confidentiality. A coffee-shop network operator, your ISP, or anyone on the path can still see that your device asked for clinic.example.com, even if the later web connection is encrypted.
Encrypted DNS closes that gap on one segment only: the link between the stub resolver (or client) and the recursive resolver. Two standards do this:
- DNS over TLS (DoT), defined in RFC 7858. It wraps the DNS wire format in a TLS session.
- DNS over HTTPS (DoH), defined in RFC 8484. It carries DNS messages inside HTTPS requests.
What each protocol protects
| Threat | Plain DNS | DNSSEC | DoT or DoH |
|---|---|---|---|
| Observer reads your queries on the path | Exposed | Exposed | Hidden from the path |
| Attacker alters an answer in transit | Possible | Detected, if the zone is signed and you validate | Prevented on the encrypted hop |
| Cache poisoning at the resolver | Possible | Detected, if the zone is signed | Not addressed |
| Resolver operator sees your queries | Yes | Yes | Yes |
| Authoritative server sees the resolver's IP | Yes | Yes | Yes |
The two mechanisms complement each other. DNSSEC authenticates the data from the zone owner. Encrypted DNS protects the transport to your resolver. Neither one hides the IP addresses you connect to afterward, and the destination IP often reveals the site anyway.
DNS over TLS
DoT opens a TCP connection to the resolver on port 853, performs a TLS handshake, and then sends ordinary DNS messages inside the TLS stream. Each message keeps the two-byte length prefix that DNS over TCP already uses.
Properties of DoT:
- It uses a dedicated port, so a network operator can see that you use DoT and can block it by dropping port 853.
- The DNS payload is identical to plain DNS, so existing DNS tooling and parsing code carry over.
- A long-lived connection avoids repeating the TCP and TLS setup. Clients should keep the session open and send several queries on it.
Authenticating the resolver
Encryption without authentication only stops passive observers. An active attacker could impersonate the resolver and read everything. RFC 7858 defines two usage profiles:
- Strict: the client has a configured resolver hostname (or a pinned public key) and fails the connection if the certificate does not match. If it fails, no DNS happens.
- Opportunistic: the client tries TLS and falls back to plain DNS if that fails, or accepts an unverified certificate. This resists passive monitoring but not an active attacker, and a downgrade attacker can simply block port 853.
Prefer strict mode when you control the configuration.
DNS over HTTPS
DoH sends each DNS message as an HTTPS request on port 443, to a URI template such as https://dns.example.net/dns-query. The media type is application/dns-message. The request can use either method:
POSTwith the binary DNS message as the body.GETwith the message base64url-encoded in adnsquery parameter.
The DNS ID field should be set to 0 in DoH requests to make responses cacheable by HTTP caches, because HTTP already matches each response to its request.
Because DoH uses HTTP, it benefits from HTTP/2 multiplexing, so several queries can be in flight on one connection. It also traffics on the same port and protocol as ordinary web browsing, so it is hard to tell apart from other HTTPS traffic without inspecting the destination.
A DoH query from Python
The script below sends a DoH query using only the standard library. It builds a minimal DNS query for the A record of example.com, posts it, and prints the header fields and the addresses in the answer. It uses Cloudflare's public endpoint; replace the URL to use another provider.
import socket
import struct
import urllib.request
URL = "https://cloudflare-dns.com/dns-query"
def build_query(name: str, qtype: int = 1) -> bytes:
# ID 0, flags 0x0100 (recursion desired), one question.
header = struct.pack("!HHHHHH", 0, 0x0100, 1, 0, 0, 0)
qname = b"".join(
bytes([len(label)]) + label.encode("ascii")
for label in name.rstrip(".").split(".")
) + b"\x00"
return header + qname + struct.pack("!HH", qtype, 1)
def skip_name(msg: bytes, pos: int) -> int:
while True:
length = msg[pos]
if length == 0:
return pos + 1
if length & 0xC0 == 0xC0: # compression pointer
return pos + 2
pos += 1 + length
def parse_answers(msg: bytes):
_id, flags, qd, an, _ns, _ar = struct.unpack("!HHHHHH", msg[:12])
rcode = flags & 0xF
pos = 12
for _ in range(qd):
pos = skip_name(msg, pos) + 4
addresses = []
for _ in range(an):
pos = skip_name(msg, pos)
rtype, _cls, ttl, rdlen = struct.unpack("!HHIH", msg[pos:pos + 10])
pos += 10
rdata = msg[pos:pos + rdlen]
pos += rdlen
if rtype == 1:
addresses.append((socket.inet_ntop(socket.AF_INET, rdata), ttl))
return rcode, addresses
request = urllib.request.Request(
URL,
data=build_query("example.com"),
headers={
"Content-Type": "application/dns-message",
"Accept": "application/dns-message",
},
method="POST",
)
with urllib.request.urlopen(request, timeout=5) as response:
body = response.read()
rcode, addresses = parse_answers(body)
print("rcode:", rcode)
for address, ttl in addresses:
print(address, "ttl", ttl)The output lists the A records and their remaining TTLs. The network observer sees only a TLS connection to cloudflare-dns.com, not the name example.com.
You can also test DoH from the command line. Recent versions of dig (9.18 and later) support both transports:
dig +tls @1.1.1.1 example.com A
dig +https @1.1.1.1 example.com AThe first command uses DoT on port 853. The second uses DoH with the default path /dns-query. Tools such as kdig from Knot DNS offer the same with +tls and +https.
DoT versus DoH
| Aspect | DoT | DoH |
|---|---|---|
| Port | 853 | 443 |
| Transport | TLS over TCP | HTTPS (HTTP/2 or HTTP/3) |
| Visible as DNS to the network | Yes, by port | No, looks like web traffic |
| Easy for administrators to block or monitor | Yes | Harder |
| Where it is configured | Usually the operating system | Often inside an application such as a browser |
| Overhead | Lower | HTTP headers add some bytes |
The difference that matters most in practice is control. DoT is a system-level feature. A network administrator can allow or block it by port, and every application on the host uses it through the OS. DoH is often implemented in the application, so a browser can use its own resolver and bypass the system's configured DNS, the hosts file excluded. That bypass is a deliberate design choice for privacy, but it surprises people in several situations.
Operational gotchas
- Split-horizon and internal names. If a browser sends queries to a public DoH resolver, names that exist only on your corporate or home network, such as
printer.corp.example, return NXDOMAIN. Browsers typically include a canary domain or a policy setting so networks can disable application-level DoH. - Filtering and parental controls. DNS-based filters configured on the local resolver are skipped when an application uses its own DoH resolver.
- Bootstrap problem. A DoT or DoH client must first learn the resolver's IP address. If you configure
dns.example.net, something has to resolve that name, usually via plain DNS or a hard-coded IP in the configuration. Configuring the IP address together with the hostname for certificate checking avoids the circularity. - Certificate validation. Strict mode needs a correct clock and a trusted CA store. A device with a wrong date fails TLS and, in strict mode, loses name resolution.
- Captive portals. Hotel or airport login pages rely on intercepting plain DNS and HTTP. Encrypted DNS can prevent the redirect, so the user must first sign in by other means or temporarily fall back.
- Centralization. Pointing every client at a few large DoH providers moves visibility from many ISPs to a few operators. Your choice of resolver is a choice of whom to trust with your query history.
- Residual leaks. The TLS Server Name Indication (SNI) field in the later connection to the website may expose the site name, unless Encrypted Client Hello is in use. Destination IP addresses also remain visible.
Discovery and upgrade
Hard-coding a resolver is not the only option. Two mechanisms let a client upgrade to encryption automatically:
- Discovery of Designated Resolvers (DDR, RFC 9462): a client queries the special name
_dns.resolver.arpafor SVCB records at its current plain-text resolver. The answer lists encrypted endpoints (DoT, DoH, or DNS over QUIC) operated by the same resolver, and the client can switch to them after verifying the certificate covers the resolver's IP address. - DHCP and Router Advertisement options (RFC 9463): a network can announce its own encrypted resolver to clients.
A third protocol, DNS over QUIC (DoQ, RFC 9250), uses UDP-based QUIC on port 853 and avoids TCP head-of-line blocking, but support is still narrower than DoT and DoH.
Checking that it works
To confirm that your device is really using an encrypted transport, capture traffic and look for port 53:
sudo tcpdump -n -i any port 53 or port 853Run a lookup in another terminal. With DoT you should see TCP traffic on port 853 and none on port 53. With DoH you will see no DNS-specific traffic at all, only TLS to the provider on port 443, so you need to check the application's settings or the provider's test page instead. If port 53 packets still appear, some application or the OS is bypassing your encrypted configuration.
Exercise
A laptop is configured with strict DoT to dns.example.net. At a hotel, the user cannot reach the captive-portal login page, and a packet capture shows repeated TCP SYN packets to port 853 with no replies. Name the likely cause, say why strict mode makes it worse than opportunistic mode would, and suggest a fix.
Show answer
The hotel network drops outbound port 853, so the TLS session never starts. In strict mode the client refuses to fall back to plain DNS, so every lookup fails, including the lookup needed to reach the login page. An opportunistic client would have fallen back to port 53 and the portal would have loaded. Fixes: temporarily switch the device to the network's own resolver or turn off encrypted DNS until you have signed in, then turn it back on. Alternatively, use DoH on port 443 if the network lets it through.
Exercise
Your company runs an internal zone corp.example that only the internal resolver can answer. After a browser update, users report that internal sites fail but public sites work. What is happening and how can you address it?
Show answer
The browser now sends queries to a public DoH resolver, which has no knowledge of the internal zone and returns NXDOMAIN. Address it by disabling application-level DoH through the browser's enterprise policy, by answering the browser's canary domain with NXDOMAIN so it turns DoH off on that network, or by running an internal DoH resolver that knows corp.example and pointing the browser at it.
Exercise
Which of these does DoH on its own protect against: (a) an ISP logging your lookups, (b) a forged answer from an attacker who has poisoned the recursive resolver's cache, (c) the recursive resolver operator logging your lookups?
Show answer
Only (a). DoH hides the queries from the path between you and the resolver, so the ISP cannot read them. It does not detect cache poisoning inside the resolver (DNSSEC validation addresses that), and the resolver operator still sees every query.