nginx文件类型错误解析漏洞

来源:互联网 发布:sql trigger insert 编辑:程序博客网 时间:2024/05/21 11:26

漏洞介绍:Nginx ("engine x") 是一个高性能的 HTTP和反向代理服务器,Nginx作为WEB服务器可以处理静态文件,索引文件以及自动索引,能够使用缓存加速反向代理,并提供简单的负载均衡及容错、模块化的架构等功能。

PHP CGI 中 fix_pathinfo 引起的解析漏洞分析

这个安全问题最早被人所了解是通过国内安全组织80sec的一篇文章《nginx文件类型错误解析漏洞》,文章指出当Nginx配置FastCGI使用PHP时,会将诸如http://www.test.com/test.jpg/anything.php 这样的请求,把test.jpg当作PHP文件来进行解析,而anything.php这个文件并不存在。实际上这并不是一个Nginx的漏洞,而是PHP5默认配置的缺陷造成的。

示例:


问题的本质是什么呢?
比如, 下面的Nginx conf(Nginx配置文件):

location ~ \.php($|/) {     fastcgi_pass   127.0.0.1:9000;     fastcgi_index  index.php;     set $script    $uri;     set $path_info "";     if ($uri ~ "^(.+\.php)(/.*)") {          set  $script     $1;          set  $path_info  $2;     }     include       fastcgi_params;     fastcgi_param SCRIPT_FILENAME   $document_root$script;     fastcgi_param SCRIPT_NAME       $script;     fastcgi_param PATH_INFO         $path_info;}
当我们发出http://www.test.com/test.jpg/anything.php这样的请求时,通过“^(.+\.php)(/.*)”这段正则匹配后,SCRIPT_NAME会被设置为“test.jpg/anything.php”,继而构造成SCRIPT_FILENAME传递给整个PHP CGI,但是PHP又为什么会接受这样的参数,并且把test.jpg解析了呢?问题就在于PHP的CGI SAPI中的参数cgi.fix_pathinfo这个参数了。
关于cgi.fix_pathinfo这个参数的描述:
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI.  PHP's; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok; what PATH_INFO is.  For more information on PATH_INFO, see the cgi specs.  Setting; this to 1 will cause PHP CGI to fix it's paths to conform to the spec.  A setting; of zero causes PHP to behave as before.  Default is 1.  You should fix your scripts; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.cgi.fix_pathinfo=1      上述描述了默认情况cgi.fix_pathinfo的值为1,那么如果开启了这个选项,就会触发在PHP中的如下逻辑:/* * if the file doesn't exist, try to extract PATH_INFO out * of it by stat'ing back through the '/' * this fixes url's like /info.php/test */if (script_path_translated &&     (script_path_translated_len = strlen(script_path_translated)) > 0 &&     (script_path_translated[script_path_translated_len-1] == '/' ||....//以下省略

PHP CGI 以 / 为分隔符号从后向前依次检查路径,当检测到test.jpg/anything.php时,PHP会认为SCRIPT_FILENAME是test.jpg, 而anything.php是PATH_INFO, 然后PHP就把test.jpg当作一个PHP文件来解释执行。

在很多使用 php-fpm (<0.6) 的主机中也会出现这个问题,但新的 php-fpm 的已经关闭了 cgi.fix_pathinfo。

提示:可使用copy命令制作图片木马,如copy a.jpg/b+a.txt/a b.gif

本文摘自:合天网安实验室

0 0
原创粉丝点击