我有一个用户可以上传文件的目录。
为了避免安全问题(例如,有人上传恶意的php脚本),例如,我目前通过附加.data
来更改文件的扩展名,但是在下载文件时,他们必须手动删除.data
。
另一种常见的解决方案是将文件上传到 Apache 未提供的目录中,并通过调用 php 脚本来管理所有下载 readfile()
。
我想做的是简单地禁止在上传文件夹中执行任何脚本(php,perl,cgi脚本,无论我将来可能安装什么)。此 SO 答案建议在该文件夹的.htaccess
文件中添加以下行:
SetHandler default-handler
但是,就我而言,这没有任何效果(我放入该文件夹中的示例 php 脚本仍在执行)。我做错了什么?
阿帕奇配置
这台机器是一个运行Debian GNU/Linux 6.0.7 (squeeze)
的VPS(虚拟专用服务器),据我所知(我记下了我在该服务器上运行的所有命令,所以我的"内存"应该非常准确),我没有在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>
把它放在你的.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,你应该很安全。
我的 Godaddy 设置不允许我编辑 httpd.conf 文件,并且 php_flag 命令不起作用,因为它们为我实现了 php。
我能够在我的.htaccess
文件中使用它:
SetHandler default-handler
AddType text/plain php
我把它放在上面允许我的FTP用户访问的目录中,这强制该目录中的所有PHP文件以及所有子目录将php显示为纯文本。
这也适用于其他文件类型。您需要做的就是添加另一行,其中包含您希望强制以纯文本显示的任何文件扩展名。 AddType text/plain cgi
例如