Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Rate limits that attackers cannot dodge

After this lesson: I can test whether a rate limit holds and key it on something an attacker cannot change.

60 min lesson6 commandsOWASP Juice Shop lab: a vulnerable web shop to attack in your own copy

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

By 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

WordWhat it meansExample
Rate limita cap on requests in a time window
Brute forcetrying every value until one works
Limit keywhat the limit counts againstan account
X-Forwarded-Fora header naming the original caller's address
Sliding windowcounting requests in the last few seconds
429the status that says too many requests

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

First, 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

Command
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; done

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Count the limit headers on sign-in

Command
curl -s -i -H "Content-Type: application/json" -d '{"email":"[email protected]","password":"guess"}' http://juice-shop:3000/rest/user/login | grep -ci ratelimit

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

The password reset has a limit

Command
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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Make 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

Command
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 -c

Shown 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

Command
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; done

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Which address did the log record?

Command
curl -s http://juice-shop:3000/support/logs/access.log.$(date +%F) | grep reset-password | tail -n 3 | cut -d " " -f 1

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A 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

Code: python
# 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.

Code: python
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

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.