Exploiting time-sensitive vulnerabilities

8 min read Hard Portswigger
Race condition
Contents

On this page

The lab

here is how portswigger describes it

This lab contains a password reset mechanism. Although it doesn't contain a race condition, you can exploit the mechanism's broken cryptography by sending carefully timed requests. To solve the lab: identify the vulnerability in the way the website generates password reset tokens, obtain a valid password reset token for the user carlos, log in as carlos, access the admin panel and delete the user carlos. You can log into your account with the following credentials: wiener:peter.

the new pieces in this lab#

this sits in the race-conditions topic but, as the brief says, it is not a race condition, it's broken cryptography that we expose by sending carefully timed requests. the idea: if a secret token is derived from the current time, two requests processed at the same instant get the same token.

  • a time-sensitive / predictable token: the password reset token is generated by hashing some internal state. a token of consistent length that changes every time is likely a hash whose input includes a changing value, an RNG, a counter, or a timestamp. if it's a timestamp, the token is predictable to anyone who can make two requests resolve to the same time.
  • the tell we're hunting: if two reset requests are processed at the exact same timestamp and produce the identical token, the timestamp is an input to the hash. that's what we try to force.
  • PHP per-session locking (the obstacle): the session cookie shows a PHP back-end, and PHP locks the session file for the duration of a request, so it processes one request at a time per session. two requests on the same session are therefore serialised, they get different timestamps and different tokens, which is why same-session parallel sends don't collide. to get true concurrency we must use two different sessions.
  • the username is a separate parameter, not part of the token. the reset link carries username and token as separate query-string values. if the username isn't hashed into the token, then a token generated for one user is valid for any username, two different users could share a token. the method: confirm the token changes per request (hash of internal state), bypass PHP's per-session lock by issuing the two reset requests from two separate sessions so they're processed concurrently, prove that simultaneous processing yields an identical token (so time is the hash input), then fire one reset for our account and one for carlos at the same instant so both get the same token, and use our copy of that token, with the username swapped to carlos, to reset carlos's password.

why the timing makes the token predictable#

the flaw is that the reset token is a hash of a value that changes with time (a timestamp) and does not include the username. two consequences follow. first, because time is the only varying input, any two requests the server happens to process within the same time tick produce the same hash, so the token is not unguessable, it's a function of when the request is handled. second, because the username is kept out of the hash, a token minted during our own reset is equally valid for carlos's reset, the token proves "a reset was requested at time T", not "a reset was requested for this user at time T". normally you can't line two requests up on the same tick because PHP serialises same-session requests, and you'd only ever see your own token anyway. we defeat both: we use two sessions so the requests run concurrently and land on the same timestamp, and we exploit the username being separate so our visible token also unlocks carlos's account. it isn't a race on shared state, it's forcing a timing collision in a time-seeded token and then reusing it across users.

Step 1 - study the reset flow#

we submit a password reset for our own account and receive an email with a reset link. the link's query string includes our username and a token, two separate values.

11

Step 2 - observe per-request tokens#

we send POST /forgot-password to Repeater and fire it a few times, then check the inbox: every reset request produces a link with a different token. we reason:

  • the token is a consistent length, so it's either a fixed-length random string or a hash of some data.
  • it changes each time, so if it's a hash it must include changing internal state, an RNG, a counter, or a timestamp. 22

Step 3 - try same-session parallelism (fails)#

we duplicate the Repeater tab, add both to a group, and send the pair of reset requests in parallel a few times. there's still a significant delay between the responses and still a different token in each email. so the requests are being processed in sequence, not concurrently, something is serialising them.

33

Step 4 - get a second session#

the session cookie indicates a PHP back-end, which likely processes one request at a time per session (per-session locking). to run two requests truly concurrently we need two sessions. we send GET /forgot-password to Repeater, remove the session cookie, and send it, the response issues a new session cookie and CSRF token.

44

Step 5 - align the two requests across sessions#

we copy the newly issued session cookie and CSRF token into one of the two POST /forgot-password requests, so the pair now comes from two different sessions. we send the two POSTs in parallel a few times. the processing times are now much more closely aligned, sometimes identical, the per-session lock no longer serialises them.

66

Step 6 - confirm a timestamp seeds the token#

back in the inbox, when the response times match for the pair, we receive two confirmation emails using an identical token. that confirms a timestamp is one of the hash inputs: same instant → same token. and since username is a separate parameter, it likely isn't hashed, so two different usernames could share a token.

Step 7 - mint carlos's token by collision#

in Repeater we change the username parameter in one of the two POST requests to carlos, leaving the other as our own account. we resend the two in parallel. if the attack lands, both users are assigned the same reset token (we can't see carlos's directly).

55

we check our inbox and this time receive only one new confirmation email (ours). we infer the other email, with the same token, has gone to carlos. we copy the link from our email and change the username in the query string to carlos. visiting the URL takes us to the set-new-password form as normal, the token is accepted for carlos because the username wasn't part of it.

77

Step 8 - reset carlos's password and finish#

we set a password we'll remember and submit the form, then log in as carlos. (if login fails, we resend the pair and repeat, it's timing-dependent.) once logged in, we visit the admin panel and delete the user carlos.

88

how the whole attack worked, in detail#

time-sensitive-token-diagram

the intended design. a password reset issues a one-time token tied to the user, emailed to that user's address; only someone who receives the email can use it. the token is supposed to be unguessable and specific to the account.

the cryptographic flaw. the token is actually a hash whose input varies with time (a timestamp) and does not include the username. a fixed-length, always-different token pointed us at "hash of changing state"; forcing two requests onto the same instant and getting the same token proved the changing state is a timestamp. so the token is (a) predictable/duplicable by controlling when requests are processed, and (b) not bound to a user, so it's valid for any username.

the obstacle: PHP per-session locking. PHP locks the session for the duration of each request, serialising same-session requests. that's why our first same-session parallel attempts still produced different timestamps and tokens, the requests never actually overlapped. this locking is also what normally hides the vulnerability.

bypassing the lock. we requested a fresh session (GET /forgot-password with no cookie → new cookie + CSRF token) and put it on one of the two reset requests. now the two requests belong to different sessions, so PHP doesn't serialise them; they run concurrently and their processing times converge, sometimes identical.

forcing the collision. with the requests overlapping, two of them processed in the same time tick hash the same timestamp and produce the same token. we confirmed this with two of our own addresses receiving identical tokens.

reusing the token across users. we then set one request's username to carlos and the other to our own account, fired both together, and (on a successful run) both were assigned the same token. ours arrived in our inbox; carlos's identical token went to carlos. because the username isn't in the hash, we took our token, changed username=carlos in the reset URL, and the server accepted it for carlos, letting us set carlos's password.

the payoff. logged in as carlos, we reach the admin panel and delete carlos. the attack is probabilistic on hitting the same tick, so we repeat the parallel pair until it works.

the root cause in one line. the reset token was a hash of a timestamp with the username left out, so making two requests resolve to the same instant produced a shared, user-agnostic token that we reused to reset another user's password.

with this, the lab is solved!