Tagged: php-cgi RSS

  • bixuan 23:49 on 2011 年 03 月 29 日 链接地址 | 回复
    Tags: , , php-cgi   

    刚做了2个测试:

    1、nginx+php-cgi fpm
    100000 fetches, 100 max parallel, 2e+06 bytes, in 39.5206 seconds
    20 mean bytes/connection
    2530.33 fetches/sec, 50606.6 bytes/sec
    msecs/connect: 0.538099 mean, 3000.11 max, 0.106 min
    msecs/first-response: 38.9175 mean, 402.378 max, 0.313 min
    HTTP response codes:
    code 200 — 100000

    2、lighttpd+php-cgi fpm
    100000 fetches, 100 max parallel, 2e+06 bytes, in 15.6593 seconds
    20 mean bytes/connection
    6385.99 fetches/sec, 127720 bytes/sec
    msecs/connect: 3.78652 mean, 3001.08 max, 0.105 min
    msecs/first-response: 9.88036 mean, 229.16 max, 0.495 min
    HTTP response codes:
    code 200 — 100000

     
  • bixuan 10:57 on 2010 年 11 月 23 日 链接地址 | 回复
    Tags: , , php-cgi, php-dws   

    PHP-DWS

    1. 关于 php-dws

    php-dws 是 PHP Direct Web Server 的缩写, 是针对 php 的一个新型 sapi 工作模块,
    通过 dwsgi 协议与 web server (如 nginx) 协同工作.

    之所以叫 Direct , 是因为它在执行 php 脚本过程中是直接把输出结果传递给 http client 的
    而不是转交 webserver(nginx) 再由 webserver 发送给 http client.

    在多数情况下, php-dws 可以用于取代 php/fastcgi 的工作, 并且能更出色的完成.

    2. 安装与配置

    相对比较复杂,包括 NGINX 模块和 PHP的 sapi 模块2个部分,详细进入我的论坛查看吧

    http://www.hightman.cn/bbs/forumdisplay.php?fid=15

    3. 后话
    php-dws 还是一个实验性的产品,不推荐运行在生产环境,由于其另类的方式在某些WEB应用的
    场合可能有需求。php-dws 的特别应用示例
    或点击查看这个刷新显示效果:http://root.twomice.net/dws-test/sleep.phpd

    From: http://www.hightman.cn/bbs/forumdisplay.php?fid=15

    关于这个设计思路,感觉挺有意思,作者加油,关注下:)

     
  • bixuan 07:29 on 2010 年 05 月 21 日 链接地址 | 回复
    Tags: , , php-cgi,   

    nginx文件类型错误解析漏洞 

    漏洞介绍: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

     
  • bixuan 10:38 on 2009 年 06 月 12 日 链接地址 | 回复
    Tags: , php-cgi, , spawn-fcgi, spawn-php.sh   

    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等,这样就可以很方便的实现高可用、高性能的分布式系统:)
     
c
写新的
j
下一篇文章/下一个回复
k
前一篇文章/以前的回复
r
回复
e
编辑
o
显示/隐藏 回复
t
回到顶部
l
go to login
h
show/hide help
esc
取消