Expert · updated

File upload vulnerabilities

PortSwigger Web Security Academy · Expert

  • #File upload
  • #Race condition

Web Shell Upload via Race Condition

The vulnerability

The server saves the file first and checks it afterwards. For a moment the web shell is there, and if your request arrives in that moment, it runs.

Fix: Check the file before it reaches a folder the web server can access.

The lab

Open the lab on PortSwigger

This lab contains a vulnerable image upload function. Although it performs robust validation on any files that are uploaded, it is possible to bypass this validation entirely by exploiting a race condition in the way it processes them.

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

Solution

1. Create a PHP Web Shell: Create a simple PHP file (e.g., exploit.php) with the following content.

<?php echo file_get_contents('/home/carlos/secret'); ?>

2. Attempt Upload and Intercept Requests: Log in as wiener:peter and try to upload the exploit.php file. The upload will be blocked. In Burp Suite's Proxy → HTTP history, find the POST /my-account/avatar request and also the subsequent GET request that would try to access a valid image. Send both requests to Repeater.

3. Group Requests for Race Condition Attack: In Repeater, right-click on the POST request and select Add tab to group -> New tab group. Then, select the GET request and add it to the same group. This will place both requests side-by-side in a new tab group.

4. Execute the Race Condition Attack: Next to the Send button, click the drop-down arrow and select "Send group in parallel (single-packet attack)". Click "Send" repeatedly until you get a HTTP/2 200 OK response from the GET request. This indicates that your GET request was processed in the brief window before the server could validate and delete the malicious file. The response body will contain the content of /home/carlos/secret, and the lab is solved.