溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何理解對于ThinkPHP框架早期版本的一個SQL注入漏洞

發(fā)布時間:2021-09-29 10:12:43 來源:億速云 閱讀:124 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容介紹了“如何理解對于ThinkPHP框架早期版本的一個SQL注入漏洞”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

使用查詢條件預處理可以防止SQL注入,沒錯,當使用如下代碼時可以起到效果:

$Model->where("id=%d and username='%s' and xx='%f'",array($id,$username,$xx))->select();

或者

$Model->where("id=%d and username='%s' and xx='%f'",$id,$username,$xx)->select();

但是,當你使用如下代碼時,卻沒有"防止SQL注入"的效果(但是官方文檔卻說可以防止SQL注入): 

$model->query('select * from user where id=%d and status=%s',$id,$status);

或者

$model->query('select * from user where id=%d and status=%s',array($id,$status));

原因分析:

ThinkPHP/Lib/Core/Model.class.php 文件里的parseSql函數(shù)沒有實現(xiàn)SQL過濾.
其原函數(shù)為: 

protected function parseSql($sql,$parse) {
// 分析表達式
if(true === $parse) {
  $options = $this->_parseOptions();
  $sql =  $this->db->parseSql($sql,$options);
}elseif(is_array($parse)){ // SQL預處理
  $sql = vsprintf($sql,$parse);
}else{
  $sql  =  strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>C('DB_PREFIX')));
}
$this->db->setModel($this->name);
return $sql;
}

驗證漏洞(舉例):
請求地址:

http://localhost/Main?id=boo" or 1="1


http://localhost/Main?id=boo%22%20or%201=%221

action代碼: 

$model=M('Peipeidui');
$m=$model->query('select * from peipeidui where name="%s"',$_GET['id']);
dump($m);exit;

或者:

$model=M('Peipeidui');
$m=$model->query('select * from peipeidui where name="%s"',array($_GET['id']));
dump($m);exit;

結(jié)果:

表peipeidui所有數(shù)據(jù)被列出,SQL注入語句起效.
 
解決方法:

可將parseSql函數(shù)修改為: 

protected function parseSql($sql,$parse) {
// 分析表達式
if(true === $parse) {
  $options = $this->_parseOptions();
  $sql =  $this->db->parseSql($sql,$options);
}elseif(is_array($parse)){ // SQL預處理
  $parse = array_map(array($this->db,'escapeString'),$parse);//此行為新增代碼
  $sql = vsprintf($sql,$parse);
}else{
  $sql  =  strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>C('DB_PREFIX')));
}
$this->db->setModel($this->name);
return $sql;
}

“如何理解對于ThinkPHP框架早期版本的一個SQL注入漏洞”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI