一个基于 Python 的检测工具,用于 CVE-2024-40725,这是一个影响 2.4.0 和 2.4.61 版本的 Apache HTTP Server 源代码泄露漏洞。
法律声明: 仅可对您拥有或已获得明确书面授权进行测试的服务器运行此扫描器。未经授权使用可能违反计算机欺诈法律(例如 CFAA、Computer Misuse Act)。
CVE-2024-40725 是 Apache 内部请求管道中的一个回归问题,该问题在 2.4.0 版本中引入(是对 CVE-2024-39884 的不完整修复)。当服务器使用旧式 AddType 指令(例如 AddType application/x-httpd-php .php)进行配置时,Apache 的内部处理程序分配会在子请求处理(由 DirectoryIndex、mod_rewrite 或 mod_dir 触发)期间被静默丢弃。
因此,PHP 脚本解释器永远不会被调用。取而代之的是,Apache 内置的 default-handler 会从磁盘打开文件并将原始字节流直接传送给客户端,其中可能包含数据库凭据、API 密钥以及应用程序逻辑。
受影响版本: Apache httpd 2.4.0 – 2.4.61
已修复版本: Apache httpd 2.4.62+
requests 库pip install requests
# Clone or download the scanner
curl -O https://your-host/cve_2024_40725_scanner.py
# Make executable (optional)
chmod +x cve_2024_40725_scanner.py
python3 cve_2024_40725_scanner.py --target <URL> [OPTIONS]
# Quickstart: scan localhost with built-in default paths
python3 cve_2024_40725_scanner.py --target http://localhost
# Scan specific paths
python3 cve_2024_40725_scanner.py \
--target http://192.168.1.10 \
--paths /index.php /admin/config.php /wp-config.php
# Use a wordlist file
python3 cve_2024_40725_scanner.py \
--target http://192.168.1.10 \
--wordlist php_paths.txt
# Combine wordlist + extra inline paths (merged, deduplicated)
python3 cve_2024_40725_scanner.py \
--target http://192.168.1.10 \
--wordlist php_paths.txt \
--paths /extra/secret.php
# HTTPS with self-signed certificate
python3 cve_2024_40725_scanner.py \
--target https://myserver.local \
--no-verify-ssl
# Polite scan with 1 second delay and JSON report
python3 cve_2024_40725_scanner.py \
--target http://myserver.local \
--wordlist php_paths.txt \
--delay 1.0 \
--output results.json \
--verbose
# CI/CD usage (exit code 1 = vulnerable, 0 = clean)
python3 cve_2024_40725_scanner.py --target http://localhost || echo "VULNERABLE"
每行一个路径。以 # 开头的行被视为注释并跳过。空白行将被忽略。前导斜杠会自动规范化。
# Common PHP entrypoints
/index.php
/info.php
/phpinfo.php
# Admin panels
/admin/index.php
/admin/config.php
# CMS files
/wp-config.php
/wp-login.php
/configuration.php
# API internals
/api/v1/status.php
扫描器对每个路径运行两种探测:
GET /index.php HTTP/1.1
检查直接请求 PHP 文件是否会返回其原始源代码。在 AddType 配置错误、即使直接请求也会受影响的服务器上,此探测会命中。
GET / HTTP/1.1
请求 父目录 而不是文件本身。Apache 在内部解析 DirectoryIndex → index.php,从而产生一个子请求。这正是 Apache 2.4.0–2.4.61 中 r->handler 被置为 NULL 的代码路径。
探测 B 是更重要、更切合实际的测试。许多服务器仅通过子请求路径易受攻击,而直接文件路径不受影响。
扫描器检查响应体中是否存在 PHP 源代码特征:
<?php<?PHP<?=当发现匹配时,它会提取泄漏位置附近 150 个字符的片段用于分析,而不会打印整个响应体。
读取 Server: 响应头以检测 Apache 版本。即使在确认泄漏之前,也会标记报告版本为 2.4.0 或 2.4.61 的服务器。
注意:加固的服务器可能会隐藏
Server:响应头(ServerTokens Prod)。扫描器仍会无条件运行所有探测。
============================================================
CVE-2024-40725 -- Apache Source Code Disclosure Scanner
============================================================
Target : http://localhost
Paths : 6
Time : 2024-08-01T12:00:00
[*] Fingerprinting server: http://localhost
Server Header : Apache/2.4.61 (Debian)
Apache Version: 2.4.61
[!] Version is in vulnerable range (2.4.0 - 2.4.61)
[*] Testing 6 path(s)...
--- /index.php
Direct (HTTP 200): OK
Subreq (HTTP 200): LEAKED
[!] VULNERABLE via: subrequest (directory index)
Leaked snippet: '<?php\n$db_pass = "supersecretpassword";'
--- /info.php
Direct (HTTP 404): OK
Subreq (HTTP 404): OK
[+] Safe
============================================================
SCAN SUMMARY
============================================================
Target : http://localhost
Apache Version : 2.4.61
Paths Tested : 6
Vulnerable Paths : 1
Safe Paths : 5
[!] RESULT: VULNERABLE
Vulnerable paths:
* /index.php (trigger: subrequest (directory index))
Remediation:
1. Upgrade to Apache httpd >= 2.4.62
2. Replace 'AddType' with 'SetHandler' in <FilesMatch> blocks
============================================================
--output results.json){
"target": "http://localhost",
"scan_time": "2024-08-01T12:00:00",
"apache_version": "2.4.61",
"apache_in_range": true,
"paths_tested": 6,
"vulnerable_paths": [
{
"path": "/index.php",
"direct_url": "http://localhost/index.php",
"directory_url": "http://localhost/",
"direct_status": 200,
"direct_leaked": false,
"direct_snippet": null,
"directory_status": 200,
"directory_leaked": true,
"directory_snippet": "<?php\n$db_pass = \"supersecretpassword\";",
"server_header": "Apache/2.4.61 (Debian)",
"vulnerable": true,
"trigger": "subrequest (directory index)"
}
],
"safe_paths": [...]
}
| 代码 | 含义 |
|---|---|
0 | 未检测到易受攻击的路径 |
1 | 发现一个或多个易受攻击的路径 |
退出代码使扫描器适用于 CI/CD 流水线和自动化基础设施审计。
# Debian / Ubuntu
sudo apt update && sudo apt install apache2
# RHEL / CentOS / AlmaLinux
sudo dnf update httpd
# Verify version (must be >= 2.4.62)
apache2 -v
AddType 替换为 SetHandler替换所有用于 PHP 执行的 AddType 指令:
# ❌ Vulnerable Configs
AddType application/x-httpd-php .php
# ✅ Safe Configs
<FilesMatch "\.php$">
SetHandler "proxy:unix:/run/php/php8.2-fpm.sock|fcgi://localhost"
</FilesMatch>
# VULNERABLE
<VirtualHost *:80>
DocumentRoot /var/www/html
AddType application/x-httpd-php .php # ← triggers CVE
DirectoryIndex index.php
</VirtualHost>
# SAFE
<VirtualHost *:80>
DocumentRoot /var/www/html
<FilesMatch "\.php$">
SetHandler "proxy:unix:/run/php/php8.2-fpm.sock|fcgi://localhost"
</FilesMatch>
DirectoryIndex index.php
</VirtualHost>
| CVE | 版本 | 描述 |
|---|---|---|
| CVE-2024-39884 | 2.4.0 | 原始源代码泄露回归问题 |
| 标志 | 类型 | 默认值 | 描述 |
|---|
--target | URL | (必需) | 要测试的 Apache 服务器的基础 URL |
--paths | PATH [PATH ...] | 内置列表 | 要直接探测的一个或多个 PHP 路径 |
--wordlist | FILE | — | 字典文件的路径(每行一个路径) |
--no-directory-check | 标志 | 关闭 | 跳过子请求/目录索引探测(探测 B) |
--timeout | int(秒) | 10 | 每个请求的超时时间 |
--delay | float(秒) | 0.0 | 请求之间的延迟(速率限制) |
--no-verify-ssl | 标志 | 关闭 | 禁用 SSL 证书验证 |
--output | FILE | — | 将完整 JSON 报告写入此文件 |
--verbose | 标志 | 关闭 | 启用调试输出 |
| CVE-2024-40725 | 2.4.61 | 不完整的修复——同类缺陷 |
| CVE-2024-40898 | 2.4.61 | mod_rewrite 中的独立 SSRF(仅限 Windows) |