针对 NGINX 配置文件的静态扫描器,用于检测与 CVE-2026-42945 相关的 rewrite/set 模式。
该扫描器使用 crossplane 解析 NGINX 配置,遍历 location 块,并打印每个疑似受影响的 location,格式如下:
/<path-to>/xxx.conf:<line-number> -> location XXXXX
示例:
/path/to/nginx.conf:39 -> location ~ ^/api/(.*)$
依赖项通过 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'
如果你希望扫描器递归展开 glob 模式,请用引号引用。如果你的 shell 先展开了 ./*.conf,则扫描器只能接收 shell 匹配到的文件。
仅扫描提供的文件,不跟随 include 指令:
uv run ./scan.py --single ./site.conf
显示解析器警告,包括缺失的 include 文件:
uv run ./scan.py -v -r /etc/nginx
go/ 目录下提供了一个 Go 实现,输出格式和退出码与 Python 版本相同。它使用 nginx-go-crossplane 解析 NGINX 配置,使用 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 配置文件,可用作示例:
对所有示例夹具运行扫描器:
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/(.*)$
如果发现受影响的 location,每项发现会单独打印在一行:
/absolute/path/to/file.conf:30 -> location ~ ^/aaaa/dddd/(.*)$
/absolute/path/to/nginx.conf:39 -> location ~ ^/api/(.*)$
无输出表示在已解析的配置中未发现受影响的 location。
这是一个静态配置扫描器。它不会:
请将发现视为需要审查和修复的位置。只有当存在有漏洞的 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/(.*)$
| 夹具 | 预期结果 |
|---|
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 | 无发现,因为没有可用捕获 |