2021-10-07

本文作为转载记录。

php的数据提交中,需要对数据进行安全的过滤,尤其是一些用户上传的图片,下面简单的说一下如果判断用户上传的图片是否为图片木马。

php 判断图片是否含有木马的方法

 

自定义php图片木马检测函数

 

/**
 * # 检测图片是否含有木马
 * @param string $image 图片地址
 * @return bool 
 */
function check_illegal($image)
{
    if (file_exists($image)) {
        $resource = fopen($image, 'rb');
        $fileSize = filesize($image);
        fseek($resource, 0);
        if ($fileSize > 512) { // 取头和尾
            $hexCode = bin2hex(fread($resource, 512));
            fseek($resource, $fileSize - 512);
            $hexCode .= bin2hex(fread($resource, 512));
        } else { // 取全部
            $hexCode = bin2hex(fread($resource, $fileSize));
        }
        fclose($resource);
        if (preg_match("/(3c25)|(3c3f.*?706870)|(3C534352495054)|(2F5343524950543E)|(3C736372697074)|(2F7363726970743E)/is", $hexCode)) {
            return 'false';
        }
    }
    return 'true';
}

 

函数使用

$image = check_illegal('mochu.jpg');
if(!$image){
    echo '图片正常';
}else{
    echo '图片不安全';
}

 

打赏

好文章,更需要你的鼓励

本文由 氢设计 创作,除注明转载/出处外,均为本站原创,转载前请务必署名

最后编辑时间为:2021-12-15 09:32:42

本文链接:https://www.h2sheji.com/show-193.html