关于某fecmaller提的关于后台图片可以上传任意图片文件的问题

技术分享 · Fecmall · 于 4年前 发布 · 2093 次阅读

关于某fecmaller提的关于后台图片可以上传任意图片文件的问题

github issues: https://github.com/fecshop/yii2_fecshop/issues/77

本人进行了测试,后台文件上传

1.上传非图片后缀的文件,譬如 11.txt, 上传失败

2.将11.txt改成 11.png,上传失败

3.将图片333.png改成 333.php,上传成功

fecmall对图片的验证,并不是对文件后缀进行的验证,而是对图片文件的验证,这里并不是你说的任意文件,譬如 1和2,就上传失败,

fecmall 图片验证代码

1.验证类型:https://github.com/fecshop/yii2_fecshop/blob/b15d7ed0f9ede3a75cbe971f10688567c5a95e9b/services/Image.php#L37

2.验证函数代码: https://github.com/fecshop/yii2_fecshop/blob/b15d7ed0f9ede3a75cbe971f10688567c5a95e9b/services/Image.php#L57

通过php函数进行图片文件验证,并不是验证后缀,而是验证图片文件二进制内容,即使你将一个图片的后缀改成其他的后缀,上传到后台并没有问题,他仍是一个图片,并不是你说的任意文件,你将一个11.txt文件改成11.php,并不能上传成功。

public function isAllowImgType($file)
    {
        $img = getimagesize($file);
        $imgType = $img['mime'];
        if (!in_array($imgType, $this->allowImgType)) {
            return false;
        
        }
        
        return true;
    }

更新

对php的函数 php getimagesize, 进行了查询,这个函数

getimagesize 函数不是完全可靠的

参考资料:https://segmentfault.com/a/1190000003911296

因此您说的这个漏洞是存在的

共收到 5 条回复
Fecmall#14年前 0 个赞

1.后台是自己人使用,为了安全,线上商城,可以把后台隐藏起来访问(后台是独立的访问域名), 在运维方面对后台的访问进行限制。

2.我在加一个图片文件后缀的检测

3.对于yii2对图片的验证,

@yii\validators\ImageValidator;

 /**
     * Validates an image file.
     * @param UploadedFile $image uploaded file passed to check against a set of rules
     * @return array|null the error message and the parameters to be inserted into the error message.
     * Null should be returned if the data is valid.
     */
    protected function validateImage($image)
    {
        if (false === ($imageInfo = getimagesize($image->tempName))) {
            return [$this->notImage, ['file' => $image->name]];
        }

        list($width, $height) = $imageInfo;

        if ($width == 0 || $height == 0) {
            return [$this->notImage, ['file' => $image->name]];
        }

        if ($this->minWidth !== null && $width < $this->minWidth) {
            return [$this->underWidth, ['file' => $image->name, 'limit' => $this->minWidth]];
        }

        if ($this->minHeight !== null && $height < $this->minHeight) {
            return [$this->underHeight, ['file' => $image->name, 'limit' => $this->minHeight]];
        }

        if ($this->maxWidth !== null && $width > $this->maxWidth) {
            return [$this->overWidth, ['file' => $image->name, 'limit' => $this->maxWidth]];
        }

        if ($this->maxHeight !== null && $height > $this->maxHeight) {
            return [$this->overHeight, ['file' => $image->name, 'limit' => $this->maxHeight]];
        }

        return null;
    }

也是使用了函数 getimagesize

Fecmall#34年前 0 个赞

fecmall关于图片文件的安全性:

1.图片文件名后缀检查

2.使用验证函数:https://github.com/fecshop/yii2_fecshop/blob/b15d7ed0f9ede3a75cbe971f10688567c5a95e9b/services/Image.php#L57 进行检查

3.图片文件使用独立域名和独立的文件夹appimage,在nginx配置部分,对图片域名的访问,进行限制,不可以运行php,只能访问图片文件,并对图片文件访问进行限制

4.fecmall后台,进行访问限制,只允许特定的ip等手段(或者开启vpn内网访问等),只能自己人访问后台

5.可以扩展用图片服务器。

gsfish#44年前 1 个赞

看到了作者的patch,应该没什么问题了。getimagesize()是通过检查文件头的方式来对图片进行验证的(很容易伪造),因此在后端做后缀名检测还是有必要的

Fecmall#54年前 0 个赞

@gsfish #4楼 后面做一下图片服务器的扩展。这样更方便一些

添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics