Skip to content
Memory corruption Write-up Advanced

Nginx Rift: Remote Code Execution via an 18-Year-Old Vulnerability

A heap buffer overflow in NGINX's rewrite module occurs when a rewrite directive with a question mark is followed by a set directive referencing a capture group, causing a length calculation mismatch. An attacker could send a crafted request to overflow the heap and potentially achieve remote code execution, though the proof of concept required ASLR to be disabled.

depthfirst.com

Published on depthfirst.com

In mid-April, I was chatting with a colleague about the most vulnerable spot in our infrastructure. Since most of our services live entirely inside a private network, our app platform is the only exposed surface.

The author, on depthfirst.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

The script engine's is_args flag is set by a rewrite directive with a question mark but is not reset before a subsequent set directive's length calculation, which uses a zeroed sub-engine, so the length pass underestimates the buffer size while the copy pass escapes characters and overflows.

What the attacker needed

NGINX config uses rewrite with ? and set with capture; Attacker can send HTTP requests; ASLR must be bypassed or disabled for RCE

The fix

The text does not state a fix; it was reported to NGINX and an advisory was released, but the fix itself is not described.

How it was found

  1. Open an initial connection and send partial headers to allocate a request pool.
  2. Open a second victim connection to allocate a victim pool adjacent to the first.
  3. Complete the initial headers to trigger the rewrite overflow into the victim pool header.
  4. Immediately close the victim connection to destroy the victim pool.
  5. Spray the heap with POST bodies containing fake cleanup structures.
  6. Close the victim socket to trigger ngx_destroy_pool and execute the injected command.

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.

NGINX is a web server that many websites use. A bug in how it handles certain configuration commands (rewrite and set) lets an attacker send a request that writes more data into memory than was reserved, which can crash the server or let the attacker run their own code. The proof of concept only worked when a security feature called ASLR was turned off.

What is Memory corruption?

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.