Several simple remote code execution in pdf-image
The pdf-image Node.js module builds shell commands from user-supplied input without escaping, so a crafted string passed to the PDFImage class can run arbitrary commands. An attacker who controls that input could execute code on the server.
Originally disclosed on HackerOne
- Triaged in 23 hours
- Fixed in 32 days
- Disclosed after 32 days
I would like to report "A simple remote code execution" in "pdf-image". It allows "a remote attacker to execute arbitrary code when several functions of the PDFImage class are called and the class loaded from user-input value".
Module
module name: pdf-image
version: latest
npm page: https://www.npmjs.com/package/pdf-image
Module Description
Provides an interface to convert PDF's pages to png files in Node.js by using ImageMagick.
Module Stats
[1] weekly downloads: 8,691
Vulnerability
Vulnerability Description
Hello there ! I understand this bug isn't eligible for a bounty. I am reporting it either way. I've found several code execution in the pdf-image class, I tested one of them. They are simple and of course come from the child_process.exec call with lack of escaping. I tested one of them.
Steps To Reproduce:
var PDFImage = require("pdf-image").PDFImage;
var pdfImage = new PDFImage('"; sleep 500 #"'); pdfImage.getInfo();
You can also exploit the vulnerability by submitting backticks (example payload: ls;sleep 5 which will be executed even though you're double-quoting the input.
Patch
You can take example on your command-exists npm class: var isUsingWindows = process.platform == 'win32' var cleanInput = function(s) { if (/[^A-Za-z0-9_/:=-]/.test(s)) { s = "'"+s.replace(/'/g,"'\''")+"'"; s = s.replace(/^(?:'')+/g, '') // unduplicate single-quote at the beginning .replace(/\'''/g, "\'" ); // remove non-escaped single-quote if there are enclosed between 2 escaped } return s; }
if (isUsingWindows) { cleanInput = function(s) { var isPathName = /[\]/.test(s); if (isPathName) { var dirname = '"' + path.dirname(s) + '"'; var basename = '"' + path.basename(s) + '"'; return dirname + ':' + basename; } return '"' + s + '"'; } }
Supporting Material/References:
https://github.com/mooz/node-pdf-image/blob/master/index.js#L27
- Linux / centOS
- v6.17.1
- 3.10.10
- N/A
- Own sample script
Wrap up
Select Y or N for the following statements:
- I contacted the maintainer to let them know: [Y/N] N
- I opened an issue in the related repository: [Y/N] N
Thanks!
Impact
Bad code relying on that class can feel foul to RCE.
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
The module passes user-controlled input into child_process.exec without escaping shell metacharacters.
Impact
An attacker who can control the input to the PDFImage class could execute arbitrary code on the server.
What the attacker needed
Application passes user input to PDFImage; attacker can control that input
The fix
The module should escape or quote user input before passing it to the shell, as shown in the suggested patch.
How it was found
- Load the pdf-image module and create a PDFImage object with a crafted string as the file path.
- Call the getInfo method on that object.
- The crafted string is interpreted by the shell, so the injected command runs.
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 module turns PDF pages into images by running a command-line tool. It puts a value you give it straight into a shell command without making it safe. A shell treats special characters like semicolons as instructions, so an attacker can add their own commands and the server runs them.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.