The lab
here is how portswigger describes it
This lab contains a vulnerable image upload function. It attempts to prevent users from uploading unexpected file types, but relies on checking user-controllable input to verify this. To solve the lab, upload a basic PHP web shell and 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#
this is the same web-shell-to-RCE goal as the previous upload lab, but now there is a validation check, it just checks the wrong thing. the new term:
- the
Content-Typeheader on a multipart upload is a label that travels with each uploaded part, declaring what kind of data it is (e.g.image/jpeg). the browser sets it, but in a proxy like Burp we set it, it is attacker-controllable input. the catch: this header is just the sender's claim about the file, not a fact derived from the file's actual bytes. this lab allows uploads only when theContent-Typeisimage/jpegorimage/png. because that header is something we control, we upload our PHP shell but simply relabel itsContent-Typeasimage/jpeg. the check passes, the real content is still PHP, and requesting the file executes it.
why trusting the content-type breaks this#
the flaw is validating the file by a value the uploader controls rather than by the file itself. the server reads the Content-Type of our upload part, sees image/jpeg, and concludes "this is an image", but that header is a self-declared label sitting in the request we crafted, not something measured from the bytes. so the check answers "did the attacker say this is an image?" instead of "is this actually an image?". we keep the body as our PHP script and change only the label, the two no longer have to agree. the server stores the file in the executable avatars directory believing it is a harmless image, and when we request it the server runs the PHP. the validation was real, but it was asking the wrong source.
Step 1 - upload a normal avatar#
we log in and upload an image as our avatar, then go back to our account page.

Step 2 - locate the stored-file request#
in Proxy > HTTP history we notice our image was fetched with GET /files/avatars/<YOUR-IMAGE>. we send this request to Repeater.

Step 3 - get blocked by the type check#
on our system we create exploit.php with a script that reads carlos's secret:
<?php echo file_get_contents('/home/carlos/secret'); ?>
we attempt to upload it as our avatar. the response tells us we may only upload files with the MIME type image/jpeg or image/png, so there is a Content-Type check to defeat.

Step 4 - spoof the Content-Type#
in the upload request we change the Content-Type of our file part to image/jpeg, leaving the PHP body untouched, and send it. the response confirms the file was uploaded successfully, the check only looked at the label.

Step 5 - execute the shell#
we switch to the Repeater tab holding the GET /files/avatars/<YOUR-IMAGE> request, replace the image filename in the path with exploit.php, and send it. carlos's secret is returned in the response.

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

with this, the lab is solved!
