Skip to content
RCE Write-up Intermediate

How I found Command Injection via Obsolete PHPThumb

An outdated phpThumb component accepted a crafted filter parameter that was passed to the shell, letting an authenticated attacker run arbitrary commands on the web server. The author confirmed this by running basic system commands and reading a system file.

Sushant Kamble Follow

Published on sushant-kamble.medium.com

Hello Readers, after a great response to my previous write-up on Account Takeover Chained to Host Header Injection. I would like to thank each and everyone for appreciating for showing their gratitude and also for those who found this helpful in their hunting journey.

Sushant Kamble, on sushant-kamble.medium.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 4 min read.

Context

phpThumb() is a PHP library that uses the GD library to create thumbnails from images (JPEG, PNG, GIF, BMP and others) on the fly. The output size is configurable, so the generated thumbnail can be larger or smaller than the source, and the source may be the entire image or only a portion of the original. True colour and resampling are used when GD v2.0+ is available; otherwise paletted colour and nearest-neighbour resizing are used. ImageMagick is used wherever possible for speed, and basic functionality remains available even when GD functions are not installed, as long as ImageMagick is installed.

Multiple vendor applications ship phpThumb(). Versions 1.7.9 and below are affected by a command injection vulnerability that allows an attacker to execute arbitrary shell commands. The flaw sits in the fltr parameter: a remote, authenticated attacker can send crafted requests to the phpThumb web page and obtain arbitrary command execution.

The researcher found this on a private program while doing recon, and describes it as a P1 issue: command injection via an obsolete copy of phpThumb.

Setup

The work started during routine reconnaissance on private programs. The researcher ran Dirbuster against the target and, after several checks, noticed an error in the application that looked familiar. The only tooling mentioned is Dirbuster for content discovery and Google for identifying the error. No account, role or credential is described, although the vulnerability description notes that exploitation is by a remote, authenticated attacker.

Two preconditions matter for this bug:

  1. The application must expose a phpThumb page that accepts the fltr parameter.
  2. ImageMagick must be installed on the server, because the command injection path runs through it.

The vulnerability does not affect the phpThumb that is included in the MODx Revolution distribution.

Finding it

After the recon process the researcher landed on an endpoint where the application was throwing errors. The error was one the researcher recognised, so the next move was to search Google for what exactly the error meant and whether it indicated a vulnerability worth exploiting further.

From the original on sushant-kamble.medium.com
Error Page

That search led to the conclusion that the application was vulnerable to command injection. The reasoning is straightforward: an exposed phpThumb endpoint on an old version, combined with an error page that reveals the component, is enough to try the known fltr payload.

From the original on sushant-kamble.medium.com

Exploitation

Step 1: Locate the erroring endpoint

During recon, Dirbuster surfaced an endpoint that returned errors. The error page is what identified the component in use.

Error Page From the original on sushant-kamble.medium.com

Step 2: Identify the vulnerability

Searching for the error text on Google showed that the application was vulnerable to command injection. The relevant background is the phpThumb fltr parameter issue affecting versions 1.7.9 and below.

From the original on sushant-kamble.medium.com

Step 3: Find a working payload

After checking several GitHub and ExploitDB sources, the researcher found a payload for this vulnerability:

Text
$target/phpThumb.php?src=file.jpg&fltr[]=blur|9 -quality 75 -interlace line fail.jpg jpeg:fail.jpg ; uname -a ; &phpThumbDebug=9

The request targets phpThumb.php with a src image and a fltr[] array entry. The filter value begins with blur|9, then appends ImageMagick options (-quality 75 -interlace line fail.jpg jpeg:fail.jpg), and then breaks out with ; uname -a ; so that the shell runs uname -a as a separate command.

Step 4: Confirm command execution

The payload was sent and returned exactly the response the researcher expected: command injection was triggered on the application.

uname -a From the original on sushant-kamble.medium.com

The screenshot is captioned uname -a, the command injected in the payload.

Step 5: Vary the payload

With execution confirmed, the researcher changed the injected command and checked the response each time.

ls -la From the original on sushant-kamble.medium.com

The screenshot is captioned ls -la, the second command tried.

cat etc/passwd From the original on sushant-kamble.medium.com

The screenshot is captioned cat etc/passwd, the third command tried.

Root cause

A command injection vulnerability exists in the phpThumb fltr parameter in versions 1.7.9 and below. A ; in the filter string therefore terminates the intended ImageMagick invocation and starts a new shell command. Because the command runs in the context of the webserver, the attacker inherits the privileges of that account.

The only stated remediation is upgrading to the latest version of phpThumb.

Impact

