Key takeaways
- ✓A login through Keycloak with PKCE worked 20 times out of 20, in a median of 64.7 ms, and six attacks on the flow were refused: a stolen one-time code redeemed without its PKCE verifier, with a wrong one, a code used twice, and an authorization request with no PKCE challenge or with a redirect address on another site.
- ✓A lockout after five failures per account stopped guessing: 980 of 1,000 attempts at one account were refused, with 20 checked, and the real Dave was then refused as well, so anyone who knows a username can lock its owner out.
- ✓The same lockout stopped nothing in credential stuffing, where each account is tried once with a password leaked from another site: all 30 accounts that had reused their password were taken over, because one try per account never reaches five failures.
- ✓A limit of 20 attempts a minute per address refused 980 of 1,000 attempts from one address, but the same stuffing spread over 500 addresses, two attempts each, took all 30 accounts again.
- ✓One argon2id password check costs 116.3 ms of CPU at the argon2-cffi library's default, so one core manages 8.6 logins a second, and all 16 CPUs together managed 40 a second, against the 138 that 16 times one core would predict.
- ✓Refusing passwords found in a known leak took the 500-address run to 0 of 30 accounts taken over, with 30 logins refused and sent to reset their password, but the lab used the attacker's exact list, which is the best case for that check.
Lucía, a platform engineer, attacks her own login with a leaked list of usernames and passwords. The users are made up, and 30 of them had reused the leaked password. First she checks the login flow an app gets from Keycloak, a free open-source login server. Then she times one password check, and finally she runs credential stuffing against the app's own password login with each defence switched on in turn. The examples come from Lab 20 (Security, Auth and Multi-Tenancy) of the system design labs, script 03_login.py, and the lab's recorded results in data/results/03_login.json. The run was on an Apple M4 Max with Docker Desktop and 16 virtual CPUs, with every service in Docker on one laptop.
This chapter is about the two ways a password gets attacked, and about how a server stores one. A login has two parts: the server proves who you are, then hands you a token that the rest of the app trusts for a while.
Keeping the password out of the server
A well-built server does not keep your password. It keeps a hash: a one-way scramble it can check a guess against, but cannot turn back into the password. The app in this lab stores its hashes with argon2id, a memory-hard hash function that OWASP (the Open Web Application Security Project) recommends for new systems. Each check takes measurable CPU time and memory on purpose, so the cost of one guess is the same for the attacker as for the server. That cost is taken up in the password-storage section below.
Many apps no longer check passwords themselves. They send the user to an identity provider, a separate login service. The standard for that is OpenID Connect (OIDC), a login layer built on OAuth 2.0. Here the identity provider is Keycloak, and the app's API is the orders service the lab uses in every chapter.
The login flow, with PKCE
The flow is the authorization code flow with PKCE (Proof Key for Code Exchange, pronounced "pixy"):
- The app makes up a random secret, the verifier, and keeps it. It sends a scrambled copy of the verifier, the challenge, to Keycloak with the redirect.
- The user types their password on Keycloak's login page. Keycloak checks it and sends the browser back with a one-time code in the address.
- The app trades the code for a token, and sends the original verifier with it. Keycloak checks the verifier against the challenge it saw in step 1.
- The app calls the orders API with the token, and the API checks the token's signature. The lab's token is signed with RS256, an RSA signature, and expires after 300 seconds.
Twenty logins ran through this flow, and all twenty got tokens and an API answer. The timings (p50, the median, in milliseconds) show where the time went:
| Step | p50 ms | p95 ms |
|---|---|---|
| authorize (Keycloak's login page) | 17.8 | 27.6 |
| login (POST the password, get a code) | 34.4 | 47.5 |
| token (code and verifier for tokens) | 6.7 | 16.3 |
| first API call with the token | 3.4 | 4.4 |
| total | 64.7 | 99.5 |
What PKCE protects
The code travels in a web address, so it can leak: into browser history, a proxy log, or a malicious app registered for the same redirect. PKCE makes a stolen code useless, because only the client that made the challenge holds the verifier. Think of a visitor badge printed for one specific visitor, which opens nothing without the matching key the app kept.
The lab tried six attacks on the flow:
| Attack | HTTP status | What Keycloak said |
|---|---|---|
| A stolen code, redeemed without the verifier | 400 | PKCE code verifier not specified |
| A stolen code, with the attacker's own verifier | 400 | PKCE verification failed: code mismatch |
| The real client, after that failed attempt | 400 | Code not valid |
| A code used a second time (replay) | 200, then 400 | the first use worked, the second was refused |
| An authorization request without PKCE | none | redirected with invalid_request |
| A redirect address on another site | none | no login form: invalid redirect_uri |
Two results matter most. A failed exchange burns the code: after the attacker's wrong verifier, the real client's correct one was refused too. That is the safe choice, because it allows one guess and no retries, but it means the user has to log in again. And a request that would send the code to another site never got a login page at all.
Two ways to attack a password
Guessing tries many passwords against one account. The lab's list is 1,000 common passwords, named common-0001 to common-1000, and the target is Dave, a made-up user whose password is number 400 on the list.
Credential stuffing tries one password per account, across many accounts. Its source is a leak from some other site, and the attacker relies on people reusing the same password. Each of the 1,000 accounts here is tried once with its password from the leak, and 30 of those users reused it on this app.
Those two attacks need different defences, and a lockout only handles one of them. A lockout refuses logins to an account after five failed attempts, counted over a 15-minute window. Against guessing it works, up to a point:
- No defence: Dave's password was found at guess 400, after 1,000 attempts in 19.7 seconds. The lab's rate puts guess 400 at about eight seconds into the run, which is an estimate.
- Lockout: 20 passwords were checked, not five, and 980 attempts were refused with HTTP 429 (too many requests). The real Dave was refused too, with 429. The lab counted the failures after each hash, and 16 attempts were in flight at once, so 16 passed the "locked?" check before the first failure was counted. Then four more got through before the count reached five.
The lockout also gives an attacker a way to lock out a real user. Anyone who knows a username can make its owner's logins fail, which is a denial of service.
Against stuffing, the lockout does nothing. Each account sees one failure, and one failure never reaches five:
- Stuffing, no defence: 30 accounts taken over, the 30 that reused their password.
- Stuffing, lockout: still 30 taken over, with 1,000 password checks run.
Adding a per-address limit of 20 attempts a minute did refuse 980 of 1,000 attempts from one address, leaving 20 checks. But the run sent all 1,000 at once and finished in 0.4 seconds, so it shows the limit works in a burst, not against a slow attacker. At 20 a minute, 1,000 attempts take about 50 minutes; that is arithmetic from the limit, not something the lab ran. The lab then faked 500 addresses, two attempts each, by setting the X-Forwarded-For header, which names the address a request came from. The app trusts that header in this setup, which is only safe behind a proxy that sets it. With 500 addresses, the lockout and the per-address limit together took all 30 accounts again.
The same limits, with the attempts spread over 500 addresses (two each). The app trusts a header that names the address.
Source IPs: 500. After this run, the real dave could log in: logs in (200). Taken over counts accounts whose login returned 200. A “password checks” count of 1,000 means every attempt paid the hashing cost.
Password storage and what a check costs
The app stores argon2id hashes made with the argon2-cffi library's default settings: 64 MiB of memory, 3 passes, and 4 threads. The lab timed one check at three settings. The first is the OWASP minimum, the second the library default, and the third a stronger setting. The timings are CPU time for one process, and the all-CPU column runs 16 processes at once:
| Setting | ms per login (wall) | CPU ms per login | Logins a second per core | Logins a second, all 16 CPUs | Cores for 500 logins a second |
|---|---|---|---|---|---|
| OWASP minimum (19 MiB, t=2, p=1) | 13.5 | 13.4 | 74 | 535 | about 15 |
| argon2-cffi default (64 MiB, t=3, p=4) | 36.1 | 116.3 | 8.6 | 40 | about 200 |
| Strong (256 MiB, t=4, p=4) | 211.5 | 734.5 | 1.4 | 10 | about 797 |
The default costs 116.3 ms of CPU, but only 36.1 ms of waiting, because its 4 threads run in parallel. That makes it 8.6 logins a second per core. All 16 CPUs together managed 40 a second, not the 138 that 16 times one core predicts. The lab did not isolate the cause. Its likely causes are a busy laptop (the load average was about 44 at the end of the run), 16 processes with 4 threads each competing for 16 CPUs, and 4 slower efficiency cores.
The hash setting is a capacity decision. The "cores for 500 logins a second" column is arithmetic on the recorded 16-CPU rate: 500 ÷ the all-CPU rate × 16. Each step up the table makes an offline guessing attack on a stolen hash database more expensive by the same factor, and it also makes every login more expensive for the servers.
Cores = peak ÷ the rate the 16 CPUs managed together × 16. This is arithmetic on the recorded rate, not a load test at that peak. The bars are scaled to 800 cores.
Refusing passwords from a known leak
The last defence is a check on the password itself. If a password appears in a known leak, the login is refused, and the user is asked to reset it. The lab uses k-anonymity, the method behind Have I Been Pwned's range API: the server sends only the first five hex characters of the password's SHA-1 hash, and gets back every leaked hash with that prefix. The full password, and most of its hash, never leave the server.
The lab's leak is the attacker's own combo list, so the check starts with the answer. With it on, the 500-address stuffing run took 0 of 30 accounts. All 30 correct passwords got a 403 with "reset your password," and the other 970 got the usual 401 for a wrong password. That 403 differs from a wrong password's 401, so the attacker still learned which 30 were right. A real site should answer both cases the same way. The other answer is a second factor, such as a code from the user's phone, which a leaked password alone cannot supply.
The workflow and the results
00_setup.pystarts the Docker services (Keycloak, Postgres, Redis and the orders app) and loads the synthetic users, tenants and leak. It is the same setup every chapter in this lab uses.03_login.pyruns the flow, the hash timings and the attacks, and writesdata/results/03_login.json. It takes about three minutes.
The full attack results, one row per attack with the same 1,000 attempts each, are below. "Real Dave after" is whether the real Dave could log in once the attack ended.
| Attack | Defences | Taken over | Refused (429) | Reset forced (403) | Password checks | Seconds | Real Dave after |
|---|---|---|---|---|---|---|---|
| Guess Dave's password, 1 address | none | 1 (at guess 400) | 0 | 0 | 1,000 | 19.7 | can log in |
| Guess Dave's password, 1 address | lockout | 0 | 980 | 0 | 20 | 0.5 | refused (429) |
| Stuffing, 1 address | none | 30 | 0 | 0 | 1,000 | 20.3 | can log in |
| Stuffing, 1 address | lockout | 30 | 0 | 0 | 1,000 | 21.0 | can log in |
| Stuffing, 1 address | lockout + 20/min per address | 0 | 980 | 0 | 20 | 0.4 | can log in |
| Stuffing, 500 addresses | lockout + 20/min per address | 30 | 0 | 0 | 1,000 | 20.9 | can log in |
| Stuffing, 500 addresses | + breached-password check | 0 | 0 | 30 | 1,000 | 20.3 | can log in |
Caveats
- One recorded run, with synthetic data. The users, passwords and leak are made up. Three percent of the accounts were set to reuse the leaked password, by design, and Dave's password was set to number 400.
- The leak is the attacker's list. The breached-password result is the best case, because the defender holds exactly the list the attacker uses. A real check depends on how large and how current the leak database is.
X-Forwarded-Foris trusted here. The 500 addresses are faked with a header the app believes. A real attacker with 500 addresses would arrive with 500 real ones, and the limit would have to be keyed on something the attacker cannot set.- The timings are on a busy laptop. The hash benchmark ran on the same 16 Docker CPUs as the app, while the laptop was under heavy load. The "less than 16×" causes were not isolated.
- The lockout's 20 depends on concurrency. The 20 checks came from 16 attempts in flight at once. A different number of parallel attempts would change it.
- The 20-per-minute result is a burst. The run finished in 0.4 seconds, so the "0 taken" reflects a burst, and the one-hour figure for a slow attacker is arithmetic.
- The OIDC run covers only the login flow. It did not test token theft after login, refresh tokens, MFA, or sign-up. The password-storage results are for one argon2id setting per row.
- Not covered: CAPTCHAs, bot detection, a real breach database, and per-account limits that span many addresses. The lab does not query Have I Been Pwned.
Check your understanding
Try each question before opening its answer.
Why does a lockout after five failures stop guessing one account, but not stuffing the same 1,000 accounts once each?
The lockout counts failures for each account. Guessing sends many failures to one account, so it reaches five and the account is refused. Stuffing sends one failure to each account, so no account ever reaches five, and the lockout never triggers. The lab's stuffing run took all 30 reused passwords even with the lockout on.
A lab run reports 20 password checks against the lockout, not 5. Where do the extra 15 come from?
The lockout checks "locked?" before the hash, but the lab counts a failure only after the hash finishes. With 16 attempts in flight at once, all 16 pass the check before any failure has been counted. Then four more get through before the count reaches five. The race is in how the count is updated, not in the lockout's limit of five.
Why does the breached-password check's 403 response still help the attacker?
Because it answers differently from a wrong password. The 30 correct passwords got a 403 ("reset your password") while the other 970 got a 401, so the attacker learns which 30 logins were right, even though none got in. A site should answer a breached password the same way it answers a wrong one.
A per-address limit of 20 a minute refuses 980 of 1,000 attempts from one address. Why is that not enough against 500 addresses?
Each address gets its own allowance. With 500 addresses, the attacker has 500 times the allowance, and the lab's two-attempts-per-address run never reached the limit at all. The limit slows one machine, and the attacker simply spreads the attempts out.
Next up: Sessions vs. JWTs and revocation: this chapter ends a login with a token. The next one asks how a banned user keeps access after the ban, and what each design costs per request.