bogdanticu88/RatRace
GitHub: bogdanticu88/RatRace
这是一个轻量级的命令行工具,用于系统化地检测和利用 Web 应用程序、API 及现代服务中的竞态条件漏洞。
Stars: 4 | Forks: 0
# RatRace
A lightweight CLI tool for systematically detecting and exploiting race conditions in web applications, APIs, and modern services.
     
## 解决的问题
竞态条件是现代应用程序中影响最大的安全漏洞之一,但自动扫描器对其的覆盖仍然不足。虽然 Nuclei、ffuf 和 sqlmap 等工具在发现静态漏洞方面表现出色,但它们根本无法发现基于并发的问题。
如今的渗透测试人员面临着一个选择:为每次项目手动编写一次性脚本,或者完全跳过竞态条件测试。这两种选择都不具备可扩展性,也不够专业。
**RatRace 解决了这个问题**,它为竞态条件测试提供了一个完善的、可重复的工作流程——将以前临时的脚本编写转变为一个系统化的过程,可以集成到 CI/CD 流水线中并生成专业的报告。
### 现实影响
竞态条件漏洞经常出现在影响巨大的领域:
- **电子商务:** 重复下单、库存超卖、折扣叠加
- **金融科技:** 双重转账、基于竞态条件的权限提升、交易重放
- **身份认证:** Token 耗尽、会话固定、并发密码重置
- **速率限制:** 通过并发请求绕过、分布式滥用
这些漏洞在漏洞赏金计划中始终能获得最高赏金,因为它们直接威胁到业务收入和客户信任。
## 核心功能
### 模板驱动测试
在 YAML 中定义一次竞态场景,并在项目中复用。其格式借鉴了 Nuclei 模板,以便熟悉:
```
id: checkout-race
info:
name: Checkout Duplicate Order Race
severity: critical
race:
mode: burst
concurrency: 20
request:
method: POST
path: /api/v1/checkout
headers:
Authorization: "Bearer {{session}}"
body: '{"item_id": "SKU-001"}'
validate:
follow_up:
method: GET
path: /api/v1/orders?user={{user_id}}
expect:
json_field: "orders.length"
condition: equals
value: 1
```
### 多种攻击模式
**Burst 模式** — 基于 Goroutine 的并行请求,具有精确的同步功能(稳定,可用于生产环境)
- 启动 N 个 Goroutine,以微秒级精度同步
- 适用于大多数竞态窗口大于 5ms 的应用程序
**Last-Byte 同步** — HTTP/1.1 TCP 级别攻击(实现就绪,测试阶段)
- 将所有连接保持在请求主体的最后一个字节
- 通过 close() 广播释放,实现亚毫秒级窗口
- 对负载均衡和限速目标有效
**Single-Packet** — 基于 HTTP/2 帧的攻击(实现就绪,测试阶段)
- 在单个 TCP 段中发送所有请求
- 利用多路复用实现几乎同时的服务器接收
- 绕过某些速率限制机制
### 智能验证
RatRace 自动:
- **通过状态码 + 正文哈希对响应进行指纹识别**,以检测异常
- **对结果进行聚类**,以识别具有统计学意义的差异
- **基于异常频率和幅度计算置信度分数**
- **执行后续请求** 以确认状态变化(例如,“该物品是否真的被购买了两次?”)
- **支持条件验证**,操作符包括:`equals`、`not_equals`、`greater_than`、`contains`
### 工作流集成
**导入现有请求:**
```
# 来自 curl 命令
ratrace import --curl 'curl -X POST https://api.example.com/checkout ...' -o template.yaml
# 来自 Burp 导出的 HAR 文件
ratrace import --har session.har -o template.yaml
```
**CI/CD 自动化:**
```
ratrace race -u $TARGET -t template.yaml --silent
echo $? # Exit code 10 = race found, 0 = clean
```
**专业报告:**
```
# 用于自动化的 JSON
ratrace race -u $TARGET -t template.yaml -o results/
# HTML 报告(计划中的 V2)
ratrace report --input results.json --format html
```
## 安装说明
### 从源码安装
```
git clone https://github.com/bogdanticu88/ratrace.git
cd ratrace
go build ./cmd/ratrace
./ratrace --help
```
**要求:** Go 1.24+
### Docker
```
docker build -t ratrace:latest .
docker run --rm -it ratrace:latest race -u https://api.example.com -t template.yaml
```
## 使用说明
### 快速开始:运行竞态测试
```
# 1. 从 curl 导入或使用现有模板
ratrace import --curl 'curl -X POST https://api.example.com/checkout ...' -o checkout.yaml
# 2. 执行竞赛
ratrace race -u https://api.example.com -t checkout.yaml -m burst -c 20
# 3. 查看发现结果
# 输出显示:
# 🔴 [CRITICAL] Checkout Duplicate Order Race
# 异常率:91.7%
# 置信度: 84%
```
### 示例:电子商务结账竞态
```
ratrace race \
-u https://shop.example.com \
-t examples/templates/checkout-race.yaml \
-m burst \
-c 25 \
--output results/
```
### 示例:静默模式(适用于 CI/CD)
```
# 在无详细日志的情况下运行,以语义代码退出
ratrace race -u https://api.example.com -t template.yaml --silent
# 检查退出代码
if [ $? -eq 10 ]; then
echo "Race condition found!"
exit 1
fi
```
## 命令参考
### race — 执行竞态攻击
```
ratrace race -u -t [options]
Options:
-m, --mode string burst|lastbyte|singlepacket (default from template)
-c, --concurrency int Goroutine count (default 20)
-H, --header strings Custom headers (repeatable)
-x, --proxy string Proxy URL for requests
-o, --output string Output directory for results
--timeout duration Request timeout (default 10s)
--insecure Skip TLS certificate verification
--dry-run Parse template and exit
--silent Suppress logs, show findings only
```
### import — 将请求转换为模板
```
ratrace import [--curl | --har ] -o
Converts existing requests (curl commands or HAR exports) into RatRace templates.
Useful for integrating with existing workflows.
```
### report — 生成报告
```
ratrace report --input results.json --format [html|json] --output report.html
Generates human-readable reports from race test results.
```
### ratelimit — 测试速率限制绕过
```
ratrace ratelimit -u -c
Tests if endpoints are vulnerable to concurrent rate limit bypass.
```
## 模板格式
模板是定义竞态场景的 YAML 文件。完整参考:
```
id: unique-identifier
info:
name: "Human-readable name"
severity: critical|high|medium|low|info
tags: [tag1, tag2]
race:
mode: burst|lastbyte|singlepacket
concurrency: 20
request:
method: POST|GET|PUT|DELETE|PATCH
path: /api/endpoint
headers:
Authorization: "Bearer {{token}}"
Custom-Header: "value"
body: '{"json": "body", "user": "{{user_id}}"}'
validate:
follow_up:
method: GET
path: /api/orders?user={{user_id}}
expect:
json_field: "orders.length"
condition: equals|greater_than|contains|not_equals
value: 1
```
**变量**(使用 `{{name}}` 占位符):
- 通过命令行 headers 替换或在模板中硬编码
- 示例:`ratrace race ... -H "Authorization: Bearer {{session_token}}"`
## 输出
### 终端输出
清晰、带颜色编码的日志显示测试进度:
```
[INF] Loading template path=checkout-race.yaml
[INF] Template loaded id=checkout-race mode=burst
[RUN] Firing burst attack concurrency=20
[INF] All requests completed total=20
[RUN] Clustering responses baseline=17 anomalous=3
[RACE] Race condition detected anomaly_rate=15% confidence=94%
[CONF] Validation passed field=orders.length expected=1
[INF] Race test complete findings=1
Templates: 1 Requests: 20 Time: 3.2s
🔴 [CRITICAL] Checkout Duplicate Order Race
Anomaly Rate: 15.0%
Confidence: 94%
Status Diff: 201 → 200
Field Diffs: order_id (100 → 101)
```
### JSON 结果
完整结果保存至 `results_.json`,用于自动化和归档:
```
{
"TemplateID": "checkout-race",
"Target": "https://api.example.com/api/v1/checkout",
"Mode": "burst",
"Concurrency": 20,
"TotalRequests": 20,
"Duration": 3200000000,
"Findings": [
{
"TemplateName": "Checkout Duplicate Order Race",
"Severity": "critical",
"AnomalyRate": 0.15,
"Confidence": 0.94,
"Diff": {
"StatusChanged": true,
"BaselineStatus": 201,
"AnomalousStatus": 200
}
}
]
}
```
## 退出代码
用于 CI/CD 集成和自动化:
| 代码 | 含义 |
|------|---------|
| 0 | 成功 — 未发现竞态条件 |
| 3 | 数据错误 — 未找到模板,解析错误 |
| 4 | 执行错误 — 网络故障,引擎错误 |
| 10 | **发现竞态条件** — 需要采取行动 |
| 11 | 确认速率限制绕过 |
| 12 | 检测到异常但验证无定论 |
## 开发
### 构建
```
make build # Compile binary
```
### 测试
```
make test # All tests
make test-unit # Unit tests only
make test-int # Integration tests
make coverage # Coverage report (HTML)
```
### Linting 与质量检查
```
make lint # Run linter
```
### 示例
```
make run-race # Build and run example
```
## 架构
```
cmd/ratrace/
└── main.go Entrypoint, CLI definition
internal/
├── cmd/ Subcommand implementations
├── engine/ Race attack modes (burst, lastbyte, singlepacket)
├── validator/ Response clustering, diffing, follow-up checks
├── importer/ HAR and curl parsers
├── reporter/ JSON and HTML output
├── template/ YAML parsing and templating
├── log/ Structured color logging
└── models/ Shared data types
```
## 状态与路线图
### V1(当前版本)
- ✅ 带 Goroutine 同步的 Burst 模式
- ✅ 响应聚类和异常检测
- ✅ 置信度评分
- ✅ 带条件的后续验证
- ✅ HAR/curl 导入
- ✅ JSON 输出
- ✅ 适用于 CI/CD 的静默模式
- ⏳ Last-byte 同步模式(实现就绪,需要测试)
- ⏳ Single-packet 模式(实现就绪,需要测试)
### V2(计划中)
- [ ] HTML 报告生成
- [ ] PoC 脚本生成
- [ ] 速率限制绕过专项功能
- [ ] Burp Suite XML 导入
- [ ] gRPC 竞态测试
- [ ] WebSocket 竞态测试
- [ ] 模板注册中心和社区模板
- [ ] Nuclei 集成
## 许可证
MIT
## 免责声明
RatRace 专为授权的安全测试、漏洞赏金计划和渗透测试而设计。**仅在您拥有或已获得明确书面许可的系统上使用。** 未经授权对他人的系统进行测试是非法的。
## 作者
**Bogdan Ticu** — 安全研究与工程
如需提交问题、提问或报告安全问题,请访问:[GitHub Issues](https://github.com/bogdanticu88/ratrace/issues)
A lightweight CLI tool for systematically detecting and exploiting race conditions in web applications, APIs, and modern services.
     
标签:API安全, AV规避, EVTX分析, Go语言, JSON输出, Web安全, 代码生成, 双花漏洞, 安全测试, 并发漏洞, 库存超卖, 攻击性安全, 日志审计, 渗透测试工具, 漏洞报告, 程序破解, 竞态条件, 蓝队分析, 请求拦截, 越权漏洞, 逻辑漏洞, 重放攻击, 限流绕过