php版本漏洞验证方法,ThinkPHP框架通杀所有版本的一个SQL注入漏洞详细分析及测试方法...

下面是摘自thinkphp官方的一个公告,官方直接贴出这些东西是非常不负责的行为,跟上次apache公开的Struts2的代码执行一样的行为,会造成很多用户被黑。建议类似的厂商不要再做这种蠢事。

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过滤.

原函数:

1

2

3

4

5

6

7

8

9

10

11

12

13protected 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代码:

1

2

3$model=M('Peipeidui');

$m=$model->query('select * from peipeidui where name="%s"',$_GET['id']);

dump($m);exit;

或者

1

2

3$model=M('Peipeidui');

$m=$model->query('select * from peipeidui where name="%s"',array($_GET['id']));

dump($m);exit;

结果:

表peipeidui所有数据被列出,SQL注入语句起效.

解决办法:

将parseSql函数修改为:

1

2

3

4

5

6

7

8

9

10

11

12

13

14protected 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;

}

总结:

不要过分依赖TP的底层SQL过滤,程序员要做好安全检查

不建议直接用$_GET,$_POST

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值