Discuz x3.4前台SSRF

补丁: https://gitee.com/ComsenzDiscuz/DiscuzX/commit/41eb5bb0a3a716f84b0ce4e4feb41e6f25a980a3php

可跨协议打
缺点: 因为payload构造中有第二次跳转,因此要求对方服务器的127.0.0.1:80也是dz
PHP版本: php ver > 5.3html

触发漏洞点

漏洞点: source/module/misc/misc_imgcropper.php:55git

$prefix = $_GET['picflag'] == 2 ? $_G['setting']['ftp']['attachurl'] : $_G['setting']['attachurl'];
    if(!$image->Thumb($prefix.$_GET['cutimg'], $cropfile, $picwidth, $picheight)) {

由于$_G['setting']['ftp']['attachurl']默认是/,传入Thumb中的$prefix.$_GET['cutimg']的后缀也可控服务器

一路跟进,source/class/class_image.php:52 -> source/class/class_image.php:118markdown

function init($method, $source, $target, $nosuffix = 0) {
        global $_G;

        $this->errorcode = 0;
        if(empty($source)) {
            return -2;
        }
        $parse = parse_url($source);
        if(isset($parse['host'])) {
            if(empty($target)) {
                return -2;
            }
            $data = dfsockopen($source);
            $this->tmpfile = $source = tempnam($_G['setting']['attachdir'].'./temp/', 'tmpimg_');
            if(!$data || $source === FALSE) {
                return -2;
            }
            file_put_contents($source, $data);
}

能够看到若是可以被parse_url函数解析出host便可进入dfsockopen里面进行curl请求
因此这个就是一个前缀限定为/,跟入parse_url函数底层会发现,它还支持这种做为url: //www.baidu.comapp

804631-20181203105241175-796158819.jpg

/php-5.4.45/ext/standard/url.cdom

804631-20181203105318534-491612829.jpg

继续跟入dz的dfsockopen函数
source/function/function_filesock.php:14curl

$matches = parse_url($url);
$scheme = $matches['scheme'];
$host = $matches['host'];
$path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
...省略
curl_setopt($ch, CURLOPT_URL, $scheme.'://'.($ip ? $ip : $host).($port ? ':'.$port : '').$path);
...省略
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
...省略

因为协议是为null的,因此最后请求的url地址就是http://://google.com/aaa,也就是http://127.0.0.1:80/google.com/aaa函数

因此此处须要一个url跳转才能进行下一步的ssrf攻击,固然这也可以攻击本地,不过很鸡肋.post

PHP版本问题

仔细研究parse_url处理无协议的url时候,//www.baidu.com在不一样的php版本还有一些小差异
相对url是在php5.4才有进行处理

大体总结为
php5.4,能解析ok: //www.baidu.com/../aaa
php5.4后,须要加上端口号: //www.baidu.com:80/../aaa

本地url跳转构造

对于跳转的要求比较高,由于须要的是get型,、不登陆、任意地址跳转
找了好久发现了一个很符合要求,logout的时候会获取referer,而后进入301跳转

804631-20181203105409874-291092112.jpg

其中跳转的地址referer会有验证,验证其是否和自己host匹配,也就是限制了你不能进行任意地址跳转

/source/function/function_core.php:1498

function dreferer($default = '') {
    global $_G;

    $default = empty($default) && $_ENV['curapp'] ? $_ENV['curapp'].'.php' : '';
    $_G['referer'] = !empty($_GET['referer']) ? $_GET['referer'] : $_SERVER['HTTP_REFERER'];
    $_G['referer'] = substr($_G['referer'], -1) == '?' ? substr($_G['referer'], 0, -1) : $_G['referer'];

    if(strpos($_G['referer'], 'member.php?mod=logging')) {
        $_G['referer'] = $default;
    }

    $reurl = parse_url($_G['referer']);

    if(!$reurl || (isset($reurl['scheme']) && !in_array(strtolower($reurl['scheme']), array('http', 'https')))) {
        $_G['referer'] = '';
    }

    if(!empty($reurl['host']) && !in_array($reurl['host'], array($_SERVER['HTTP_HOST'], 'www.'.$_SERVER['HTTP_HOST'])) && !in_array($_SERVER['HTTP_HOST'], array($reurl['host'], 'www.'.$reurl['host']))) {
        if(!in_array($reurl['host'], $_G['setting']['domain']['app']) && !isset($_G['setting']['domain']['list'][$reurl['host']])) {
            $domainroot = substr($reurl['host'], strpos($reurl['host'], '.')+1);
            if(empty($_G['setting']['domain']['root']) || (is_array($_G['setting']['domain']['root']) && !in_array($domainroot, $_G['setting']['domain']['root']))) {
                $_G['referer'] = $_G['setting']['domain']['defaultindex'] ? $_G['setting']['domain']['defaultindex'] : 'index.php';
            }
        }
    } elseif(empty($reurl['host'])) {
        $_G['referer'] = $_G['siteurl'].'./'.$_G['referer'];
    }

    $_G['referer'] = durlencode($_G['referer']);
    return $_G['referer'];
}

由于跳转地址是否合法性的验证是经过parse_url解析出host,与$_SERVER['HTTP_HOST']进行判断。后面跳转后的地址是进入了curl中进行请求。因此这里牵涉到一个东西就是parse_urlcurl的差别性。

当地址为下面连接时,parse_url解析出来为localhost,可是进入curl后即是www.baidu.com

http://localhost#@www.baidu.com/

因此最终跳转的连接以下

htp://localhost/code-src/dz/Discuz_TC_BIG5/upload/member.php?mod=logging&action=logout&XDEBUG_SESSION_START=13904&referer=http://localhost%23%40www.baidu.com&quickforward=1

804631-20181203105442274-1690896815.jpg

最后的利用

整个攻击流程以下:

804631-20181203105500718-1349113987.jpg

Exp内容(须要post方式,而且还有referer验证):

防止恶意利用

php为5.4的时候,须要去掉www.baidu.com的端口号.

formhash能够从首页的html中获取,home.php?mod=spacecp&ac=pm

804631-20181203105526572-671454487.jpg

Finally Exploit:

804631-20181203105557153-981022284.jpg

转载于:https://www.cnblogs.com/iamstudy/articles/discuz_x34_ssrf_1.html