全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

IP归属甄别会员请立即修改密码
查看: 14981|回复: 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 原创内容

查看全部评分

43#
发表于 2009-7-15 21:03:34 | 只看该作者
原帖由 cpuer 于 2009-7-15 20:35 发表


有解决办法了没

没有。                           
42#
发表于 2009-7-15 20:35:02 | 只看该作者
原帖由 gdtv 于 2009-7-15 20:31 发表

我本来不太确定是apache还是mysql的问题,既然也你说了,我就确定是mysql的问题了


有解决办法了没
41#
发表于 2009-7-15 20:31:11 | 只看该作者
原帖由 cpuer 于 2009-7-15 19:50 发表


嗯,发现了,我装了个文本的论坛,速度也很好,奇怪,

我本来不太确定是apache还是mysql的问题,既然也你说了,我就确定是mysql的问题了
40#
发表于 2009-7-15 19:50:44 | 只看该作者
原帖由 gdtv 于 2009-7-15 19:26 发表
好像不是apache问题,是mysql问题


嗯,发现了,我装了个文本的论坛,速度也很好,奇怪,
39#
发表于 2009-7-15 19:26:44 | 只看该作者

回复 38# 的帖子

好像不是apache问题,是mysql问题
38#
发表于 2009-7-15 18:42:49 | 只看该作者
原帖由 gdtv 于 2009-7-15 18:34 发表
我重装了系统,装lxadmin,现在速度比ngnix慢了几十倍,郁闷


PhotonVPS 的OpenVZ装Apache感觉效率不好,不知道是不是他们配置的架构有问题。
XEN的未知。
37#
发表于 2009-7-15 18:34:32 | 只看该作者
我重装了系统,装lxadmin,现在速度比ngnix慢了几十倍,郁闷
36#
 楼主| 发表于 2009-7-15 13:54:22 | 只看该作者
原帖由 gdtv 于 2009-7-15 12:03 发表
你是怎么安装ngnix的?哪里有你的教程?我按照你的试试


和你看的应该是一样的,我看的是以前的教程版本,那个新浪工程师的,网上流传很广的那个,加了一些我自己的改动而已。后面的教程版本我就没怎么看了。

[ 本帖最后由 freebsd 于 2009-7-15 13:55 编辑 ]
35#
 楼主| 发表于 2009-7-15 13:53:21 | 只看该作者
原帖由 gdtv 于 2009-7-15 11:56 发表
是复制

复制为什么读的还是原来的目录呢?


有可能没配置好,程序读的还是原来的目录

[ 本帖最后由 freebsd 于 2009-7-15 13:54 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-12-21 22:08 , Processed in 0.107365 second(s), 15 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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