当前位置:七道奇文章资讯系统安全Linux安全
日期:2011-10-09 15:35:00  来源:本站整理

关于Nginx+PHP的虚拟主机目录权限掌握的根究[Linux安全]

赞助商链接



  本文“关于Nginx+PHP的虚拟主机目录权限掌握的根究[Linux安全]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

Nginx的利用者近来越来越多,很多大型网站也都从Apache或其他平台迁移到了Nginx.但在我利用Nginx的历程中有个问题一向未得到办理,就是若何限制Nginx+PHP的目录权限

我们知道,在Apache中可以很简单的对虚拟目录举行权限掌握,如:

<VirtualHost www.xpb.cn>

ServerAdmin xiaopb@live.com

DocumentRoot /usr/www/xpb/

ServerName www.xpb.cn:80

ServerAlias www.xpb.cn

ErrorLog logs/default-error_log

php_admin_value open_basedir "/tmp/:/usr/www/xpb/"

</VirtualHost>

关键是背面的这句php_admin_value,这样就限制了php的操作目录仅限于/tmp/和/usr/www/xpb/这两个目录了.关于Apache虚拟主机来说,这个设置非常有效,结合在php.ini中禁用一些php函数,几近可以根绝PHP木马对其他站点及系统的危害.我虽没专业做过Linux下的虚拟主机,但相信各大虚拟主机商也是这么做的.

看来关于Apache最好的办法还是利用"在php.ini中禁用一些危险的php函数和在Apache虚拟主机中配置php_admin_value"的方法来做虚拟主机的安全.

关于Nginx的配置文件,参考了很多资料,仿佛是不支持php_admin_value open_basedir,也就是Nginx暂时还没有Apache的php_myadmin_value这类的设置.假如用Nginx做虚拟主机,各用户之间的目录安全掌握若何来做呢?网上很多人说,限制上传文件范例,做好程序安全不就行了么?对,关于自己的站点来说这样完好可以.但假如虚拟主机是给别人用的,又赐与了FTP权限,总不能不让人上传php 文件吧.参考以上,假如用Nginx来做虚拟主机,目前看来安全的配置办法是:

、用低权限账号运行Nginx.

2、在php.ini中禁用危险的函数.如:system,passthru,shell_exec,exec,popen,proc_open,chroot,scandir,chgrp,chown等,但禁止太多的函数大概对某些php程序的正常运行产生影响.

3、在php.ini中设置open_basedir,如:open_basedir = "/usr/local/webserver/nginx /html/www.xpb.cn_7da347bc1a9fd621/:/usr/local/webserver/nginx/html/www2.xpb.cn_7da347bc1a9fd621/"

4、各个虚拟主机用户放在不易于猜到的目录,如:www.xpb.cn_7da347bc1a9fd621、www2.xpb.cn_7da347bc1a9fd621

5、自己找一个php木马,自我测试服务器安全!

6 在运行spawn-fcgi 的时刻带上参数-d open_basedir 便可,比方:/usr /sbin/spawn-fcgi -a 127.0.0.1 -p 10080 -C 20 -u www -f "/usr/sbin/php-cgi -d open_basedir=/var/www/wwwroot/:/tmp/"

7 先来看两份配置文件的部份,只跟大家讲原理,省略了和主题无关的部份,请勿复制就用,懂得了原理,就知道该怎么做了.

php.ini

; open_basedir, if set, limits all file operations to the defined directory

; and below. This directive makes most sense if used in a per-directory

; or per-virtualhost web server configuration file. This directive is

; *NOT* affected by whether Safe Mode is turned On or Off.

open_basedir = "/myserver/:/tmp/:/var/tmp/"

nginx.conf

http

