TOCTOU Vulnerability: Race Window Mechanics in Web Applications
A race condition (CWE-362 according to the MITRE catalog) is a concurrent code sequence that requires temporary exclusive access to a shared resource, but there is a window during which another thread modifies that resource. According to CWE-362, the consequences include: DoS via CPU consumption, process crash, and data leakage. Platforms include C, C++, Java, Python, and Mobile, but web applications on any stack are equally susceptible.
The TOCTOU subtype (CWE-367, Time-of-Check to Time-of-Use) is a child element of CWE-362 and describes a specific pattern: the server checks the resource's state before using it, but the state can change between the check and use. According to CWE-367, the consequences are integrity violations: changes in execution logic and modification of application data.
In practice, it looks simple. The server receives POST /apply-coupon, reads from the database SELECT used FROM coupons WHERE code = ?, sees used = false, applies the discount and only then does UPDATE coupons SET used = trueBetween SELECT And UPDATE— race window. If a second request flies into this window, it also sees used = falseand is being verified. The one-time coupon can be applied as many times as there were requests before the update.
The window size depends on the architecture. A monolith with a single thread has a near-zero margin. A Gunicorn application with multiple workers means competition between processes is real. A microservice design, where balance checking is done through one service and debiting through another with an intermediate HTTP call, extends the window to tens of milliseconds. CTFs typically use Flask, Django, or Express with multiple threads—the window is in milliseconds, and a single-packet attack reliably hits it.
The business logic for exploiting a race condition boils down to three scenarios. Double spending—one payment request generates two orders; Limit overrun—a promo code is used multiple times; and Account takeover—a race in the email or OAuth authorization flow. In MITRE ATT&CK terms, exploiting a race condition in a web application falls under the Exploit Public-Facing Application (T1190, Initial Access) technique . Consequences include Stored Data Manipulation (T1565.001, Impact) when modifying database data and Financial Theft (T1657, Impact) when double-spending.
Single Packet Attack Race Condition: Eliminating Network Jitter
The main problem with exploiting race conditions isn't sending multiple requests, but delivering them to the server simultaneously. Network jitter—random delays on each hop—spreads requests out over time. A 2ms race window is easy to miss if requests arrive 50ms apart. Until 2023, exploiting race conditions was like playing a lottery: open twenty threads, send an HTTP request from each, and hope for the best. Sometimes it worked. More often, it didn't.
HTTP/2 single-packet attack. A technique by James Kettle of PortSwigger Research, presented at Black Hat USA 2023 ( whitepaper "Smashing the State Machine" ). HTTP/2 allows multiplexing multiple requests over a single TCP connection. The key trick is to pack all HTTP/2 frames (HEADERS + DATA) into a single TCP packet up to ~1500 bytes in size (MTU limit). The server's network stack receives them atomically—not "approximately simultaneously," but in a single read from the socket. According to PortSwigger, 20-30 short requests fit into a single packet. Network jitter is completely eliminated: the difference in delivery time is zero.
HTTP/1.1 last-byte synchronization. For servers without HTTP/2. All requests are sent over separate TCP connections without the last byte of the body. Connections are kept open. Then the last bytes are sent simultaneously. The server begins processing only after receiving the full body—jitter only affects the delivery of a single byte over an already established connection. In the original study, last-byte sync yielded a 4-10 ms range, compared to less than 1 ms for a single-packet attack. For CTF, this is usually sufficient, but requires more attempts and more TCP connections (one for each request).
Starting with a 2023 version, Burp Suite automatically selects the technique for parallel sending: HTTP/2 — single-packet attack, HTTP/1 — last-byte sync. Turbo Intruder provides explicit control via a parameter. engine.
Why 20-30 requests instead of 2? Server jitter (GC pauses, thread pool locks, cache misses) still exists. The more requests in a batch, the higher the chance that several of them will hit the race window simultaneously. For reconnaissance, 20-30. When the behavior is confirmed and a clean exploit is needed, we reduce it to 2-5.
Types of race conditions in CTF tasks
In CTFs, race conditions are usually hidden in tasks with themes like "shop," "balance," "voting," and "file upload." If you see words like "balance," "coupon," "redeem," "transfer," or "upload" in the task description, check those endpoints for race conditions first. According to the OWASP classification, race conditions affect A04 (Insecure Design) — when the architecture doesn't provide for synchronization; A01 (Broken Access Control) — bypassing permission checks through a race condition; and A08 (Software and Data Integrity Failures) — TOCTOU and state corruption due to concurrent access.
Limit overrun race condition
The most common and most down-to-earth type. There's a limit: a one-time coupon, a maximum of three votes, and one free credit. The application reads a value from the database, checks the condition, performs an action, and updates the counter. With concurrent requests, several of them slip through before the update. According to PortSwigger, limit overrun is a subtype of TOCTOU vulnerabilities.
CTF and bug bounty variations: multiple gift card redemptions, bypassing the OTP rate limit (the race turns into a brute force), reusing the CAPTCHA answer, multiple rating votes, and withdrawing excess funds.
Exploitation is straightforward: intercept a coupon request, send 20 copies in parallel via a single-packet attack, and check the balance. If the discount was applied more than once, the race window exists. That's it, you can dig deeper.
Single-endpoint collision: hidden substates
A more subtle type, which, according to PortSwigger Research, is referred to as hidden multi-step sequences. One request initiates a multi-step process on the server, within which intermediate states—sub-states—exist. A second request to the same endpoint enters an intermediate state and modifies the data before the first completes.
According to NVD: CVSS 6.4 (MEDIUM), vector CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:C/C:L/I:L/A:N, CWE-362. Affected all GitLab versions up to 15.5.7, from 15.6 to 15.6.4, and from 15.7 to 15.7.2. Mechanics: two simultaneous requests to change email. The first one sets pending_email = "[email protected]", the second one overwrites pending_email = "[email protected]"When the server takes pending_emailTo generate a token, the victim's address is already there. The attacker receives a confirmation token for someone else's email address. This leads to the forgery of verified emails and account takeover when using GitLab as an OAuth provider. According to CISA (SSVC Decision: Track), exploitation is not automated, and the technical impact is partial.
A separate subtype is the partial construction race. During registration, the server first creates a user record, then generates and stores a confirmation token. Between these steps, the token is still empty. If you send a confirmation request with an empty token value at this point, some frameworks will accept it as valid. According to PortSwigger, partial construction is one of the least studied subtypes. CTF designers adore it precisely because few people know about it.
A race condition (CWE-362 according to the MITRE catalog) is a concurrent code sequence that requires temporary exclusive access to a shared resource, but there is a window during which another thread modifies that resource. According to CWE-362, the consequences include: DoS via CPU consumption, process crash, and data leakage. Platforms include C, C++, Java, Python, and Mobile, but web applications on any stack are equally susceptible.
The TOCTOU subtype (CWE-367, Time-of-Check to Time-of-Use) is a child element of CWE-362 and describes a specific pattern: the server checks the resource's state before using it, but the state can change between the check and use. According to CWE-367, the consequences are integrity violations: changes in execution logic and modification of application data.
In practice, it looks simple. The server receives POST /apply-coupon, reads from the database SELECT used FROM coupons WHERE code = ?, sees used = false, applies the discount and only then does UPDATE coupons SET used = trueBetween SELECT And UPDATE— race window. If a second request flies into this window, it also sees used = falseand is being verified. The one-time coupon can be applied as many times as there were requests before the update.
The window size depends on the architecture. A monolith with a single thread has a near-zero margin. A Gunicorn application with multiple workers means competition between processes is real. A microservice design, where balance checking is done through one service and debiting through another with an intermediate HTTP call, extends the window to tens of milliseconds. CTFs typically use Flask, Django, or Express with multiple threads—the window is in milliseconds, and a single-packet attack reliably hits it.
The business logic for exploiting a race condition boils down to three scenarios. Double spending—one payment request generates two orders; Limit overrun—a promo code is used multiple times; and Account takeover—a race in the email or OAuth authorization flow. In MITRE ATT&CK terms, exploiting a race condition in a web application falls under the Exploit Public-Facing Application (T1190, Initial Access) technique . Consequences include Stored Data Manipulation (T1565.001, Impact) when modifying database data and Financial Theft (T1657, Impact) when double-spending.
Single Packet Attack Race Condition: Eliminating Network Jitter
The main problem with exploiting race conditions isn't sending multiple requests, but delivering them to the server simultaneously. Network jitter—random delays on each hop—spreads requests out over time. A 2ms race window is easy to miss if requests arrive 50ms apart. Until 2023, exploiting race conditions was like playing a lottery: open twenty threads, send an HTTP request from each, and hope for the best. Sometimes it worked. More often, it didn't.
HTTP/2 single-packet attack. A technique by James Kettle of PortSwigger Research, presented at Black Hat USA 2023 ( whitepaper "Smashing the State Machine" ). HTTP/2 allows multiplexing multiple requests over a single TCP connection. The key trick is to pack all HTTP/2 frames (HEADERS + DATA) into a single TCP packet up to ~1500 bytes in size (MTU limit). The server's network stack receives them atomically—not "approximately simultaneously," but in a single read from the socket. According to PortSwigger, 20-30 short requests fit into a single packet. Network jitter is completely eliminated: the difference in delivery time is zero.
HTTP/1.1 last-byte synchronization. For servers without HTTP/2. All requests are sent over separate TCP connections without the last byte of the body. Connections are kept open. Then the last bytes are sent simultaneously. The server begins processing only after receiving the full body—jitter only affects the delivery of a single byte over an already established connection. In the original study, last-byte sync yielded a 4-10 ms range, compared to less than 1 ms for a single-packet attack. For CTF, this is usually sufficient, but requires more attempts and more TCP connections (one for each request).
Starting with a 2023 version, Burp Suite automatically selects the technique for parallel sending: HTTP/2 — single-packet attack, HTTP/1 — last-byte sync. Turbo Intruder provides explicit control via a parameter. engine.
Why 20-30 requests instead of 2? Server jitter (GC pauses, thread pool locks, cache misses) still exists. The more requests in a batch, the higher the chance that several of them will hit the race window simultaneously. For reconnaissance, 20-30. When the behavior is confirmed and a clean exploit is needed, we reduce it to 2-5.
Types of race conditions in CTF tasks
In CTFs, race conditions are usually hidden in tasks with themes like "shop," "balance," "voting," and "file upload." If you see words like "balance," "coupon," "redeem," "transfer," or "upload" in the task description, check those endpoints for race conditions first. According to the OWASP classification, race conditions affect A04 (Insecure Design) — when the architecture doesn't provide for synchronization; A01 (Broken Access Control) — bypassing permission checks through a race condition; and A08 (Software and Data Integrity Failures) — TOCTOU and state corruption due to concurrent access.
Limit overrun race condition
The most common and most down-to-earth type. There's a limit: a one-time coupon, a maximum of three votes, and one free credit. The application reads a value from the database, checks the condition, performs an action, and updates the counter. With concurrent requests, several of them slip through before the update. According to PortSwigger, limit overrun is a subtype of TOCTOU vulnerabilities.
CTF and bug bounty variations: multiple gift card redemptions, bypassing the OTP rate limit (the race turns into a brute force), reusing the CAPTCHA answer, multiple rating votes, and withdrawing excess funds.
Exploitation is straightforward: intercept a coupon request, send 20 copies in parallel via a single-packet attack, and check the balance. If the discount was applied more than once, the race window exists. That's it, you can dig deeper.
Single-endpoint collision: hidden substates
A more subtle type, which, according to PortSwigger Research, is referred to as hidden multi-step sequences. One request initiates a multi-step process on the server, within which intermediate states—sub-states—exist. A second request to the same endpoint enters an intermediate state and modifies the data before the first completes.
According to NVD: CVSS 6.4 (MEDIUM), vector CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:C/C:L/I:L/A:N, CWE-362. Affected all GitLab versions up to 15.5.7, from 15.6 to 15.6.4, and from 15.7 to 15.7.2. Mechanics: two simultaneous requests to change email. The first one sets pending_email = "[email protected]", the second one overwrites pending_email = "[email protected]"When the server takes pending_emailTo generate a token, the victim's address is already there. The attacker receives a confirmation token for someone else's email address. This leads to the forgery of verified emails and account takeover when using GitLab as an OAuth provider. According to CISA (SSVC Decision: Track), exploitation is not automated, and the technical impact is partial.
A separate subtype is the partial construction race. During registration, the server first creates a user record, then generates and stores a confirmation token. Between these steps, the token is still empty. If you send a confirmation request with an empty token value at this point, some frameworks will accept it as valid. According to PortSwigger, partial construction is one of the least studied subtypes. CTF designers adore it precisely because few people know about it.