Partial construction race conditions

9 min read Hard Portswigger
Race condition
Contents

On this page

The lab

here is how portswigger describes it

This lab contains a user registration mechanism. A race condition enables you to bypass email verification and register with an arbitrary email address that you do not own. To solve the lab, exploit this race condition to create an account, then log in and delete the user carlos.

the new pieces in this lab#

this is the subtlest race in the topic. the others raced mutations of fully-formed state; here we exploit an object while it is only partially constructed, caught in a half-built sub-state that lasts for a sliver of time.

  • partial construction: creating a record often takes several steps, e.g. (1) insert the new user row, (2) generate a confirmation token and store it on that row, (3) email the token. between step 1 and step 2 the row exists but its token column is still uninitialised (null, empty, or a default). that transient, half-built state is the "partially constructed" object.
  • the race window is the gap between the row being inserted and the token being written. inside it, the user exists with a null/uninitialised token. if we can submit a confirmation whose token matches that uninitialised value, we confirm the account without ever seeing the real token, bypassing email verification entirely.
  • the obstacle: we can only register @ginandjuice.shop addresses and can't read their mail, so the normal confirmation link is unreachable. the partial-construction window is our only way in.
  • matching "null": submitting an empty token (token=) returns Forbidden, the developers patched the obvious "empty token matches uninitialised token" case. but passing an empty array (token[]=) returns Invalid token: Array instead, so the array form isn't caught by that patch and can still match an uninitialised token in the window.
  • the tooling: a single-packet attack that, per attempt, fires one registration plus many confirmation requests on the same release gate. because the confirmation response is always much faster than registration, a single confirmation would arrive before the user row even exists; spraying ~50 confirmations per registration blankets the window so some land during the partial-construction sub-state. the method: learn from the leaked JavaScript that confirmation is POST /confirm?token=..., find that token[]= dodges the empty-token patch, then use Turbo Intruder to fire, per attempt, a registration followed by a burst of token[]= confirmations so one hits the window where the token is still null, confirming an account we can log in to.

why the half-built record can be confirmed#

registration builds the user across more than one database operation, and for a brief moment the row exists with no real token yet, just an uninitialised placeholder. the confirmation logic looks up a pending user whose stored token equals the token we submit; the developers assumed that by the time any confirmation arrives, the token has been generated and stored, so only the genuine emailed token could match. but the insert and the token-write aren't atomic. if a confirmation arrives in the gap, the stored token is still the uninitialised value, and a request carrying a matching "null-equivalent" token (the empty array, which slips past the Forbidden patch on empty strings) satisfies the lookup. the account is marked confirmed against a token that was never emailed. the flaw is that a security-critical value (the confirmation token) has a window of being absent on an otherwise-usable record, and the confirmation check can be made to match that absence.

Step 1 - study registration and spot the email gate#

we study the user registration mechanism and observe that we can only register with @ginandjuice.shop email addresses, and completing registration requires visiting a confirmation link sent by email. since we have no @ginandjuice.shop inbox, we have no normal route to a valid confirmation link.

111

Step 2 - read the leaked confirmation flow#

in the proxy history we notice a request fetching /resources/static/users.js.

222

studying the JavaScript, it dynamically generates the confirmation-page form (presumably linked from the confirmation email), leaking that the final confirmation is a POST /confirm with the token in the query string.

333

Step 3 - craft a confirmation request#

in Repeater we recreate what the browser would send when clicking the confirmation link:

POST /confirm?token=1 HTTP/2
Host: YOUR-LAB-ID.web-security-academy.net
Content-Type: x-www-form-urlencoded
Content-Length: 0
444

we probe the token parameter:

  • an arbitrary token → Incorrect token: <YOUR-TOKEN>
  • removing the parameter → Missing parameter: token
  • an empty token (token=) → Forbidden the Forbidden on empty token suggests the developers patched a vulnerability where an empty token could be exploited, exactly the "empty token matches an uninitialised token" partial-construction bug. so there may be a window between registering a user and the token being stored, in which null (or equivalent) is a valid token.

Step 4 - bypass the empty-token patch with an array#

we try other ways to submit a null-equivalent token. passing an empty array:

POST /confirm?token[]=

returns Invalid token: Array instead of Forbidden. that shows we successfully passed an empty array, which isn't caught by the empty-string patch and could match an uninitialised registration token during the window.

555

Step 5 - benchmark registration and confirmation#

we send POST /register to Repeater and experiment: registering the same username twice gives a different response (an "account already exists" path), so each attempt needs a fresh username.

666

