JWT Attacks

Listen to this Post

Featured Image
JSON Web Tokens (JWT) are widely used for authentication and authorization in web applications. However, they can be vulnerable to various attacks if not implemented securely. Below are key attack vectors and mitigation techniques.

You Should Know:

1. None Algorithm Attack

Some JWT libraries accept tokens with the `alg` field set to none, allowing attackers to bypass signature verification.

Exploit Command:

echo -n '{"alg":"none","typ":"JWT"}' | base64 | tr -d '=' 

Mitigation:

  • Reject tokens with alg: none.
  • Use strong algorithms like `RS256` instead of HS256.

2. Weak Secret (Brute Force Attack)

If HMAC (HS256/HS384/HS512) is used with a weak secret, attackers can brute-force the key.

Brute-Force with Hashcat:

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

Mitigation:

  • Use long, random secrets.
  • Prefer RSA-based algorithms (RS256).

3. Key Confusion Attack

If a server expects an RSA-signed token but accepts HMAC, attackers can forge tokens by switching the algorithm.

Exploit Steps:

1. Obtain the public key (`pub.pem`).

2. Convert it to HMAC key:

openssl rsa -pubin -in pub.pem -text -noout | grep -E 'modulus|exponent' 

3. Craft a malicious JWT using `jwt_tool`:

python3 jwt_tool.py <JWT> -X k -pk public.pem 

Mitigation:

  • Explicitly validate the algorithm before processing.

4. JWK/JKU Header Injection

Attackers can inject a malicious `jku` (JWK Set URL) or `jwk` (embedded key) to bypass validation.

Exploit:

{
"alg": "RS256",
"jku": "https://attacker.com/malicious_keys.json"
}

Mitigation:

  • Restrict allowed `jku` domains.
  • Disable dynamic key fetching.

5. Token Expiry Bypass

If expiry (exp) claims aren’t validated, tokens remain valid indefinitely.

Check with `jq`:

echo $JWT | cut -d '.' -f 2 | base64 -d | jq 

Mitigation:

  • Always validate exp, nbf, and `iat` claims.

6. Kid Manipulation

The `kid` (Key ID) header can lead to SQLi, path traversal, or SSRF.

Path Traversal Example:

{
"kid": "../../../../dev/null",
"alg": "HS256"
}

Mitigation:

  • Sanitize `kid` values.

7. Cross-Service Replay Attacks

Tokens valid across multiple services can be reused maliciously.

Mitigation:

  • Use unique `aud` (Audience) claims per service.

What Undercode Say:

JWT attacks exploit misconfigurations, weak algorithms, and poor validation. Always:
– Use `RS256` over HS256.
– Validate all claims (exp, aud, iss).
– Reject unsigned tokens (alg: none).
– Monitor for abnormal token usage.

Bonus Commands:

  • Decode JWT:
    jq -R 'split(".") | .[bash] | @base64d | fromjson' <<< "$JWT" 
    
  • Test with jwt_tool:
    python3 jwt_tool.py <JWT> -T 
    

Expected Output:

A secure JWT implementation with proper algorithm enforcement, claim validation, and key management.

Reference:

References:

Reported By: Dharamveer Prasad – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 Telegram