The lab
here is how portswigger describes it
This lab contains a vulnerable image upload function. The server is configured to prevent execution of user-supplied files, but this restriction can be bypassed by exploiting a secondary vulnerability. 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#
here the site happily accepts our PHP file, there is no type check to beat. the defence is different: the directory where avatars land (/files/avatars/) is configured not to execute scripts, so requesting our uploaded .php just returns its source as plain text instead of running it. the bypass is to get the file out of that non-executable folder.
- the filename we submit lives in the
Content-Dispositionheader of the upload part (filename="..."). the server uses it to decide where to save the file. if it builds the save path by sticking our filename onto a base directory without sanitising it, a path traversal sequence (../) in the filename lets us climb up and write the file into a different directory, one that does execute PHP. - the twist: the server strips a literal
../, but it URL-decodes the filename at some point. so if we send..%2f(the URL-encoded form of../), the stripping step doesn't recognise it, but a later decode turns it back into../, and the traversal takes effect. (this is the same parser-discrepancy idea from the encoding notes: one stage decodes what another stage already "cleaned".) so we upload withfilename="..%2fexploit.php", which lands the file one level up in/files/(an executable directory), and requesting it runs our shell.
why the traversal bypasses the no-exec rule#
the protection here isn't about what we upload, it's about where it executes, the avatars folder is sandboxed so scripts in it are served inert. that is a reasonable control, but it only holds if the uploader cannot choose the destination. the server derives the save path from the attacker-supplied filename, so the filename is really a path we control. it does attempt to defend this, it strips ../ sequences, but it strips them before a later URL-decode step. by encoding the slash as %2f, our traversal survives the strip (it doesn't look like ../ yet) and is reconstituted when the filename is decoded, so the file is written to /files/ instead of /files/avatars/. that higher directory is not sandboxed, so the PHP executes. the sandbox was sound; the path-building around it was not, and the encode/strip ordering is the exact seam we slip through.
Step 1 - locate the stored-file request#
we log in, upload an image as our avatar, and go back to our account page. in Proxy > HTTP history we see our image was fetched with GET /files/avatars/<YOUR-IMAGE>, and send this request to Repeater.

Step 2 - upload the PHP shell#
we create exploit.php with a script that reads carlos's secret:
<?php echo file_get_contents('/home/carlos/secret'); ?>
we upload it as our avatar, and the site does not prevent us from uploading PHP files.

Step 3 - confirm the no-exec directory#
in the Repeater tab with the GET /files/avatars/<YOUR-IMAGE> request, we replace the image filename with exploit.php and send it. instead of executing the script, the server returns the PHP source as plain text, so the avatars directory is configured not to run scripts.

Step 4 - try a plain traversal#
we find the POST /my-account/avatar upload request and send it to Repeater. in the Content-Disposition header we add a traversal sequence to the filename:
Content-Disposition: form-data; name="avatar"; filename="../exploit.php"
the response says The file avatars/exploit.php has been uploaded, so the server stripped the ../, the traversal was removed.

Step 5 - obfuscate the traversal#
we URL-encode the forward slash to slip past the stripping:
filename="..%2fexploit.php"

Step 6 - land the file in an executable directory#
now the response says The file avatars/../exploit.php has been uploaded, so the server URL-decoded our %2f back into a slash after the strip step, and the traversal took effect. we request GET /files/avatars/..%2fexploit.php and carlos's secret is returned, the file was written up into /files and executed. (equivalently we could request GET /files/exploit.php.)

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

with this, the lab is solved!
