[文章作者:张宴 本文版本:v1.2 最后修改:2010.05.24 转载请注明原文链接:http://blog.zyan.cc/nginx_0day/]

  注:2010年5月23日14:00前阅读本文的朋友,请按目前v1.1版本的最新配置进行设置。

  昨日,80Sec 爆出Nginx具有严重的0day漏洞,详见《Nginx文件类型错误解析漏洞》。只要用户拥有上传图片权限的Nginx+PHP服务器,就有被入侵的可能。

  其实此漏洞并不是Nginx的漏洞,而是PHP PATH_INFO的漏洞,详见:http://bugs.php.net/bug.php?id=50852&edit=1

  例如用户上传了一张照片,访问地址为http://www.domain.com/images/test.jpg,而test.jpg文件内的内容实际上是PHP代码时,通过http://www.domain.com/images/test.jpg/abc.php就能够执行该文件内的PHP代码。

  网上提供的临时解决方法有:

  方法①、修改php.ini,设置cgi.fix_pathinfo = 0;然后重启php-cgi。此修改会影响到使用PATH_INFO伪静态的应用,例如我以前博文的URL:http://blog.zyan.cc/read.php/348.htm 就不能访问了。

  方法②、在nginx的配置文件添加如下内容后重启:if ( $fastcgi_script_name ~ \..*\/.*php ) {return 403;}。该匹配会影响类似 http://www.domain.com/software/5.0/test.php(5.0为目录),http://www.domain.com/goto.php/phpwind 的URL访问。

  方法③、对于存储图片的location{...},或虚拟主机server{...},只允许纯静态访问,不配置PHP访问。例如在金山逍遥网论坛、SNS上传的图片、附件,会传送到专门的图片、附件存储服务器集群上(pic.xoyo.com),这组服务器提供纯静态服务,无任何动态PHP配置。各大网站几乎全部进行了图片服务器分离,因此Nginx的此次漏洞对大型网站影响不大。



  本人再提供一种修改nginx.conf配置文件的临时解决方法,兼容“http://blog.zyan.cc/demo/0day/phpinfo.php/test”的PATH_INFO伪静态,拒绝“http://blog.zyan.cc/demo/0day/phpinfo.jpg/test.php”的漏洞攻击:
location ~* .*\.php($|/)
{
      if ($request_filename ~* (.*)\.php) {
            set $php_url $1;
      }
      if (!-e $php_url.php) {
            return 403;
      }

      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      include fcgi.conf;
}


  也可将以下内容写在fcgi.conf文件中,便于多个虚拟主机引用:
if ($request_filename ~* (.*)\.php) {
    set $php_url $1;
}
if (!-e $php_url.php) {
    return 403;
}

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $uri;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;




  附:文章修改历史

  ● [2010年05月21日] [Version 1.0] 新建

  ● [2010年05月23日] [Version 1.1] 针对网友michael提出的“如果构造一个形如/..trojan.jpg/dummy.php/?abcd=1,似乎可以绕过防范的nginx配置”,进行了配置修改,防范了此类情况发生。提供测试的URL如下,拒绝漏洞访问:
  http://blog.zyan.cc/demo/0day/phpinfo.jpg (里面是PHP代码)
  http://blog.zyan.cc/demo/0day/phpinfo.jpg/.php
  http://blog.zyan.cc/demo/0day/phpinfo.jpg/dummy.php
  http://blog.zyan.cc/demo/0day/phpinfo.jpg/dummy.php/?abcd=1

  同时兼容正常的PATH_INFO伪静态请求,测试URL如下:
  http://blog.zyan.cc/demo/0day/phpinfo.php (这是正常的PHP文件)
  http://blog.zyan.cc/demo/0day/phpinfo.php/test
  http://blog.zyan.cc/demo/0day/phpinfo.php/news123.html
  http://blog.zyan.cc/read.php/348.htm

  ● [2010年05月24日] [Version 1.2] 修正文字描述错误。


Tags: , ,



技术大类 » Web服务器 | 评论(82) | 引用(0) | 阅读(129675)
Yation Homepage
2010-7-12 05:40
张老师您好,这个 漏洞还是有情况下 可以执行
abc.jpg 是PHP可执行代码,5.php是文件夹。虽然情况比较少见,但还是隐患,求教。
http://127.0.0.1/5.php/abc.jpg
http://127.0.0.1/5.php/abc.jpg/?sdf
小菜鸟
2010-7-30 10:32
方法3如何实现?
白癜风 Homepage
2010-8-20 11:03
http://www.tjpf120.comuplook
502 Email Homepage
2010-9-11 17:08
按照你说的方法修改了,但发现有个问题,类似下面的url返回500错误

http://127.0.0.1/index.php?user/ajax/0.1231432

请问有解决办法吗
502
2010-9-15 11:05
原来是程序的问题
ali
2010-10-15 17:01
泡妞秘籍s Email Homepage
2011-2-12 22:00
菜鸟来学习了!!真是太好了!
最好的巧克力品牌 Homepage
2011-2-25 17:23
最好的巧克力品牌
<a href=http://www.seo5200.com>最好的巧克力品牌</a>
seo52002011353761593-2011.2.25
电视调制器
2011-3-14 19:52
电视调制器   谢谢分享
kakalong Email Homepage
2011-3-15 03:25
Yation (第三页第一行) 的问题我也发现了 应该把

if ($request_filename ~* (.*)\.php) {
    set $php_url $1;
}

这个改为
set $php_url "";
set $script $request_filename;
if ($script ~* (.*)\.php$) { // 这里加了$, 加了这个之后就不支持所谓的PATHINFO了, 这里要在rewrite 后面多一步处理
    set $php_url $1;
}
if ($php_url) {  // 这里是防止 http://s135.com/path/to/qq.jpg  执行 http://s135.com/path/to/.php(这个文件是可以存在的)
    return 403;
}
if (!-f $php_url.php) { // Yation的问题把e改为f 就可以解决,如果不考虑准确性,上面的$可以不要
   return 403;
}
// 以上处理之后 http://s135.com/path/to.php/plmm.jpg (path/to.php/plmm.jpg为存在的图片) 就访问不到了

// PATHINFO解决思路, 这个放在 location ~* .*\.php($|/) 上面
if (!-e $script) {
   rewrite ^/(.*)$ /index.php/$1 last;
   set $script $document_root/index.php;  //加粗斜杠应替换成  入口文件  相对站点目录的   路径
}
减肥瘦身易丽美 Homepage
2011-4-19 09:14
光哥
2011-5-5 17:51
$_SERVER["DOCUMENT_ROOT"] 错误,

始终显示/usr/local/nginx/html

不是配置的root路径
joypan
2011-5-6 02:27
ngix.conf
location ~  .php {        
           fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            fastcgi_param PATH_INFO $fastcgi_path_info;
}

php.ini
cgi.force_redirect = 0
cgi.fix_pathinfo=0

经测试可以正常支持path_info,同时又可以防止aa.jpg/aa.php之类的攻击。
只在本机测试,请多指教。
赵容 Email Homepage
2011-6-8 23:18
晕,真的是很危险啦,感谢高手!赶紧去修改!
厚工坊
2011-11-18 10:50
不懂啊  像我这种站要什么弄啊?如厚工坊
louis vuitton uk Email Homepage
2011-11-22 16:59
Louis Vuitton is one of the world's leading international fashion houses.The catching styles and upmarket qualities endeared louis vuitton uk to almost everyone.louis vuitton Store Online Handbags can also bring great accuracy as well as practical applicability and fashionable.
louis vuitton uk Email Homepage
2011-11-22 17:11
This louis vuitton uk for sale belongs to the sounding just what are termed as Louis Vuitton vintage best sellers, many other products and services for the reason that range appearing companies.You will easily notice the unfold zippers of this coach outlet store online. That is the decoration. There are some inside pockets for you as well. They are easy to match your clothes and to carry.Let us inspire your inner beauty with fine christian louboutin sale. Purse the elegance in bridal wedding. Enjoy the fashion.
armani watches Email Homepage
2011-11-23 10:12
If you are looking for armani Bags, our armani watches Handbags Canada outlet store is your first choice. We promise Original Packing and Best Discount,3-5 Workdays To Your Door!If you buy bags and purses of the latest new designs from the louis vuitton outlet now, you can enjoy special discounts. What else are you waiting for?After you choose the right kind of plants and the size pot for the plant.
nosun
2011-12-6 19:32
非常感谢大侠的创作
分页: 3/5 第一页 上页 1 2 3 4 5 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]