{

server

{

listen 80;

server_name host1.com;

root /myserver/host1;

location ~ .*/.(php|php5)?$

{

#fastcgi_pass unix:/tmp/php-cgi.sock;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fcgi.conf;

}

}

server

{

listen 80;

server_name host2.com;
root /myserver/host2;

location ~ .*/.(php|php5)?$

{

#fastcgi_pass unix:/tmp/php-cgi.sock;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fcgi.conf;

}

}

server

{

listen 80;

server_name host3.com;

root /myserver/host3;

location ~ .*/.(php|php5)?$

{

#fastcgi_pass unix:/tmp/php-cgi.sock;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fcgi.conf;

}

}

}

配置的基本情形是 运行3个网站host1.com host2.com host3.com ,php.ini的配置,限制php脚本只能在这三个网站目录的父目录/myserver/ 下面履行.

这时刻我们知道,假如在某一个站点上上传了一个php木马,那么这个木马将可以拜候其他两个站点的文件.nignx 没有apache那样可以单独设置每个网站的php只能在本目录下拜候的功效.这时刻我们就要用一点取巧的办法了.

来看这个php.ini的配置.

open_basedir = "/myserver/:/tmp/:/var/tmp/"

其实这个途径也支持(.) [一个点] 和(..) [两个点],也就是当前目录、父目录.于是有下面的配置办法

open_basedir = ".:/tmp/:/var/tmp/" 把php文件限制在当前目录,的确,这样确切是拜候不到别的两个网站的目录了,但是拜候某些页面会呈现No input file specified. .

为什么呢,因为上面的这个限制,当你运行大概引用了网站目录下的子目录(大概子目录的子目录....)里的php文件(假定为/myserver /host1/dir1/myphp.php),而这个子目录文件又要拜候上级目录里的文件(/myserver/host1/config.php),这时刻问题就来了,php.ini里设置了myphp.php只能拜候该本级目录(/myserver/host1/dir1/)以下的文件,而不能拜候/myserver/host1下的直接文件,于是提醒:No input file specified.

目前办理办法来了:lol

再看两个配置文件:

下面的这个/subX1/subX2/subX3/..........(N层) ,N为你网站上最底层的php文件嵌套级数,假如你网站最多有5级子目录下有php文件,那么就嵌套5层以上.

php.ini

; open_basedir, if set, limits all file operations to the defined directory

; and below. This directive makes most sense if used in a per-directory

; or per-virtualhost web server configuration file. This directive is

; *NOT* affected by whether Safe Mode is turned On or Off.

open_basedir = "../../.......(N层):/tmp/:/var/tmp/"

nginx.conf

http

{

server

{

listen 80;
server_name host1.com;

root /myserver/subA1/subA2/subA3/..........(N层)/host1;

location ~ .*/.(php|php5)?$

{

#fastcgi_pass unix:/tmp/php-cgi.sock;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fcgi.conf;

}

}

server

{

listen 80;

server_name host2.com;

root /myserver/subB1/subB2/subB3/..........(N层)/host2;

location ~ .*/.(php|php5)?$

{

#fastcgi_pass unix:/tmp/php-cgi.sock;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fcgi.conf;

}

}

server www.2cto.com

{

listen 80;

server_name host3.com;

root /myserver/subC1/subC2/subC3/..........(N层)/host3;

location ~ .*/.(php|php5)?$

{

#fastcgi_pass unix:/tmp/php-cgi.sock;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fcgi.conf;

}

}

}

举例N等于5....运行,当拜候最底层的php文件/myserver/subA1/subA2/subA3/subA4/subA5/host1 /dir1/dir2/dir3/dir4/myphp.php,这个php文件所能拜候的上级层到/myserver/subA1/subA2 /subA3/subA4/subA5/host1,当拜候/myserver/subA1/subA2/subA3/subA4/subA5 /host1/myphp2.php 文件时刻,它所能最多拜候到的上级层/myserver/subA1 ,不能跃出拜候到其他站目录里的文件

这样就限制了该站目录下的php程序不能拜候别的网站,而对自己网站的拜候又充分不受限制   以上是“关于Nginx+PHP的虚拟主机目录权限掌握的根究[Linux安全]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:

  • 关于Nginx+PHP的虚拟主机目录权限掌握的根究
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .