<span id="7ztzv"></span>
<sub id="7ztzv"></sub>

<span id="7ztzv"></span><form id="7ztzv"></form>

<span id="7ztzv"></span>

        <address id="7ztzv"></address>

            原文地址:http://drops.wooyun.org/papers/2803

            0x01 寫在前面


            本文總結了烏云主站最近提交的 由于第三方接口程序導致的高危漏洞 本文實例都是使用率高,且在近期爆出漏洞的API,具有一定現實意義

            在程序中嵌入第三方程序,可以追溯到discuz!。后來的各種SNS程序,CMS,BBS都紛紛效仿,他們或由官方 或由站長自己添加了各種插件和api,一方面 這些腳本增加了用戶體驗,然而在黑客眼中,實則是增大了入侵幾率。

            各種接口的添加,一般是在整站程序開發周期之后,那么開發全局觀的不同步是顯而易見的后果,簡而言之,前期種種過濾和安全防護,可能由于后期開發對第三方插件盲目的信任,被徹底繞過,導致漏洞形成。

            0x02? 實例分析


            看看那些廠商都是怎么被出賣的

            第一彈 Alipay支付插件那碎一地的節操

            影響程序:Ecshop,Dedecms,Cmseasy ...  
            lib\plugins\pay\alipay.php 
            

            我們看看cmseasy怎么被自己人干掉的吧 在alipay.php中 自己定義的數據庫函數在rec_update中

            #!php
            public function rec_update($row , $where){ ... 省略無關代碼 ...
                        $sql="UPDATE `".$this->tablename."` SET ".$sqlud." WHERE ".$where; //漏洞出在這里 一只裸體的$where
                        return $this->simpledb->execute($sql);
                    }
            

            可以看到 where 是沒有單引號的,我們看看where從哪里能傳進來。

            #!php
            public static function changeorders($id,$orderlog) {
                    //file_put_contents('logs.txt', $id);
                    $where=array();
                    $where['id']=$id;
                    $where['status']=4;
                    //$where['orderlog']=serialize($orderlog);
                    $update=orders::getInstance()->rec_update($where,$id);
                    if($update<1) {
                        exit('改變訂單狀態出錯,請聯系管理員');
                    }
                }
            

            繼續追蹤changeorders

            #!php
            function respond() {
                    if (!empty($_POST)) {
                        foreach($_POST as $key =>$data) {
                            $_GET[$key] = $data;
                        }
                    }
                    $payment  = pay::get_payment($_GET['code']);
                    $seller_email = rawurldecode($_GET['seller_email']);
                    $order_sn = str_replace($_GET['subject'],'',$_GET['out_trade_no']);
                    $order_sn = trim($order_sn);
                                     ....省略....
                        pay::changeorders($order_sn,$_GET);
            

            看到沒$order_sn 沒有過濾 注入于是產生了 由于沒有顯示點 延時注入即可。

            第二彈 Tenpay支付插件也瘋狂

            lib\plugins\pay\tenpay.php 
            

            阿里巴巴那么瘋狂,BAT的三弟騰訊怎么能示弱?? 組隊坑廠商什么的最有愛了~ 由于Cmseasy最信任插件了。。所以又是他受傷。。。

            #!php
            class tenpay {
            ......
                function respond() {
                    require_once ("tenpay/PayResponseHandler.class.php");
                    $resHandler = new PayResponseHandler();
                    $sp_billno = $resHandler->getParameter("sp_billno"); //騰訊的函數,類似于$_GET['sp_billno']或者$_POST['sp_billno']
            
            
            //上面談到GET不受過濾影響,本地問價內包含POST,GET提交都可,但是注入的話必須提交POST,因為GET是URL碼.
            //sp_lillno=sp_billno=-1-1-../../../demo 包含根目錄的demo.php
            
                    preg_match_all("/-(.*)-(.*)-(.*)/isu",$sp_billno,$oidout);
                    $paytype = $where['pay_code'] = $oidout[3][0];
                    include_once ROOT.'/lib/plugins/pay/'.$paytype.'.php';//匹配上面正則就行,包含之,觸發,但是實在找不到能截斷的PHP文件了,所以雞肋了。
                    $pay = pay::getInstance()->getrows($where); //SQL注入,跟進0x02
                ......
            
            }
            

            看到where沒? 難道又是一只裸體動物? 答案是NO 這次是Cmseasy本身代碼和API共同狼狽為奸 看getrows

            #!php
            function getrow($condition,$order='1 desc',$cols='*') {
                    $this->condition($condition); //OMG跟進,又是這個函數
                    return $this->rec_select_one($condition,'*',$order);
                }
            
            function condition(&$condition) {
                    if (isset($condition) &amp;&amp;is_array($condition)) {
                        $_condition=array();
                        foreach ($condition as $key=>$value) {
                            $value=str_replace("'","\'",$value);//問題函數在這里 MARK
                            $_condition[]="`$key`='$value'";
                        }
                        $condition=implode(' and ',$_condition);
                    }
                    ......
                }
            

            試想一下,當我們POST提交'evalsql 由于gpc作用 會變成 \'evalsql 經過剛剛mark的那一行代碼 就變成了 \\'evalsql 單引號被解救了 - -

            所以賣隊友成功

            2014081318305040309.jpg

            第三彈 騰訊連彈 換個姿勢咱們繼續賣隊友

            微信sdk接口(weixin.php) 影響程序:數不清,這個太多了點,從大到小

            挑個超級典型的對象 PHPYUN
            /phpyun的全局過濾堪稱典范 所有特殊字符入庫一律被替換成&acute 再加上360wscan 銅墻鐵壁的防御
            那么壯士yun是不是革命成功? 錯! 汪精衛weixin笑而不語。。。
            /
            這里引入一個古老而又粗暴的漏洞 XML實體注入,也許下一篇drop文就是這個的科普

            #!php
            //weixin/model/index.class.php  
            private function responseMsg()
                {
                    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
                    if (!empty($postStr)){
                            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                            $fromUsername = $postObj->FromUserName;
                            $toUsername = $postObj->ToUserName;
                            $keyword = trim($postObj->Content);
                            $time = time();
                            $textTpl = "<xml>
                                        <ToUserName><![CDATA[%s]]></ToUserName>
                                        <FromUserName><![CDATA[%s]]></FromUserName>
                                        <CreateTime>%s</CreateTime>
                                        <MsgType><![CDATA[%s]]></MsgType>
                                        <Content><![CDATA[%s]]></Content>
                                        <FuncFlag>0</FuncFlag>
                                        </xml>";
            
                            if(!empty( $keyword ))
                            {
                                $msgType = "text";
                                $contentStr = "Welcome to wechat world!";
                                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                                echo $resultStr;
                            }else{
                                echo "Input something...";
                            }
                    }else {
                        echo "";
                        exit;
                    }
                }
            

            首先介紹下

            $GLOBALS["HTTP_RAW_POST_DATA"]這個變量是講POST提交來的數據原封不動的接收 比如POST a=1

            $GLOBALS["HTTP_RAW_POST_DATA"] 就是“a=1” 不受gpc 360wscan的影響 然后接收到$postStr之后 又沒有處理 于是乎...

            #!php
            if($MsgType=='event')
                          {
                            $MsgEvent = $postObj->Event;
                            if ($MsgEvent=='subscribe')
                            {
                                $centerStr = "<Content><![CDATA[歡迎您關注".iconv('gbk','utf-8',$this->config['sy_webname'])."!\n 1:您可以直接回復關鍵字如【銷售】、【南京 銷售】、【南京 銷售 XX公司】查找您想要的職位\n綁定您的賬戶體驗更多精彩功能\n感謝您的關注!]]></Content>";
                                $this->MsgType = 'text';
                            }elseif ($MsgEvent=='CLICK')
                            {
                                $EventKey = $postObj->EventKey;
                                if($EventKey=='我的帳號'){
                                    $centerStr = $this->bindUser($fromUsername);
            

            傳進bindUser之后 最終到達

            #!php
            isBind private function isBind($wxid='')
                {
                    if($wxid)
                    {
                        $User = $this->obj->DB_select_once("member","`wxid`='".$wxid."'","`uid`,`username`");
            

            wxid就是一開始POST來的數據里面的參數 注入赤果果的產生

            2014081318470353538.jpg

            微信團隊開發這個API的時候,其實是驗證了來源的
            驗證了signature(簽名)
            驗證的條件是 檢查signature是否非空 非空 非空...

            0x03 卷尾語


            這樣的第三方插件 應該成為程序員或者白帽子重點檢查對象,原因如上0x02 這貨們如同后門一般有魅力...
            一般來講 造成此類漏洞的原因有兩種
            一,雙方相互信任 (你不打單引號,咱不過濾,好基友一起死)
            二,簽名未設置或者驗證
            為各位客官提供點思路

            <span id="7ztzv"></span>
            <sub id="7ztzv"></sub>

            <span id="7ztzv"></span><form id="7ztzv"></form>

            <span id="7ztzv"></span>

                  <address id="7ztzv"></address>

                      亚洲欧美在线