找回密码
 立即注册
欢迎中测联盟老会员回家,1997年注册的域名
查看: 2182|回复: 0
打印 上一主题 下一主题

ThinkPHP框架通杀所有版本的一个SQL注入漏洞

[复制链接]
跳转到指定楼层
楼主
发表于 2013-7-27 18:30:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
下面是摘自thinkphp官方的一个公告,官方直接贴出这些东西是非常不负责的行为,跟上次apache公开的Struts2的代码执行一样的行为,会造成很多用户被黑。建议类似的厂商不要再做这种蠢事。* D3 B  F. C! N+ S
ThinkPHP 3.1.3及之前的版本存在一个SQL注入漏洞,漏洞存在于ThinkPHP/Lib/Core/Model.class.php 文件% q. o) C5 g6 z6 V: b$ J7 j( n
根据官方文档对”防止SQL注入”的方法解释(见http://doc.thinkphp.cn/manual/sql_injection.html)
3 `, Y! N/ N( x使用查询条件预处理可以防止SQL注入,没错,当使用如下代码时可以起到效果:
5 X# Q- c6 A# g+ e7 U* M8 j8 Y) {( ?! B$Model->where("id=%d and username='%s' and xx='%f'",array($id,$username,$xx))->select();
# A+ @% b( t8 z) Z- L/ o9 ]1 x8 A3 _9 _
或者; u3 u3 h  E3 I! z  t
$Model->where("id=%d and username='%s' and xx='%f'",$id,$username,$xx)->select();
; e% e2 h0 ]& o  c& a# r, B$ b; @5 K; n" p8 [
但是,当你使用如下代码时,却没有”防止SQL注入”效果(而官方文档却说可以防止SQL注入):
! k5 u8 Q: x6 y: l  g0 q: Z8 M  U$model->query('select * from user where id=%d and status=%s',$id,$status);
6 [6 }' l& U% G9 R# B) m  B* n; m' J2 N3 X/ S# I/ T+ Y0 a) w
或者; F0 ]! Y1 u2 Y: Q+ J! `
$model->query('select * from user where id=%d and status=%s',array($id,$status));( @! w: k5 j; N7 ^

4 E, g4 `: a! d) h2 l% c) _ 原因:" ?0 Z  h# R; A/ a& n
ThinkPHP/Lib/Core/Model.class.php 文件里的parseSql函数没有实现SQL过滤.- T4 @7 _, q1 n# J% d( J
原函数:" l+ l& Y: L3 s  b& F9 Y
protected function parseSql($sql,$parse) {
- l" V6 z/ y3 d; K; J0 W; {5 i2 Y! h        // 分析表达式0 p0 p* v2 G+ \  B
        if(true === $parse) {
( k; F: p( E$ K. c# x* k# l            $options =  $this->_parseOptions();
& p- r2 f( v& \! ]. @) c            $sql  =   $this->db->parseSql($sql,$options);* ~4 \  [/ P/ Z
        }elseif(is_array($parse)){ // SQL预处理
  A; T- \" e" b            $sql  = vsprintf($sql,$parse);" q2 _. m: T/ K! i3 v6 \" }+ h. }
        }else{' d& U! r" O1 W0 _# |+ \/ d- c
            $sql    =   strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>C('DB_PREFIX')));
  D, G5 s1 q1 h4 ~( s        }0 E  A7 v& `( k' Y# C' s
        $this->db->setModel($this->name);( a5 k0 Y. B: `9 J+ w  ~
        return $sql;; N0 n1 s/ h" j! |. N/ s
    }' j. K& |# x) ~7 w$ n1 c' r6 W
% V1 ]6 G- f% {' j: J
验证漏洞(举例):  }  S% g% h+ R4 q8 V
请求地址:3 t! X- F' x4 n# C. Y
http://localhost/Main?id=boo” or 1=”1+ p1 x( y; w$ F- l% T
, a4 }" |3 T* |
http://localhost/Main?id=boo%22%20or%201=%221) t6 }. C8 D6 s
action代码:5 |! I  _8 ~0 S* B
$model=M('Peipeidui');
0 @8 z8 m4 A# ^0 v        $m=$model->query('select * from peipeidui where name="%s"',$_GET['id']);( h& @; q7 t7 G7 [& t  V9 J6 G6 z' e
        dump($m);exit;
' ^. V7 W) b4 b* g+ ~或者
$ ?# z, U' {2 w6 N3 T8 R& C& [$model=M('Peipeidui');
7 `$ L' M  }7 w! Y* n1 O        $m=$model->query('select * from peipeidui where name="%s"',array($_GET['id']));' k5 x( @+ Y3 T, P/ g8 A
        dump($m);exit;- R, J5 _/ _& d- _7 K9 h2 _
结果:
/ `  L* A4 N5 d4 |4 ~表peipeidui所有数据被列出,SQL注入语句起效.; p4 s2 H+ U" P' T% k( l/ B& [
解决办法:
6 D9 }% I* g+ x' ~将parseSql函数修改为:  _- I! o/ T5 T* w' ^6 Y. `8 t
protected function parseSql($sql,$parse) {
! n0 m, a: V/ J* S( F        // 分析表达式9 q  t4 i* X# R! r8 k+ W
        if(true === $parse) {
) c& l: M3 ^3 p* J& x) m6 Y            $options =  $this->_parseOptions();1 E# ]+ }% ?# z4 D* M
            $sql  =   $this->db->parseSql($sql,$options);
  }" j7 f8 b5 W" U+ q        }elseif(is_array($parse)){ // SQL预处理. k" `; y3 k( g! x) k
            $parse = array_map(array($this->db,'escapeString'),$parse);//此行为新增代码
8 g$ ~' E" z- n            $sql  = vsprintf($sql,$parse);
+ w. C; Q) N' n1 P9 D2 ~7 @        }else{8 e4 n( N( K: E
            $sql    =   strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>C('DB_PREFIX')));
4 G8 L# Q) J: O        }( b! S5 H" _, w' p4 q0 U5 u
        $this->db->setModel($this->name);
/ v! _: }! G2 p( L; O4 P( b8 t# \# D/ c        return $sql;
& Y9 ~  o8 T1 @3 g# c/ g    }+ D+ I& ]7 r% l
) M% r- J9 S* C. Z7 [
总结:5 r& |" Q# {- i! {7 v' h
不要过分依赖TP的底层SQL过滤,程序员要做好安全检查
. c* r( W2 T2 z5 Y% J8 R" `不建议直接用$_GET,$_POST% }7 q+ p8 X# H: h, ?0 p: M. E
[/td][/tr]# J- _" ~; `4 v; O- r! |& A2 n: N
[/table]+1
/ i& P# q3 r# _! d* \% O0 M7 a' T0 f# T% R4 {' `# ^& [

; ]) |: q& D4 [: d* P; ^
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表