Successful exploitation results in arbitrary command execution in the context of the webserver. In this case the researcher demonstrated host identification (uname -a), directory listing (ls -la) and reading /etc/passwd. The report notes that exploitation requires ImageMagick to be installed, and that the phpThumb bundled with MODx Revolution is not affected.

Fix

The remediation given is to upgrade to the latest version of phpThumb. No other change, configuration or code fix is described.

From the original on sushant-kamble.medium.com

Variants to test

  1. Other phpThumb parameters that reach the shell. Try the same ; command ; breakout in parameters other than fltr, such as the source or output handling. A positive result is command output appearing in the response or in a generated file.
  2. Debug output as an oracle. On any phpThumb instance, request the page with phpThumbDebug=9 and see whether diagnostic output is returned. If it is, injected commands are likely to be visible without out-of-band channels.
  3. Blind execution via timing or out-of-band. Where debug output is disabled, inject a command that sleeps or makes an outbound request. A delayed response or a callback confirms execution even without visible output.
  4. Version fingerprinting. Check whether the deployed phpThumb is 1.7.9 or below, for example through error pages or static files. A positive result means the fltr injection is worth attempting.
  5. ImageMagick presence. Since exploitation depends on ImageMagick being installed, test a benign filter and observe whether ImageMagick-specific behaviour or errors appear. A positive result means the vulnerable code path is reachable.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 12 min read.

What happened, in plain words

A security researcher was testing websites that had invited him to look for problems (these are called private programs: companies pay or thank people who report bugs before criminals find them). While mapping out one of these sites, he found a page that showed an error message. The error looked familiar to him, so he searched for it on Google and learned that the page was running an old piece of software called phpThumb. That old version had a known flaw: if you put certain text into one of its settings, the server would run it as a command on the computer itself. He tried a known attack string, and the server ran his commands and showed him the results. He ran commands to identify the operating system, list files and read /etc/passwd. He described it as a P1 vulnerability.

This article walks through the same story slowly, explaining every term as it appears, so that someone who has only ever used a browser can follow it.

Background: what phpThumb is and why it matters

Before the exploitation, it helps to understand what phpThumb actually does.

phpThumb is a small program written in a language called PHP, which runs on web servers. Its job is to make thumbnails: small copies of pictures. Imagine a photo gallery website. Instead of sending a full 5 megabyte photo to every visitor's phone, the site sends a tiny version. phpThumb creates that tiny version on the fly, whenever it is asked.

According to the description the researcher quotes, phpThumb uses another piece of software called the GD library to create thumbnails from images in formats such as JPEG, PNG, GIF and BMP. The size of the output can be set by whoever makes the request, so the thumbnail can be larger or smaller than the original picture. The source can be the whole image or just a part of it. When a newer version of GD (version 2.0 or above) is available, phpThumb uses true colour and resampling; otherwise it falls back to paletted colour and a simpler resizing method called nearest-neighbour. Wherever possible it uses ImageMagick for speed. ImageMagick is a separate, very popular command-line tool for editing images. Basic functionality still works even if GD is not installed, as long as ImageMagick is installed.

Many different companies' applications include phpThumb. The important sentence for us is this: phpThumb versions 1.7.9 and below have a command injection vulnerability that lets an attacker run arbitrary shell commands.

From the original on sushant-kamble.medium.com

What "command injection" means

A command here is an instruction given to the operating system of a computer, the same way you might type a command into a terminal window. A shell is the program that reads those commands and carries them out.

Command injection is when an attacker manages to sneak their own command into a place where the application expected only ordinary data. A simple everyday comparison: imagine a form at a dry cleaner that says "write the name of the garment". You write "blue jacket". But if the shop assistant blindly copies whatever is written into a separate instruction to the back room, and you write "blue jacket. Also, open the cash register", the assistant might follow both instructions. The application did not mean to pass your text on as an instruction, but it did.

In phpThumb's case, the vulnerable spot is a parameter called fltr. A parameter (also called a query parameter) is a named value attached to the end of a web address, after a question mark. For example, in shop.example.com/search?colour=blue, the parameter is colour and its value is blue. Parameters are how a browser tells a website what it wants.

The flaw works like this: phpThumb takes the value of the fltr parameter and builds a command line for ImageMagick out of it. In versions 1.7.9 and below, it does not neutralise special characters that the shell treats as instructions. One such character is the semicolon ;, which in a shell means "end of this command, start another one". So an attacker can put a ; in the filter value, and everything after it is run as a separate command.

The description also notes that a remote, authenticated attacker can exploit this by sending crafted requests to the phpThumb web page. Remote means over the network, from another computer. Authenticated means the attacker has logged in somewhere first. Crafted requests means requests deliberately built to carry the malicious text.

Two conditions that must be true

