Command Injection Vulnerability in win-fork/win-spawn Packages
The win-spawn and win-fork packages are advertised as behaving like spawn, but on Windows they actually pass the command through cmd.exe, so shell metacharacters are interpreted. An attacker who can influence the command string could therefore run additional commands.
Originally disclosed on HackerOne
- Triaged in 7 hours
- Disclosed after 4 days
I would like to report a command injection vulnerability in win-fork and win-spawn packages. It allows an attacker to inject multiple commands in exec-like manner.
Module
module name: win-spawn
version: 2.0.0
npm page: https://www.npmjs.com/package/win-spawn
npm page: https://www.npmjs.com/package/win-fork
Module Description
Spawn for node.js but in a way that works regardless of which OS you're using. Use this if you want to use spawn with a JavaScript file. It works by explicitly invoking node on windows. It also shims support for environment variable setting by attempting to parse the command with a regex. Since all modification is wrapped in if (os === 'Windows_NT') it can be safely used on non-windows systems and will not break anything.
Module Stats
21,929+36,468 downloads in the last week
Vulnerability
Vulnerability Description
Even though this module is advertised to work like spawn, on windows, it works like exec.
Steps To Reproduce:
To check the params passed to cmd.exe:
var os = require('os').type = function() {return "Windows_NT"};
require("child_process").spawn = function(a, b) { console.log(a); console.log(b)};
var spawn = require("win-fork");
spawn('dir C:// && date /T', [], {stdio: 'inherit'});
It effectively runs "cmd /c 'dir C:// && date /T'" which allow the attacker to run both the commands. Moreover, I believe parameters to win-spawn/win-fork may also be used for injection, but I did not investigate this further.
Patch
N/A at a minimum, document this behaviour in the package's documentation.
Wrap up
- I contacted the maintainer to let them know: N
- I opened an issue in the related repository: N
Impact
This issue is more a documentation/API issue. The package should state clearly what it does and alert its dependents that on windows, the parameters should be treated as parameters to exec.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the package builds a cmd.exe command line from the caller's string instead of passing arguments directly to the target program, so shell metacharacters are interpreted.
Impact
An attacker who can control the command string passed to these packages on Windows could run additional commands with the privileges of the calling process; the reporter notes the parameters may also be injectable but did not investigate.
What the attacker needed
Runs on Windows; attacker controls the command string
The fix
The package should clearly document that on Windows the input is treated as an exec command line.
How it was found
- Replace the os.type function so the package believes it is running on Windows.
- Replace child_process.spawn with a stub that logs the arguments it receives.
- Load win-fork and call it with a command string containing a shell operator.
- Observe that the logged command line is passed to cmd.exe with the operator intact.
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 report has not been written yet. The Original tab links to the source.
When a program starts another program, there are two common ways: spawn runs it directly, while exec first hands the text to a command shell. A shell treats characters like && as 'run another command', so text that looks like one command can become several. This package claimed to use the safer direct way but on Windows used the shell instead.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.