标签:
杂谈 |
ThinkPHP
3.1.3及之前的版本存在一个SQL注入漏洞,漏洞存在于ThinkPHP/Lib/Core/Model.class.php
文件
根据官方文档对"防止SQL注入"的方法解释(见http://doc.thinkphp.cn/manual/sql_injection.html)使用查询条件预处理可以防止SQL注入,没错,当使用如下代码时可以起到效果:
-
$Model->where("id=%d
and ,array($id,$username,$xx))->select();username='%s' and xx='%f'"
复制代码
或者
-
$Model->where("id=%d
and ,$id,$username,$xx)->select();username='%s' and xx='%f'"
复制代码
但是,当你使用如下代码时,却没有"防止SQL注入"效果(而官方文档却说可以防止SQL注入):
-
$model->query('select
* ,$id,$status);from user where id=%d and status=%s'
复制代码
或者
-
$model->query('select
* ,array($id,$status));from user where id=%d and status=%s'
复制代码
原因:ThinkPHP/Lib/Core/Model.class.php 文件里的parseSql函数没有实现SQL过滤.
原函数:
-
protected
function parseSql ($sql,$parse){ -
分析表达式 -
=== $parse ){ -
$options $this -
$sql $this -
// SQL预处理 -
$sql vsprintf ($sql,$parse); -
-
$sql strtr -
-
$this -
$sql ; -
复制代码
验证漏洞(举例):请求地址:
http://localhost/Main?id=boo" or 1="1
或
http://localhost/Main?id=boo" or 1="1
action代码:
- $model=M('Peipeidui');
-
$m * ,$_GET['id']);from peipeidui where name="%s"' -
复制代码
或
- $model=M('Peipeidui');
-
$m * ,array($_GET['id']));from peipeidui where name="%s"' -
复制代码
结果:表peipeidui所有数据被列出,SQL注入语句起效.
解决办法:
将parseSql函数修改为:
-
protected
function parseSql ($sql,$parse){ -
分析表达式 -
=== $parse ){ -
$options $this -
$sql $this -
// SQL预处理 -
$parse array_map (array($this->db,'escapeString'),$parse);//此行为新增代码 -
$sql vsprintf ($sql,$parse); -
-
$sql strtr -
-
$this -
$sql ; -
复制代码
总结:不要过分依赖TP的底层SQL过滤,程序员要做好安全检查
不建议直接用$_GET,$_POST