How we exploited a remote code execution vulnerability in math.js
The math.js API allowed evaluation of JavaScript expressions, and the sandbox could be escaped by accessing the Function constructor indirectly through an existing function's constructor property. An attacker could then access the Node.js process object, read files, and spawn arbitrary processes, achieving remote code execution.
Published on jwlss.pw
This article explains in short how we found, exploited and reported a remote code execution (RCE) vulnerability. It is meant to be a guide to finding vulnerabilities, as well as reporting them in a responsible manner.
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 math.js expression evaluator allowed access to JavaScript's Function constructor through the constructor property of existing functions, which enabled escaping the sandbox and executing arbitrary code.
Impact
An attacker could execute arbitrary code on the server, read files, and run shell commands, potentially leading to full server compromise.
What the attacker needed
Access to the math.js API
The fix
The vulnerability was fixed by the maintainer after being reported, and a follow-up bypass using .apply was also fixed.
How it was found
- Send a math expression to the API that uses cos.constructor to evaluate JavaScript code.
- Use the evaluated code to access the process object and its environment variables.
- Use process.binding to read files and spawn processes, achieving remote code execution.
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.
The math.js API is a service that calculates math expressions. The researchers found that they could sneak in JavaScript code that the service would run, and by using a clever trick they could break out of the restricted environment and run commands on the server. This is called remote code execution because an attacker can run their own code on someone else's machine.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.