andybest
V2EX  ›  问与答

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

  •  
  •   andybest · Feb 10, 2014 · 4148 views
    This topic created in 4479 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
        1
    lichao  
       Feb 10, 2014
    这个应该去设置 Apache 什么的,跟 PHP 似乎没关系
    yangqi
        2
    yangqi  
       Feb 10, 2014   ❤️ 1
    php禁止读取根目录了那还怎么执行目录下的php文件?
    andybest
        3
    andybest  
    OP
       Feb 10, 2014
    @lichao 我的意思是,我自建一个用户用于run php,然后禁止这个php列出根目录的权限
    andybest
        4
    andybest  
    OP
       Feb 10, 2014
    @yangqi 有道理。。。我想能否禁止列出比如 /etc/ 这样比较敏感的目录?
    lichao
        5
    lichao  
       Feb 10, 2014
    @andybest 哦,我理解错了
    yangqi
        6
    yangqi  
       Feb 10, 2014
    @andybest 默认php应该是运行在www-data之类的用户下面吧,这样的话应该是只能读取apache的www目录下的文件的
    andybest
        7
    andybest  
    OP
       Feb 10, 2014
    @yangqi 不是的,只要权限为:drwxr-xr-x 的PHP都可以读取,最简单的验证方法是下一个php文件管理器,在里面改变目录为/后看看
    yangqi
        8
    yangqi  
       Feb 10, 2014
    @andybest 好吧...不过这个要求很怪, 你是对你自己写的代码没有信心,没有安全感么?
    andybest
        9
    andybest  
    OP
       Feb 10, 2014
    @yangqi 是对一些 PHP 程序不信任,做最坏的假设比如他就是个文件管理器(木马),让它运行在我服务器上仍然可以将其权限限制在其目录中,不会造成很大影响
    sarices
        10
    sarices  
       Feb 10, 2014   ❤️ 2
    open_basedir
    andybest
        11
    andybest  
    OP
       Feb 10, 2014
    @sarices 非常感谢,原来 PHP 已经有解决我这个问题的方案了,给你 32 个赞 :)
    wzzyj8
        12
    wzzyj8  
       Feb 10, 2014   ❤️ 1
    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   ·   Solana   ·   3106 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 03:20 · PVG 11:20 · LAX 20:20 · JFK 23:20
    ♥ Do have faith in what you're doing.