上传的白名单或黑名单文件扩展名?

发布于 2025-01-07 06:32:37 字数 290 浏览 2 评论 0原文

我正在制作一个新闻通讯编辑器,它将允许文件上传(新闻通讯的发件人可以将文件上传到将在电子邮件中链接到的服务器)。

该站点的设置使得只有 .do URI 实际由 servlet 执行/处理,因此不会有太大的安全风险,但有人告诉我将 .jsp、.php、.asp、.aspx、.exe 列入黑名单。 .com 和 .bat。我觉得这并不是一个全面的黑名单,而且我的印象是黑名单不是一个好政策。

另一方面,白名单可能有几十个长。识别允许/不允许的扩展的正确方法是什么?或者只允许任何内容并通过病毒扫描程序或这些的某种组合来运行它是否更合适?

I'm making a newsletter editor which will allow file uploads (the sender of the newsletter can upload files to the server which will be linked to in the email).

The site is set up so that only .do URIs are actually executed/handled by servlets so it's not much of a security risk, but I've been told to blacklist .jsp, .php, .asp, .aspx, .exe, .com, and .bat. This does not strike me as a comprehensive blacklist, and I've the impression that blacklists are not a good policy.

On the other hand, a whitelist would be dozens long. What's the correct way to identify allowable/disallowable extensions? Or is it more proper to just allow anything and run it by a virus scanner, or some combination of these?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

三寸金莲 2025-01-14 06:32:37

我允许上传任何文件扩展名,但我会将文件存储在不直接由 Web 服务器提供服务的文件夹中。然后,我将创建一个从电子邮件链接到的 HTTP 处理程序,该处理程序将传输所请求的文件。可以通过原始文件名、系统生成的文件名或 ID 来请求文件。无论哪种方式,我都会清理参数以防止目录遍历攻击。

例如 www.example.com/FileLink.ashx?FileName=Word.docx

这样,您无需担心将来是否希望将其他文件扩展名作为可执行文件类型提供服务,因为任何文件都直接从字节流提供文件系统,并且永远不会通过 Web 服务器处理程序传递。

您还可以使用处理程序来检查当前用户是否具有加载文件的正确权限。

对每个文件进行病毒扫描也是值得的,以防时事通讯作者上传(恶意或意外)的文件攻击订阅者的计算机而不是服务器。

还要确保 Content-Disposition设置为 attachment

Content-Disposition: attachment; filename="filename.html"

这可以防止通过上传包含脚本标记的 HTML 或其他同源策略来实现 XSS 绕过 Flash 或 PDF 文件。这里的场景是一名新闻通讯编辑危及另一名新闻通讯编辑的会话。还值得设置 X-Content-Options: nosniff,这也可以防止这种情况。 xap 文件 (Silverlight) 也可能绕过同源策略,因此请检查文件名是否不能以 .xap 结尾来请求您的文件,

例如www.example.com/FileLink.ashx/x.xap?FileName=Word.docx

并且您可以将 Silverlight 的内容类型设置列入黑名单,作为针对这种特殊情况的额外保护。 来源此处

注意:.XAP 文件可以重命名为任何其他扩展名,但不能
不再跨域加载。 Silverlight 似乎找到了该文件
根据提供的 URL 扩展名,如果不是 .XAP,则忽略它。
如果网站允许用户使用“;”,则仍然可以利用此漏洞。或者
在实际文件名后添加“/”以添加“.XAP”扩展名。

注意:Silverlight跨域请求.XAP文件时,内容
类型必须是:application/x-silverlight-app。

我自己也验证了这些场景,它们是目前有效的攻击媒介。

I would allow any file extension to be uploaded, but I would store the files in a folder that is not directly served by the web server. I would then create a HTTP handler that would be linked to from the email, which would stream the requested file. The file could be requested either by original file name, a system generated file name or by an ID. Either way, I would sanitise the parameter to guard against directory traversal attacks.

e.g. www.example.com/FileLink.ashx?FileName=Word.docx

This way you do not need to worry if in future you wish to serve additional file extensions as executable file types, as any file is served directly from a byte stream from the file system and is never passed through the web server handlers.

You can also use the handler to check that the current user has the correct permissions to load the file.

It would also be worth virus scanning each file, just in case the newsletter author uploads (either maliciously or accidentally) a file that would attack subscribers' computers rather than the server.

