Latest Posts »
Latest Comments »
Popular Posts »

nginx文件类型错误解析漏洞

Written by bixuan on 2010年05月21号 – 07:29

漏洞介绍: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
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.80sec.com/robots.txt/80sec.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的变化说明了后端负责解析的变化,该站点就可能存在漏洞。

漏洞厂商:http://www.nginx.org

解决方案:

我们已经尝试联系官方,但是此前你可以通过以下的方式来减少损失

关闭cgi.fix_pathinfo为0

或者

if ( $fastcgi_script_name ~ \..*\/.*php ) {
return 403;
}

本文来自:http://www.80sec.com/nginx-securit.html


Tags: , , ,
Posted in Nginx, application | No Comments »

spawn-fcgi

Written by bixuan on 2009年06月12号 – 10:38

spawn-fcgi

1.概况

spawn-fcgi官方:http://redmine.lighttpd.net/projects/spawn-fcgi/wiki,可以从这里下载到最新稳定版本。
lighttpd官方:http://www.lighttpd.net/
其实spawn-fcgi是lighttpd的一个分支项目,是一个cgi进程的管理器,效率不错。

2.安装

  1. 下载:
    wget -c http://www.lighttpd.net/download/spawn-fcgi-1.6.2.tar.bz2
    wget -c http://www.lighttpd.net/download/lighttpd-1.4.22.tar.bz2 #(spawn-php.sh这个文件需要从这里提取,具体在:lighttpd-1.4.22/doc/下)
  2. spawn-fcgi安装:
    tar jxvfp spawn-fcgi-1.6.2.tar.bz2
    cd spawn-fcgi-1.6.2
    ./configure
    make
    make install

3.配置

这里主要整理spawn-fcgi相关配置!
安装好后,可以通过:spawn-fcgi -h
Usage: spawn-fcgi [options] [-- <fcgiapp> [fcgi app arguments]]
spawn-fcgi v1.6.2 (ipv6) - spawns FastCGI processes
Options:
-f <path>      filename of the fcgi-application (ignored if <fcgiapp> is given)
-d <directory> chdir to directory before spawning
-a <address>   bind to IPv4/IPv6 address (defaults to 0.0.0.0)
-p <port>      bind to TCP-port
-s <path>      bind to Unix domain socket
-M <mode>      change Unix domain socket mode
-C <children>  (PHP only) numbers of childs to spawn (default: not setting
the PHP_FCGI_CHILDREN environment variable - PHP defaults to 0)
-F <children>  number of children to fork (default 1)
-P <path>      name of PID-file for spawned process (ignored in no-fork mode)
-n             no fork (for daemontools)
-v             show version
-?, -h         show this help
(root only)
-c <directory> chroot to directory
-S             create socket before chroot() (default is to create the socket in the chroot)
-u <user>      change to user-id
-g <group>     change to group-id (default: primary group of user if -u is given)
-U <user>      change Unix domain socket owner to user-id
-G <group>     change Unix domain socket group to group-id
看到详细的参数说明,不过这里为了方便,还是喜欢通过这个spawn-php.sh来管理php-cgi比较方便^_^
有个地方需要注意:spawn-php.sh这个文件有个bug,见下面红色字体部分:
# cat spawn-php.sh
#!/bin/bash
## ABSOLUTE path to the spawn-fcgi binary
SPAWNFCGI=”/usr/local/bin/spawn-fcgi
## ABSOLUTE path to the PHP binary
FCGIPROGRAM=”/app/app/php5-fastcgi/bin/php-cgi
## TCP port to which to bind on localhost
FCGIPORT=”9002″
## number of PHP children to spawn
PHP_FCGI_CHILDREN=10
## maximum number of requests a single PHP process can serve before it is restarted
PHP_FCGI_MAX_REQUESTS=1000
## IP addresses from which PHP should access server connections
FCGI_WEB_SERVER_ADDRS=”192.168.11.159
pripath=”/app/wwwroot/www.ourlinux.net”
# allowed environment variables, separated by spaces
ALLOWED_ENV=”pripath PATH USER”
## if this script is run as root, switch to the following user
USERID=nobody
GROUPID=nobody
################## no config below this line
if test x$PHP_FCGI_CHILDREN = x; then
PHP_FCGI_CHILDREN=5
fi
export PHP_FCGI_MAX_REQUESTS
export FCGI_WEB_SERVER_ADDRS
ALLOWED_ENV=”$ALLOWED_ENV PHP_FCGI_MAX_REQUESTS FCGI_WEB_SERVER_ADDRS”
if test x$UID = x0; then
EX=”$SPAWNFCGI -a $FCGI_WEB_SERVER_ADDRS -p $FCGIPORT -f $FCGIPROGRAM -u $USERID -g $GROUPID -C $PHP_FCGI_CHILDREN”
else
EX=”$SPAWNFCGI -a $FCGI_WEB_SERVER_ADDRS -p $FCGIPORT -f $FCGIPROGRAM -C $PHP_FCGI_CHILDREN”
fi
# copy the allowed environment variables
E=
for i in $ALLOWED_ENV; do
E=”$E $i=${!i}”
done
# clean the environment and set up a new one
env - $E $EX
以上红色部分是要自己补上去,不然默认监听在0.0.0.0,绿色字体部分是要根据具体情况做相应的修改!蓝色字体部分是根据自己的需求加的参数,意思是:php的代码放在pripath这个变量所在的目录下,这个是为了跨机器执行php-cgi,这样可以通过proxy来调用,可以提升cpu利用率(将php-cgi进程跑在cpu消耗低的机器上)

4.后记

如果结合lvs,nginx(lighttpd等L7的负载均衡软件)、memcahced等,这样就可以很方便的实现高可用、高性能的分布式系统:)

Tags: , , , ,
Posted in 运维小技巧 | No Comments »
 Page 1 of 1  1