in a separate tab we build a confirmation request with an arbitrary token and our session cookie:

POST /confirm?token=1 HTTP/2
Host: YOUR-LAB-ID.web-security-academy.net
Cookie: phpsessionid=YOUR-SESSION-ID
Content-Type: application/x-www-form-urlencoded
Content-Length: 0
777

we add both to a group and send them sequentially and in parallel a few times (changing the username each time to avoid the "account already exists" path). we notice the confirmation response consistently arrives much faster than the registration response, so a single confirmation would land before the user row is even created.

Step 6 - set up the single-packet attack#

we need the server to begin creating the pending user, then hit it with a confirmation carrying the null-equivalent token before user creation finishes. since confirmation is always faster, we must send many confirmations per registration to blanket the window. we highlight the username value in POST /register, right-click Extensions > Turbo Intruder > Send to turbo intruder. the username is auto-marked %s; we set email to an arbitrary unused @ginandjuice.shop address and note the static password (needed later). we select the examples/race-single-packet-attack.py template and edit it:

python
def queueRequests(target, wordlists):
    engine = RequestEngine(endpoint=target.endpoint,
                            concurrentConnections=1,
                            engine=Engine.BURP2
                            )
 
    confirmationReq = '''POST /confirm?token[]= HTTP/2
Host: YOUR-LAB-ID.web-security-academy.net
Cookie: phpsessionid=YOUR-SESSION-TOKEN
Content-Length: 0
 
'''
    for attempt in range(20):
        currentAttempt = str(attempt)
        username = 'User' + currentAttempt
 
        # queue a single registration request (new username each attempt)
        engine.queue(target.req, username, gate=currentAttempt)
 
        # queue 50 confirmation requests on the same gate to blanket the window
        for i in range(50):
            engine.queue(confirmationReq, gate=currentAttempt)
 
        # release this attempt's registration + 50 confirmations together
        engine.openGate(currentAttempt)
 
def handleResponse(req, interesting):
    table.add(req)

each attempt uses a fresh username and its own gate, and fires one registration plus 50 token[]= confirmations simultaneously.

888

Step 7 - launch and find the winning account#

we launch the attack and sort the results by Length. a successful hit shows one or more 200 responses to a confirmation request containing Account registration for user <USERNAME> successful , a confirmation landed in the partial-construction window and matched the uninitialised token.

999

we note the username from such a response (e.g. User4).

Step 8 - log in and delete carlos#

in the browser we log in with that username and the static password we used in the registration request. we access the admin panel and delete carlos.

xxx

how the whole attack worked, in detail#

partial-construction-race-diagram

the intended design. registration is gated on email verification: you register, the server stores a secret token on your pending account and emails it to you, and only clicking the emailed link (proving you control the inbox) activates the account. restricting registrations to @ginandjuice.shop and requiring the emailed token is meant to stop anyone registering an address they don't own.

the multi-step construction. creating the account isn't atomic. the server (1) inserts the user row, (2) generates a token and writes it onto that row, (3) sends the email. between (1) and (2) the row already exists but its token column holds an uninitialised placeholder (null/empty/default). that's the partially constructed object.

the confirmation check and the patch. /confirm looks up a pending user whose stored token equals the submitted token. originally, submitting an empty token matched the uninitialised token of a half-built row, confirming it without the email. the developers patched the empty-string case (token= → Forbidden). but token[]= submits an empty array, which the patch doesn't catch (Invalid token: Array), and which can still match the uninitialised value during the window.

the timing problem. the confirmation endpoint is far faster than registration, so a lone confirmation fired alongside a registration returns long before the user row exists, there's nothing to match yet. to land inside the narrow gap between insert and token-write, we spray many confirmations per registration.

the single-packet burst. using Turbo Intruder, each attempt queues one registration (fresh username) and 50 token[]= confirmations on the same gate, then opens the gate so all 51 requests fire together. the registration begins constructing the row; the 50 confirmations arrive in a tight cluster, and with luck one of them hits the moment the row exists but the token is still uninitialised. that confirmation's empty-array token matches the null token, and the account is activated. 20 attempts give 20 chances.

the payoff. a 200 "Account registration for user UserN successful" marks a confirmation that won the race. we log in as that user with the static password from the registration request, reach the admin panel, and delete carlos. email verification was bypassed entirely, we never received or needed a real token.

the root cause in one line. the user record was usable before its confirmation token was written, so a confirmation carrying a null-equivalent token (an empty array, dodging the empty-string patch) could match the uninitialised token during the construction window and activate the account without email verification.

with this, the lab is solved!