Skip to content
Proof-of-concept to exploit the flaw in the PHP-GD built-in function, imagecreatefromjpeg()
PHP
Branch: master
Clone or download
Fakhri Zulkifli
Fakhri Zulkifli Update README.md
Latest commit 1d701b0 Jun 28, 2015
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
README.md Update README.md Jun 28, 2015
payload.php

README.md

Exploiting PHP-GD imagecreatefromjpeg() function

Proof-of-concept to exploit the flaw in the PHP-GD built-in function, imagecreatefromjpeg(). Inspired by one of Reddit's comment on my previous thread regarding exploiting the imagecreatefromgif() PHP-GD function.

Warning: This POC was tested using libJPEG v8.0 only. The image requires the same libJPEG version in order to be able to execute the PHP code.

This is the script to generate the payload

<?php
$jpg = imagecreatefromjpeg('image.jpg');
imagejpeg($jpg, 'poc.jpg');
imagedestroy($jpg);
?>

This is the hexadecimal dump for the image.jpg before the recreation. Nothing fancy here, just some junk and EXIF data.

before

So this is what happens after the recreation of JPEG file, all the EXIF data is removed and not much empty space where we can append the PHP backdoor.

after

However, there are several important parts in the JPEG file format which can be exploited.

parts

So according to this JPEG file format, where would be the place to put the PHP backdoor?. Search for the Start of Scan (SOS) marker which is FF DA, as you can see there are Scan Header Length and Scan Header after the SOS marker. The place to be put PHP backdoor is right after the Scan Header (00 0C 03 01 00 02 11 03 11 00 3F 00).

final

Run through the payload script again, and then the PHP backdoor will not get removed even after multiple times going through recreation process

<?php
$jpg = imagecreatefromjpeg('poc.jpg');
imagejpeg($jpg, 'exploit.jpg');
imagedestroy($jpg);
?>
You can’t perform that action at this time.