服务器维护,服务器代维,安全设置,漏洞扫描,入侵检测服务

dirtysea 发表于 2010-6-28 11:05:49

nginx解析漏洞利用方法

系统编号: WAVDB-01623
影响版本:nginx 0.5.x/0.6.x/0.7.x/0.8.x
程序介绍:nginx是一款高性能的web服务器,使用非常广泛,其不仅经常被用作反向代理,也可以非常好的支持PHP的运行。
漏洞分析:nginx默认以cgi的方式支持php的运行,譬如在配置文件当中可以以

location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}

的方式支持对php的解析,location对请求进行选择的时候会使用URI环境变量进行选择,其中传递到后端Fastcgi的关键变量SCRIPT_FILENAME由nginx生成的$fastcgi_script_name决定,而通过分析可以看到$fastcgi_script_name是直接由URI环境变量控制的,这里就是产生问题的点。而为了较好的支持PATH_INFO的提取,在PHP的配置选项里存在cgi.fix_pathinfo选项,其目的是为了从SCRIPT_FILENAME里取出真正的脚本名。
那么假设存在一个http://www.target.com/jpg.jpg,我们以如下的方式去访问
http://www.target.com/jpg.jpg/php.php
将会得到一个URI
/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的变化说明了后端负责解析的变化,该站点就可能存在漏洞。
一句话说,要利用的话,就是在存在漏洞的服务器上传一个任意扩展名(一般都是), PHP脚本, 继而构造类似这样的访问请求: xx.jpg/php.php 然后成功执行脚本
临时解决方案:关闭cgi.fix_pathinfo为0
或者
if ( $fastcgi_script_name ~ \..*\/.*php ) {
return 403;
}

附:Nginx-scan漏洞扫描器代码:来自 http://www.80sec.com/nginx-securit.html
#!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";
}
}

=====================2010年5月22日 12:03:54补充
有人说,漏洞应该不是nginx的引起的,而是 php-fpm 因为:
1.两种方案的修改都是针对fastcgi做的
2.这个漏洞只提出针对Nginx+PHP CGI有效
3. 漏洞利用的最终后缀一定是.php,而不是输入任何后缀都被当作PHP执行

dirtysea 发表于 2010-6-28 11:06:43

再提供一种解决Nginx文件类型错误解析漏洞的方法

dirtysea 发表于 2010-6-28 11:07:44

