JWT attacks
A JWT (JSON Web Token) carries signed claims between parties; it’s heavily used for sessions and API authorization. Its security depends entirely on the server verifying the signature correctly and pinning the expected algorithm. When that verification is weak, optional or confusable, the attacker forges or tampers the token and becomes another user or an admin —without touching passwords or sessions—. It’s one of the highest-impact bug-bounty flaws because the token is usually the only proof of identity the backend accepts.
flowchart LR
A[Attacker] -->|"tamper payload: role=admin"| T["Forged token"]
T -->|"sig: none / HMAC with public key / cracked secret"| V["JWT verifier"]
V -->|"verifies sig and alg?"| D{Weak?}
D -->|"yes"| W["Access as admin"]
D -->|"no"| R["401 / rejected"]
Threat model
Section titled “Threat model”What the attacker gains depends on what the server verifies poorly:
- Impersonation: change
sub/user/emailand act as another account. - Privilege escalation: set
role: admin,isAdmin: true,scope/groups. - Authentication bypass: forge a valid token with no credentials.
- Persistence: with no
exp, or if it isn’t honored, or there’s no revocation, a stolen or forged token works indefinitely. - Cross-service pivot: reuse one service’s token on another that trusts the same
issuer but doesn’t validate
aud.
Anatomy
Section titled “Anatomy”Three Base64URL parts separated by dots: header.payload.signature.
- header: algorithm (
alg) and, optionally, parameters saying where the key comes from:kid(key id),jku/x5u(URL to a JWKS/cert),jwk(embedded public key). - payload: the claims (
sub,role,exp,iss,aud,nbf…). It is signed, not encrypted: anyone can read it. - signature: symmetric HMAC (
HS256, shared key) or asymmetric signature (RS256/ES256, private key signs, public key verifies).
The invariant every correct verifier holds —and every attack breaks— is: (1) it
checks the signature, (2) against an algorithm the server chooses (not the token),
(3) with a key from a trusted source, and (4) it validates the claims
(exp/iss/aud).
Variants
Section titled “Variants”alg: none— the token declares it is unsigned and the verifier accepts it.- Algorithm confusion (RS256 → HS256) — the server verifies with HMAC using the RSA public key as the secret, which the attacker knows.
- Weak HMAC secret —
HS256with a guessable secret: crack it offline and sign at will. kidinjection — thekidvalue is used unsanitized in a file path, a SQL query or a command.jku/x5u/jwk— the attacker controls the source of the verification key (their own JWKS, or a key embedded in the header).- Claim-validation flaws —
expnot honored,aud/issnot checked, cross-tenant reuse.
Red Team
Section titled “Red Team”Discovery and fingerprint
Section titled “Discovery and fingerprint”Find the JWT (cookie, Authorization: Bearer, JSON body, parameter). Decode header and
payload (they’re Base64URL, not encrypted):
# decode header and payload with no librariescut -d. -f1 token.txt | base64 -d 2>/dev/null; echocut -d. -f2 token.txt | base64 -d 2>/dev/null; echoNote alg (HS or RS/ES?), whether there’s kid/jku/x5u/jwk, and which claim
decides access (role, sub, scope). Two gold tests before anything else:
- Is the signature verified? Alter a payload claim without touching the
signature and resend. If you get through, the server doesn’t verify (or accepts
none). - Is
exphonored? Resend an expired token. If it still works, there’s no validity control and any stolen token is permanent.
alg: none
Section titled “alg: none”The verifier accepts a token that declares it is unsigned. Set {"alg":"none"} in the
header, alter the payload and leave the signature empty (the token ends in a dot):
eyJhbGciOiJub25lIn0.eyJzdWIiOiJhZG1pbiIsInJvbGUiOiJhZG1pbiJ9.Try case variants (None, NONE, nOnE) to bypass poor blocklists. With a tool:
jwt_tool <token> -X a.
Algorithm confusion (RS256 → HS256)
Section titled “Algorithm confusion (RS256 → HS256)”If the server expects RS256 but lets the token choose alg and verifies HS256 using
the public key as the HMAC secret, you can sign it yourself: the public key is
public. Steps:
- Get the public key:
/.well-known/jwks.json, the IdP endpoint, or recover it from two legitimate tokens withrsa_sign2n. - Change
algtoHS256, alter the payload and sign with HMAC-SHA256 using the public key’s PEM as the key.
jwt_tool <token> -X k -pk public.pem # automated key confusionWeak HMAC secret
Section titled “Weak HMAC secret”If HS256 uses a short or dictionary secret (common in templates and copy-pasted
examples), crack it offline and then sign any token:
hashcat -m 16500 token.jwt /usr/share/wordlists/rockyou.txt# other lists: jwt.secrets.list, SecLists; also john --format=HMAC-SHA256Once cracked, sign yourself a role: admin with jwt_tool <token> -S hs256 -p '<secret>'.
kid injection
Section titled “kid injection”kid tells the verifier which key to use; if it’s interpolated unsanitized:
- Path traversal:
"kid":"../../../../dev/null"forces an empty/known key and you sign the token with that same known key (e.g. HMAC with an empty secret). - SQLi: if
kidqueries a key table, inject so it returns a value you control (see SQL injection). - Command injection / LFI: depending on how the value is used.
jku / x5u / jwk — controlling the key source
Section titled “jku / x5u / jwk — controlling the key source”If the verifier fetches the public key from where the header says, the attacker controls it:
jku/x5u: point to a JWKS/cert on your server with your public key and sign with your private key. Often chains with SSRF (the server makes the outbound request).jwk: embed your own public key in the header and sign with the private key; if the verifier trusts the token’sjwk, it passes.
{"alg":"RS256","typ":"JWT","jku":"https://attacker.tld/jwks.json"}Automated: jwt_tool <token> -X s (jwk), -X i (jku), -X x (x5u).
Claim validation and logic
Section titled “Claim validation and logic”Even with the signature verified correctly, check: exp/nbf not checked, aud
missing (one service’s token accepted by another), iss not validated, and
authorization claims the backend trusts without re-checking against the database
(trusts the token’s role instead of the server).
By hand (the researcher’s flow)
Section titled “By hand (the researcher’s flow)”- Is the signature verified? (alter a claim; if you pass, it doesn’t).
- Which
algfamily? HS → try weak secret andnone; RS/ES → try confusion,jku/x5u/jwk, and, on Java, blank signatures (see CVEs). - Do I control the key source? (
kid,jku,x5u,jwk). - Does the claim I forge grant real access? Prove impact with a minimal PoC.
Tooling
Section titled “Tooling”jwt_tool (automates none, confusion, jku/x5u/jwk, kid, claim fuzzing),
hashcat -m 16500 and john (HMAC secret), Burp’s JWT Editor extension (edit
and re-sign in Repeater), jwt.io (decode/inspect).
Impact and chaining
Section titled “Impact and chaining”Full ATO, admin access, and access to other users’ data combined with IDOR/BOLA when the forged token reaches an API that trusts it. A JWT forged in the OAuth/OIDC flow can lead to account takeover across every service behind the SSO.
Blue Team
Section titled “Blue Team”Detection
Section titled “Detection”- Tokens with
alg: none/None,algother than expected, or family changes (RS→HS) within the same flow. jku/x5upointing to domains not in an allow-list, orjwkpresent on inbound tokens.kidwith../, quotes, SQL metacharacters or nulls.- Expired tokens being accepted; spikes of verification errors (sign of secret brute-forcing or claim fuzzing).
Telemetry and sources
Section titled “Telemetry and sources”Verifier/IdP logs with the alg and kid used, API gateway/WAF, service egress
(outbound requests to external JWKS via jku), and token-validation metrics.
Hardening
Section titled “Hardening”- Pin the algorithm on the server (allow-list); never derive it from the header.
Reject
none. - Don’t use the same routine for HS and RS: separate symmetric from asymmetric verification to prevent confusion.
- Strong HMAC secret (random, ≥256 bits) or well-managed, rotated asymmetric keys.
kid,jku,x5uonly against an origin allow-list; prefer local keys. Ignore inboundjwk.- Always validate
iss,aud,exp,nbf. Short expiry + revocable refresh tokens; don’t store secrets in the payload (it’s readable). - Keep the JWT library current (see CVEs); on Java ≥15, patch ECDSA verification.
Response
Section titled “Response”Rotate the signing key/secret (invalidates all tokens), review access made with
suspicious tokens, fix the verification, and deploy a detection rule for none/external
jku.
CVEs and real-world cases
Section titled “CVEs and real-world cases”- Auth0 / Tim McLean (2015) — foundational disclosure of
alg:noneand RS/HS confusion across multiple JWT libraries; the origin of almost all current defenses. - CVE-2015-9235 —
node-jsonwebtoken< 4.2.2: algorithm confusion letting an attacker forge tokens by treating an RS public key as the HS secret. - CVE-2022-21449 “Psychic Signatures” — Java 15–18 accepted blank ECDSA signatures
(r=s=0) as valid: any
ES256/ES384/ES512JWT with a null signature passed verification. A critical crypto flaw. - CVE-2022-23529 —
node-jsonwebtoken< 9.0.0: a craftedsecretOrPublicKeyinjwt.verify()could lead to code execution on the verifier.
Per-library, per-version CVEs in NVD (https://nvd.nist.gov/vuln/search) and GitHub Advisories (https://github.com/advisories).
Testing checklist
Section titled “Testing checklist”- Is the signature verified? (alter a claim without touching the signature).
-
alg:noneand case variants tested. - RS256→HS256 algorithm confusion tested (public key as secret).
- HS256 secret cracking attempted (hashcat
-m 16500). -
kid(path traversal/SQLi),jku,x5u,jwktested. -
exp/nbf/iss/audvalidation and cross-service reuse checked. - On a Java backend, blank ECDSA signature tested (CVE-2022-21449).
- Impact proven with a minimal PoC (e.g. admin access), without harming real data.