根據(jù)官方文檔對"防止SQL注入"的方法解釋(參考http://doc.thinkphp.cn/manual/s " /> 亚洲福利网站,欧美日韩国产麻豆,天堂成人国产精品一区

色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

對于ThinkPHP框架早期版本的一個SQL注入漏洞詳細分析

Thinkphp官網(wǎng)上曾有一段公告指出,在Thinkphp 3.1.3及之前的版本存在一個SQL注入漏洞,漏洞存在于Thinkphp/Lib/Core/Model.class.php 文件
根據(jù)官方文檔對"防止SQL注入"的方法解釋(參考http://doc.thinkphp.cn/manual/sql_injection.html)
使用查詢條件預處理可以防止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;}

總結(jié):
1.不要過分依賴TP的底層SQL過濾,程序員要做好安全檢查
2.不建議直接用$_GET,$_POST

php技術對于ThinkPHP框架早期版本的一個SQL注入漏洞詳細分析,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 绥棱县| 临朐县| 阜新| 嘉祥县| 宾阳县| 罗田县| 武义县| 杨浦区| 泽州县| 奈曼旗| 南木林县| 湖北省| 广安市| 阿拉善左旗| 罗平县| 包头市| 永昌县| 鸡东县| 阳西县| 嘉禾县| 康定县| 天门市| 岳池县| 化隆| 和田市| 肥乡县| 临漳县| 潢川县| 萍乡市| 沾益县| 盐源县| 西吉县| 满城县| 万全县| 东乌珠穆沁旗| 大埔区| 普宁市| 永善县| 阳新县| 瓦房店市| 葵青区|