ThinkPHP框架通杀所有版本的一个SQL注入漏洞详细分析及测试方法
2016-11-05 00:00
1101 查看
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 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函数没有实现SQL过滤.
原函数:
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;
结果:
表peipeidui所有数据被列出,SQL注入语句起效.
解决办法:
将parseSql函数修改为:
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;
}
根据官方文档对"防止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函数没有实现SQL过滤.
原函数:
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;
结果:
表peipeidui所有数据被列出,SQL注入语句起效.
解决办法:
将parseSql函数修改为:
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注入漏洞详细分析
- 对于ThinkPHP框架早期版本的一个SQL注入漏洞详细分析
- ThinkPHP框架任意代码执行漏洞的利用及其修复方法
- ThinkPHP框架任意代码执行漏洞的利用及其修复方法
- 判断101-200之间有多少个素数,并输出所有素数。 程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除, 则表明此数不是素数,反之是素数。
- [分析]-DedeCMS全版本通杀SQL注入漏洞
- [分析]-DedeCMS全版本通杀SQL注入漏洞
- 基本上包含了Foudation框架内的所有函数和方法。 使用方法:全选,自己建一个文本类的文件,如果要使用某个方法却想不起来方法名,直接在文本中搜索关键字即可。
- 修改一个方法,应针对所有可能影响到的地方进行测试
- 如何使用PDO查询Mysql来避免SQL注入风险?ThinkPHP 3.1中的SQL注入漏洞分析!
- 三位一体的漏洞分析方法-web应用安全测试方法
- ThinkPHP最新版本SQL注入漏洞
- ThinkPHP最新版本SQL注入漏洞
- ThinkPHP 3.1中的SQL注入漏洞分析---论ThinkPHP 3.1中的半吊子的PDO封装
- ThinkPHP框架中的U方法分析
- 《JAVA继承与多态》改写程序清单10-6中的Course类,编写一个测试类测试所有的方法
- EventBus源码分析(三):post方法发布事件【获取事件的所有订阅者,反射调用订阅者事件处理方法】(2.4版本)
- 如何使用PDO查询Mysql来避免SQL注入风险?ThinkPHP 3.1中的SQL注入漏洞分析!
- 一个网站的详细测试方法要求和步骤
- thinkphp(tp)框架封装一个自己写的方法到类里面.可以在其他控制器里面调用