Trustwave SpiderLabs Uncovers Ov3r_Stealer Malware Spread via Phishing and Facebook Advertising. Learn More

Trustwave SpiderLabs Uncovers Ov3r_Stealer Malware Spread via Phishing and Facebook Advertising. Learn More

Services
Capture
Managed Detection & Response

Eliminate active threats with 24/7 threat detection, investigation, and response.

twi-managed-portal-color
Co-Managed SOC (SIEM)

Maximize your SIEM investment, stop alert fatigue, and enhance your team with hybrid security operations support.

twi-briefcase-color-svg
Advisory & Diagnostics

Advance your cybersecurity program and get expert guidance where you need it most.

tw-laptop-data
Penetration Testing

Test your physical locations and IT infrastructure to shore up weaknesses before exploitation.

twi-database-color-svg
Database Security

Prevent unauthorized access and exceed compliance requirements.

twi-email-color-svg
Email Security

Stop email threats others miss and secure your organization against the #1 ransomware attack vector.

tw-officer
Digital Forensics & Incident Response

Prepare for the inevitable with 24/7 global breach response in-region and available on-site.

tw-network
Firewall & Technology Management

Mitigate risk of a cyberattack with 24/7 incident and health monitoring and the latest threat intelligence.

Solutions
BY TOPIC
Microsoft Exchange Server Attacks
Stay protected against emerging threats
Rapidly Secure New Environments
Security for rapid response situations
Securing the Cloud
Safely navigate and stay protected
Securing the IoT Landscape
Test, monitor and secure network objects
Why Trustwave
About Us
Awards and Accolades
Trustwave SpiderLabs Team
Trustwave Fusion Security Operations Platform
Trustwave Security Colony
Partners
Technology Alliance Partners
Key alliances who align and support our ecosystem of security offerings
Trustwave PartnerOne Program
Join forces with Trustwave to protect against the most advance cybersecurity threats
SpiderLabs Blog

My Other Ride is Your Image Upload Script(1)

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.

Latest SpiderLabs Blogs

Welcome to Adventures in Cybersecurity: The Defender Series

I’m happy to say I’m done chasing Microsoft certifications (AZ104/AZ500/SC100), and as a result, I’ve had the time to put some effort into a blog series that hopefully will entertain and inform you...

Read More

Trustwave SpiderLabs: Insights and Solutions to Defend Educational Institutions Against Cyber Threats

Security teams responsible for defending educational institutions at higher education and primary school levels often find themselves facing harsh lessons from threat actors who exploit the numerous...

Read More

Breakdown of Tycoon Phishing-as-a-Service System

Just weeks after Trustwave SpiderLabs reported on the Greatness phishing-as-a-service (PaaS) framework, SpiderLabs’ Email Security team is tracking another PaaS called Tycoon Group.

Read More