JWT attack vulnerabilities in Web App Penetration Testing | 2023 (2024)

In this section, we’ll explain what an JWT Attack is, describe some types, explain how to find and exploit JWT, and summarize how to prevent from JWT | Karthikeyan Nagaraj

JWT attack vulnerabilities in Web App Penetration Testing | 2023 (2)

JSON web tokens (JWTs) are a standardized format for sending cryptographically signed JSON data between systems. They can theoretically contain any kind of data, but are most commonly used to send information (“claims”) about users as part of authentication, session handling, and access control mechanisms.

Unlike with classic session tokens, all of the data that a server needs is stored client-side within the JWT itself. This makes JWTs a popular choice for highly distributed websites where users need to interact seamlessly with multiple back-end servers.

A JWT consists of 3 parts: a header, a payload, and a signature. These are each separated by a dot, as shown in the following example:

eyJraWQiOiI5MTM2ZGRiMy1jYjBhLTRhMTktYTA3ZS1lYWRmNWE0NGM4YjUiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJwb3J0c3dpZ2dlciIsImV4cCI6MTY0ODAzNzE2NCwibmFtZSI6IkNhcmxvcyBNb250b3lhIiwic3ViIjoiY2FybG9zIiwicm9sZSI6ImJsb2dfYXV0aG9yIiwiZW1haWwiOiJjYXJsb3NAY2FybG9zLW1vbnRveWEubmV0IiwiaWF0IjoxNTE2MjM5MDIyfQ.SYZBPIBg2CRjXAJ8vCER0LA_ENjII1JakvNQoP-Hw6GG1zfl4JyngsZReIfqRvIAEi5L4HV0q7_9qGhQZvy9ZdxEJbwTxRs_6Lb-fZTDpW6lKYNdMyjw45_alSCZ1fypsMWz_2mTpQzil0lOtps5Ei_z7mM7M8gCwe_AGpI53JxduQOaB5HkT5gVrv9cKu9CsW5MS6ZbqYXpGyOG5ehoxqm8DL5tFYaW3lB50ELxi0KsuTKEbD0t5BCl0aCR2MBJWAbN-xeLwEenaqBiwPVvKixYleeDQiBEIylFdNNIMviKRgXiYuAvMziVPbwSgkZVHeEdF5MQP1Oe2Spac-6IfA

The header and payload parts of a JWT are just base64url-encoded JSON objects. The header contains metadata about the token itself, while the payload contains the actual “claims” about the user. For example, you can decode the payload from the token above to reveal the following claims:

{
"iss": "portswigger",
"exp": 1648037164,
"name": "Carlos Montoya",
"sub": "carlos",
"role": "blog_author",
"email": "carlos@carlos-montoya.net",
"iat": 1516239022
}

In most cases, this data can be easily read or modified by anyone with access to the token. Therefore, the security of any JWT-based mechanism is heavily reliant on the cryptographic signature.

The server that issues the token typically generates the signature by hashing the header and payload. In some cases, they also encrypt the resulting hash. Either way, this process involves a secret signing key. This mechanism provides a way for servers to verify that none of the data within the token has been tampered with since it was issued:

  • As the signature is directly derived from the rest of the token, changing a single byte of the header or payload results in a mismatched signature.
  • Without knowing the server’s secret signing key, it shouldn’t be possible to generate the correct signature for a given header or payload.

The JWT specification is actually very limited. It only defines a format for representing information (“claims”) as a JSON object that can be transferred between two parties. In practice, JWTs aren’t really used as a standalone entity. The JWT spec is extended by both the JSON Web Signature (JWS) and JSON Web Encryption (JWE) specifications, which define concrete ways of actually implementing JWTs.

In other words, a JWT is usually either a JWS or JWE token. When people use the term “JWT”, they almost always mean a JWS token. JWEs are very similar, except that the actual contents of the token are encrypted rather than just encoded.

JWT attacks involve a user sending modified JWTs to the server in order to achieve a malicious goal. Typically, this goal is to bypass authentication and access controls by impersonating another user who has already been authenticated.

The impact of JWT attacks is usually severe. If an attacker is able to create their own valid tokens with arbitrary values, they may be able to escalate their own privileges or impersonate other users, taking full control of their accounts.

  • JWT vulnerabilities typically arise due to flawed JWT handling within the application itself.
  • The various specifications related to JWTs are relatively flexible by design, allowing website developers to decide many implementation details for themselves. This can result in them accidentally introducing vulnerabilities even when using battle-hardened libraries.
  • These implementation flaws usually mean that the signature of the JWT is not verified properly.
  • This enables an attacker to tamper with the values passed to the application via the token’s payload.
  • Even if the signature is robustly verified, whether it can truly be trusted relies heavily on the server’s secret key remaining a secret. If this key is leaked in some way, or can be guessed or brute-forced, an attacker can generate a valid signature for any arbitrary token, compromising the entire mechanism.
  • By design, servers don’t usually store any information about the JWTs that they issue.
  • Instead, each token is an entirely self-contained entity. This has several advantages, but also introduces a fundamental problem — the server doesn’t actually know anything about the original contents of the token, or even what the original signature was.
  • Therefore, if the server doesn’t verify the signature properly, there’s nothing to stop an attacker from making arbitrary changes to the rest of the token.

For example, consider a JWT containing the following claims:

{
"username": "carlos",
"isAdmin": false
}

If the server identifies the session based on this username, modifying its value might enable an attacker to impersonate other logged-in users. Similarly, if the isAdmin value is used for access control, this could provide a simple vector for privilege escalation.

