全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

IP归属甄别会员请立即修改密码
查看: 14980|回复: 42
打印 上一主题 下一主题

轻松解决nginx的php跨网站目录访问的安全问题

[复制链接]
跳转到指定楼层
1#
发表于 2009-7-14 22:42:56 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
非常简单,没有什么技术性,但这毕竟是网上公开的第一个解决方案,相信是有比较重要的意义的。

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

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.

现在解决办法来了

再看两个配置文件:

下面的这个 /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
        {
                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程序不能访问别的网站,而对自己网站的访问又充分不受限制。很简单,到此结束。

[ 本帖最后由 freebsd 于 2009-7-15 00:14 编辑 ]

评分

参与人数 1威望 +30 收起 理由
cpuer + 30 原创内容

查看全部评分

2#
 楼主| 发表于 2009-7-14 22:57:02 | 只看该作者
刚才都要的,现在全跑了,也没人吱一声
3#
发表于 2009-7-14 23:00:08 | 只看该作者

回复 1# 的帖子

大概原理已经明了,如果2个程序目录系统相似的话?
4#
发表于 2009-7-14 23:04:18 | 只看该作者
加分飘红讨论,
5#
 楼主| 发表于 2009-7-14 23:05:52 | 只看该作者
原帖由 cpuer 于 2009-7-14 23:00 发表
大概原理已经明了,如果2个程序目录系统相似的话?


两个网站是吧,目录相似又不要紧的

/myserver/subA1/subA2/subA3/subA4/subA5/host1/same/same/same/
/myserver/subB1/subB2/subB3/subB4/subB5/host2/same/same/same/

双方根本不会冲突的,两条独立的路径树,你配置下,用个木马访问试一下就知道了,我这里是测试成功的。

[ 本帖最后由 freebsd 于 2009-7-14 23:07 编辑 ]
6#
发表于 2009-7-14 23:07:30 | 只看该作者
原帖由 freebsd 于 2009-7-14 23:05 发表


两个网站是吧,目录相似又不要紧的

/myserver/subA1/subA2/subA3/subA4/subA5/host1/same/same/same/
/myserver/subB1/subB2/subB3/subB4/subB5/host2/same/same/same/

双方根本不会冲突的,两条独立的路径树,你配置 ...


我来实际测试下。
7#
 楼主| 发表于 2009-7-14 23:19:10 | 只看该作者
原帖由 cpuer 于 2009-7-14 23:07 发表


我来实际测试下。


嗯,有什么问题一起讨论。
8#
发表于 2009-7-14 23:23:17 | 只看该作者
原帖由 freebsd 于 2009-7-14 22:42 发表
当你运行或者引用了网站目录下的子目录(或者子目录的子目录....)里的php文件(假定为/myserver/host1/dir1/myphp.php),而这个子目录文件又要访问上级目录里的文件(/myserver/host1/config.php),这时候问题就来了

还没看完,暂时看到这里,发现问题

我把PHP文件放在网站的根目录,也不行。

我原来的问题是:我设置了host1.com  host2.com host3.com三个网站,只有host2.com能运行,其他网站根目录下(子目录没试过)的PHP都是No input file specified.

[ 本帖最后由 gdtv 于 2009-7-14 23:24 编辑 ]
9#
发表于 2009-7-14 23:29:13 | 只看该作者
我晕了,这个人好事艾米论坛的老大啊,是个女管理员。
10#
 楼主| 发表于 2009-7-14 23:29:34 | 只看该作者
原帖由 gdtv 于 2009-7-14 23:23 发表

还没看完,暂时看到这里,发现问题

我把PHP文件放在网站的根目录,也不行。

我原来的问题是:我设置了host1.com  host2.com host3.com三个网站,只有host2.com能运行,其他网站根目录下(子目录没试过)的PHP都是No input file  ...


配置贴下
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2025-12-21 00:31 , Processed in 0.198665 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表