Cybersecurity-Mastery

Home › Web Security › JSON Web Tokens

JSON Web Tokens

JWS/JWE structure, alg confusion, kid injection, claims validation and revocation strategy.

Intermediate⌨ ~48 min hands-on📖 ~16 min read

▶ Here to build, not read? Jump to the Hands-On Practice.

Prerequisites: Web Authentication · HMAC · Asymmetric Cryptography

Overview

JWS/JWE structure, alg confusion, kid injection, claims validation and revocation strategy. In one line: JSON web tokens decides where trust is placed, and misplaced trust is where the security problem lives. That's the theory you need — the understanding comes from doing it, so the walkthrough below is the heart of this chapter.

How it works

Mechanically, JSON web tokens comes down to three things: what data is trusted, where it crosses a boundary, and what happens on the other side. The diagram traces that path; you'll walk it for real, step by step, in the practice below.

flowchart TD
    A["JSON Web Tokens"] --> B["Concepts & terminology"]
    A --> C["Internal mechanics"]
    C --> D["Trust boundaries"]
    D --> E["Attack surface"]
    D --> F["Defensive controls"]
    E --> G["Detection & telemetry"]
    F --> G
    G --> H["Verification in the lab"]
🔑 Key terms
  • Trust boundary — where data or control passes between components of different privilege or trust.
  • Attack surface — the set of points an attacker can interact with.
  • Primary control — the single measure that most reduces this technique's impact.
  • Telemetry — the log or signal a defender uses to detect the technique.

Hands-On Practice

Tip
This is the main event. Bring up the lab, then work the walkthrough — every command has a ▸ Run button (needs the lab runner) and a 📋 Copy button. ~48 min, Kali attacker, fully local.

Mission. Break weak JWT verification (alg confusion, weak secret) then harden it.

🧪 Lab setup — Web Security

  • bWAPP — http://localhost:8081 — 100+ web bugs, best for targeted practice
  • DVWA — http://localhost:8082 — adjustable security levels
  • Juice Shop — http://localhost:8083 — modern SPA, full OWASP Top 10
  • WebGoat — http://localhost:8084 — guided lessons
docker-compose.yml
# web-lab/docker-compose.yml  —  Web Security part environment
services:
  bwapp:
    image: raesene/bwapp
    ports: ["8081:80"]
    networks: [labnet]
  dvwa:
    image: vulnerables/web-dvwa
    ports: ["8082:80"]
    networks: [labnet]
  juice-shop:
    image: bkimminich/juice-shop
    ports: ["8083:3000"]
    networks: [labnet]
  webgoat:
    image: webgoat/webgoat
    ports: ["8084:8080"]
    networks: [labnet]

networks:
  labnet:
    driver: bridge
    # routed here so your browser/Burp on the host can reach the apps;
    # for a jeopardy-style setup put the attacker box on labnet and set internal: true

Target for this lesson: A lab JWT app (or Juice Shop token). Tooling: jwt_tool, hashcat.. Full setup: Web Security lab environment.

Guided walkthrough

1Decode the token
command
jwt_tool eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Expected output
Header {alg: HS256}, claims {user: bob, role: user}
JWTs are signed, not encrypted — you can read (not yet forge) the claims.
2Test alg:none
command
jwt_tool <token> -X a
Expected output
If accepted, the server trusts an unsigned token.
Broken libraries treat alg:none as valid — instant forgery.
3Crack a weak HMAC secret
command
hashcat -m 16500 token.txt rockyou.txt
Expected output
Recovered secret: 'secret'
A guessable HS256 key lets you sign arbitrary tokens.
4Forge an admin token
command
jwt_tool <token> -S hs256 -p 'secret' -pc role -pv admin
Expected output
A valid token with role=admin.
Present it and the app grants admin — authentication bypass.
5Fix
command
# pin alg server-side; reject none; use a 256-bit random key; validate exp/aud/iss
Expected output
Forged/none/weak-key tokens are all rejected.
Verification must be strict about algorithm and key, not just 'signature present'.

Live terminal

kali@jwt — start the lab runner for a live shelloffline
Start the Lab Runner (see labserver/README.md), then click Connect lab runner and paste the token. Until then, copy commands from the walkthrough and run them in your own Kali terminal.
$

Try it yourself

