PHP运行模式与SAPI简介
CVE-2012-1823曝光时被称为“PHP远程代码执行漏洞”,曾引发广泛关注。当时笔者尚处于安全入门阶段,直到近期受tomato师傅启发回顾此案例,才重新审视这一漏洞。通过搭建Vulhub环境并进行原理剖析,发现其机制颇具趣味,特此撰文分享心得。
首先简要介绍PHP的运行模式。
下载PHP源码后可见sapi目录。SAPI在PHP中扮演消息“传递者”的角色,例如在《Fastcgi协议分析 && PHP-FPM未授权访问漏洞 && Exp编写》一文中提到的fpm,其职责是接收Web容器通过FastCGI协议封装的数据,并提交给PHP解释器执行。
除FPM外,最常见的SAPI是用于Apache的mod_php,负责处理PHP与Apache之间的数据交换。
php-cgi同样属于SAPI。早期Web应用架构简单,Web容器接收HTTP包后,提取用户请求的文件(即CGI脚本),fork出子进程(解释器)执行该文件,获取结果后直接返回给用户,随后子进程终止。基于Bash、Perl等语言的Web应用多采用此方式,称为CGI。安装Apache时默认的cgi-bin目录最初便是存放此类CGI脚本的位置。
然而,CGI模式存在致命缺陷:进程创建和调度消耗资源,且进程数量有限。基于CGI运行的网站难以承受高并发请求,若每个请求生成一个子进程,可能导致服务器过载。因此,FastCGI应运而生。FastCGI进程常驻后台,通过FastCGI协议接收数据包并执行,执行完毕后不退出,仅返回结果。
PHP提供名为php-cgi的SAPI,具备两种功能:一是提供CGI方式的交互,二是提供FastCGI方式的交互。这意味着我们可以像使用Perl一样,让Web容器直接fork一个php-cgi进程执行脚本;也可以在后台运行 php-cgi -b 127.0.0.1:9000 (php-cgi作为FastCGI管理器),使Web容器通过FastCGI协议与9000端口交互。
那么前文提到的FPM又是什么?为何PHP有两个FastCGI管理器?确实如此,php-cgi和FPM均以FastCGI模式运行。但FPM是在PHP 5.3版本后引入的更高效管理器,其优势众多,此处不再赘述,读者可自行查阅源码。鉴于FPM优点更多,如今越来越多的Web应用采用php-fpm来运行PHP。
CVE-2012-1823漏洞原理与历史成因
回到本漏洞。CVE-2012-1823是php-cgi SAPI存在的漏洞。上文介绍了php-cgi的两种运行方式:CGI和FastCGI,本漏洞仅出现在以CGI模式运行的PHP环境中。
简而言之,该漏洞的核心在于用户请求中的Query String被当作了php-cgi的参数,从而引发一系列后果。
探究一下原理, RFC3875 中规定,当querystring中不包含没有解码的 = 号的情况下,要将querystring作为cgi的参数传入。所以,Apache服务器按要求实现了这个功能。
但PHP并未关注RFC的这一规则。或许曾注意并处理过,当时的处理方式是在Web上下文中禁止传入参数。然而在2004年,某位开发者发表了如下言论:
From: Rasmus Lerdorf
Subject: [PHP-DEV] php-cgi command line switch memory check
Newsgroups: gmane.comp.php.devel
Date: 2004-02-04 23:26:41 GMT (7 years, 49 weeks, 3 days, 20 hours and 39 minutes ago)
In our SAPI cgi we have a check along these lines:
if (getenv("SERVER_SOFTWARE")
|| getenv("SERVER_NAME")
|| getenv("GATEWAY_INTERFACE")
|| getenv("REQUEST_METHOD")) {
cgi = 1;
}
if(!cgi) getopt(...)
As in, we do not parse command line args for the cgi binary if we are
running in a web context. At the same time our regression testing system
tries to use the cgi binary and it sets these variables in order to
properly test GET/POST requests. From the regression testing system we
use -d extensively to override ini settings to make sure our test
environment is sane. Of course these two ideas conflict, so currently our
regression testing is somewhat broken. We haven't noticed because we
don't have many tests that have GET/POST data and we rarely build the cgi
binary.
The point of the question here is if anybody remembers why we decided not
to parse command line args for the cgi version? I could easily see it
being useful to be able to write a cgi script like:
#!/usr/local/bin/php-cgi -d include_path=/path
<?php
...
?>
and have it work both from the command line and from a web context.
As far as I can tell this wouldn't conflict with anything, but somebody at
some point must have had a reason for disallowing this.
-Rasmus
显然,这位开发者为了方便使用类似 #!/usr/local/bin/php-cgi -d include_path=/path 的写法进行测试,认为不应限制php-cgi接受命令行参数,且该功能与其他代码无冲突。
于是, if(!cgi) getopt(...) 被删除了。
但显然,根据RFC中对Command Line的说明,命令行参数不仅可以通过 #!/usr/local/bin/php-cgi -d include_path=/path 的方式传入php-cgi,更可以通过Query String的方式传入。
这便是本漏洞的历史成因。
命令行参数利用与漏洞修复绕过
那么,可控的命令行参数能做什么呢?
通过阅读源码,发现CGI模式下有以下可用参数:
-c 指定php.ini文件的位置
-n 不要加载php.ini文件
-d 指定配置项
-b 启动fastcgi进程
-s 显示文件源码
-T 执行指定次该文件
-h 和 -? 显示帮助
最简单的利用方式当然是 -s ,可直接显示源码:

