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程序不能访问别的网站,而对自己网站的访问又充分不受限制

转载请注明来自WebShell'S Blog,本文地址:https://www.webshell.cc/1774.html