19

我有一个用户可以上传文件的目录。

为了避免安全问题(例如,有人上传了恶意 php 脚本),我目前通过附加来更改文件的扩展名.data,但是在下载文件时,他们必须手动删除.data.

另一种常见的解决方案是将文件上传到 Apache 不提供服务的目录中,并让一个 php 脚本通过调用来管理所有下载readfile()

我想做的是简单地禁止在上传文件夹中执行任何脚本(php、perl、cgi 脚本,无论我将来安装什么)。这个 SO answer.htaccess建议在该文件夹的文件中添加以下行:

SetHandler default-handler

但是,在我的情况下,这没有效果(我放在该文件夹中的示例 php 脚本仍然执行)。我究竟做错了什么?

阿帕奇配置

这台机器是一个正在运行的 VPS(虚拟专用服务器)Debian GNU/Linux 6.0.7 (squeeze),据我所知(我记下了我在该服务器上运行的所有命令,所以我的“记忆”应该非常准确),我在 apache2 中没有改变任何东西配置,从运行开始sudo apt-get install php5,并创建/etc/apache2/sites-enabled/mysite.com具有以下内容的文件:

<VirtualHost *:80>
  ServerAdmin webmaster@localhost
  ServerName  mysite.com
  ServerAlias www.mysite.com

  DocumentRoot /home/me/www/mysite.com/www/
  <Directory />
    Options FollowSymLinks
    AllowOverride All 
  </Directory>
  <Directory /home/me/www/mysite.com/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All 
    Order allow,deny
    allow from All
  </Directory>

  ErrorLog ${APACHE_LOG_DIR}/error.log

  # Possible values include: debug, info, notice, warn, error, crit,
  # alert, emerg.
  LogLevel warn

  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
4

2 回答 2

34

把它放在你的.htaccess

<Files *>
    # @mivk mentionned in the comments that this may break
    # directory indexes generated by Options +Indexes.
    SetHandler default-handler
</Files>

但这有一些安全漏洞:可以在子目录中上传 .htaccess,并覆盖这些设置,它们也可能会覆盖 .htaccess 文件本身!

如果您偏执地认为该选项的行为将来会发生变化,请将其放在您的/etc/apache2/sites-enabled/mysite.com

    <Directory /home/me/www/upload/>
            # Important for security, prevents someone from
            # uploading a malicious .htaccess
            AllowOverride None

            SetHandler none
            SetHandler default-handler

            Options -ExecCGI
            php_flag engine off
            RemoveHandler .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo
            <Files *>
                    AllowOverride None

                    SetHandler none
                    SetHandler default-handler

                    Options -ExecCGI
                    php_flag engine off
                    RemoveHandler .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo
            </Files>
    </Directory>

如果无法修改 apache 配置,则将文件放在.htaccess具有以下目录结构的目录中:

/home/me/www/
          |- myuploadscript.php
          |- protected/
              |- .htaccess
              |- upload/
                  |- Uploaded files go here

这样,没有人应该能够覆盖您的.../protected/.htaccess文件,因为他们的上传文件位于 的子目录中.../protected,而不是protected其本身。

AFAICT,你应该很安全。

于 2013-09-22T19:56:40.593 回答
2

我的 Godaddy 设置不允许我编辑 httpd.conf 文件,并且 php_flag 命令不起作用,因为他们为我实现了 php。

我能够在我的.htaccess文件中使用它:

SetHandler default-handler
AddType text/plain php

我把它放在上面允许我的 FTP 用户访问的目录中,这会强制该目录中的所有 PHP 文件以及所有子目录将 php 显示为纯文本。

这也适用于其他文件类型。您需要做的就是添加另一行,其中包含您希望强制以纯文本显示的任何文件扩展名。AddType text/plain cgi例如

于 2015-11-30T22:51:30.700 回答