【发布时间】:2013-11-17 16:59:21
【问题描述】:
使用 PHP 从图像中删除 EXIF 数据是否足以防止恶意代码在服务器中执行?
我想保护服务器免受this blog post 中描述的做法:
<?php
$img = imagecreatefromjpeg('malicious_codes.jpg');
$w = imagesx($img);
$h = imagesy($img);
$trans = imagecolortransparent($img);
if($trans >= 0) {
$rgb = imagecolorsforindex($img, $trans);
$oldimg = $img;
$img = imagecreatetruecolor($w,$h);
$color = imagecolorallocate($img,$rgb['red'],$rgb['green'],$rgb['blue']);
imagefilledrectangle($img,0,0,$w,$h,$color);
imagecopy($img,$oldimg,0,0,0,0,$w,$h);
}
imagejpeg($img,'safe_image.jpg');
?>
【问题讨论】:
-
我认为这仅在您使用 include 或 require 打开此类 jpeg 时有害(如示例中所示)。与 readfile/file_get_contents/... 的主要区别在于这些语句在读取后评估内容。至少你应该确保在输出之前用 htmlspecialchars 屏蔽 exif 数据。
-
所以我不需要做这一切hungred.com/useful-information/… 对吧?
-
内容类型验证 - 验证图像文件内容 - 验证文件扩展名 - 验证会话 - 随机文件名 ...等
-
这取决于你想对上传的文件做什么。
-
图片只能从服务器返回为 html