简体   繁体   English

安全图像(JPG、PNG、动画 GIF)上传

[英]Secure Image (JPG, PNG, Animated GIF) Upload

I am looking for the best solution (=> secure, quick, independent) way to solve an image upload (also I want to provide download from URL) for static jpg / png and animated gif files (if a file is a gif but isn't animated, make it a png).我正在寻找最佳解决方案(=> 安全、快速、独立)来解决静态 jpg / png 和动画 gif 文件(如果文件是 gif 但不是不动画,将其设为 png)。

Bonus Information: I am executing the upload script in PHP on an Amazon EC2 server and will upload the image to an Amazon S3 bucket.奖励信息:我正在 Amazon EC2 服务器上用 PHP 执行上传脚本,并将图像上传到 Amazon S3 存储桶。 If that changes anything, please tell.如果这有什么改变,请告诉。

Bonus Information 2: After I have solved this problem, I want to add a DAMNLOL-Like watermark below the image (like this: http://www.damnlol.com/i/916d72c17e572b14a14c283b2f96f997.jpg ).奖励信息2:解决这个问题后,我想在图像下方添加一个类似DAMNLOL的水印(像这样: http ://www.damnlol.com/i/916d72c17e572b14a14c283b2f96f997.jpg)。

How I have solved the problem until now:到目前为止我是如何解决这个问题的:

  1. User selects file to upload in form / from URL, continues to PHP script (which doesnt abort on user abort).用户从 URL 中选择要上传的文件,继续到 PHP 脚本(不会在用户中止时中止)。

  2. The script takes the image, does some checks: max file size, $_FILES['file']['type'] must be of "image/gif", "image/png", "image/jpg".脚本获取图像,进行一些检查:最大文件大小, $_FILES['file']['type'] _ $_FILES['file']['type']必须是“image/gif”、“image/png”、“image/jpg”。 Max / min width & height checks.最大/最小宽度和高度检查。 If everything is okay, continue.如果一切正常,请继续。

    • If the user uploads the image from his local computer, store the image on the server with a temp name.如果用户从他的本地计算机上传图像,请使用临时名称将图像存储在服务器上。 After that the php script calls http://regex.info/exif.cgi with CURL and the temp url as input.之后,php 脚本使用 CURL 和临时 url 作为输入调用http://regex.info/exif.cgi
    • or in case of a submitted url, the php script calls http://regex.info/exif.cgi with CURL and the submitted url as input.或者在提交 url 的情况下,php 脚本使用 CURL 和提交的 url 作为输入调用http://regex.info/exif.cgi
  3. Jeffrey (Jeffrey stands for Jeffrey's Exif viewer (regex.info/exif.cgi)) gives me a response. Jeffrey(Jeffrey 代表 Jeffrey's Exif viewer (regex.info/exif.cgi))给了我一个回应。 If it implies that the image is indeed of type PNG, JPG or GIF the script downloads the file (in case of a submitted url) or moves the file (in case of an uploaded image) to the final image folder.如果这意味着图像确实是 PNG、JPG 或 GIF 类型,脚本会下载文件(如果是提交的 url)或将文件(如果是上传的图像)移动到最终图像文件夹。 Success!成功!

As you can see, the script is vulnerable when you submit a file via URL and the URL redirects Jeffrey to a valid image but my server to a malicious code.如您所见,当您通过 URL 提交文件并且 URL 将 Jeffrey 重定向到有效图像但我的服务器重定向到恶意代码时,该脚本很容易受到攻击。 At least that's what I think.至少我是这么认为的。

This solution is slow but rather secure in my opinion.在我看来,这个解决方案很慢但相当安全。 An upload takes ~2-5 seconds, depending on the file size.上传需要大约 2-5 秒,具体取决于文件大小。 GIFs tend to take ~4-10 seconds. GIF 往往需要大约 4-10 秒。 Other sites don't take that long and are still secure.其他站点不需要那么长时间并且仍然是安全的。


Ideas on how to solve the problem:关于如何解决问题的想法:

  • Apparently Jeffrey is able to find out the actual file type EVERYTIME.显然,Jeffrey 每次都能找出实际的文件类型。 Is there such a bullet-proof method in PHP? PHP中有这样的防弹方法吗? Obviously, deciding the file type by the name of the file is a very poor way to solve this.显然,通过文件名来决定文件类型是一种非常糟糕的解决方法。

  • Combined with Information 2, I could simply take the submitted image, get the width & height info, create a new blank image with (new_image_width = submitted_image_width, new_image_height = submitted_image_height + watermark_height) and store the submitted image with the watermark below it.结合信息2,我可以简单地获取提交的图像,获取宽度和高度信息,创建一个新的空白图像(new_image_width = submit_image_width, new_image_height = submit_image_height + watermark_height)并将提交的图像与它下面的水印一起存储。 If the whole process fails, the submitted image is not of type JPG / PNG / GIF and the script aborts.如果整个过程失败,则提交的图像不是 JPG / PNG / GIF 类型,脚本将中止。 It could get a bit tricky when adding a watermark to Animated GIFs, but it's clearly possible (--> http://forgifs.com/gallery/d/206365-1/Fitness-ball-roof-jump.gif ).将水印添加到动画 GIF 时可能会有点棘手,但显然是可能的(--> http://forgifs.com/gallery/d/206365-1/Fitness-ball-roof-jump.gif )。

What do you think about my ideas?你觉得我的想法怎么样? Do you have any detailed solutions to this particular problem?您对这个特定问题有任何详细的解决方案吗? I've known about it for a long time and done several researches on the topic, but I never found "the best" solution.我已经知道很长时间了,并就该主题进行了多次研究,但我从未找到“最佳”解决方案。

Thank you for your time.感谢您的时间。 May we finally solve this problem and help me and thousands of people who search for the solution of this problem on Google day for day.愿我们最终解决这个问题,并帮助我和成千上万在谷歌日日夜夜寻找这个问题的解决方案的人。

我使用http://www.webmotionuk.co.uk/php-jquery-image-upload-and-crop/的修改版本至少你可以查看代码并获得一些关于制作文件上传过程的想法有点安全。

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

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