简体   繁体   English

阻止上传php脚本执行

[英]prevent upload php script to be executed

I have a system where user pay for support, each user have a folder. 我有一个用户付费支持的系统,每个用户都有一个文件夹。 I have many (like 200+) sub folderד in my website, each of these needs the CSS, images, JS etc... 我的网站上有很多(比如200+)子文件夹,每个都需要CSS,图像,JS等...

I also create folders every week for new users when they register, each user can upload PHP script or JS script or images. 我还每周为新用户注册时创建文件夹,每个用户都可以上传PHP脚本或JS脚本或图像。 (screenshot of their problem) (他们问题的截图)

My problem is: in my /.htacess , I have a rule that checks for PHP script and redirects to the proper page eg site.com/user/page will go to site.com/user/page.php 我的问题是:在我的/.htacess ,我有一个检查PHP脚本并重定向到正确页面的规则,例如site.com/user/page将转到site.com/user/page.php

What I want to do is prevent the user from breaking the system, for example by: 我想要做的是防止用户破坏系统,例如:

site.com/user/upload/test will go to his test.php and run it. site.com/user/upload/test将转到他的test.php并运行它。

How can I prevent these kind of attacks? 我怎样才能防止这种攻击呢?

Block access to PHP files in you htaccess, put this file inside the folder you want to block files: 阻止访问htaccess中的PHP文件,将此文件放在要阻止文件的文件夹中:

<Files ^(*.php|*.phps)>
    order deny,allow
    deny from all
</Files>

Or in root .htaccess file you can: 或者在root .htaccess文件中,您可以:

<Directory ^user/upload>
    <Files ^(*.php|*.phps)>
        order deny,allow
        deny from all
    </Files>
</Directory>

Will block access to all php files inside the user/upload folder, even if mod_rewrite is used. 即使使用mod_rewrite,也会阻止访问user / upload文件夹中的所有php文件。

But, if you want to keep the .php files accessible for download and don't want they execute it, you can use this on .htaccess: 但是,如果你想让.php文件可供下载而不想让它们执行,你可以在.htaccess上使用它:

<FilesMatch "(.+)$">
    ForceType text/plain
</FilesMatch>

All files in the folder will return as text/plain . 文件夹中的所有文件将以text/plain返回。 You can bind this in the Directory tag to get a similar result of deny access from the second example. 您可以在Directory标记中绑定它以从第二个示例获得类似的拒绝访问结果。

You also can chose the file extensions you want to delivery as text/plain : 您还可以选择要作为text/plain传递的文件扩展名:

<FilesMatch "\.(php|pl|py|jsp|asp|htm|shtml|sh|cgi.+)$">
    ForceType text/plain
</FilesMatch>

Please remember that Apache might have more extensions to handle by PHP type handler, and it indeed has. 请记住,Apache可能有更多的扩展来处理PHP类型处理程序,它确实有。 Here is the .htaccess content that works fine for our server. 这是.htaccess内容,适用于我们的服务器。

<FilesMatch "(?i)\.(php5|php4|php|php3|php2|phtml|pl|py|jsp|asp|htm|shtml|sh|cgi)$">
    ForceType text/plain
</FilesMatch>

It is working fine for us. 它对我们来说很好。

My problem is: in my /.htacess, I have a rule that checks for PHP script and redirects to the proper page eg site.com/user/page will go to site.com/user/page.php 我的问题是:在我的/.htacess中,我有一个检查PHP脚本并重定向到正确页面的规则,例如site.com/user/page将转到site.com/user/page.php

Why not just create the users page as site.com/user/page/index.php ? 为什么不将用户页面创建为site.com/user/page/index.php?

site.com/user/upload/test will go to his test.php and run it site.com/user/upload/test将转到他的test.php并运行它

Then your rewrite rule is wrong - but you didn't show us what it is. 然后你的重写规则是错误的 - 但你没有告诉我们它是什么。 Also your code for handling file uploads is wrong - and its not just PHP which is the problem - you could be acting as a mule site for all sorts of malware. 您处理文件上传的代码也是错误的 - 它不仅仅是PHP的问题 - 您可以充当各种恶意软件的骡子站点。

When allowing users to upload content, you should never store it in such a way that it is directly addressable by the webserver (except maybe for very large files of very specific and VERIFIED file types - such as videos). 当允许用户上传内容时,您永远不应该以可以由网络服务器直接寻址的方式存储它(除非是非常大的特定和VERIFIED文件类型的文件 - 例如视频)。 All access should be mediated by a control script (which may set the mime type and filename for the content it channels). 所有访问都应该由控制脚本调解(控制脚本可以为其引导的内容设置mime类型和文件名)。

好吧,我有一个想法,你重定向到test.php,你重定向到一个页面,将验证代码属于X用户,如果分配,遵循例程和重定向,如果你不属于,激发404

Sorry, I misunderstood the question. 对不起,我误解了这个问题。 I would think the best way to prevent these attacks from happening would be to save the files as .php.txt or something, so it's non-executable. 我认为防止这些攻击发生的最佳方法是将文件保存为.php.txt或其他东西,因此它是不可执行的。

But print out the file's contents via f_open(); 但是通过f_open();打印出文件的内容f_open(); or file_get_contents(); file_get_contents();

If this is not what you are looking for, can you provide information about what your website does, exactly? 如果这不是您想要的,您能提供有关您的网站的确切信息吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM