ealeng

Episode 49 of 55 · Security

Credential stuffing and password storage: a lockout stopped none of 30 reused passwords, and a leak check stopped all 30

Twenty logins through Keycloak with PKCE all worked, in a median of 64.7 ms, and six attacks on that flow were refused. Against the app's own password login, a lockout after five failures stopped guessing one account but let 30 of 30 reused passwords through when they were tried once each. A limit of 20 attempts a minute per address stopped one machine and not 500 addresses. Only refusing passwords from a known leak stopped the 500-address run. One argon2id check costs 116.3 ms of CPU at the library default, which is why a peak of 500 logins a second needs about 200 cores.

Lab 20 · Security, Auth and Multi-Tenancy12 min read
Video transcript9:12

0:00Cold open

0:01

I attacked my own login with a list of a thousand usernames and passwords, leaked from another site. The users are made up. The attack is real.

0:11

Thirty of those users had reused that password with me. I turned on a lockout and a limit per internet address. Spread over five hundred addresses, the attacker still took all thirty.

0:25

And against a single guesser, the lockout did something worse: it locked the real user out of his own account.

0:33

One more check took it to zero.

0:37

So which defence stops which attack? And what does checking one password cost?

0:44Intro

0:46

Hands-on system design, part forty-nine.

0:49

Today: logging in. A login service, what a password check costs, and the attacks on it.

0:56Meet Lucía

0:58

I’m Lucía, a platform engineer, and I’m looking at our login. Authentication: proving who you are.

1:05

A well-built server never keeps your password. It keeps a hash: a one-way scramble it can check a guess against, but can’t turn back into the password.

1:15

A good login gets a token: a pass, stamped by the server so it can’t be faked, that the app accepts for a while.

1:24

And the attacker has a leaked list: usernames and passwords from some other site. People reuse passwords.

1:32

Think of an office building. Logging in is showing ID at the front desk, and the token is your visitor badge.

1:40

Credential stuffing is keys stolen from another building, tried once on every door. A lockout is a door that jams after five wrong keys.

1:5201 · What you need

1:53

To run it yourself: Docker, for a login service, the orders app and its database, all on one laptop.

2:01

The quick tests run without Docker, in a couple of seconds. Every user and password here is made up, and nothing costs money.

2:1202 · A login service

2:13

First, logging in the way many apps do now: through a separate login service, using a standard called OpenID Connect. Here the login service is Keycloak, a free, open-source login server.

2:27

The app makes up a secret and keeps it, then sends you to the login service with a scrambled copy, called the challenge.

2:35

You type your password there. The login service checks it, and sends back a one-time code.

2:42

The app trades that code for a token, and sends the original secret with it, called the verifier. That’s PKCE.

2:51

Then the app calls the orders service, its API, with the token, and the API checks its signature.

2:59

Twenty logins, and all twenty worked. A whole login typically took about sixty-five milliseconds, on a busy laptop.

3:0903 · Attacking the flow

3:11

Then I attacked the flow itself.

3:14

The code travels in a web address, so it can leak. Trading it without the app’s secret, or with a made-up one, was refused.

3:23

And that failed try burned the code. The real app, with the right verifier, was refused too. Its user has to log in again, which is the safe choice.

3:35

A code used twice: refused the second time. A login request with no challenge: refused.

3:43

And a request that would send the code to someone else’s site got no login page at all.

3:5004 · One password check

3:52

Now the app’s own password login, the one it has besides Keycloak. It stores hashes with argon2id, a password hash, at its library’s default. I timed one check, over and over, in one program, and counted the processor time it used.

4:09

At the minimum setting a security guide called OWASP recommends: thirteen milliseconds of processor time.

4:18

At the library’s default: a hundred sixteen milliseconds of processor time. One core can do under nine logins a second.

4:27

That’s slow on purpose. If the hashes are ever stolen, every guess costs the attacker about as much, even on their own hardware.

4:3805 · Every core

4:39

Then all sixteen cores at once. At the default, sixteen times one core would be about a hundred thirty-eight logins a second. Measured: forty.

4:51