Note
Try each for real. Open the hint only when stuck, the solution only to check.

Challenge 1. Perform an RS256→HS256 algorithm-confusion forgery.

Hint

Sign with HS256 using the server's public key as the HMAC secret.

Solution

jwt_tool <token> -X k -pk public.pem — if the server uses one 'verify' call, it treats the public key as the HMAC secret.

Challenge 2. Forge a token via kid header injection.

Hint

kid may be used to select a key from a path or DB.

Solution

Point kid at a file with known contents (/dev/null → empty key) or inject SQL into a kid lookup.

Challenge 3. Explain why short expiries don't fix a weak signing key.

Hint

Forgery mints fresh tokens at will.

Solution

If you hold the key you set exp yourself; key strength, not lifetime, is the control.

Quick quiz

A JWT verifier accepts `alg: none`. What does that allow?
alg:none means the token is unsigned; if accepted, an attacker forges any claims (e.g. role=admin) with no key.

Detect & defend (blue-team view)

  • Log and alert on tokens with alg=none or unexpected alg values reaching verification.
  • Sudden role escalations without a corresponding privileged action are a forged-token signal.

Skills check

You can move on when you can, without notes:

Check Yourself

Quick self-test — answers hidden. If you can't answer without notes, redo the relevant walkthrough step.

Q1. In one sentence, what security problem does JSON Web Tokens address or create?

Show answer

JSON Web Tokens matters because its behaviour determines where trust is placed; misplacing that trust is the root of the associated bug class.

Q2. Name one trust boundary involved in JSON Web Tokens and what crosses it.

Show answer

Any point where attacker-influenced data meets privileged processing — e.g. user input reaching an interpreter, or a token reaching a verifier.

Q3. Give one attack against JSON Web Tokens and the single control that most reduces its impact.

Show answer

State the attack precisely, then the primary control (validation, encoding, isolation, authentication, or least privilege) — not a laundry list.

Interview angles

  • Walk me through how JSON Web Tokens works, then tell me where you would attack it.
  • How would you detect JSON Web Tokens being abused in an environment you defend?
  • What is the difference between mitigating and eliminating the risk associated with JSON Web Tokens?

Pitfalls & Best Practice

Common mistakes

  • Treating JSON web tokens as a checkbox instead of understanding the mechanism — defences copied without comprehension fail silently.
  • Trusting client-side or default controls to enforce a server-side security property.
  • Testing only the happy path and never the abuse cases or malformed input.
  • Fixing the symptom (one payload) instead of the class (the missing control).

Do this instead

  • Push security decisions to a single, well-tested enforcement point rather than scattering checks.
  • Fail closed: when in doubt, deny and log, so misconfiguration reduces access rather than expanding it.
  • Instrument the trust boundaries so the same event is visible to offence (as a target) and defence (as a signal).
  • Validate your understanding of JSON web tokens empirically in the lab before trusting it in production.
  • Prefer safe, high-level APIs and secure defaults over hand-rolled primitives.

Reference

MITRE ATT&CK Mapping

No single ATT&CK technique maps cleanly to this concept. Once you apply it offensively or defensively, map the specific behaviour using the ATT&CK chapter.

OWASP Mapping

CWE Mapping

CWEWeakness
CWE-347Improper Verification of Cryptographic Signature
CWE-321Use of Hard-coded Cryptographic Key

CAPEC Mapping

  • No single attack pattern; see the CAPEC chapter to map behaviour to patterns.

CVE References

  • No canonical CVE for a concept page. Search the NVD for concrete instances when studying real cases.

NIST References

RFC References

Research Papers

  • Reflections on Trusting Trust — Ken Thompson, 1984
  • Smashing the Stack for Fun and Profit — Aleph One, Phrack 49, 1996
  • The Protection of Information in Computer Systems — Saltzer & Schroeder, 1975

Books

  • The Web Application Hacker's Handbook, 2nd ed. — Stuttard & Pinto
  • Real-World Bug Hunting — Peter Yaworski
  • The Tangled Web — Michal Zalewski
  • Web Security for Developers — Malcolm McDonald

Official Documentation


Part of the Web Security section of Cybersecurity-Mastery. 🟡 Intermediate · ~48 min hands-on · Last updated 2026-08-12.

Cybersecurity-Mastery — interactive security learning · runs locally