那么假设存在一个http://www.target.com/jpg.jpg,我们以如下的方式去访问
http://www.target.com/jpg.jpg/php.php
将会得到一个URL
/jpg.jpg/php.php
经过location指令,该请求将会交给后端的fastcgi处理,nginx为其设置环境变量SCRIPT_FILENAME,内容为
/scripts/jpg.jpg/php.php
而在其他的webserver如lighttpd当中,我们发现其中的SCRIPT_FILENAME被正确的设置为
/scripts/jpg.jpg
所以不存在此问题。
后端的fastcgi在接受到该选项时,会根据fix_pathinfo配置决定是否对SCRIPT_FILENAME进行额外的处理,一般情况下如果不对fix_pathinfo进行设置将影响使用PATH_INFO进行路由选择的应用,所以该选项一般配置开启。Php通过该选项之后将查找其中真正的脚本文件名字,查找的方式也是查看文件是否存在,这个时候将分离出SCRIPT_FILENAME和PATH_INFO分别为
/scripts/jpg.jpg和php.php
最后,以/scripts/jpg.jpg作为此次请求需要执行的脚本,攻击者就可以实现让nginx以php来解析任何类型的文件了。
一、漏洞利用
访问一个nginx来支持php的站点,在一个任何资源的文件如robots.txt后面加上/php.php,这个时候你可以看到如下的区别:
访问http://www.target.com/robots.txt
HTTP/1.1 200 OK Server: nginx/0.6.32 Date: Thu, 20 May 2010 10:05:30 GMT Content-Type: text/plain Content-Length: 18 Last-Modified: Thu, 20 May 2010 06:26:34 GMT Connection: keep-alive Keep-Alive: timeout=20 Accept-Ranges: bytes
访问http://www.target.com/robots.txt/php.php
HTTP/1.1 200 OK Server: nginx/0.6.32 Date: Thu, 20 May 2010 10:06:49 GMT Content-Type: text/html Transfer-Encoding: chunked Connection: keep-alive Keep-Alive: timeout=20 X-Powered-By: PHP/5.2.6
其中的Content-Type的变化说明了后端负责解析的变化,该站点就可能存在漏洞。
二、利用方法
在存在漏洞的服务器上传一个任意扩展名, 继而构造类似这样的访问求:xx.jpg/php.php,然后成功执行脚本
三、临时解决方案
关闭cgi.fix_pathinfo为0
或者
if ( $fastcgi_script_name ~ \..*\/.*php ) {
return 403;
}
附:Nginx-scan漏洞扫描器代码
#!usr/bin/perl -w
use LWP;
use LWP::ConnCache;
my $browser = LWP::UserAgent->new;
$browser->timeout( 15 );
my $conncache = LWP::ConnCache->new;
$browser->conn_cache($conncache);
#先用列表吧,没有用记事本保存列表再读再扫嘿嘿
my @bbslist1;
push @bbslist1,"http://bbs.xxx.com/robots.txt";
push @bbslist1,"http://bbs.yyy.com/robots.txt" ;
push @bbslist1,'http://bbs.pctutu.com/robots.txt';
push @bbslist1,'http://bbs.yahoo.cn/robots.txt';
#Server: nginx/0.8.13
#Content-Type: text/html
print "\t\tNginx漏洞扫描程序 By x13ky\@qq.com\n\n";
foreach my $url (@bbslist1){
print "目前正在扫描:$url\n";
my $response= $browser->get( $url);
$response->is_success or say("Failed to get '$url':\n", $response->status_line);
my $servertype = $response->server;
print "$servertype\n";
if ($servertype=~/nginx/){
my $typeold=$response->content_type;
print "$typeold\n";
my $url2=$url.'/xysky.php';
my $response2 = $browser->get( $url2);
$response2->is_success or say("Failed to get '$url2':\n", $response->status_line);
my $typenew=$response2->content_type;
print "$typenew\n";
if ($typeold eq $typenew){
print "站点 $url 暂没有发现漏洞.\n\n";
}else{
print "站点 $url 存在该漏洞.\n\n";
}
}else{
print "站点不是nginx,Sorry!\n\n";
}
}
有人说,漏洞应该不是nginx的引起的,而是 php-fpm 因为:
1. 两种方案的修改都是针对fastcgi做的
2. 这个漏洞只提出针对Nginx+PHP CGI有效
3. 漏洞利用的最终后缀一定是.php,而不是输入任何后缀都被当作PHP执行