Likely reasons, not isolated: this laptop was busy, and each check runs four threads, so sixty-four threads fought over sixteen cores. And some of the cores are slower ones.

5:04

To handle five hundred logins a second at the default, you’d need somewhere between sixty and two hundred cores. The hash setting is a capacity decision.

5:1606 · Guessing one account

5:17

Now the attacks, on that same app login, not on Keycloak. First, guessing one account: a thousand common passwords against one user, Dave. I set his password to number four hundred on the list.

5:31

With no defence, the attacker was in at guess four hundred, about eight seconds into a twenty-second run, estimated from the rate.

5:41

With a lockout, five failures and the account refuses logins for fifteen minutes, and again every time they try: not in. The rest were refused at once.

5:53

But twenty passwords were checked, not five. Sixteen guesses were already being checked when the first one failed, and four more got in before the count reached five.

6:05

And then the real Dave logged in, with his correct password: refused. Anyone who knows a username can lock its owner out.

6:1507 · Credential stuffing

6:17

Credential stuffing is different: each of the thousand accounts tried just once, with its password from the leaked list.

6:25

With no defence: thirty accounts taken. Those are exactly the users who had reused their password.

6:33

With the lockout: still thirty. One try per account never reaches five failures.

6:40

Add a limit of twenty attempts a minute per address. From one address: twenty tries, then refused. None taken in this run. But it’s a speed limit: one patient machine would get all thirty in under an hour.

6:55

I faked five hundred addresses, with a note in each request saying which address it came from, which this app believes. Real attackers rent real ones. Two tries each: thirty taken again.

7:0908 · In code

7:11

In code, the order matters. First the limit per address, then the lockout. Both refuse before any hashing, so a refusal costs almost nothing.

7:21

Then the hash check. A failure is counted only after it finishes. That’s how fifteen extra guesses got past the lockout.

7:31

And a correct password meets one last check: is it in a known leak?

7:3709 · Known leaks

7:39

With that check on, the same attack from five hundred addresses: none taken. Those thirty logins were refused with “reset your password”, so the attacker got no way in, though the different answer still showed which thirty passwords were right.

7:55

To be fair, I gave the defender the attacker’s exact list. Real sites use a big leak database, like Have I Been Pwned. The other answer is a second factor, like a code from your phone.

8:09

Each refused try is nearly free; each checked one costs a hundred sixteen milliseconds. So a per-address limit also saves your processors.

8:21Back to Lucía

8:22

So, three lessons.

8:25

A lockout stops guessing one account, and lets anyone lock a real user out. Against stuffing, it did nothing.

8:34

Limits per address slow one machine, and don’t stop many.

8:39

And check passwords against known leaks. Then budget the hash: every login costs real processor time.

8:47

Next time: banning a user. Sessions against tokens, what each check costs, and rotating database passwords.

8:56

For today: know the leak, and budget the hash.Lucía: Know the leak. Budget the hash.

9:00Summary

9:01

Lockouts stop guessing, not stuffing; per-address limits only slow one machine; check known leaks, and budget the hash.

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"):

  1. 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.
  2. 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.
  3. 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.
  4. 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.

Try it · Which defence stops which attackrecorded run, 03_login.py · 1,000 attempts each

The same limits, with the attempts spread over 500 addresses (two each). The app trusts a header that names the address.

Taken over
30
Refused (429)
0
Password checks
1,000
Seconds
20.9
Taken over: 30Wrong password (401): 970Reset forced (403): 0Refused (429): 0

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.

Try it · What one login costs, and the cores a peak needsrecorded run, 03_login.py · part b
CPU per login
116.3 ms
Wall per login
36.1 ms
Logins/s per core
8.6
Logins/s, all 16 CPUs
40.0
OWASP minimum15 cores
argon2-cffi default200 cores
Strong797 cores

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

  1. 00_setup.py starts 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.
  2. 03_login.py runs the flow, the hash timings and the attacks, and writes data/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-For is 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.

Learning something useful?

New labs, articles, and videos on evaluating and testing AI systems come out regularly. Follow along so you don't miss the next one.