JSON Web Token Hacking
JSON Web Token Hacking
Structure of a JWT
A JWT consists of three parts, separated by dots (.), and encoded in Base64:
1. Header
2. Payload
3. Signature
A JWT looks like this:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZ
SI6IkpvbiBEb2UiLCJpYXQiOjE1MTYyMzkwMjJ9.SflKxwRJSMeKKF2QT4fwpMeJf3
6POk6yJV_adQssw5c
1. Header
The header typically consists of two parts:
• Type of the token (always JWT).
• Signing algorithm used (e.g., HMAC, SHA256, RSA).
Example:
{
"alg": "HS256",
"typ": "JWT"
}
2. Payload
The payload contains the claims. Claims are statements about an entity
(typically, the user) and additional data. There are three types of claims:
• Registered claims: Predefined claims with common uses, such as iss
(issuer), exp (expiration time), sub (subject), and aud (audience).
• Public claims: Claims that can be defined by the user, like name, email, or
role.
• Private claims: Custom claims that are used to share information
between parties that agree on their usage.
Example:
{
"sub": "1234567890",
"name": "John Doe",
"admin": true,
"iat": 1516239022
}
3. Signature
The signature is used to verify that the token has not been altered and to
ensure the authenticity of the sender. To create the signature, the encoded
header, payload, and a secret key are combined and passed through a
cryptographic hashing algorithm.
Example (using HMAC SHA256):
HMACSHA256(
base64UrlEncode(header) + "." + base64UrlEncode(payload),
secret
)
If the token is signed with an asymmetric algorithm (e.g., RSA), a private key is
used to sign the token, and a public key is used to verify the signature.
Benefits of JWT
1. Stateless: JWTs are self-contained and do not require the server to store
session data, making them ideal for distributed applications (e.g.,
microservices).
2. Compact: JWTs are lightweight and can easily be transmitted via URL,
HTTP headers, or inside request bodies.
3. Secure: JWTs are signed, ensuring their integrity. They can also be
encrypted for additional confidentiality.
4. Easy to Use: JWTs are easy to generate and verify using common libraries
in most programming languages.
Use Cases
1. Authentication:
o JWT is often used to authenticate users by sending a token that
represents the user's identity. This is the basis of many single sign-
on (SSO) mechanisms.
2. Authorization:
o After a user is authenticated, each subsequent request can include
the JWT to verify their identity and access level without needing to
re-authenticate on each request.
3. Information Exchange:
o JWTs can be used to securely transmit information between
parties. Since they can be signed, the parties involved can verify
that the data has not been tampered with.
Security Considerations
1. Keep Secrets Safe: If using symmetric algorithms (e.g., HMAC), ensure
that the secret key is securely stored.
2. Use HTTPS: Always send JWTs over HTTPS to prevent man-in-the-middle
(MITM) attacks.
3. Short Expiration Times: Ensure tokens have a reasonable expiration time
to limit the impact of token theft.
4. Revoking Tokens: Since JWTs are stateless, they cannot be easily revoked.
Use mechanisms like blacklists or short-lived tokens to mitigate this risk.