Bypassing rate limits via race conditions

7 min read Medium Portswigger
Race condition
Contents

On this page

The lab

here is how portswigger describes it

This lab's login mechanism uses rate limiting to defend against brute-force attacks. However, this can be bypassed due to a race condition. To solve the lab: work out how to exploit the race condition to bypass the rate limit, successfully brute-force the password for the user carlos, log in and access the admin panel, delete the user carlos. You can log in to your account with the following credentials: wiener:peter

the new pieces in this lab#

this applies the race-condition idea from the limit-overrun lab to a security control: a login rate limiter. the mechanics are the same check-then-act race, but the limit we overrun is "3 failed attempts", and overrunning it lets us brute-force a password that the rate limiter was supposed to make impossible.

  • a login rate limiter caps how many failed attempts can be made against an account in a short time, here, 3 wrong passwords per username before a temporary lockout. it's a brute-force defense.
  • the limiter is per-username and server-side: trying a different username still returns the normal "Invalid username or password", and the lockout follows the account regardless of session. so the server must be storing a failed-attempt counter per username somewhere shared, exactly the shared state a race needs.
  • the race window here is the gap between when a login attempt is submitted (and its password checked) and when the server increments that username's failed-attempt counter. for a single request this gap is invisible; under concurrency, many requests can submit and have their passwords checked before the counter has been bumped past the lock threshold.
  • the tool: the single-packet attack (Turbo Intruder's race-single-packet-attack.py, using Engine.BURP2 with one connection over HTTP/2). it packs many requests so their final bytes arrive together, making them land in the same tiny window. we queue one login per candidate password and release them all at once. the method: confirm the limiter is per-username and server-side, benchmark that 3+ serial attempts lock the account, prove that a parallel burst slips more than 3 attempts through before the lock engages, then fire the whole password list in one single-packet burst so carlos's correct password is checked inside the window, giving a 302 success.

why the rate limit can be bypassed#

the rate limiter is a check-then-act control: for each login it (1) checks whether the username's failed-attempt counter is below the threshold, (2) verifies the submitted password, and (3) increments the counter if the attempt failed. the lockout depends on step 3 having run enough times. but steps 1โ€“2 and step 3 are not atomic, there's a window between checking/verifying and the counter actually being updated. the developer assumed attempts arrive one at a time, so the counter would reach 3 and block the 4th. under concurrency that breaks: if we submit, say, 28 login attempts simultaneously, they all pass the "counter < 3" check and all have their passwords verified before the counter has climbed to 3, because none of the increments have landed yet. the lockout does eventually trigger, but only after many more than three attempts have already been evaluated. every one of those evaluated attempts tells us whether its password was correct. by queueing one attempt per candidate password and firing them in a single packet, carlos's real password gets checked inside that window and returns a success, the rate limit never got the chance to stop it.

Step 1 - probe the rate limit#

we experiment with the login function, deliberately submitting wrong passwords for our own account. after 3 incorrect attempts we're temporarily blocked from further login attempts for that account.

11

Step 2 - establish that the limit is per-username and server-side#

we try logging in with a different arbitrary username and get the normal Invalid username or password message, not a lockout. so the rate limit is enforced per-username, not per-session. we deduce the number of failed attempts per username is stored server-side, and reason there's a race window between submitting a login attempt and the server incrementing that username's failed-attempt counter.

Step 3 - benchmark serially#

from the proxy history we find a POST /login with a failed attempt for our own account and send it to Repeater. we add the tab to a group, right-click Duplicate tab, and make 19 duplicates (all auto-added to the group). we send the group in sequence, using separate connections to reduce interference. as expected, after two more failed attempts we're locked out, serial requests respect the limit.

22

Step 4 - probe with a parallel burst#

we send the same group again, but this time in parallel. studying the responses, although the account lock does eventually trigger, more than three requests received the normal "Invalid username or password" response instead of the lockout. this proves that if we're fast enough, we can get more than three attempts evaluated before the lock engages, the race window is real and exploitable.

33

Step 5 - set up the single-packet attack#

in Repeater we highlight the password parameter value in the POST /login request, right-click, and choose Extensions > Turbo Intruder > Send to turbo intruder. the password value is auto-marked as a payload position with %s. we change the username to carlos and select the examples/race-single-packet-attack.py template, editing it so it queues one request per candidate password:

python
def queueRequests(target, wordlists):
    # target supports HTTP/2 โ†’ single-packet attack: Engine.BURP2 + concurrentConnections=1
    engine = RequestEngine(endpoint=target.endpoint,
                           concurrentConnections=1,
                           engine=Engine.BURP2
                           )
 
    # candidate passwords come from the clipboard
    passwords = wordlists.clipboard
 
    # queue one login per password; 'gate' withholds the final byte of each
    for password in passwords:
        engine.queue(target.req, password, gate='1')
 
    # release every gated request at once โ†’ they land together
    engine.openGate('1')
 
 
def handleResponse(req, interesting):
    table.add(req)

we copy the candidate password list to the clipboard (the script reads wordlists.clipboard).

44

Step 6 - launch and read the winner#

we launch the attack and study the responses. if there are no successful logins, we wait for the lock to reset and repeat (optionally pruning passwords we already know are wrong). a 302 response marks a successful login, we note the corresponding password from the Payload column.

55
66

Step 7 - log in and delete carlos#

we log in as carlos with the identified password, access the admin panel, and delete the user carlos.

how the whole attack worked, in detail#

rate-limit-race-diagram

the intended (serial) defense. the rate limiter is meant to make brute force infeasible: for each login against a username it reads the failed-attempt counter, and if the counter is already at the threshold (3) it refuses the attempt outright; otherwise it verifies the password and, on failure, increments the counter. after 3 failures the account is locked, so an attacker can only ever test 3 passwords per lockout period, far too few to brute-force.

the race window. the weakness is that "check the counter + verify the password" and "increment the counter" are separate, non-atomic steps with a gap between them. in a single request nothing happens in that gap. but it's real server time, and if many requests overlap, a request can pass the "counter < 3" check before earlier requests have incremented the counter.

the collision. the counter is shared server-side state keyed on the username (Step 2), so all of our concurrent attempts against carlos read and bump the same counter, that's the shared record a collision needs. using the single-packet attack, we align the final bytes of ~28 login requests (one per candidate password) so they arrive within microseconds of each other. they all pass the "counter < 3" check and all have their passwords verified before any increment has landed. the lockout still fires eventually, but only after far more than three attempts have already been evaluated.

turning the bypass into a brute-force. because every evaluated attempt uses a different candidate password and returns a distinguishable result (failed login vs a 302 redirect on success), the burst is effectively a full password spray that the rate limiter never throttled. carlos's correct password, checked inside the window, returns 302. we read it off the results, log in, and delete carlos.

the root cause in one line. a security limit (3 attempts) was enforced by a non-atomic check-then-increment on a shared per-username counter, so a simultaneous burst had all its passwords verified before the counter could reach the lock threshold.

with this, the lab is solved!