对于ThinkPHP框架早期版本的一个SQL注入漏洞详细分析 |
|
ThinkPHP官网上曾有一段公告指出,在ThinkPHP 3.1.3及之前的版本存在一个SQL注入漏洞,漏洞存在于ThinkPHP/Lib/Core/Model.class.php 文件
$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; 结果: 可将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;
}
总结: |