Har*_*hal 5 php security .htaccess upload file
我正在执行一项使 PHP 文件上传安全的任务,我的客户希望遵循网站http://www.acunetix.com/websitesecurity/upload-forms-threat.htm上提到的指南
我们能够遵循本网站上提到的所有准则接受 htaccess 规则。他们提到要做以下事情。
Define a .htaccess file that will only allow access to files with allowed extensions.
Do not place the .htaccess file in the same directory where the uploaded files will be stored. It should be placed in the parent directory.
A typical .htaccess which allows only gif, jpg, jpeg and png files should include the following (adapt it for your own need). This will also prevent double extension attacks.
deny from all
<Files ~ "^\w+\.(gif|jpe?g|png)$">
order deny,allow
allow from all
</Files>
If possible, upload the files in a directory outside the server root.
Prevent overwriting of existing files (to prevent the .htaccess overwrite attack).
Create a list of accepted mime-types (map extensions from these mime types).
Generate a random file name and add the previously generated extension.
Don’t rely on client-side validation only, since it is not enough. Ideally one should have both server-side and client-side validation implemented.
Run Code Online (Sandbox Code Playgroud)
这样就不会执行除 gif、jpg 和 png 之外的其他文件。但是他们不希望 htaccess 位于我们上传图像的文件夹中,因为该文件夹具有权限并且可以覆盖 htaccess。所以他们告诉将文件保持在根级别。
我想知道,如果我们将文件保留在根级别并只允许执行图像类型,那么我的 php 脚本将如何执行?当我在根级别添加这个 htaccess 时,当然我的 php 脚本不起作用并返回权限错误。努力完成这项工作。
你能帮我让这个工作或任何其他有效的方法来做这个安全检查。我们不想在系统上留下安全漏洞。
任何帮助将不胜感激。
谢谢你们。
代码可以编码到图像文件中,因此您应该禁用此目录的 PHP 引擎:
<IfModule mod_php5.c>
php_flag engine off
</IfModule>
Run Code Online (Sandbox Code Playgroud)
或者,如果您无法在 .htaccess 文件中进行设置,则可以在 httpd.conf 或 vhost conf 文件中进行设置:
<VirtualHost *:80>
# ...
<Directory /path/to/webroot/path/to/upload/folder>
deny from all
<Files ~ "^\w+\.(gif|jpe?g|png)$">
order deny,allow
allow from all
</Files>
<IfModule mod_php5.c>
php_flag engine off
</IfModule>
</Directory>
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)