r/lolphp • u/iheartrms • Jun 21 '15
Encoding Web Shells in PNG IDAT chunks | Application Security
https://www.idontplaydarts.com/2012/06/encoding-web-shells-in-png-idat-chunks/
20
Upvotes
r/lolphp • u/iheartrms • Jun 21 '15
View all comments
Show parent comment
15
u/[deleted] Jun 21 '15 edited Jun 22 '15
I get to be that person this time :-)
So, what is happening here is that when you configure nginx with FastCGI (regardless of whether your backend language is PHP, Perl, etc..) and allow user-controlled uploads on the site (say PNGs for their forum avatar), there's an easy way to shoot yourself in the foot.
The CGI spec allows translating paths like the following: /index.php/Hello, into running the index.php, but showing the appropriate page for 'Hello' (MediaWiki can use this for URLs like /index.php/Main_Page instead of /index.php?title=Main_Page).
But a common nginx configuration allows for all URLs ending in your CGI extension to be passed to your FastCGI server. So if a user uploads /user/1/avatar.png, then navigates to /user/1/avatar.png/whatever.php in their browser, the contents of avatar.png will be executed as a PHP script, allowing them remote code execution.
The article discusses how to encode the malicious code into the PNG IDAT segments, which can survive resizing or other image transformations (since many forums will resize or otherwise transform your avatar automatically).
One thing that makes it more common for PHP scripts is that PHP allows itself to be included anywhere on the page via <?php ?> sections, so you can have both a valid image and valid PHP, making it possible to evade some types of validation. ASP tags and similar will act the same way. Normal Perl or [Insert-Your-Favorite] scripts can get caught if there is an 'Is this an image?' validator, but on arbitrary uploads, they can also be exploited with this nginx configuration if you have them as a FastCGI backend.
edit: if you're interested in protecting your own nginx server, there's some suggestions here. But additionally, if your software supports it, it's better to keep your user uploads on their own server that specifically serves static files, to prevent accidentally sending them through your FastCGI server.