Description
I found a web app that can help process images: PNG images only!
Setup
Web shell upload
Connect to the instance to access the PNG processing app link.
Solution
- Step 1Understand the vulnerabilityThis File Upload Exploitation guide explains the upload exploit well. The file upload tries to make sure that the file is a PNG by looking at the magic bytes and file extension. However, the validation is weak:
- It checks for PNG magic bytes at the start of the file
- It checks that the filename contains "png" anywhere in it
- Step 2Download the PHP web shellDownload the phpbash.php script (or any simple PHP shell). This will be used to gain command execution on the server.
- Step 3Convert the shell to hexUse CyberChef's "To Hex" recipe to convert the PHP script to hexadecimal format.
- Step 4Prepend PNG magic bytesAdd the PNG file signature (found here) to the beginning of the hex data. This will trick the file type validation into thinking it's a valid PNG image.89 50 4E 47 0D 0A 1A 0A
- Step 5Convert back to binaryUse CyberChef's "From Hex" recipe to convert the modified hex back to binary and download the file.
- Step 6Upload with double extensionUpload the modified file with a name like filename.png.php. The server only checks that 'png' appears somewhere in the name; it doesn't have to be at the end. This allows us to use a double extension which passes the validation but executes as PHP.
- Step 7Access the web shellOnce successfully uploaded, the file can be accessed at the uploads directory. Navigate to this URL in your browser to access the web shell interface.http://atlas.picoctf.net:60322/uploads/filename.png.php
- Step 8Explore the file systemThe shell starts in /var/www/html/uploads. Check your current directory and list the parent directory to find interesting files.pwdls ../
- Step 9Read the flag fileYou'll find a file called MFRDAZLDMUYDG.txt in /var/www/html/. Read it to get the flag.cat ../MFRDAZLDMUYDG.txt
Flag
picoCTF{c3rt!fi3d_Xp3rt_tr1ckst3r_ab0e...}
Once the shell is uploaded, the flag text file is one directory up from the uploads folder.