文件共享网站中应避免的危险文件类型
我正在制作一个小型文件共享网站,用户可以在其中上传内容。最近有人上传了一个 PHP 脚本,可能是为了损害该网站。这让我思考:我应该阻止用户上传哪些文件类型?我已经阻止了 .exe
文件。还有哪些其他文件类型可能会对我的网站或其用户造成损害?
I am making a small file-sharing website where users can upload content. Recently somebody uploaded a PHP script that was presumably intended to harm the site. It got me thinking: what file types should I block users from uploading? I have already blocked .exe
files. What other file types could cause harm to either my website or its users?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要将文件存储在可直接访问的位置 - 仅通过您控制的脚本提供访问权限。不要使用用户提供的文件名存储文件 - 使用您生成的文件名(最好的选择是将文件详细信息存储在数据库中,包括原始文件名,并使用该数据库记录的主键字段存储实际文件
。第二,人们可以上传他们想要的任何内容,并且文件在您的服务器上执行/解释的机会为零,因为它永远不会处于可以执行/解释的位置。
Don't store the files where they're directly accessible - only provide access via a script YOU control. Don't store the files using their user-supplied filename - use a filename YOU generate (best option is to store file details in a database, including the original filename, and store the actual file using that db record's primary key field.
With those two, people can upload antyhing they want, and there'll be zero chance of the file being executed/interpreted on your server, because it's never in a position where it CAN be executed/interpreted.
看起来脚本在仍在定义函数时就被切断了,所以我无法弄清楚它在做什么。
但是,如果您操作正确,您的“上传文件”目录中应该有一个
.htaccess
文件:这将确保访问该目录中的任何文件都会导致下载,并且脚本将不会运行。
(实际上更好的方法是将文件放在 webroot 之外,并使用“下载器”php 脚本
回显
文件内容)It looks like the script is cut off while it's still defining functions, so I can't make out what it's doing.
However, if you're doing things correctly you should have an
.htaccess
file in your "uploaded files" directory with:This will ensure that accessing any file in that directory will result in a download, and that script will not be run.
(Actually even better is to have the files outside the webroot, and have a "downloader" php script
echo
ing the file contents)该脚本可以委婉地描述为远程管理脚本。
您应该始终使用白名单,而不是黑名单。不要“列举坏处”,而是列出允许的文件类型并拒绝其他所有文件类型。
此外,所有上传的文件都应该放在不运行 PHP 处理程序或任何其他脚本处理程序的目录中(例如,检查用 PHP 编写的其他内容管理系统在
.htaccess
中执行的操作)为他们的上传目录)。将上传的文件放在一个单独的子域中也是一个好主意,该子域无法访问主域的 cookie,以避免尝试在与主站点相同的源上运行 JavaScript 代码的攻击(白名单)内容类型对此还不够,因为已知某些浏览器会猜测内容类型并将非 HTML 文件视为 HTML)。
That script could euphemistically be described as a remote administration script.
You should always use a whitelist, not a blacklist. Instead of "enumerating badness", make a list of allowed file types and reject everything else.
Also, all files uploaded should be put in a directory which does not run the PHP handler, or any other script handlers at all (check for instance what other content management systems written in PHP do in the
.htaccess
for their upload directories).It is also a good idea to put the uploaded files in a separate subdomain which does not have any access to the cookies of the main domain, to avoid attacks which attempt to run JavaScript code on the same origin as the main site (a whitelist of content types is not enough for this, since some browsers are known to guess the content type and treat non-HTML files as HTML).