My Other Ride is Your Image Upload Script

Many security issues are based upon mistaken assumptions. For instance, when testing applications, I often find that the user inputs left unsanitized are the ones that the developer does not believe can be modified, such as inputs from drop-down menus. The truth is, all user input can be modified. Sometimes, to a greater degree than you think…

Imagine that you have developed an image upload script. In order to prevent abuses, you allow the upload of only files which are valid PNG files, and which are only 30x30 pixels or less in size.
What if I told you that I could probably upload code or exhaust your disk quota, despite your protections? Are you chuckling? What if I told you that there are methods by which you can combine multiple files into one, allowing the resulting file to be parsed as either type? Still chuckling? Let's talk for a moment about the PNG and PHP formats, and three ways in which we can make a hybrid PNG / PHP file.

PNG files are made up of a PNG signature followed by a series of chunks. The signature itself is a set of values, namely "137 80 78 71 13 10 26 10" in decimal. Each chunk defines some bit of information. Each chunk has four fields. The first defines the length of data in the chunk. The second defines the type of chunk to follow. The third is the data of the chunk. The fourth is a CRC for the chunk type and data fields to prevent transmission errors.

How the data is parsed depends on the particular type of chunk. To understand the three methods of combining these files, we'll need to understand the function of three particular PNG chunks: iTXt, iEND, and luLZ. iTXt allows for textual comments about the image. Its data field is interpreted as a textual comment, iEND defines the end of the PNG, and luLZ is just a chunk I made up just now.

The contents of iTXt are read by humans. As such, its contents will be shown by programs parsing the PNG, but don't really conform to any special format, so we could theoretically put whatever we like in there and the image will still display just fine.

iEND defines the end of the PNG. No data after the iEND chunk will be read. If any exists, it is ignored and the image will display just fine.

luLZ may not be a real chunk, but the PNG format allows for private chunks outside the standard that will be read only by programs which support the chunk. If I place a luLZ chunk in a valid PNG between two valid chunks, any program which does not parse the luLZ chunk will ignore it and the image will display just fine.

So now, we have three places in a PNG file where we can place data without affecting the PNG itself. One based on metadata, another based on data which goes unreferenced in the file, and another outside the start and end markers in the file.

So now, I can exceed your disk quota by placing a large volume of data in a comment (or many comments) or in private chunks, or at the end of the file. But denial of service is always the lowest form of attack we can achieve with any vulnerability. I think we can do better, don't you?

To do better, it is important to understand what is and isn't parsed in a PHP script. PHP looks for instances of "<?php" (possibly "<?" as well depending on configuration) and "?>", then interprets the data between the two as code. After executing the code, any output replaces the code and PHP outputs the contents of the file, post modification. Any parts of the file not flagged as code are essentially ignored.

So, if we place valid PHP code between "<?php" and "?>" in any of the places previously mentioned for data inside a PNG, it will be both a valid PHP file and valid PNG file. If the conditions are right, we might just be able to execute this as code once it's uploaded to the server.

This very nicely illustrates a point about security concepts in general: There will always be vulnerabilities you are unaware of. A single layer of defense is better than none, but redundant defenses are better. If your upload script is only secure as long as no one can figure out how to upload code, your system is broken. The more barriers in the way of exploitation, the more difficult an attack becomes.

Trustwave reserves the right to review all comments in the discussion below. Please note that for security and other reasons, we may not approve comments containing links.