一聚教程网:一个值得你收藏的教程网站

热门教程

php ssrf漏洞修复代码

时间:2022-06-24 15:43:16 编辑:袖梨 来源:一聚教程网

这个存在漏洞的url是http://bbs.demo.com//forum.php?mod=ajax&action=downremoteimg&message=

攻击者把非法文件传入到参数进行SSRF攻击:

http://bbs.demo.com//forum.php?mod=ajax&action=downremoteimg&message=[img]http://tv.phpinfo.me/exp.php?s=ftp%26ip=127.0.0.1%26port=6379%26data=helo.jpg[/img]

这里message参数传的是用户在论坛发布的图片地址,正常情况下是图片格式后缀,虽然程序有对后缀进行了判断,但是判断不严格,只需要通过在url加图片后缀就可以蒙混过去。

解决办法是修改对url的合法性判断,后缀即使合法,也可能通过url rewrite方式伪造,所以进一步通过curl获取文件头信息,根据返回的报文Content-Type参数判断文件格式是否合法。

于是,在文件/source/module/forum/forum_ajax.php文件新增以下几个函数:

PHP

//获取上传图片url列表
function getImageList($temp)
{
    $urlList = array();
    foreach ($temp as $item) {
        $urlList[] = $item[1];
    }
    return $urlList;
}

/**
 * 检查content-type是否合法
 * @param $imageList array 图片url列表
 * @return bool true合法 false非法
 */
function checkContentType($imageList)
{
    $allowExtensions = array('jpg', 'jpeg', 'png', 'gif', 'bmp');
    $allowTypes = array('image/png', 'image/x-png', 'image/gif', 'image/jpeg', 'image/pjpeg');
    foreach ($imageList as $url) {
        $extension = getUrlExtension($url);
        if(!in_array(strtolower($extension), $allowExtensions)){
            return false;
        }
        $contentType = getContentType($url);
        if (!in_array($contentType, $allowTypes)) {
            return false;
        }
    }
    return true;
}

//获取content-type
function getContentType($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_exec($ch);
    $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    return $contentType;
}

//获取url后缀
function getUrlExtension($url)
{
    $parseurl = parse_url($url);
    $extension = pathinfo($parseurl['path'], PATHINFO_EXTENSION);
    return $extension;
}






在具体接口处理的地方新增:

//检测图片是否合法 Added by tanteng 2016.07.07
if(is_array($temp) && !empty($temp)){
   $imageList = getImageList($temp);
   $check = checkContentType($imageList);
   if ($check === false) {
       die('file upload error!');
   }
}
首先判断文件后缀是否合法,再通过curl请求判断header的Content-Type是否合法,因为后者不容易伪造。其中curl判断文件Content-Type方法:

PHP

//获取content-type
function getContentType($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_exec($ch);
    $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    return $contentType;
}

再通过乌云公开的漏洞地址,url传入非法格式的文件,程序不再继续执行。

不过,这样做有一个问题,有的图片地址并不是真实的静态文件,如:http://image.demo.com/avatar/100/150*150,这个路径就是一个图片,尽管不是图片格式的静态路径,那么这样判断就会误判,不过这种情况是很少数,暂且不管。

热门栏目