但阅读过笔者FastCGI相关文章的同学应很快想到更好的利用方法:通过使用 -d 指定 auto_prepend_file 来制造任意文件读取漏洞,进而执行任意代码:

注意,空格需使用 + 或 %20 代替, = 需用URL编码代替。
漏洞曝光后,PHP官方进行了修补,发布了新版本5.4.2及5.3.12,但该修复不完全,可被绕过,进而衍生出CVE-2012-2311漏洞。
PHP的修复方法是对 - 进行了检查:
if(query_string = getenv("QUERY_STRING")) {
decoded_query_string = strdup(query_string);
php_url_decode(decoded_query_string, strlen(decoded_query_string));
if(*decoded_query_string == '-' && strchr(decoded_query_string, '=') == NULL) {
skip_getopt = 1;
}
free(decoded_query_string);
}
可见,获取Query String后进行解码,如果第一个字符是 - 则设置skip_getopt,即不获取命令行参数。
此修复方法的不安全之处在于,如果运维对php-cgi进行了一层封装:
#!/bin/sh
exec /usr/local/bin/php-cgi $*
通过使用空白符加 - 的方式,也能传入参数。此时Query String的第一个字符变为空白符而非 - ,从而绕过了上述检查。
于是,在php5.4.3和php5.3.13中继续进行了修改:
if((query_string = getenv("QUERY_STRING")) != NULL && strchr(query_string, '=') == NULL) {
/* we've got query string that has no = - apache CGI will pass it to command line */
unsigned char *p;
decoded_query_string = strdup(query_string);
php_url_decode(decoded_query_string, strlen(decoded_query_string));
for (p = decoded_query_string; *p && *p <= ' '; p++) {
/* skip all leading spaces */
}
if(*p == '-') {
skip_getopt = 1;
}
free(decoded_query_string);
}
先跳过所有空白符(小于等于空格的所有字符),再判断第一个字符是否是 - 。
该漏洞当年的影响中等。因为PHP-CGI SAPI在当时因性能等问题已逐渐退出历史舞台。但考虑到PHP在Web领域的举足轻重地位,多年积累的巨大用量,许多老旧设备和服务器仍在运行有漏洞的版本和PHP-CGI,故其影响不可低估。
不过,站在2026年的今天,分析此漏洞已非为了谈论影响,而是因其思路有趣,再次让笔者领会到阅读RFC的重要性。