For this bug to be exploitable, two things must hold:

  1. The application must expose a phpThumb page that accepts the fltr parameter. In other words, the page must be reachable and must listen to that setting.
  2. ImageMagick must be installed on the server, because the command injection path runs through it. If ImageMagick is not there, the vulnerable code is never reached in the same way.

One exception is worth remembering: the vulnerability does not affect the phpThumb that is included in the MODx Revolution distribution. MODx Revolution is a content management system (a tool for building websites), and its bundled copy of phpThumb is not affected.

How the researcher found it

Reconnaissance

The work started during routine reconnaissance (often shortened to recon) on private programs. Recon is the information-gathering phase: before attacking anything, you learn what the target is made of, which pages exist, which software runs, and so on. It is like walking around a building and noting the doors and windows before trying any of them.

The researcher ran Dirbuster against the target. Dirbuster is a tool that tries a long list of common file and folder names against a website and reports which ones exist. If a site has a page at /admin/ or /backup.zip, Dirbuster will find it by simply asking for it and seeing whether the server says yes or no.

After several checks, he noticed an error in the application that looked familiar to him. The only tools mentioned in the report are Dirbuster for finding content and Google for identifying the error. No account, role or credential is described, even though the vulnerability description mentions an authenticated attacker.

From the original on sushant-kamble.medium.com
Error Page

Identifying the error

Once he saw the error page, the next move was to search Google for what exactly the error meant and whether it indicated a vulnerability worth exploiting further. This is a normal habit in security work: error messages often name the software and version behind them, and that name is a lead.

From the original on sushant-kamble.medium.com

That search led to the conclusion that the application was vulnerable to command injection. The reasoning is simple: an exposed phpThumb page running an old version, plus an error page that reveals the component, is enough to justify trying the known fltr attack.

Exploitation, step by step

Step 1: Locate the erroring endpoint

During recon, Dirbuster surfaced an endpoint: a specific address on the server that the application responds to. That endpoint returned errors. The error page is what identified the component in use.

Error Page From the original on sushant-kamble.medium.com

Step 2: Identify the vulnerability

Searching for the error text on Google showed that the application was vulnerable to command injection. The relevant background is the phpThumb fltr parameter issue affecting versions 1.7.9 and below.

From the original on sushant-kamble.medium.com

Step 3: Find a working payload

A payload is the piece of text an attacker sends to trigger a vulnerability. After checking several GitHub and ExploitDB sources (GitHub is a site where people share code; ExploitDB is a public archive of known exploits), the researcher found a payload for this vulnerability:

Text
$target/phpThumb.php?src=file.jpg&fltr[]=blur|9 -quality 75 -interlace line fail.jpg jpeg:fail.jpg ; uname -a ; &phpThumbDebug=9

Let us take this apart piece by piece, because every part has a purpose.

  • $target is a placeholder for the address of the victim site. It is not a real value; it stands for whatever the target's address happens to be.
  • /phpThumb.php is the page being requested. The .php ending tells us it is a PHP script, which matches the software we suspect.
  • ? marks the start of the parameters, the values sent along with the request.
  • src=file.jpg tells phpThumb which image to work on. src is short for source.
  • & separates one parameter from the next.
  • fltr[]=blur|9 ... is the vulnerable parameter. The [] notation means the parameter is an array, a list of values rather than a single one. The value starts with blur|9, which asks for a blur effect. Then come ImageMagick options: -quality 75 -interlace line fail.jpg jpeg:fail.jpg. These are ordinary settings for the image tool.
  • ; uname -a ; is the attack. The semicolon ends the intended ImageMagick command, and uname -a is a standard command that prints information about the operating system and machine. The second semicolon closes it off so the rest of the original command line can continue.
  • &phpThumbDebug=9 turns on phpThumb's debug output. Debug output is extra diagnostic information a program can be told to print. This is the part that makes the result of the injected command visible in the response, instead of disappearing silently.

In short: the request asks for a thumbnail, but smuggles an extra command into the filter setting, and asks the program to show its working so the output can be read.

Step 4: Confirm command execution

The payload was sent, and it returned exactly the response the researcher expected: command injection was triggered on the application.

uname -a From the original on sushant-kamble.medium.com

The screenshot is captioned uname -a, the injected command. "In the context of the web server" means the command ran with the same permissions as the account the web server uses. Whatever that account can do, the attacker can now do.

Step 5: Vary the payload

With execution confirmed, the researcher changed the injected command and checked the response each time. This is how you move from "I can run one command" to "I can run any command".

ls -la From the original on sushant-kamble.medium.com

The screenshot is captioned ls -la, the next command tried. ls lists files in a folder, and -la asks for a long format that includes hidden files. Seeing the folder contents proves the attacker can look around the server.

