對(duì)于ThinkPHP框架早期版本的一個(gè)SQL注入漏洞詳細(xì)分析
ThinkPHP官網(wǎng)上曾有一段公告指出,在ThinkPHP 3.1.3及之前的版本存在一個(gè)SQL注入漏洞,漏洞存在于ThinkPHP/Lib/Core/Model.class.php 文件
根據(jù)官方文檔對(duì)"防止SQL注入"的方法解釋(參考http://doc.thinkphp.cn/manual/sql_injection.html)
使用查詢條件預(yù)處理可以防止SQL注入,沒錯(cuò),當(dāng)使用如下代碼時(shí)可以起到效果:
$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();
但是,當(dāng)你使用如下代碼時(shí),卻沒有"防止SQL注入"的效果(但是官方文檔卻說(shuō)可以防止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ù)沒有實(shí)現(xiàn)SQL過濾.
其原函數(shù)為:
protected function parseSql($sql,$parse) {
// 分析表達(dá)式
if(true === $parse) {
$options = $this->_parseOptions();
$sql = $this->db->parseSql($sql,$options);
}elseif(is_array($parse)){ // SQL預(yù)處理
$sql = vsprintf($sql,$parse);
}else{
$sql = strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>C('DB_PREFIX')));
}
$this->db->setModel($this->name);
return $sql;
}
驗(yàn)證漏洞(舉例):
請(qǐng)求地址:
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注入語(yǔ)句起效.
解決方法:
可將parseSql函數(shù)修改為:
protected function parseSql($sql,$parse) {
// 分析表達(dá)式
if(true === $parse) {
$options = $this->_parseOptions();
$sql = $this->db->parseSql($sql,$options);
}elseif(is_array($parse)){ // SQL預(yù)處理
$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;
}
總結(jié):
1.不要過分依賴TP的底層SQL過濾,程序員要做好安全檢查
2.不建議直接用$_GET,$_POST
相關(guān)文章
PHP + plupload.js實(shí)現(xiàn)多圖上傳并顯示進(jìn)度條加刪除實(shí)例代碼
本篇文章主要介紹了PHP + plupload.js實(shí)現(xiàn)多圖上傳并顯示進(jìn)度條加刪除實(shí)例代碼。具有一定的參考價(jià)值,有興趣的可以了解一下。2017-03-03
簡(jiǎn)單PHP會(huì)話(session)說(shuō)明介紹
下面小編就為大家?guī)?lái)一篇簡(jiǎn)單PHP會(huì)話(session)說(shuō)明介紹。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2016-08-08
Drupal讀取Excel并導(dǎo)入數(shù)據(jù)庫(kù)實(shí)例
這篇文章主要介紹了Drupal利用PHPExcel讀取Excel并導(dǎo)入數(shù)據(jù)庫(kù)的例子,需要的朋友可以參考下2014-03-03
單一index.php實(shí)現(xiàn)PHP任意層級(jí)文件夾遍歷(Zjmainstay原創(chuàng))
本程序?qū)崿F(xiàn)了使用一個(gè)index.php文件來(lái)實(shí)現(xiàn)所有文件夾的遍歷效果,避免了需要無(wú)窮復(fù)制index.php至文件夾下才能實(shí)現(xiàn)的效果2012-07-07
PHP遞歸獲取目錄內(nèi)所有文件的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇PHP遞歸獲取目錄內(nèi)所有文件的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2016-11-11

