JWT

Introduction:

  • JWT is most commonly used in authorization.

  • A JSON Web Token (JWT) is a standardised format for securely exchanging data between two parties.

  • It is compact, readable and digitally signed using a private key/ or a public key pair by the Identity Provider(IdP). So the integrity and authenticity of the token can be verified by other parties involved.

  • The goal of using JWT is not to hide data, but to ensure the data's authenticity. JWT is signed and encoded rather than encrypted.

  • JWT is a stateless authentication mechanism based on tokens. Because it is a client-side stateless session, the server does not have to rely solely on a datastore (database) to save session information.

  • It consists of three elements:

    1. Header - JWT header consists of token type and algorithm used for signing and encoding. Algorithms can be HMAC, SHA256, RSA, HS256 or RS256.

    2. Payload - This is also a JSON object and is used to store the user’s information like id, username, role, token generation time and other custom claims.

    3. Signature - The most crucial aspect of a JSON Web Token is its signature (JWT). The signature is generated by encoding the header and payload with Base64url Encoding and concatenating them with a period separator(.). This information is subsequently passed to the cryptography algorithm. As a result, if the header or payload changes, the signature must be computed again. Only the Identity Provider (IdP) has access to the private key used to generate the signature, which prohibits token manipulation.

    header.payload.signature
  • JWT can be generated with two encryption mechanisms called Symmetric and Asymmetric encryption.

    • Symmetric: This mechanism requires a single key to create and verify the JWT. The most common algorithm for this type is HS256.

    • Asymmetric: This mechanism requires a Public key for verification and a Private key for signing the Signature. The most common algorithm for this type is RS256.

  • Key ID (kid) is an optional header with a string type that is used to identify a specific key in the filesystem or database and then use its content to validate the Signature. This argument is useful if the Application has several keys for signing tokens, but it can be problematic if it is injectable since an attacker can refer to a specific file with predictable content.

  • In addition to a key ID, JSON web token standards also provide developers with the ability to specify keys via a URL.

    • The token header contains a version (“ver”) claim. It contains the version of the JWT Token library used.

    • jku header parameter - JKU is an abbreviation for "JWK Set URL." It is an optional header field that specifies a URL that refers to a collection of keys needed to validate the token. If this field is not properly controlled and is permitted, an attacker might host their own key file and declare that the application uses it to validate tokens.

    • jwk header parameter - The optional JWK (JSON Web Key) header parameter allows attackers to embed the key used to verify the token directly in the token.

    • x5u and x5c header parameter - The x5u and x5c header arguments, like the jku and jwk headers, allow attackers to define the public key certificate or certificate chain used to verify the token. x5u defines information in URI form, whereas x5c permits certificate data to be incorporated in the token.

    • x5t parameter - The "x5t" (x.509 certificate thumbprint) header argument returns a base64url encoded SHA-256 thumbprint (i.e., digest) of an X.509 certificate's DER encoding, which may be used to match a certificate. As a result, it is equivalent to the key identifier or the kid claim!!

  • Inside Payload section you may also find: ****

    • jti param which is used to prevent replay attack on JWT

    • iss param — The name of the entity that issued the token.

    • iat param — Identifies the time at which the JWT token was issued.

    • nbf param — Identifies the time before which the JWT token MUST NOT be accepted for processing.

    • exp param — Identifies the expiration time on or after which the JWT MUST NOT be accepted for processing.

    • aud (audience) claim — identifies the recipients that the JWT is intended for.

Workflow:

  • Basically the identity provider(IdP) generates a JWT certifying user identity and Resource server decodes and verifies the authenticity of the token using secret salt / public key.

https://cdn.hashnode.com/res/hashnode/image/upload/v1616225393075/v2TJSq1Hb.png?auto=compress,format&format=webp

Tools:

GitHub - ticarpi/jwt_tool: A toolkit for testing, tweaking and cracking JSON Web Tokensarrow-up-right

GitHub - hahwul/jwt-hack: 🔩 jwt-hack is tool for hacking / security testing to JWT. Supported for En/decoding JWT, Generate payload for JWT attack and very fast cracking(dict/brutefoce)arrow-up-right

GitHub - mazen160/jwt-pwn: Security Testing Scripts for JWTarrow-up-right

GitHub - brendan-rius/c-jwt-cracker: JWT brute force cracker written in Carrow-up-right

GitHub - jmaxxz/jwtbrute: Brute forcing jwt tokens signed with HS256 since 2014arrow-up-right

GitHub - Sjord/jwtcrack: Crack the shared secret of a HS256-signed JWTarrow-up-right

JSON Web Tokensarrow-up-right

