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

前端 未结 2 1429
不思量自难忘°
不思量自难忘° 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:

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

    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

        <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>
    

    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.

    0 讨论(0)
  • 2020-12-08 16:38

    My Godaddy setup wont allow me to edit the httpd.conf files, and the php_flag command doesn't work due to how they've implemented php for me.

    I was able to use this in my .htaccess file:

    SetHandler default-handler
    AddType text/plain php
    

    I put this in the directory above where my FTP user is allowed to access, which forces all PHP files in that directory, as well as all sub-directories to show php as plain text.

    This will work for other file types as well. All you need to do is add another line with whatever extension of file you want to be forced to display in plain text. AddType text/plain cgi for example

    0 讨论(0)
提交回复
热议问题