Skip to content
RCE Write-up Advanced

ImageMagick – Shell injection via PDF password

ImageMagick failed to properly sanitise the PDF password parameter, allowing shell injection when the password contained certain characters. An attacker could execute arbitrary commands by supplying a malicious MSL or SVG file that sets the authenticate parameter.

insert-script.blogspot.com

Published on insert-script.blogspot.com

In 2016 ImageTragick was revealed. The associated reseachers showed that ImageMagick is not only powerful, eg you can read local files, but that it is possible to execute shell commands via a maliciously crafted image.

The author, on insert-script.blogspot.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.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

The PDF password was inserted into a shell command without proper escaping, allowing an attacker to break out of the quoted string and inject commands.

Impact

An attacker could execute arbitrary shell commands on the server if they can supply a malicious file and ImageMagick processes it with the authenticate parameter set.

What the attacker needed

ImageMagick not using third-party PDF library; Attacker can set authenticate parameter; Victim processes malicious file

The fix

The issue was fixed by properly escaping the password and by correcting the MSL authenticate handling to set the option correctly.

How it was found

  1. Create an MSL file with an authenticate attribute containing a double quote and shell commands.
  2. Convert the MSL file using ImageMagick, which triggers the shell injection.
  3. Observe that the injected command executes (e.g., creates a file with the output of 'id').

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 write-up has not been written yet. The Original tab links to the source.

ImageMagick is a tool that converts images. When it handles PDFs, it passes a password to another program called Ghostscript. The password was not properly cleaned, so an attacker could sneak in extra commands that the computer would run. This means someone could take over the server if they can upload a malicious file.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

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.