RahulMohapatra1/jwt-security-tester
GitHub: RahulMohapatra1/jwt-security-tester
一款 JWT 安全测试 CLI 工具,针对 JSON Web Token 的 12 类常见漏洞(弱密钥、算法攻击、Claim 篡改、JWK 注入等)进行自动化扫描与验证。
Stars: 1 | Forks: 0
# JWT 安全测试工具
一款命令行安全工具,用于扫描 JSON Web Token (JWT) 的 12 个常见漏洞。由一名应用安全工程师基于 Web 和 API 渗透测试项目中发现的实际 JWT 弱点构建。
## 兼容性
| 平台 | 支持 | 已测试 |
|---|---|---|
| Windows 10 / 11 | ✅ | Python 3.8+ |
| macOS 12+ (Intel & Apple Silicon) | ✅ | Python 3.8+ |
| Linux (Ubuntu, Debian, Kali, Arch) | ✅ | Python 3.8+ |
**Python 版本要求:** 3.8 或更高版本
**无特定于操作系统的依赖。** 所使用的库(`cryptography`, `pyfiglet`)均为纯 Python 库,并为上述三种平台提供了预构建的 wheel 包。
## 安装说明
在 Windows、macOS 和 Linux 上的操作完全相同。
**第 1 步 — 克隆仓库:**
```
git clone https://github.com/RahulMohapatra1/jwt-security-tester
cd jwt-security-tester
```
**第 2 步 — 安装依赖:**
```
pip install -r requirements.txt
```
这就是全部步骤。不需要环境配置,不需要 Docker,也不需要额外设置。
## 用法
### 交互模式 — 在提示时粘贴 token(推荐)
```
# Windows
python jwt_tester.py
# macOS / Linux
python3 jwt_tester.py
```
### 通过 flag 直接传递 token
```
python3 jwt_tester.py -t
```
### 使用包含 10.3 万词的完整词表进行彻底的暴力破解
```
python3 jwt_tester.py -t -w wordlists/jwt-secrets-full.txt
```
### 跳过暴力破解以加快运行速度(其余 11 项检查仍会执行)
```
python3 jwt_tester.py -t --skip-bruteforce
```
### 仅运行指定的检查
```
python3 jwt_tester.py -t --checks none,expiry,claims,jti
```
可用的检查名称:`none`, `brute`, `confusion`, `sensitive`, `expiry`, `kid`, `jwk`, `jku`, `x5c`, `claims`, `rsa`, `jti`
## 漏洞检查
| # | 检查项 | 发现时的严重程度 | 描述 |
|---|---|---|---|
| 1 | None 算法攻击 | 严重 | 使用 `alg:none` 构造的 token — 无需签名验证 |
| 2 | 弱密钥暴力破解 | 严重 | 使用 5000 个高概率密钥测试 HMAC 密钥 |
| 3 | 算法混淆 (RS256→HS256) | 高危 | 检测到非对称加密算法的混淆攻击机会 |
| 4 | payload 中包含敏感数据 | 高危 | 扫描解码后的 payload 中的密码、API 密钥、PII |
| 5 | 缺失 / 过期的 `exp` Claim | 严重 / 中危 | token 永不过期或未验证过期时间 |
| 6 | `kid` Header 注入 | 严重 | key ID header 中存在路径遍历和 SQLi 模式 |
| 7 | 内嵌 JWK 注入 | 严重 | token header 中嵌入了攻击者控制的公钥 |
| 8 | JKU / x5u URL 注入 | 高危 | 密钥 URL 被重定向到攻击者控制的服务器 |
| 9 | x5c 证书链注入 | 严重 | header 中嵌入了自签名或恶意证书 |
| 10 | Claim 篡改与提权 | 严重 | 检测到管理员/超级用户角色 + 篡改 token 演示 |
| 11 | 弱 RSA 密钥长度 | 严重 | 从嵌入的 JWK/x5c 中检测到长度小于 2048 位的 RSA 密钥 |
| 12 | 缺失 `jti` Claim | 中危 | 无 JWT ID — token 无法被撤销或进行重放保护 |
## 词表
| 文件 | 密钥数量 | 用例 |
|---|---|---|
| `wordlists/jwt-secrets-top5000.txt` | 5,000 | **默认。** 根据概率智能排序 — 优先测试 JWT 特定模式 |
| `wordlists/jwt-secrets-full.txt` | 103,838 | 完整扫描。当 top5000 未找到密钥时,使用 `-w` flag |
| `wordlists/common_secrets.txt` | 109 | 精心挑选的列表。top5000 的子集 |
**为什么默认使用 top5000?**
这 5000 个密钥根据概率进行了排序 — 包含 `jwt`、`secret`、`token`、`key` 和框架名称(`flask`、`django`、`laravel`)的密钥会被优先测试。这能在 0.02 秒内命中现实世界中的弱密钥,同时保证工具每次运行都保持快速。
**将词表更新至最新版本:**
```
python3 wordlists/update_wordlists.py
```
这会从 `danielmiessler/SecLists` 和 `wallarm/jwt-secrets` 拉取最新内容,并将它们合并到 `jwt-secrets-full.txt` 中。
## 示例输出
```
___ _ _ _____ _____ _
|_ || | | |_ _| |_ _| | |
| || | | | | | | | ___ ___| |_ ___ _ __
| || |/\| | | | | |/ _ \/ __| __/ _ \ '__|
/\__/ /\ /\ / | | | | __/\__ \ || __/ |
\____/ \/ \/ \_/ \_/\___||___/\__\___|_|
By Rahul Mohapatra
github.com/RahulMohapatra1
········~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Scans a JWT for 12 security vulnerabilities including
weak secrets, algorithm attacks, claim tampering,
replay risks, and certificate chain injection.
········~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
v2.0 · 12 checks · AppSec Research Tool
JWT >
── CHECK 10: Claim Tampering & Privilege Escalation ──
[CRITICAL] HIGH-PRIVILEGE value detected: 'role' = 'admin'
[CRITICAL] High-privilege role found in token.
Even without cracking the secret, this token carries admin-level claims.
Any successful algorithm attack would allow forging an admin token.
SUMMARY: Critical:1 Warnings:2 Passed:8 Other:1
```
## 项目结构
```
jwt-security-tester/
├── jwt_tester.py # Main CLI tool — all 12 checks
├── wordlists/
│ ├── common_secrets.txt # 109 curated high-probability secrets
│ ├── jwt-secrets-top5000.txt # 5,000 smart-ranked secrets (default)
│ ├── jwt-secrets-full.txt # 103,838 secrets (full scan)
│ └── update_wordlists.py # Pulls latest from SecLists + wallarm
├── tests/
│ └── test_tokens.py # Automated tests with vulnerable tokens
├── requirements.txt # cryptography, pyfiglet
└── README.md
```
## 运行测试
```
python3 tests/test_tokens.py
```
生成已知存在漏洞的 token,并针对它们测试所有检查项。涵盖所有 12 个漏洞类别的正向(存在漏洞)和负向(安全)测试用例。
## 词表来源
所有来源均为 MIT 许可的公共安全研究仓库:
- **SecLists** — `danielmiessler/SecLists` — `Passwords/scraped-JWT-secrets.txt`
- **Wallarm JWT Secrets** — `wallarm/jwt-secrets` — `jwt.secrets.list`
这些列表与 `hashcat`、`john` 和 `jwt_tool` 等行业标准工具使用的列表相同。
## 背景
该工具的构建旨在自动化在 API 渗透测试期间手动执行的 JWT 安全检查。在对 REST API 和微服务进行评估时,JWT 配置错误一直位列三大最常见发现之中 — 尤其是弱密钥、缺失过期时间 claim 以及没有 audience 验证的管理员角色 token。
这些检查基于:
- [OWASP 测试指南 — 测试 JSON Web Token](https://owasp.org/www-project-web-security-testing-guide/)
- [PortSwigger JWT 攻击](https://portswigger.net/web-security/jwt)
- [RFC 7519 — JSON Web Token](https://datatracker.ietf.org/doc/html/rfc7519)
- CVE-2018-0114 (Embedded JWK injection)
- CVE-2022-21449 (ECDSA psychic signatures)
## 路线图
- [ ] `--url` flag — 将构造的攻击 token 发送到实时的 API endpoint 并显示响应代码
- [ ] `--output report.json` — 将所有发现导出为结构化的 JSON,用于开发者报告
- [ ] ES256 / ECDSA psychic signatures 检查 (CVE-2022-21449)
- [ ] 数字角色提权测试(`role: 5` → `role: 0`, `role: 1`)
- [ ] `nbf` 绕过构造 token 生成
- [ ] 针对 Go 微服务的 Semgrep 自定义规则(配套项目)
## 作者
**Rahul Mohapatra** — 应用安全工程师
[github.com/RahulMohapatra1](https://github.com/RahulMohapatra1) | [linkedin.com/in/rahul-mohapatra-25428718a](https://linkedin.com/in/rahul-mohapatra-25428718a)
*仅用于教育和授权的安全测试目的。*
标签:JWT, Python, 数据处理, 无后门, 逆向工具