首页
社区
课程
招聘
[讨论]对于thinkphp框架的注入问题
发表于: 2014-9-26 13:08 1433

[讨论]对于thinkphp框架的注入问题

2014-9-26 13:08
1433
ThinkPHP 3.1.3及之前的版本存在一个SQL注入漏洞,漏洞存在于ThinkPHP/Lib/Core/Model.class.php 文件
根据官方文档对"防止SQL注入"的方法解释(见31cK9s2c8@1M7q4)9K6b7g2)9J5c8W2)9J5c8X3c8G2j5#2)9J5k6i4c8Z5K9h3&6C8M7r3S2H3i4K6u0W2j5$3&6Q4x3V1k6E0j5h3&6#2j5h3I4Q4x3V1k6K6M7h3I4Q4y4h3k6A6L8X3A6W2j5%4c8A6L8$3&6Q4x3X3g2Z5N6r3#2D9i4K6t1&6
使用查询条件预处理可以防止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;
    }

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回