Signed with nothing at all
A JWT library once read the token's own header to decide how to check it. When a token said its algorithm was "none", the library checked nothing, and the API let it in.
Mistakes like this have appeared in real libraries and real APIs. In this lesson you forge two tokens for Ben, a teacher, and watch the weak Noticeboard hand you every student's marks.
Only your own lab
Warning
Forge tokens only for the Noticeboard in your own lab, which has no way out to the internet. Sending one to a system you do not own, without written permission, is a crime in the UK under the Computer Misuse Act 1990.
By the end of this lesson
Note
I can make an API refuse a forged or misdirected JWT.
Starter
Have a go
Starter
3 questionsKey words
| Word | What it means |
|---|---|
| Bearer token | a token that works for whoever presents it |
| Signature | proof that a key holder made these exact bytes |
| alg none | a JWT header saying the token is not signed |
| Algorithm confusion | checking a token with a different algorithm from the one meant |
| Pinning | the verifier fixing the algorithm and key itself |
The cells students fill in are left blank in this preview.
An API checks every request's token
An API receives a bearer token in the Authorization header and believes nothing until it has checked it. RS256 means an RSA signature over SHA-256: the provider signs with its private key, and anybody can check with the public key from the JWKS.
First get a real token for Amira, meant for the Noticeboard, and send it to the safe copy.
Get Amira a token for the Noticeboard
docker exec tools python3 steps/token.py amira noticeboardShown here, not run. Press Start lab to type it into your own lab’s terminal.
Send it to the safe copy's API
docker exec tools python3 steps/api.py safe amiraShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsalg none: a token with no signature
A JWT header carries alg. The JWT rules allow alg none, an unsigned token, for cases where something else already protects it. A verifier that reads alg from the token and obeys it will accept a token with no signature at all.
forge.py writes one: claims for Ben, a teacher, with the right issuer and audience, and an empty signature.
Forge an unsigned token for Ben
docker exec tools python3 steps/forge.py noneShown here, not run. Press Start lab to type it into your own lab’s terminal.
Send it to the weak copy
docker exec tools python3 steps/api.py weak noneShown here, not run. Press Start lab to type it into your own lab’s terminal.
Send it to the safe copy
docker exec tools python3 steps/api.py safe noneShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsTry it: pin the algorithm
# A verifier decides the algorithm itself. It never asks the token.
ALLOWED = {"RS256"}
headers = [{"alg": "RS256", "kid": "k1"}, {"alg": "none"}, {"alg": "HS256", "kid": "k1"}]
for header in headers:
if header.get("alg") in ALLOWED:
print(header["alg"], "-> check the signature with the RSA key named by kid")
else:
print(header.get("alg"), "-> refuse before reading anything else")
# Add "HS256" to ALLOWED. Which forged token from the lesson would now be checked with a key?
Make a free account to save your work and track your progress.
Algorithm confusion: the public key as a secret
HS256 is an HMAC: one shared secret both makes and checks it. If a verifier is given the provider's public key and lets the token choose HS256, it uses that public key as the HMAC secret.
The public key is public. So anybody can sign with it, and the weak verifier will agree. This is algorithm confusion. The fix is pinning: the verifier decides RS256 and which key, never the token.
Sign Ben a token with the public key
docker exec tools python3 steps/forge.py hs256Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Send it to the weak copy
docker exec tools python3 steps/api.py weak hs256Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Send it to the safe copy
docker exec tools python3 steps/api.py safe hs256Shown here, not run. Press Start lab to type it into your own lab’s terminal.
A common mistake
Warning
Many people think a token is safe if its signature checks out. A valid signature only proves who made it. If the API also skips the audience, a genuine token meant for another app gets in. Check aud, iss and exp too.
A genuine token for the wrong app
Dex signs tokens for every app it serves. A token Amira got for the Timetable has a perfect signature and aud timetable. If the Timetable were careless or hostile, it could replay its users' tokens to the Noticeboard.
The safe copy checks that aud holds noticeboard, that iss is Dex and that exp has not passed, with 60 seconds' leeway for clocks.
Get Amira a token for the Timetable
docker exec tools python3 steps/token.py amiraShown here, not run. Press Start lab to type it into your own lab’s terminal.
Replay it to the weak copy
docker exec tools python3 steps/api.py weak amira-timetableShown here, not run. Press Start lab to type it into your own lab’s terminal.
Replay it to the safe copy
docker exec tools python3 steps/api.py safe amira-timetableShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsFixing it and spotting it
The fixes: pin RS256 and pick the key by kid from the provider's JWKS, then check iss, aud and exp. Keep access tokens short-lived, as Dex's ten minutes are. Use a maintained library and tell it the expected algorithm.
To spot attacks, log every refusal with its reason. A token with alg none or HS256 at an RS256 API is never an accident. The app's log has a line for each token you sent.
Find the forged tokens in the log
docker logs appShown here, not run. Press Start lab to type it into your own lab’s terminal.
Assignment: a pinned verifier
Write verify_hs256(token, key, audience, now) for an API whose tokens are signed with HS256 and a secret key (bytes). Return the claims as a dictionary when the token is good. Otherwise raise ValueError with one of these messages, checking in this order:
"format"when the token is not three parts joined by dots, or a part is not base64url JSON."alg"when the header'salgis not exactly"HS256", whatever else it says."signature"when the HMAC-SHA256 of the first two parts, with the dot, underkeyis not the third part. Compare withhmac.compare_digest."aud"whenaud(a string or a list) does not holdaudience."exp"when there is noexpor it is 60 seconds or more beforenow.
def verify_hs256(token, key, audience, now):
return {}
Make a free account to save your work and track your progress.
Exit quiz
Have a go