<span class="Apple-style-span" style="color: rgb(0, 0, 0); font-family: Tahoma, Arial; line-height: normal; font-size: 12px; "><div><span class="Apple-style-span" style="color: rgb(0, 0, 0); font-family: Tahoma, Arial; line-height: normal; font-size: 12px; "><span class="Apple-style-span" style="color: rgb(255, 255, 255); font-weight: bold; "><a href="http://blog.s135.com/nginx_0day/nginx_0day/" style="text-decoration: none; color: rgb(255, 255, 255); ">再提供一种解决Nginx文件类型错误解析漏洞的方法</a></span></span></div><div><span class="Apple-style-span" style="color: rgb(0, 0, 0); font-family: Tahoma, Arial; line-height: normal; font-size: 12px; "><br></span></div>网上提供的临时解决方法有:<br><br>  方法①、修改php.ini,设置cgi.fix_pathinfo = 0;然后重启php-cgi。此修改会影响到使用PATH_INFO伪静态的应用,例如我以前博文的URL:<a href="http://blog.s135.com/read.php/348.htm" target="_blank" style="text-decoration: none; color: rgb(79, 99, 113); ">http://blog.s135.com/read.php/348.htm</a>&nbsp;就不能访问了。<br><br>  方法②、在nginx的配置文件添加如下内容后重启:if ( $fastcgi_script_name ~ \..*\/.*php ) {return 403;}。该匹配会影响类似<a href="http://www.domain.com/software/5.0/test.php" target="_blank" style="text-decoration: none; color: rgb(79, 99, 113); ">http://www.domain.com/software/5.0/test.php</a>(5.0为目录),<a href="http://www.domain.com/goto.php/phpwind" target="_blank" style="text-decoration: none; color: rgb(79, 99, 113); ">http://www.domain.com/goto.php/phpwind</a>&nbsp;的URL访问。<br><br>  方法③、对于存储图片的location{...},或虚拟主机server{...},只允许纯静态访问,不配置PHP访问。例如在金山逍遥网论坛、SNS上传的图片、附件,会传送到专门的图片、附件存储服务器集群上(pic.xoyo.com),这组服务器提供纯静态服务,无任何动态PHP配置。各大网站几乎全部进行了图片服务器分离,因此Nginx的此次漏洞对大型网站影响不大。<br><br><hr style="height: 1px; border-top-width: 1px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-top-style: solid; border-top-color: rgb(99, 180, 205); "><br>  本人再提供一种修改nginx.conf配置文件的临时解决方法,兼容“<a href="http://blog.s135.com/demo/0day/phpinfo.php/test" target="_blank" style="text-decoration: none; color: rgb(79, 99, 113); ">http://blog.s135.com/demo/0day/phpinfo.php/test</a>”的PATH_INFO伪静态,拒绝“<a href="http://blog.s135.com/demo/0day/phpinfo.jpg/test.php" target="_blank" style="text-decoration: none; color: rgb(79, 99, 113); ">http://blog.s135.com/demo/0day/phpinfo.jpg/test.php</a>”的漏洞攻击:<br><div style="margin-top: 5px; margin-right: 5px; margin-left: 5px; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin-bottom: 0px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: rgb(0, 160, 198); border-right-color: rgb(0, 160, 198); border-bottom-color: rgb(0, 160, 198); border-left-color: rgb(0, 160, 198); background-color: rgb(255, 255, 255); ">location ~* .*\.php($|/)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($request_filename ~* (.*)\.php) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set $php_url $1;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!-e $php_url.php) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 403;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fastcgi_pass&nbsp;&nbsp;127.0.0.1:9000;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fastcgi_index index.php;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include fcgi.conf;<br>}</div><br><br>  也可将以下内容写在fcgi.conf文件中,便于多个虚拟主机引用:<br><div style="margin-top: 5px; margin-right: 5px; margin-left: 5px; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; margin-bottom: 0px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: dashed; border-right-style: dashed; border-bottom-style: dashed; border-left-style: dashed; border-top-color: rgb(0, 160, 198); border-right-color: rgb(0, 160, 198); border-bottom-color: rgb(0, 160, 198); border-left-color: rgb(0, 160, 198); background-color: rgb(255, 255, 255); ">if ($request_filename ~* (.*)\.php) {<br>&nbsp;&nbsp;&nbsp;&nbsp;set $php_url $1;<br>}<br>if (!-e $php_url.php) {<br>&nbsp;&nbsp;&nbsp;&nbsp;return 403;<br>}<br><br>fastcgi_param&nbsp;&nbsp;GATEWAY_INTERFACE&nbsp;&nbsp;CGI/1.1;<br>fastcgi_param&nbsp;&nbsp;SERVER_SOFTWARE&nbsp;&nbsp;&nbsp;&nbsp;nginx;<br><br>fastcgi_param&nbsp;&nbsp;QUERY_STRING&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $query_string;<br>fastcgi_param&nbsp;&nbsp;REQUEST_METHOD&nbsp;&nbsp;&nbsp;&nbsp; $request_method;<br>fastcgi_param&nbsp;&nbsp;CONTENT_TYPE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $content_type;<br>fastcgi_param&nbsp;&nbsp;CONTENT_LENGTH&nbsp;&nbsp;&nbsp;&nbsp; $content_length;<br><br>fastcgi_param&nbsp;&nbsp;SCRIPT_FILENAME&nbsp;&nbsp;&nbsp;&nbsp;$document_root$fastcgi_script_name;<br>fastcgi_param&nbsp;&nbsp;SCRIPT_NAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$uri;<br>fastcgi_param&nbsp;&nbsp;REQUEST_URI&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$request_uri;<br>fastcgi_param&nbsp;&nbsp;DOCUMENT_URI&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $document_uri;<br>fastcgi_param&nbsp;&nbsp;DOCUMENT_ROOT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$document_root;<br>fastcgi_param&nbsp;&nbsp;SERVER_PROTOCOL&nbsp;&nbsp;&nbsp;&nbsp;$server_protocol;<br><br>fastcgi_param&nbsp;&nbsp;REMOTE_ADDR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$remote_addr;<br>fastcgi_param&nbsp;&nbsp;REMOTE_PORT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$remote_port;<br>fastcgi_param&nbsp;&nbsp;SERVER_ADDR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$server_addr;<br>fastcgi_param&nbsp;&nbsp;SERVER_PORT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$server_port;<br>fastcgi_param&nbsp;&nbsp;SERVER_NAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$server_name;<br><br># PHP only, required if PHP was built with --enable-force-cgi-redirect<br>fastcgi_param&nbsp;&nbsp;REDIRECT_STATUS&nbsp;&nbsp;&nbsp;&nbsp;200;</div><br></span>
页: [1]
查看完整版本: nginx解析漏洞利用方法