realityone/cve-2026-42945-scan
GitHub: realityone/cve-2026-42945-scan
针对 CVE-2026-42945 的 NGINX 配置静态扫描器,用于检测 rewrite/set 指令组合产生的高危模式。
Stars: 1 | Forks: 0
# cve-2026-42945-scan
用于检测与 CVE-2026-42945 相关的 rewrite/set 模式的 NGINX 配置文件静态扫描器。
该扫描器使用
[crossplane](https://github.com/nginxinc/crossplane) 解析 NGINX 配置,遍历 `location` 块,
并以如下格式打印出每个看起来受影响的位置:
```
//xxx.conf: -> location XXXXX
```
示例:
```
/path/to/nginx.conf:39 -> location ~ ^/api/(.*)$
```
参考:[CVE-2026-42945-POC](https://github.com/p3Nt3st3r-sTAr/CVE-2026-42945-POC)。
## 要求
- Python 3.10 或更新版本
- [uv](https://docs.astral.sh/uv/)
依赖项由 `uv` 在 `pyproject.toml` 中管理,并锁定在 `uv.lock` 中。
唯一的运行时依赖是 `crossplane`。
## 设置
在仓库根目录下执行:
```
uv sync
```
您也可以跳过单独的设置步骤,让 `uv run` 在首次使用时创建环境。
## 用法
扫描单个配置文件:
```
uv run ./scan.py /etc/nginx/nginx.conf
```
递归扫描目录:
```
uv run ./scan.py -r /etc/nginx
```
从当前目录递归扫描 `*.conf` 文件:
```
uv run ./scan.py -r './*.conf'
```
当您希望扫描器以递归方式展开通配符时,请为通配符加上引号。如果您的 shell
首先展开了 `./*.conf`,扫描器将仅接收到 shell 匹配到的文件。
仅扫描提供的文件,不遵循 `include` 指令:
```
uv run ./scan.py --single ./site.conf
```
显示解析器警告,包括缺失的 include 文件:
```
uv run ./scan.py -v -r /etc/nginx
```
## Go 版本
在 `go/` 目录下提供了一个具有相同输出格式和退出码的 Go 实现。它使用
[nginx-go-crossplane](https://github.com/nginxinc/nginx-go-crossplane) 来解析 NGINX
配置,并使用 [pflag](https://github.com/spf13/pflag) 来处理 CLI 标志。
它需要 Go 1.23 或更新版本。
使用 `go run` 运行:
```
cd go
go run . -r ../tests/fixtures
```
当存在发现时,扫描器将以状态码 `1` 退出。`go run` 将在此时打印
额外的 `exit status 1` 行;当您只需要扫描器输出时,请构建二进制文件。
构建独立二进制文件:
```
cd go
go build -o cve-2026-42945-scan-go .
./cve-2026-42945-scan-go -r ../tests/fixtures
```
## 它能检测什么
有风险的配置模式是包含以下内容的 `location`:
- 一个 `rewrite` 指令,其替换内容包含未转义的 `?`
- 没有终止重写标志,如 `last`、`break`、`redirect` 或 `permanent`
- 一个后续的 `set` 指令,用于复制正则捕获,例如 `$1`、`${1}`、
`$name` 或 `${name}`
易受攻击的模式示例:
```
location ~ ^/api/(.*)$ {
rewrite ^/api/(.*)$ /internal?migrated=true;
set $original_endpoint $1;
}
```
扫描器报告的是 `location` 行,而不是 `rewrite` 或 `set` 行,
因为 location 是需要检查和修复的可操作块。
## 测试夹具
`tests/fixtures/` 目录包含可用作示例的小型 NGINX 配置:
| 夹具 | 预期结果 |
| --- | --- |
| `tests/fixtures/vulnerable.conf` | 报告一个位置捕获发现 |
| `tests/fixtures/named_capture.conf` | 报告一个命名捕获发现 |
| `tests/fixtures/missing_include_still_scans.conf` | 即使缺少 include 也报告一个发现 |
| `tests/fixtures/safe_break_flag.conf` | 无发现,因为 rewrite 使用了 `break` |
| `tests/fixtures/safe_no_capture.conf` | 无发现,因为没有可用的捕获 |
针对所有示例夹具运行扫描器:
```
uv run ./scan.py -r tests/fixtures
```
预期发现:
```
/absolute/path/to/tests/fixtures/missing_include_still_scans.conf:6 -> location ~ ^/partial/(.*)$
/absolute/path/to/tests/fixtures/named_capture.conf:2 -> location /users
/absolute/path/to/tests/fixtures/vulnerable.conf:2 -> location ~ ^/api/(.*)$
```
## 输出
如果找到了受影响的位置,每项发现将打印在单独的行上:
```
/absolute/path/to/file.conf:30 -> location ~ ^/aaaa/dddd/(.*)$
/absolute/path/to/nginx.conf:39 -> location ~ ^/api/(.*)$
```
无输出意味着在解析的配置中未发现受影响的位置。
## 局限性
这是一个静态配置扫描器。它不会:
- 验证正在运行的 NGINX 版本
- 证明可利用性
- 对服务器执行或测试请求
- 完全评估动态变量或生成的配置
- 检测隐藏在缺失 include 文件中的易受攻击片段
将发现结果视为需要审查和修复的位置。只有当易受攻击的 NGINX 版本范围和有风险的
配置模式同时存在时,主机才会真正受到影响。
## 开发
运行测试套件:
```
uv run python -m unittest discover -s tests -v
```
运行 Go 测试套件:
```
cd go
go test ./...
```
运行语法检查:
```
uv run python -m py_compile scan.py
```
针对内置的 POC 配置运行:
```
uv run ./scan.py CVE-2026-42945-POC/env/nginx.conf
```
预期输出:
```
/absolute/path/to/CVE-2026-42945-POC/env/nginx.conf:39 -> location ~ ^/api/(.*)$
```
标签:crossplane, CVE-2026-42945, DevSecOps, EVTX分析, Go, GPT, NGINX, Python, Ruby工具, Web服务器安全, XXE攻击, 上游代理, 云安全监控, 文件解析, 无后门, 无线安全, 日志审计, 漏洞管理, 逆向工具, 配置扫描, 静态分析