In the first couple of labs, you’ll see some examples of how these vulnerabilities might look in real-world applications.

JWT libraries typically provide one method for verifying tokens and another that just decodes them. For example, the Node.js library jsonwebtoken has verify() and decode().

Occasionally, developers confuse these two methods and only pass incoming tokens to the decode() method. This effectively means that the application doesn't verify the signature at all.

JWT attack vulnerabilities in Web App Penetration Testing | 2023 (2024)

FAQs

What are the vulnerabilities of JWT authentication? ›

JWT attacks involve a user sending modified JWTs to the server in order to achieve a malicious goal. Typically, this goal is to bypass authentication and access controls by impersonating another user who has already been authenticated.

Are JWT tokens prone to XSS attacks? ›

Implementing token expiration policies and providing mechanisms for token refresh can help balance security and usability. Cross-Site Scripting (XSS):XSS attacks can be used to steal JWTs stored in client-side storage (e.g., local storage or cookies) by injecting malicious scripts into vulnerable web pages.

Which of the following is a potential security vulnerability when using JWT? ›

JWTs rely on digital signatures to verify their authenticity and integrity. While this is a robust mechanism, it can also introduce potential weaknesses. Signature-based attacks, such as algorithm substitution attacks, can exploit vulnerabilities in the JWT verification process.

Is it possible to hack a JWT token? ›

By targeting files with predictable content, it's possible to forge a valid JWT. For instance, the /proc/sys/kernel/randomize_va_space file in Linux systems, known to contain the value 2, can be used in the kid parameter with 2 as the symmetric password for JWT generation.

Is JWT more secure than API key? ›

A private key JWT is more secure, as you won't risk exposing the secret value that accidentally creates similar access concerns as an API key.

What are the risks of JWT authentication? ›

This includes personally identifiable information (PII), authentication credentials, and session tokens. Without proper encryption measures, such as JSON Web Encryption (JWE), organizations risk exposing critical data to unauthorized parties, leading to privacy violations, identity theft, and fraud.

Is JWT enough for security? ›

It's important to note that a JWT guarantees data ownership but not encryption. The reason is that the JWT can be seen by anyone who intercepts the token because it's serialized, not encrypted. It is strongly advised to use JWTs with HTTPS, a practice that extends to general web security.

Is it okay to show JWT in localStorage? ›

If you store it inside localStorage, it's accessible by any script inside your page. This is as bad as it sounds; an XSS attack could give an external attacker access to the token. To reiterate, whatever you do, don't store a JWT in local storage (or session storage).

Is JWT vulnerable to CSRF? ›

JWTs by themselves do not prevent CSRF attacks. Here's why: - JWTs may be sent automatically by the browser if authentication cookies or local storage tokens are set. An attacker can leverage this to send the JWT without the user knowing.

What are common JWT mistakes? ›

Six threats to JWTs
  • Allowing the server to use a token without validation. ...
  • Using the same private key for different applications. ...
  • Using a weak signing algorithm. ...
  • Choosing a short and/or low-entropy private key. ...
  • Keeping sensitive data in a JWT's payload. ...
  • Confusing the keys.
Jun 7, 2023

Which security algorithm is best for JWT? ›

The option with the best security and performance is EdDSA, though ES256 (The Elliptic Curve Digital Signature Algorithm (ECDSA) using P-256 and SHA-256) is also a good choice.

What is the impact of JWT attacks? ›

The impact of JWT attacks is usually severe. If an attacker is able to create their own valid tokens with arbitrary values, they may be able to escalate their own privileges or impersonate other users, taking full control of their accounts.

What is the JWT secret key vulnerability? ›

The vulnerability is identified as CVE-2022-23529, rated high severity (CVSS 7.6). By exploiting this vulnerability, attackers could achieve remote code execution (RCE) on a server verifying a maliciously crafted JSON web token (JWT) request.

What is safer than JWT? ›

Security: OAuth is a secure way to manage authorization flows, while JWT is a lightweight and self-contained token. It does not provide security on its own, but can be secure as part of a well designed authentication system.

What happens if someone steals JWT? ›

Because JWTs are used to identify the client, if one is stolen or compromised, the attacker has full access to the user's account in the same way they would if the attacker had compromised the user's username and password instead.

How secure is JWT authentication? ›

It's important to note that a JWT guarantees data ownership but not encryption. The reason is that the JWT can be seen by anyone who intercepts the token because it's serialized, not encrypted. It is strongly advised to use JWTs with HTTPS, a practice that extends to general web security.

What is the vulnerability of bearer authentication? ›

This is one of the main vulnerabilities of a bearer token. If the tokens fall into the hands of bad actors, they can be misused. With the stolen or leaked tokens, bad actors can easily impersonate the user and obtain unauthorized access to protected resources.

Top Articles
Latest Posts
Article information

Author: Neely Ledner

Last Updated:

Views: 6313

Rating: 4.1 / 5 (42 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Neely Ledner

Birthday: 1998-06-09

Address: 443 Barrows Terrace, New Jodyberg, CO 57462-5329

Phone: +2433516856029

Job: Central Legal Facilitator

Hobby: Backpacking, Jogging, Magic, Driving, Macrame, Embroidery, Foraging

Introduction: My name is Neely Ledner, I am a bright, determined, beautiful, adventurous, adventurous, spotless, calm person who loves writing and wants to share my knowledge and understanding with you.