How can you exploit a JWT token?

JWT or JSON Web Token shares information between two entities, usually between clients and servers. It primarily contains JSON objects which have data that needs to be shared. Every JWT is signed using a cryptographic hashing function to ensure that the contents within the token cannot be modified. JSON Web Token is compact and URL-safe as it maintains integrity and, therefore, can be used to transfer between two parties.  

A JWT is structured in three parts

  • The Header consists of two parts 

    • A signing algorithm

    • Type of Token

  • A payload that contains claims or the JSON object. 

  • A Signature generated using a cryptographic hashing algorithm that maintains the token's integrity. 

Header:
{
  "alg": "HS256",
  "typ": "JWT"
}
Payload:
{
  "sub": "1234567890",
  "name": "Alice Bob",
  "iat": 1516239022
}
Signature:
HMACSHA256(
  base64UrlEncode(header) + "." +
  base64UrlEncode(payload),your-256-bit-secret) secret base64 encoded

What is JWT used for? 

  • Authentication: When a user successfully logs in using their credentials, an ID token is always returned as a JWT. A Single-Sign-On solution widely uses JWT because of its packed format. A JSON web token is much more consolidated to pass between HTTP environments. 

  • Secure: The JSON web token can be signed using a hashing algorithm like HMAC, it can also use a public/private key pair in an X.509certificate. 

  • Acceptability: JSON programming language is more commonly used than XML as XML does not have a natural document-to-object mapping process; therefore, it is better accustomed to working with SSO & SAML  

How can an attacker exploit a JWT?

JWT, when implemented the right way, is secure. However, implementing the wrong way can lead to exposure to the JWT and, therefore, to an account or environment. 

  1. None Algorithm: The JWT supports the "none" algorithm where the "alg" field can be set to none. If no signature is attached to the token, any token can be considered valid.

  2. Bruteforcing: An attacker would be able to perform brute force on the key. If the key is not complex, an attacker might be able to break in.

  3. Pivoting to an existing Vulnerability: If the attacker cannot brute force the key, they might be able to take advantage of another current vulnerability and try leaking the secret key wherever it is stored.

  4. KID Manipulation: The "Key_ID" parameter can be manipulated by an attacker. If not sanitized, the "kid" can be used as an attack vector. Two primary vectors that can exploit an account or system are directory traversal and SQL injection attacks.

  5. JKU Spoofing: JKU stands for "JWT Set URL." A header field specifies a URL that points to a set of keys and verifies the token. An attacker can tamper with the token and perform MITM to monitor traffic.

Some security best practices for keeping the JSON web token safe. 

  • The signing key should be kept secret and safe like any other credential.

  • An attacker can easily decode Tokens; therefore, sensitive information should not be passed within the token.

  • Every token signed should be assigned a validity/expiration period so that the token is revoked.

  • Send tokens over secure HTTP connections as those requests can be intercepted and tokens compromised.

  • Store token in a secure location. Instead of creating multiple tokens, reuse the token until its validity expires therefore helping to optimize token limits.

Previous
Previous

How to secure your AWS CloudTrail?

Next
Next

Blue-Green vs Canary Deployments