Skip to content
Cache bugs Write-up Advanced

Nginx DNS resolver vulnerabilities allow cache poisoning attack

Nginx's built-in DNS resolver generates predictable transaction IDs and reuses the same UDP source port for all queries, allowing an attacker to spoof DNS replies and poison the resolver cache. This causes nginx to proxy HTTP requests to an attacker-chosen server, potentially serving malicious content.

blog.zorinaq.com

Published on blog.zorinaq.com

Never configure nginx with the resolver directive pointing to a resolver on the Internet like Google Public DNS, OpenDNS, or your ISP’s resolver. Many nginx users make this exact mistake.

The author, on blog.zorinaq.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

Nginx used a non-cryptographic random number generator for DNS transaction IDs and reused a single UDP source port per worker, making both predictable.

Impact

An attacker could poison nginx's DNS cache and cause it to proxy requests to a server of their choice, potentially serving malicious content; the attack works on all nginx versions with the resolver directive, though some issues were later patched.

What the attacker needed

Nginx configured with resolver directive; attacker can send spoofed DNS packets; attacker knows hostnames and resolver

The fix

Nginx should use a cryptographically secure random number generator for DNS transaction IDs and randomize the UDP source port for every DNS query; some issues were fixed but others remain unpatched.

How it was found

  1. The attacker determines which hostnames nginx resolves and which resolver it uses.
  2. The attacker predicts the next DNS transaction ID (e.g., by leaking random() values or knowing the PRNG sequence).
  3. The attacker waits for the cached DNS entry to expire and nginx to send a new query.
  4. During the query window, the attacker sends many spoofed DNS replies to different UDP ports with the predicted transaction ID.
  5. If the spoofed reply arrives first and matches, nginx caches the attacker's IP address.
  6. Nginx then proxies requests to the attacker's server until the TTL expires.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

When nginx needs to look up a domain name, it sends a DNS query and expects a reply with a matching random number (the transaction ID) and from the same port. If an attacker can guess that number and port, they can send a fake reply before the real one arrives, tricking nginx into sending traffic to a server the attacker controls. This report shows that nginx's numbers and ports were too predictable.

What is Cache bugs?

A cache and the origin disagree about a request, so a poisoned response is served to everyone or a private page is stored as public.

Similar reports

View all

Same bug class and techniques, clearest first.

Web cache poisoning and deception

The lesson behind this report.

A cache and the origin disagree about a request, so a poisoned response is served to everyone or a private page is stored as public.