gpt4 book ai didi

apache - 使用.htaccess禁用目录的所有CGI(php,perl等)

转载 作者:行者123 更新时间:2023-12-03 13:23:56 24 4
gpt4 key购买 nike

我有一个目录,用户可以在其中上传文件。

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

另一个常见的解决方案是将文件上载到Apache不在的目录中,并通过调用readfile()上载到have a php script manage all downloads

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

SetHandler default-handler


但是,就我而言,这没有任何效果(放置在该文件夹中的示例php脚本仍在执行)。我究竟做错了什么?

Apache配置

该机器是运行 Debian GNU/Linux 6.0.7 (squeeze)的VPS(虚拟专用服务器),据我所记得(我记下了在该服务器上运行的所有命令,因此我的“内存”应该非常准确),我没有改变apache2配置中的任何内容,请从运行 sudo apt-get install php5并通过以下内容创建文件 /etc/apache2/sites-enabled/mysite.com的appart:

<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,您应该对此非常安全。

关于apache - 使用.htaccess禁用目录的所有CGI(php,perl等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18932756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com