Disable all CGI (php, perl, …) for a directory using .htaccess

前端 未结 2 1431
不思量自难忘°
不思量自难忘° 2020-12-08 16:09

I have a directory where users can upload files.

To avoid security issues (e.g. somebody uploading a malicious php script), I currently change the files\' extension

2条回答
  •  悲哀的现实
    2020-12-08 16:19

    Put this in your .htaccess:

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

    But this has a few security holes: one can upload a .htaccess in a subdirectory, and override these settings, and they might also overwrite the .htaccess file itself!

    If you're paranoid that the behaviour of the option should change in the future, put this in your /etc/apache2/sites-enabled/mysite.com

        
                # 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
                
                        AllowOverride None
    
                        SetHandler none
                        SetHandler default-handler
    
                        Options -ExecCGI
                        php_flag engine off
                        RemoveHandler .cgi .php .php3 .php4 .php5 .phtml .pl .py .pyc .pyo
                
        
    

    If you can't modify the apache configuration, then put the files in a .htaccess with the following directory structure:

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

    That way, nobody should be able to overwrite your .../protected/.htaccess file since their uploads go in a subdirectory of .../protected, not in protected itself.

    AFAICT, you should be pretty safe with that.

提交回复
热议问题