cat etc/passwd From the original on sushant-kamble.medium.com

The screenshot is captioned cat etc/passwd, the third command tried. On Linux systems, /etc/passwd is a file that lists user accounts. It does not normally contain passwords in modern systems, but it is a classic proof that a file can be read.

Why the bug exists

A command injection vulnerability exists in the phpThumb fltr parameter in versions 1.7.9 and below. Shell metacharacters are characters like ;, |, & and others that the shell treats as instructions rather than as plain text. A ; in the filter string therefore ends the intended ImageMagick command and starts a new shell command.

Because the command runs in the context of the webserver, the attacker inherits the privileges of that account. The only stated remediation is upgrading to the latest version of phpThumb.

Impact

Successful exploitation results in arbitrary command execution in the context of the webserver. In this case the researcher demonstrated host identification (uname -a), directory listing (ls -la) and reading /etc/passwd. The report notes that exploitation requires ImageMagick to be installed, and that the phpThumb bundled with MODx Revolution is not affected.

Fix

The remediation given is to upgrade to the latest version of phpThumb. No other change, configuration or code fix is described.

From the original on sushant-kamble.medium.com

Variants to test

These are follow-up ideas for someone who finds a similar setup. Each one is a way of asking the same question in a different manner.

  1. Other phpThumb parameters that reach the shell. Try the same ; command ; breakout in parameters other than fltr, such as the source or output handling. A positive result is command output appearing in the response or in a generated file.
  2. Debug output as an oracle. On any phpThumb instance, request the page with phpThumbDebug=9 and see whether diagnostic output is returned. If it is, injected commands are likely to be visible without out-of-band channels. An oracle here means any signal that tells you whether something happened.
  3. Blind execution via timing or out-of-band. Where debug output is disabled, inject a command that sleeps or makes an outbound request. A delayed response or a callback confirms execution even without visible output. Blind means you cannot see the output directly, so you infer it from side effects.
  4. Version fingerprinting. Check whether the deployed phpThumb is 1.7.9 or below, for example through error pages or static files. A positive result means the fltr injection is worth attempting. Fingerprinting is identifying software and its version from clues.
  5. ImageMagick presence. Since exploitation depends on ImageMagick being installed, test a benign filter and observe whether ImageMagick-specific behaviour or errors appear. A positive result means the vulnerable code path is reachable.

Glossary

  • API: a defined way for one program to ask another program to do something, like a menu of requests a kitchen accepts from a waiter.
  • Authenticated: having logged in, so the server knows who you are.
  • Blind (injection): an attack where you cannot see the output directly and must infer success from side effects such as delays.
  • Command: an instruction given to a computer's operating system.
  • Command injection: tricking an application into running an attacker's command by hiding it inside ordinary input.
  • Crafted request: a web request deliberately built to carry malicious or unusual values.
  • Debug output: extra diagnostic information a program can be told to print, useful for developers and, unfortunately, for attackers.
  • Dirbuster: a tool that tries many common file and folder names against a website to discover what exists.
  • Endpoint: a specific address on a server that an application responds to.
  • ExploitDB: a public archive of known exploits and proof-of-concept code.
  • Fingerprinting: identifying software and its version from clues such as error pages or file names.
  • GD library: a software library for creating and editing images, used by phpThumb to make thumbnails.
  • GitHub: a website where people share and collaborate on code.
  • ImageMagick: a popular command-line tool for editing and converting images.
  • Metacharacter: a character such as ;, | or & that a shell treats as an instruction rather than plain text.
  • MODx Revolution: a content management system for building websites; its bundled phpThumb is not affected by this bug.
  • Oracle: any signal that tells you whether an action succeeded, even indirectly.
  • Out-of-band: a separate channel, such as a request to your own server, used to confirm something when the normal response does not show it.
  • Parameter (query parameter): a named value attached to a web address after a question mark, used to tell the site what you want.
  • Payload: the text an attacker sends to trigger a vulnerability.
  • PHP: a programming language commonly used to build web pages on servers.
  • phpThumb: a PHP library that creates image thumbnails on the fly.
  • Private program: a bug bounty arrangement where only invited researchers may test a company's systems.
  • P1: the highest priority rating in many bug bounty programmes, meaning a severe issue.
  • Recon (reconnaissance): the information-gathering phase before testing, learning what a target is made of.
  • Remote: over the network, from another computer.
  • Resampling: recalculating an image's pixels when resizing it, to keep it looking smooth.
  • Shell: the program that reads commands and carries them out.
  • Thumbnail: a small copy of an image, used to save bandwidth and space.
  • True colour: a way of storing images with many colours per pixel, as opposed to a limited palette.
  • Vulnerability: a weakness in software that an attacker can use to make it behave in unintended ways.

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.