Ten thousand guesses in a minute
A four-digit code has 10,000 possible values. A person guessing one a second needs hours. A program can try them all in a minute.
A rate limit caps how many requests a caller may make in a time. Whether it helps depends on who it thinks the caller is.
The one rule
Warning
Send floods of guesses only at your own lab copy. Guessing passwords on a real service, or flooding it, is a crime in the UK under the Computer Misuse Act 1990. Testing a real system needs the owner's written permission first.
Quick check: the rule
Have a go
Quick check: the rule
3 questionsBy the end of this lesson
Note
I can test whether a rate limit holds and key it on something an attacker cannot change.
Key words
| Word | What it means | Example |
|---|---|---|
| Rate limit | a cap on requests in a time window | |
| Brute force | trying every value until one works | |
| Limit key | what the limit counts against | an account |
| X-Forwarded-For | a header naming the original caller's address | |
| Sliding window | counting requests in the last few seconds | |
| 429 | the status that says too many requests |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsFirst, is there a limit at all?
Send ten wrong passwords to the sign-in endpoint and read the statuses. A limit would answer 429 at some point.
Then look for rate limit headers. Many limiters announce themselves with headers such as X-RateLimit-Limit.
Ten wrong passwords in a row
for p in 1 2 3 4 5 6 7 8 9 10; do curl -s -o /dev/null -w "%{http_code} " -H "Content-Type: application/json" -d "{\"email\":\"[email protected]\",\"password\":\"guess$p\"}" http://juice-shop:3000/rest/user/login; doneShown here, not run. Press Start lab to type it into your own lab’s terminal.
Count the limit headers on sign-in
curl -s -i -H "Content-Type: application/json" -d '{"email":"[email protected]","password":"guess"}' http://juice-shop:3000/rest/user/login | grep -ci ratelimitShown here, not run. Press Start lab to type it into your own lab’s terminal.
The password reset has a limit
curl -s -i -H "Content-Type: application/json" -d "{\"email\":\"[email protected]\",\"answer\":\"blue\",\"new\":\"Ana_99999\",\"repeat\":\"Ana_99999\"}" http://juice-shop:3000/rest/user/reset-password | grep -i -e "limit-limit" -e "remaining"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsMake the limit answer 429
The reset endpoint allows 100 requests in five minutes. The next Run sends 100 more wrong answers and counts each status.
In a fresh copy the first request above used one, so 99 come back 401 and the last comes back 429.
One hundred wrong answers
for i in $(seq 1 100); do curl -s -o /dev/null -w "%{http_code}\n" -H "Content-Type: application/json" -d "{\"email\":\"[email protected]\",\"answer\":\"x$i\",\"new\":\"Ana_99999\",\"repeat\":\"Ana_99999\"}" http://juice-shop:3000/rest/user/reset-password; done | sort | uniq -cShown here, not run. Press Start lab to type it into your own lab’s terminal.
Who does the limit think you are?
The shop counts requests by address. Behind a proxy, it reads the address from X-Forwarded-For, a header the caller can write.
So send a different made-up address with each request. If the limit is keyed on that header, each request starts a fresh count.
Three more, each from a made-up address
for i in 1 2 3; do curl -s -o /dev/null -w "%{http_code} " -H "X-Forwarded-For: 10.0.0.$i" -H "Content-Type: application/json" -d "{\"email\":\"[email protected]\",\"answer\":\"y\",\"new\":\"Ana_99999\",\"repeat\":\"Ana_99999\"}" http://juice-shop:3000/rest/user/reset-password; doneShown here, not run. Press Start lab to type it into your own lab’s terminal.
Which address did the log record?
curl -s http://juice-shop:3000/support/logs/access.log.$(date +%F) | grep reset-password | tail -n 3 | cut -d " " -f 1Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA common mistake
Warning
Many people think limiting by address is enough. An attacker can send a fake header, or many real addresses, and a whole college shares one address. Limit sign-in by account as well, and take the address only from your own proxy, never from what the caller sent.
Try it: a sliding window per account
# A sliding window: at most 3 requests in any 10 seconds, per account.
LIMIT, WINDOW = 3, 10
seen = {}
def allow(account, now):
recent = [t for t in seen.get(account, []) if now - t < WINDOW]
if len(recent) >= LIMIT:
seen[account] = recent
return 429
seen[account] = recent + [now]
return 200
for now in [0, 1, 2, 3, 11, 12]:
print(now, allow("ana", now))
# Change LIMIT to 5. Which request is refused now?
Make a free account to save your work and track your progress.
Fix it and watch for it
Fix: limit sign-in and reset per account and per address. Trust X-Forwarded-For only from proxies you run, reading it from the right. Answer 429 with Retry-After, and slow down rather than lock an account for good.
Detect: alert on many failed sign-ins for one account, or one account seen from many addresses in a minute.
Assignment: a limiter keyed safely
Write three functions for a limiter behind your own proxies.
client_address(remote, forwarded, trusted): remote is the address the connection came from, forwarded is the X-Forwarded-For text (addresses joined by ", ", possibly empty) and trusted is the set of your proxies. If remote is not trusted, return it. Otherwise walk forwarded from the right and return the first address that is not trusted; if every one is trusted, return the leftmost, or remote when forwarded is empty.
allow(history, now, limit, window): history is a list of earlier times for one key. Keep the times with now - t < window; if fewer than limit remain, return (True, kept + [now]), else (False, kept).
replay(events, limit, window): events is a list of (time, key) in order. Return the list of statuses, 200 or 429, one per event, keeping a history per key.
def client_address(remote, forwarded, trusted):
return forwarded.split(", ")[0] if forwarded else remote
def allow(history, now, limit, window):
return (True, history + [now])
def replay(events, limit, window):
return [200 for event in events]
Make a free account to save your work and track your progress.
Exit quiz
Have a go