# JWT Attacks

JSON Web Tokens (JWTs) are a popular mechanism for securely transmitting information between parties. However, like any security measure, JWTs are not without vulnerabilities. This page explores various attacks that malicious actors can exploit to compromise JWT-based authentication and authorization systems.

#### Understanding the Threat Landscape 🚨

Here are some of the common JWT attack techniques:

* **Token Hijacking:** Stealing a user's JWT token from client-side storage (e.g., cookies, local storage) or intercepting it during transmission over an insecure channel (e.g., unencrypted HTTP). 🚫
* **Token Replay:** Reusing an expired or invalid JWT to gain unauthorized access. Hackers can capture valid tokens during legitimate sessions and replay them later to bypass authentication. 🔄
* **Token Forgery:** Creating a valid JWT without authorization by modifying the payload or signature. This can be achieved through vulnerabilities in the signing algorithm or key management practices. 📝
* **Algorithm Weakness:** Exploiting weaknesses in the cryptographic algorithms used to sign or encrypt JWTs. Weak algorithms offer less protection against malicious manipulation. 🔍
* **Key Management Issues:** Mismanaging or leaking secret keys used to sign JWTs. If an attacker obtains the secret key, they can forge tokens at will. 🔑

#### Technical Examples and Detection Methods 💻

**Token Hijacking:**

* **XSS Attack:** Injecting malicious JavaScript code into a web page to steal JWTs stored in local storage or cookies. 💉
* **Man-in-the-Middle Attack:** Intercepting network traffic to capture JWTs transmitted over insecure channels. 🕵️‍♀️

**Detection:**

* Monitor for suspicious activity in user sessions, such as sudden location changes or unexpected actions. ⚠️
* Implement strict Content Security Policy (CSP) to prevent XSS attacks. 🛡️
* Always use HTTPS to encrypt data transmission between client and server. 🔒

**Token Replay:**

* **Lack of Proper Token Invalidation:** Reusing a JWT after a user logs out or changes their password. The server should invalidate tokens when these events occur. ❌
* **Replay Attack:** Sending a previously recorded JWT to a server to bypass authentication. 🔄

**Detection:**

* Implement server-side mechanisms to track and invalidate used JWTs. 🔄
* Include the "jti" (JWT ID) claim in tokens and ensure it's unique for each request. 🔑

**Token Forgery:**

* **Signature Modification:** Modifying the payload of a JWT and recalculating the signature using the same secret key. This requires knowledge of the secret key, which should be kept confidential. 🤫
* **Algorithm Weakness:** Exploiting weaknesses in the signing algorithm to create a valid JWT with a modified payload. 🔍

**Detection:**

* Use strong and well-tested cryptographic algorithms for signing JWTs (e.g., HMAC-SHA256, RS256). 🔐
* Regularly update cryptographic libraries and frameworks to address known vulnerabilities. 🔄

#### Prevention Measures 🛡️

There are several steps you can take to mitigate JWT attacks:

* **HTTPS:** Always use HTTPS to encrypt data transmitted between the client and server, protecting JWTs from interception. 🔒
* **Secure Storage:** Store JWTs securely on the client side using mechanisms like `HttpOnly` cookies or `sessionStorage`. 🔐
* **Token Rotation:** Regularly renew JWTs to mitigate the risk of compromised tokens. This reduces the window of opportunity for attackers to exploit stolen tokens. 🔄
* **Token Revocation:** Invalidate JWTs when a user logs out or changes their password. This prevents attackers from reusing old tokens. ❌
* **Algorithm Selection:** Choose strong and secure cryptographic algorithms for signing and encrypting JWTs. Don't rely on deprecated or weak algorithms. 🔑
* **Key Management:** Protect secret keys used to sign JWTs and ensure they are not leaked. Implement secure key storage and access control mechanisms. 🔒
* **Input Validation:** Validate user input to prevent injection attacks that could lead to JWT forgery. ✅
* **Tools:** Utilize tools like `jwt_tool` to test JWT security and identify potential vulnerabilities in your implementation. 🛠️

#### References 📚

* **OWASP JWT Cheat Sheet:** <https://cheatsheetseries.owasp.org/cheatsheets/JSON_Web_Token_for_Java_Cheat_Sheet.html> &#x20;
* **HackTricks JWT Vulnerabilities:**

  <https://book.hacktricks.xyz/pentesting-web/hacking-jwt-json-web-tokens>