Also ensure that the Content-Disposition is set to attachment:

Content-Disposition: attachment; filename="filename.html"

This guards against XSS being achieved by upload of HTML containing script tags, or other Same Origin Policy bypasses using Flash or PDF files. The scenario here is one newsletter editor compromising the session of another newsletter editor. It is worth also setting X-Content-Options: nosniff, which can also protect against this. xap files (Silverlight) could also bypass the Same Origin Policy, so check that the filename cannot be ended in .xap to request your file

e.g. www.example.com/FileLink.ashx/x.xap?FileName=Word.docx

and you could blacklist the setting the content type for Silverlight as extra protection for this special case. Source here:

Note: .XAP files can be renamed to any other extension but they cannot
be load cross-domain anymore. It seems Silverlight finds the file
extension based on the provided URL and ignores it if it is not .XAP.
This can still be exploited if a website allows users to use ";" or
"/" after the actual file name to add a ".XAP" extension.

Note: When Silverlight requests a .XAP file cross-domain, the content
type must be: application/x-silverlight-app.

I've also verified these scenarios myself and are are currently valid attack vectors.

久光 2025-01-14 06:32:37

或者只允许任何内容并通过病毒扫描程序运行它是否更合适。

是的。

黑名单和白名单都可以被轻易规避,只会给管理带来麻烦,而且不提供任何安全性。

Or is it more proper to just allow anything and run it by a virus scanner.

Yes.

Both blacklists and whitelists are trivially circumvented and cause just administration pain and provide no security whatsoever.

月野兔 2025-01-14 06:32:37

在我看来,尽管白名单维护起来可能有点麻烦,但它比使用黑名单安全得多。

忘记将某些内容添加到白名单并不得不返回并更改它,比忘记将新的文件扩展名添加到黑名单并被黑客攻击要好得多。

除了白名单之外,我仍然会对上传的文件进行病毒扫描,因为即使是看似无害的文件(例如.pdf或.doc)也可能含有恶意代码(.pdf支持javascript和.doc宏)

In my opinion, even though the whitelist maybe a bit of a chore to maintain, it's far more secure than using a blacklist.

It's much better to forget to add something to the whitelist, and have to go back and change it, than to forget to add a new file extension to the blacklist and get hacked.

In addition to the whitelist, I would still virus scan the uploaded files, as even seemingly harmless files (such as .pdf or .doc) can have malicious code (.pdf's support javascript, and .doc macros)

涫野音 2025-01-14 06:32:37

我建议您:

  • 使用白名单方法(由于上述原因,这是相当合理的)
    更安全)
  • 检查文件类型(可绕过,但仍是一项措施)
  • 将上传的文件存储在不向公众公开的内部文件夹中(使用非枚举 ID)
  • 对包含上传文件的文件夹设置低级权限
  • 设置对上传文件的权限尽可能少
  • 确保是否没有可用的安全库来上传文件。

防病毒软件不值得。大多数 Web shell 签名确实很容易绕过,因为任何“黑客”都可以添加一些随机代码并在 Web shell 上执行不同类型的编码。当然,它会保护您免受 C99 等常见 shell 的侵害,但现在有数以千计的此类工具是公开可用的,并且完全无法检测到。关于保护您的用户免受网站上托管的可执行文件或受感染 PDF 的侵害,如果有人能够在您的网站上获取 shell 并启动恶意软件活动,那么它就不会使用防病毒签名已发现的恶意软件或病毒。

I would recommend you:

  • Use a whitelist approach (by the reasons described above, it's fairly
    more secure)
  • Check the filetype (bypassable but still one more measure)
  • Store uploaded files in internal folders not exposed to the public (using non enumerative IDs)
  • Set-up a low grade permission to the folder that will contain the uploaded files
  • Set-up less privileges as possible to the uploaded files
  • Make sure if there aren't secure libraries to upload files already available.

Antivirus aren't worth it. Most of web shells signatures are really easy to bypass since any 'hacker' can add some random code and do different types of encodings on an web shell. Sure it will protect you from common shells such as C99 but nowadays there are thousands of these tools publicly available and fully undetectable. And regarding protecting your users from executable files or infected PDF's hosted on your websites, if someone it's capable of get a shell on your site and start up a malware campaign it won't use malware or virus already spotted by av signatures.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文