Skip to content
RCE Write-up Advanced

MongoDB Zero Day Exploit

A MongoDB server-side JavaScript injection flaw allowed calling an internal nativeHelper function with attacker-controlled arguments, leading to a crash and potentially remote code execution. The author developed a working exploit that bypasses NX and ASLR on 32-bit x86 by using heap spraying and a ROP chain.

blog.scrt.ch

Published on blog.scrt.ch

Trying some server side javascript injection in mongodb, I wondered if it would be possible to pop a shell. The run method seems good for this :

The author, on blog.scrt.ch

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

Likely the nativeHelper function in MongoDB's SpiderMonkey engine trusts a numeric property from a JavaScript object as a function pointer and calls it without validation.

Impact

An attacker could crash the MongoDB server and potentially execute arbitrary code, though the exploit was demonstrated only on 32-bit x86 and required the vulnerable JavaScript engine.

What the attacker needed

MongoDB with SpiderMonkey engine; Ability to run $where queries; 32-bit x86 target

The fix

The default JavaScript engine was changed in a later version, removing the nativeHelper.apply function, but no patch was committed for the reported issue.

How it was found

  1. Use a $where query to call nativeHelper.apply with a crafted object and arguments.
  2. Observe that the server crashes when the object's x property is an invalid address.
  3. Debug the crash to find that eax points to the BSONObj containing the arguments.
  4. Find a gadget that dereferences eax and calls a controlled address, then pivot the stack.
  5. Heap spray with NOP sled and shellcode, then use a ROP chain to call mmap64 and mark memory executable.
  6. Trigger the exploit via nativeHelper.apply with the crafted object and arguments.

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.

MongoDB lets you run JavaScript on the server for some queries. The author found that a hidden function called nativeHelper could be tricked into calling any memory address, which crashed the server and could let an attacker run their own code. This is dangerous because it gives an attacker control over the database server.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.