如何验证上传的文件是视频? [英] How do I validate that an uploaded file is a video?

查看:114
本文介绍了如何验证上传的文件是视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器上有一些非常敏感的信息,因此安全性是一个大问题.用户需要能够上传视频.我知道允许用户上传文件会带来安全威胁,因为没有 100% 的方法可以阻止他们上传非视频.但我显然可以选择服务器将保留哪些文件.

I have a server that has some very sensitive information on it, so security is a big issue. The user needs to be able to upload a video. I know allowing users to upload files poses a security threat because there is no 100% way to keep them from uploading non-videos. But I obviously can choose which files the server will keep.

我知道检查文件扩展名是不够的.检查 MIME 类型更好,但它仍然可以伪造.那么如何检查文件是否为视频?

I know that checking the file-extension won't suffice. Checking the MIME type is better but it can still be faked. So how do I go about checking if the file is a video?

推荐答案

Play it!

唯一确定的方法是使用一些代码来解码相关类型的视频,查看它(并检查是否有合理的结果,例如非零持续时间).

Only way to be sure is to have some code that decodes videos of the type in question, take a look at it (and check there's sensible results, like a non-zero duration).

否则,您的风险很低:

非恶意场景:

  1. 上传者上传视频,内容类型为 video/*.
  2. 您存储八位字节和内容类型.
  3. 下载器下载视频,您使用收到的内容类型.
  4. 下载者观看视频(或抱怨编解码器等)

恶意场景 1:

  1. 上传者上传了一个令人讨厌的木马,其内容类型为 video/*.
  2. 您存储八位字节和内容类型.
  3. 下载程序下载令人讨厌的木马,而您使用的是您收到的内容类型.
  4. 下载器在视频播放器中打开令人讨厌的木马.讨厌的木马什么都不做,因为它不是视频.用户抱怨编解码器.更糟糕的情况是,他们在 ubuntu 论坛上咆哮着缺乏对专有格式的支持,在您的页面上添加了关于网站如何糟糕的拼写错误的评论,因为视频无法正常工作,等等.

恶意场景 2:

  1. 上传者上传恶意木马,该木马被写入视频,利用流行视频播放器的某些缓冲区溢出问题.
  2. 您存储...
  3. 下载器...
  4. 可能只是上述情况之一,但也可能是他们受到了攻击(如果他们使用的是受影响的播放器).

关于场景 2 需要注意的三件事:

Three things to note about scenario 2 though:

  1. 测试视频并不能保证安全,因为它也可能在某些播放器中正常运行.
  2. 如果该漏洞存在于 ffmpeg 中,则测试它的视频可能会使您的服务器容易受到攻击!
  3. 这种类型的漏洞利用既罕见又难以实现.一般风险与上传下载jpegs或pngs相同.确实它有点小(确实有这种类型的漏洞影响了常用的 jpeg 库一段时间).

总而言之,只需确保您只输出您接受的内容类型,并强制文件扩展名匹配它们;如果用户上传名为 haha​​ha.exe 的视频/mpeg,则将其重命名为 haha​​ha.mpg

In all, just make sure you only output with the content-types you accept, and force file-extensions to match them; if the user uploads a video/mpeg called hahaha.exe, then rename it hahaha.mpg

哦,还有:

恶意场景 3:

上传者上传的视频以使用大量资源的方式利用某些玩家.在这种情况下,下载器只会 kill-9/ctrl-alt-delete/your-OSs-kill-them-all-of-choice,但是如果您的服务器正在测试它是一个视频,那么它最终可能会遇到麻烦,因为有没有人介入并杀死它试图解释的 200 个(并且随着脚本小子的脚本不断上传更多)视频".

Uploader uploads video that exploits some players in a way that uses a lot of resources. In this case a downloader will just kill-9/ctrl-alt-delete/your-OSs-kill-them-all-of-choice, but if your server is testing it's a video, then it could end up in trouble as there's no one on hand to step in and kill the 200 (and growing as the script-kiddies's script keeps uploading more) "videos" it's trying to interpret.

只需进行普通的视频处理就足以向您介绍 DoS 能力(毕竟视频处理相对繁重),因此测试文件可能会带来更多的危险,而不是让您远离.

Just doing normal video-processing could be enough to introduce the ability to DoS you (video processing is relatively heavy after all), so testing the file could introduce more dangers than it saves you from.

这篇关于如何验证上传的文件是视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