andybest
V2EX  ›  问与答

有没办法禁止 PHP 列出 Linux 根目录?

  •  
  •   andybest · Feb 10, 2014 · 4521 views
    This topic created in 4651 days ago, the information mentioned may be changed or developed.
    Ubuntu Server 的根目录,系统安装完后大部分文件与目录权限为:drwxr-xr-x
    因为是任何人可读,导致 PHP 可以列出根目录并下载任何人可读的文件
    有没有可能限制PHP禁止读取根目录?
    12 replies  •  1970-01-01 08:00:00 +08:00
    lichao
    lichao
    Feb 10, 2014
    1
    这个应该去设置 Apache 什么的,跟 PHP 似乎没关系
    yangqi
    yangqi
    Feb 10, 2014❤️1
    2
    php禁止读取根目录了那还怎么执行目录下的php文件?
    andybest
    andybest
    OP
    Feb 10, 2014
    3
    @lichao 我的意思是,我自建一个用户用于run php,然后禁止这个php列出根目录的权限
    andybest
    andybest
    OP
    Feb 10, 2014
    4
    @yangqi 有道理。。。我想能否禁止列出比如 /etc/ 这样比较敏感的目录?
    lichao
    lichao
    Feb 10, 2014
    5
    @andybest 哦,我理解错了
    yangqi
    yangqi
    Feb 10, 2014
    6
    @andybest 默认php应该是运行在www-data之类的用户下面吧,这样的话应该是只能读取apache的www目录下的文件的
    andybest
    andybest
    OP
    Feb 10, 2014
    7
    @yangqi 不是的,只要权限为:drwxr-xr-x 的PHP都可以读取,最简单的验证方法是下一个php文件管理器,在里面改变目录为/后看看
    yangqi
    yangqi
    Feb 10, 2014
    8
    @andybest 好吧...不过这个要求很怪, 你是对你自己写的代码没有信心,没有安全感么?
    andybest
    andybest
    OP
    Feb 10, 2014
    9
    @yangqi 是对一些 PHP 程序不信任,做最坏的假设比如他就是个文件管理器(木马),让它运行在我服务器上仍然可以将其权限限制在其目录中,不会造成很大影响
    sarices
    sarices
    Feb 10, 2014❤️2
    10
    open_basedir
    andybest
    andybest
    OP
    Feb 10, 2014
    11
    @sarices 非常感谢,原来 PHP 已经有解决我这个问题的方案了,给你 32 个赞 :)
    wzzyj8
    wzzyj8
    Feb 10, 2014❤️1
    12
    open_basedir不够完全,单用这一个设置完全可以被绕过,网上绕过open_basedir的方法有很多。顺手应该禁用掉一些函数,比如:
    http://blog.robotshell.org/2013/php-disable-functions/
    http://www.cnblogs.com/chengmo/archive/2010/07/08/1773859.html

    如果是apache虚拟主机的话最好是用上suExec/suPHP之类的来进行隔离。

    如果是Nginx的话,可以参考这里:
    http://www.howtoforge.com/php-fpm-nginx-security-in-shared-hosting-environments-debian-ubuntu
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2892 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 12:05 · PVG 20:05 · LAX 04:05 · JFK 07:05
    ♥ Do have faith in what you're doing.