The lab
here is how portswigger describes it
This lab contains a vulnerable image upload function. Certain file extensions are blacklisted, but this defense can be bypassed using a classic obfuscation technique. To solve the lab, upload a basic PHP web shell, then use it to exfiltrate the contents of the file
/home/carlos/secret. Submit this secret using the button provided in the lab banner. You can log in to your own account using the following credentials:wiener:peter
the new piece in this lab#
again an extension filter blocks non-image files, this time the obfuscation trick is the null byte.
- a null byte is the character with value zero, written
%00when URL-encoded. in many older languages and libraries (especially ones with C underpinnings, like some PHP builds), a null byte marks the end of a string. so code that reads a filename may stop at the null byte, while a different layer keeps the whole string. - that split is the bug: we submit a filename like
exploit.php%00.jpg. the validation layer sees the full string ending in.jpgand allows it, but when the file is actually saved, the null byte truncates the name back toexploit.php. the two layers disagree about where the filename ends, exactly the "one string, two parsers" pattern again. so the.jpgis there only to satisfy the check; the null byte makes it fall away on disk, leaving an executable.php.
why the null byte bypasses the check#
the filter wants to confirm the filename ends in an allowed image extension, and exploit.php%00.jpg does, as far as a modern, null-tolerant string check is concerned, its suffix is .jpg. but the component that writes the file to disk treats the null byte as a string terminator and stops reading at it, so it stores the file as exploit.php. the .jpg that satisfied the validator never makes it to the filesystem. the whole exploit lives in that disagreement: one layer reads the padded name, the other reads the truncated name, and we arrange for the safe-looking version to pass the check while the dangerous version is what gets saved. with exploit.php now sitting in an executable directory, requesting it runs our shell.
Step 1 - locate the stored-file request#
we log in, upload an image avatar, and return to our account page. in Proxy > HTTP history we see our image fetched with GET /files/avatars/<YOUR-IMAGE>, and send this request to Repeater.

Step 2 - obfuscate the filename with a null byte#
on our system we create exploit.php with a script that reads carlos's secret:
<?php echo file_get_contents('/home/carlos/secret'); ?>
uploading it plainly is rejected, only JPG and PNG are allowed. we find the POST /my-account/avatar request and send it to Repeater. in the file part's Content-Disposition header, we set the filename to include a URL-encoded null byte followed by .jpg:
filename="exploit.php%00.jpg"

Step 3 - confirm truncation and execute#
we send the request and the file uploads successfully, and the message refers to it as exploit.php, so the null byte and .jpg were stripped on save. we switch to the GET /files/avatars/<YOUR-IMAGE> tab, replace the filename in the path with exploit.php, and send it. carlos's secret is returned in the response.

Step 4 - submit the secret#
we submit the recovered secret.

with this, the lab is solved!
