<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Licess&#039;s Blog &#187; 漏洞</title>
	<atom:link href="http://blog.licess.org/tag/%e6%bc%8f%e6%b4%9e/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.licess.org</link>
	<description>关注VPS Linux Nginx MySQL PHP WEB开发 系统管理 服务器架构 Debian CentOS</description>
	<lastBuildDate>Wed, 11 Jan 2012 11:56:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>nginx文件类型错误解析漏洞</title>
		<link>http://blog.licess.org/nginx-securit-script_filename/</link>
		<comments>http://blog.licess.org/nginx-securit-script_filename/#comments</comments>
		<pubDate>Fri, 21 May 2010 05:01:37 +0000</pubDate>
		<dc:creator>licess</dc:creator>
				<category><![CDATA[Nginx]]></category>
		<category><![CDATA[LNMP]]></category>
		<category><![CDATA[漏洞]]></category>

		<guid isPermaLink="false">http://blog.licess.org/?p=805</guid>
		<description><![CDATA[漏洞介绍：nginx是一款高性能的web服务器，使用非常广泛，其不仅经常被用作反向代理，也可以非常好的支持PHP的运行。80sec发现 其中存在一个较为严重的安全问题，默认情况下可能导致服务器错误的将任何类型的文件以PHP的方式进行解析，这将导致严重的安全问题，使得恶意的攻击者可 能攻陷支持php的nginx服务器。 漏洞分析：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.80sec.com/80sec.jpg，我们以如下的方式去访问 http://www.80sec.com/80sec.jpg/80sec.php 将会得到一个URI /80sec.jpg/80sec.php 经过location指令，该请求将会交给后端的fastcgi处理，nginx为其设置环境变量SCRIPT_FILENAME，内容为 /scripts/80sec.jpg/80sec.php 而在其他的webserver如lighttpd当中，我们发现其中的SCRIPT_FILENAME被正确的设置为 /scripts/80sec.jpg 所以不存在此问题。 后端的fastcgi在接受到该选项时，会根据fix_pathinfo配置决定是否对SCRIPT_FILENAME进行额外的处理，一般情况下如果不 对fix_pathinfo进行设置将影响使用PATH_INFO进行路由选择的应用，所以该选项一般配置开启。Php通过该选项之后将查找其中真正的脚 本文件名字，查找的方式也是查看文件是否存在，这个时候将分离出SCRIPT_FILENAME和PATH_INFO分别为 /scripts/80sec.jpg和80sec.php 最后，以/scripts/80sec.jpg作为此次请求需要执行的脚本，攻击者就可以实现让nginx以php来解析任何类型的文件了。 POC： 访问一个nginx来支持php的站点，在一个任何资源的文件如robots.txt后面加上/80sec.php，这个时候你可以看到如下的区别： 访问http://www.80sec.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 [...]]]></description>
			<content:encoded><![CDATA[<p>漏洞介绍：nginx是一款高性能的web服务器，使用非常广泛，其不仅经常被用作反向代理，也可以非常好的支持PHP的运行。80sec发现  其中存在一个较为严重的安全问题，默认情况下可能导致服务器错误的将任何类型的文件以PHP的方式进行解析，这将导致严重的安全问题，使得恶意的攻击者可  能攻陷支持php的nginx服务器。<br />
漏洞分析：nginx默认以cgi的方式支持php的运行，譬如在配置文件当中可以以</p>
<p><code><br />
location ~ \.php$ {<br />
root           html;<br />
fastcgi_pass   127.0.0.1:9000;<br />
fastcgi_index  index.php;<br />
fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;<br />
include        fastcgi_params;<br />
}<span id="more-805"></span><br />
</code><br />
的方式支持对php的解析，location对请求进行选择的时候会使用URI环境变量进行选择，其中传递到后端Fastcgi的关键变量  SCRIPT_FILENAME由nginx生成的$fastcgi_script_name决定，而通过分析可以看  到$fastcgi_script_name是直接由URI环境变量控制的，这里就是产生问题的点。而为了较好的支持PATH_INFO的提取，在PHP  的配置选项里存在cgi.fix_pathinfo选项，其目的是为了从SCRIPT_FILENAME里取出真正的脚本名。<br />
那么假设存在一个http://www.80sec.com/80sec.jpg，我们以如下的方式去访问</p>
<p>http://www.80sec.com/80sec.jpg/80sec.php</p>
<p>将会得到一个URI<br />
<code><br />
/80sec.jpg/80sec.php<br />
</code><br />
经过location指令，该请求将会交给后端的fastcgi处理，nginx为其设置环境变量SCRIPT_FILENAME，内容为<br />
<code><br />
/scripts/80sec.jpg/80sec.php<br />
</code><br />
而在其他的webserver如lighttpd当中，我们发现其中的SCRIPT_FILENAME被正确的设置为<br />
<code><br />
/scripts/80sec.jpg<br />
</code><br />
所以不存在此问题。<br />
后端的fastcgi在接受到该选项时，会根据fix_pathinfo配置决定是否对SCRIPT_FILENAME进行额外的处理，一般情况下如果不   对fix_pathinfo进行设置将影响使用PATH_INFO进行路由选择的应用，所以该选项一般配置开启。Php通过该选项之后将查找其中真正的脚  本文件名字，查找的方式也是查看文件是否存在，这个时候将分离出SCRIPT_FILENAME和PATH_INFO分别为<br />
<code><br />
/scripts/80sec.jpg和80sec.php<br />
</code><br />
最后，以/scripts/80sec.jpg作为此次请求需要执行的脚本，攻击者就可以实现让nginx以php来解析任何类型的文件了。</p>
<p>POC：	  访问一个nginx来支持php的站点，在一个任何资源的文件如robots.txt后面加上/80sec.php，这个时候你可以看到如下的区别：</p>
<p>访问http://www.80sec.com/robots.txt<br />
<code><br />
HTTP/1.1 200 OK<br />
Server: nginx/0.6.32<br />
Date: Thu, 20 May 2010 10:05:30 GMT<br />
Content-Type: text/plain<br />
Content-Length: 18<br />
Last-Modified: Thu, 20 May 2010 06:26:34 GMT<br />
Connection: keep-alive<br />
Keep-Alive: timeout=20<br />
Accept-Ranges: bytes<br />
</code><br />
访问访问http://www.80sec.com/robots.txt/80sec.php<br />
<code><br />
HTTP/1.1 200 OK<br />
Server: nginx/0.6.32<br />
Date: Thu, 20 May 2010 10:06:49 GMT<br />
Content-Type: text/html<br />
Transfer-Encoding: chunked<br />
Connection: keep-alive<br />
Keep-Alive: timeout=20<br />
X-Powered-By: PHP/5.2.6<br />
</code><br />
其中的Content-Type的变化说明了后端负责解析的变化，该站点就可能存在漏洞。</p>
<p>漏洞厂商：http://www.nginx.org</p>
<p>解决方案：</p>
<p>我们已经尝试联系官方，但是此前你可以通过以下的方式来减少损失</p>
<p><code><br />
解决方案1：修改/usr/local/php/etc/php.ini将cgi.fix_pathinfo设为0 （注：前面可能有注释符号;  需要删除掉。），执行/usr/local/php/sbin/php-fpm restart重启。</code></p>
<p><a href="http://lnmp.org/" target="_blank">lnmp一键安装包</a>用户可以直接执行命 令：sed -i 's/; cgi.fix_pathinfo=0/cgi.fix_pathinfo=0/g'  /usr/local/php/etc/php.ini 再执行：<code>/usr/local/php/sbin/php-fpm  restart重启即可修复完成。</code></p>
<p>解决方案2：为nginx虚拟主机添加如下内容：<br />
<code><br />
if  ( $fastcgi_script_name ~ \..*\/.*php )  {<br />
return 403;<br />
}<br />
</code></p>
<p>PS: 鸣谢<a href="http://www.laruence.com/2010/05/20/1495.html">laruence</a>大  牛在分析过程中给的帮助</p>
<p>转载自：<a title="nginx文件类型错误解析漏洞" href="http://www.80sec.com/nginx-securit.html">http://www.80sec.com/nginx-securit.html</a></p>
<h2  class="related_post_title">相关文章：</h2><ul class="related_post"><li><a href="http://blog.licess.org/lnmp-upgrade-any-nginx-version/" title="LNMP一键安装包 自动无缝升级Nginx至任意版本">LNMP一键安装包 自动无缝升级Nginx至任意版本</a></li><li><a href="http://blog.licess.org/lnmp-v0-3/" title="LNMP &#8211; CentOS/RadHat/Debian/Ubuntu下Nginx+PHP+MySQL一键安装包V0.3发布">LNMP &#8211; CentOS/RadHat/Debian/Ubuntu下Nginx+PHP+MySQL一键安装包V0.3发布</a></li><li><a href="http://blog.licess.org/lnmp-debian-ubuntu/" title="LNMP Debian/Ubuntu下Nginx+MySQL+PHP+phpMyAdmin+Zend一键安装包">LNMP Debian/Ubuntu下Nginx+MySQL+PHP+phpMyAdmin+Zend一键安装包</a></li><li><a href="http://blog.licess.org/lnmp/" title="LNMP-Linux下Nginx+MySQL+PHP+phpMyAdmin+eAcelerator一键安装包(2011年12月14日更新)">LNMP-Linux下Nginx+MySQL+PHP+phpMyAdmin+eAcelerator一键安装包(2011年12月14日更新)</a></li><li><a href="http://blog.licess.org/lnmp-v0-8/" title="LNMP一键安装包 V0.8发布">LNMP一键安装包 V0.8发布</a></li><li><a href="http://blog.licess.org/lnmp-0-7-lnmpa/" title="LNMP一键安装包 0.7发布 &#8211; 支持LNMPA">LNMP一键安装包 0.7发布 &#8211; 支持LNMPA</a></li><li><a href="http://blog.licess.org/lnmp-screen/" title="安装lnmp前请先运行screen">安装lnmp前请先运行screen</a></li><li><a href="http://blog.licess.org/nginx-nginx-s-reload/" title="Nginx 新的重载方式 (nginx -s reload)">Nginx 新的重载方式 (nginx -s reload)</a></li><li><a href="http://blog.licess.org/lnmp-0-5/" title="LNMP一键安装包 0.5 正式发布">LNMP一键安装包 0.5 正式发布</a></li><li><a href="http://blog.licess.org/lnmp-install-php-extensions/" title="lnmp之安装PHP模块(不需要重装PHP)">lnmp之安装PHP模块(不需要重装PHP)</a></li></ul><hr />
<p><small>© licess for <a href="http://blog.licess.org">Licess&#039;s Blog</a>, 2010. |
<a href="http://blog.licess.org/nginx-securit-script_filename/">Permalink</a> |
<a href="http://blog.licess.org/nginx-securit-script_filename/#comments">2 comments</a> |
Add to
<a href="http://del.icio.us/post?url=http://blog.licess.org/nginx-securit-script_filename/&title=nginx文件类型错误解析漏洞">del.icio.us</a>
<br/>
Post tags: <a href="http://blog.licess.org/tag/lnmp/" rel="tag">LNMP</a>, <a href="http://blog.licess.org/tag/nginx/" rel="tag">Nginx</a>, <a href="http://blog.licess.org/tag/%e6%bc%8f%e6%b4%9e/" rel="tag">漏洞</a><br/>
<br/>
<img src="http://www.vpser.net/images/tuijian.gif"> <a href="http://www.vpser.net/usa-vps/">美国VPS推荐</a>  | <a href="http://shop63846532.taobao.com/">军哥代购 - 提供美国及海外VPS/VPN/域名代购，美元/欧元代付</a> QQ：503228080
</small></p>]]></content:encoded>
			<wfw:commentRss>http://blog.licess.org/nginx-securit-script_filename/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

