Skip to content
RCE Disclosed report Beginner

[diskstats] Command Injection via insecure command concatenation

The diskstats npm module builds a shell command by concatenating a caller-supplied path into a df invocation run through child_process.exec. An attacker who can control that path could inject extra shell commands and run them on the victim's machine.

d3lla Follow

Originally disclosed on HackerOne

Critical View original
  • Triaged in 3 hours
  • Fixed in 3 months
  • Disclosed after 3 months

I would like to report a Command Injection issue in the diskstats module. It allows to execute arbitrary commands on the victim's PC.

Module

module name: diskstats version: 0.0.2 npm page: https://www.npmjs.com/package/diskstats

Module Description

This library uses df to pull disk information such as free space & inode utilization on your system. This library only works on systems where df is installed and present within your path.

Module Stats

[2] 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/PhilipSkinner/diskstats/blob/master/lib/stat.js#L44
....
stat.prototype._fetchSpace = function(path) {
	return new Promise((resolve, reject) => {
		this.child_process.exec('df ' + this._ensureAbsPath(path), (err, stdout) => {  // <-- injection
			if (err) {
				return reject(err);
			}			

			return resolve(this._parseResponse(stdout));
		});
	});
};

// https://github.com/PhilipSkinner/diskstats/blob/master/lib/stat.js#L56
stat.prototype._fetchInodes = function(path) {
	return new Promise((resolve, reject) => {
		this.child_process.exec('df -i ' + this._ensureAbsPath(path), (err, stdout) => {  // <-- injection
			if (err) {
				return reject(err);
			}

			return resolve(this._parseResponse(stdout));
		});
	});
};
...
module.exports = function(child_process, path) {
	if (!child_process) {
		child_process = require('child_process');
	}

	if (!path) {
		path = require('path');
	}

	return new stat(child_process, path);
}

The path parameter is used to build the command that is passed to the child_process.exec function without any check.

Steps To Reproduce:

  • create a directory for testing

    • mkdir poc
    • cd poc/
  • install diskstats module:

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

JavaScript
const diskstats = require('diskstats');
diskstats.check('; touch HACKED', (err, results) => {});

  • 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_diskstats.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.

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 diskstats 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 path argument is concatenated into a shell command string passed to child_process.exec, so shell metacharacters in the path are interpreted as commands.

Impact

An attacker who can influence the path passed to the module could execute arbitrary commands on the victim's PC.

What the attacker needed

Attacker controls the path argument; victim runs code using the module

The fix

The module should stop concatenating user input into a shell command and instead use execFile or spawn with the path passed as a separate argument, validating the input as well.

How it was found

  1. Create and enter a test directory.
  2. Install the diskstats module with npm.
  3. Create a small script that calls the module's check function with a crafted path.
  4. Confirm the marker file does not exist yet.
  5. Run the script with node.
  6. 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 asks the operating system's df tool how much disk space is free. It builds that request as a text string and hands it to a shell, so anything extra a caller puts in the path is treated as more shell commands. That means someone who controls the path can make the computer run their own commands.

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.