PHP图像上传。如何防止包含代码的图像? [英] PHP image uploads. how do I protect against images containning code?

查看:155
本文介绍了PHP图像上传。如何防止包含代码的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,图像(jpeg,gif等)可能包含有效的php / python / perl等代码。所以 - 任何人都可以创建一个有效的jpeg文件,同时可以通过PHP解释器执行。 (这里是描述:链接

所以 - 我想知道 - 有没有一种方法从图像中去除恶意代码?会记录图像与GD或imagemagic的工作?

谢谢!

>实际的图像将不包含代码。但是没有任何东西阻止某人试图获取上传的图片文件,然后尝试去执行。



您的解释器(Perl,PHP,不管)应该设置为只执行某些文件类型(即.php或.php5)。没有理由Perl或PHP应该解析图像文件。



只需使用以下常识来保护自己:

< 1)验证文件的mime类型

2)强制执行仅允许上传某个扩展名的文件的策略

3)不要接受面值的文件名。生成自己的内部文件名,并使用数据库表来保存映射。

4)如果你是真正的偏执狂,找一些代码来检查字节签名对于给定的文件类型上传是否有效。

From what I understand, - images (jpeg, gif etc.) might contain valid php/python/perl etc. code. So - anyone can create file that will be valid jpeg at the same time can be executed by PHP interpreter. (here is description: link)

So - I was wondering - is there a way to strip malicious code from images? would recoding images with GD or imagemagic work?

Thanks!

解决方案

Actual images will not contain code. But there's nothing stopping someone from trying to get an "image" file uploaded, and then try and get it to execute.

Your interpreters (Perl, PHP, whatever) should be set up so that they only execute certain file types (i.e. .php or .php5). There's no reason Perl or PHP should be parsing image files.

Just use the following common sense to protect yourself:

1) Verify the mime type of the document
2) Enforce policies that only allow uploading files of a certain extension
3) Don't accept filenames at face value. Generate your own internal file name and use a database table to keep the mapping.
4) If you're truly paranoid, find some code to check that the byte signatures are valid for the given file type upload.

这篇关于PHP图像上传。如何防止包含代码的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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