JSON Web Token Attackerarrow-up-right

GitHub - wallarm/jwt-heartbreaker: The Burp extension to check JWT (JSON Web Tokens) for using keys from known from public sourcesarrow-up-right

JWTweakarrow-up-right

Attacking JWT:

  1. Check for sensitive data in the JWT

  1. None algorithm

  1. Change algorithm from RS256 to HS256

  1. Signature not being checked

  1. Crack the secret key

  1. Attacks using kid in JWT token.

  1. Forged Header Parameter

Other Attacks:

Key Database Mismanagement

Hacking JWT Tokens: Key Database Mismanagementarrow-up-right

Verification Key Mismanagement

Hacking JWT Tokens: Verification Key Mismanagementarrow-up-right

Hacking JWT Tokens: Verification Key Mismanagement IIarrow-up-right

Hacking JWT Tokens: Verification Key Mismanagement IIIarrow-up-right

Hacking JWT Tokens: Verification Key Mismanagement IVarrow-up-right

Vulnerable Key Generator

Hacking JWT Tokens: Vulnerable Key Generatorarrow-up-right

Transaction Replay

Hacking JWT Tokens: Transaction Replayarrow-up-right

Hacking JWT Tokens: Transaction Replay IIarrow-up-right

JWS Standard for JWT

Hacking JWT Tokens: JWS Standard for JWTarrow-up-right

Hacking JWT Tokens: JWS Standard for JWT IIarrow-up-right

Bypassing NBF Claim

Hacking JWT Tokens: Bypassing NBF Claimarrow-up-right

Special Version Claim

Hacking JWT Tokens: Special Version Claimarrow-up-right

Cross Service Relay Attack — Missing audience claim

Hacking JWT Tokens: Cross Service Relay Attack -  Missing audience claimarrow-up-right

Cross Service Relay Attack — Misconfigured audience claim

Hacking JWT Tokens: Cross Service Relay Attack - Misconfigured audience claimarrow-up-right

Client Side Token Decode

Hacking JWT Tokens: Client Side Token Decodearrow-up-right

Quick Methodology:

MindMap:

https://pbs.twimg.com/media/EhjmMwGWkAApmeq?format=jpg&name=large
https://twitter.com/busk3r/status/1408420714852487172/photo/1

Labs:

TokenLab : JWTLabsarrow-up-right

https://github.com/h-a-c/jwt-labarrow-up-right

GitHub - Sjord/jwtdemo: Practice hacking JWT tokensarrow-up-right

Reference:

Write-ups & Reports:

CTFtime.org / Union CTF 2021 / Cr0wnAir / Writeuparrow-up-right

Web - JWT - Cr0wnAir - Union CTF [Walkthrough]arrow-up-right

Exploiting JWT to Account Takeoverarrow-up-right

The Bad Twin: a peculiar case of JWT exploitation scenarioarrow-up-right

Hijacking accounts by retrieving JWT tokens via unvalidated redirectsarrow-up-right

Mail.ru disclosed on HackerOne: [smena.samokat.ru] Predictable JWT...arrow-up-right

Trint Ltd disclosed on HackerOne: Insecure Zendesk SSO...arrow-up-right

HackerOne disclosed on HackerOne: HackerOne Jira integration plugin...arrow-up-right

Hacking JWT Tokens: "kid" Claim Misuse - Command Injectionarrow-up-right

Hacking JWT Tokens: Blind SQLiarrow-up-right

Hacking JWT Tokens: jku Claim Misusearrow-up-right

Blogs:

Attack Methodology · ticarpi/jwt_tool Wikiarrow-up-right

Attacking JSON Web Tokens (JWTs)arrow-up-right

JSON Web Token Exploitation for Red Teamarrow-up-right

https://medium.com/swlh/hacking-json-web-tokens-jwts-9122efe91e4aarrow-up-right

JWT Hacking 101arrow-up-right

Attacking JWT authenticationarrow-up-right

How JSON Web Token(JWT) authentication works?arrow-up-right

Attacks on JSON Web Token (JWT)arrow-up-right

Get a Feel of JWT ( JSON Web Token )arrow-up-right

Hacker Tools: JWT_Tool - The JSON Web Token Toolkit - Intigritiarrow-up-right

Video:

How to Exploit "Json Web Token"(JWT) vulnerabilities | Full Practicalarrow-up-right

ATTACKING JWT FOR BEGINNERS!arrow-up-right

Tips:

https://pbs.twimg.com/media/EZkyGeGUYAEsz4e?format=jpg&name=small

Author:

KathanP19arrow-up-right

Last updated

Was this helpful?