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

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

[复制链接]
跳转到指定楼层
楼主
发表于 2013-7-27 18:30:26 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
下面是摘自thinkphp官方的一个公告,官方直接贴出这些东西是非常不负责的行为,跟上次apache公开的Struts2的代码执行一样的行为,会造成很多用户被黑。建议类似的厂商不要再做这种蠢事。! \( G8 j4 O* `
ThinkPHP 3.1.3及之前的版本存在一个SQL注入漏洞,漏洞存在于ThinkPHP/Lib/Core/Model.class.php 文件
6 N9 r0 a- V8 B9 r4 j) |- ]: L# R( v根据官方文档对”防止SQL注入”的方法解释(见http://doc.thinkphp.cn/manual/sql_injection.html)8 a& j5 W* t" C: d+ |& k) s" x0 p
使用查询条件预处理可以防止SQL注入,没错,当使用如下代码时可以起到效果:" a/ R. J+ l$ J) h5 Q$ L
$Model->where("id=%d and username='%s' and xx='%f'",array($id,$username,$xx))->select();5 W. j  Z1 [$ U8 R0 l7 h

9 }$ L4 |$ p/ ]+ r 或者
6 d- ]5 a& c1 K+ e) |$Model->where("id=%d and username='%s' and xx='%f'",$id,$username,$xx)->select();
! A6 `5 e6 `* }9 A' p) s$ v% p9 O9 _% ]5 x, L$ ^
但是,当你使用如下代码时,却没有”防止SQL注入”效果(而官方文档却说可以防止SQL注入):1 c. [6 _* `# q7 X2 r% c
$model->query('select * from user where id=%d and status=%s',$id,$status);8 M# f( F! n0 N6 C7 ]

! z* I7 a$ Q6 F& g4 w, l! w或者- E2 [3 Z$ C9 y& L9 E; M1 ]
$model->query('select * from user where id=%d and status=%s',array($id,$status));  y. v$ G* V2 |0 q! p/ c* x1 K

& s9 {% l8 X4 x 原因:* Q, L9 K& L" h* [* U1 E: Q
ThinkPHP/Lib/Core/Model.class.php 文件里的parseSql函数没有实现SQL过滤.$ x! ~1 j1 ^; A8 M' ^: w
原函数:
5 p* R- c3 w+ z; Rprotected function parseSql($sql,$parse) {
0 g# Q5 O) {' Z' D: q/ W        // 分析表达式
( H+ R" R6 Q% t' |4 [3 Z# i        if(true === $parse) {
" r: o" u/ B  a+ g9 c  G6 q' l            $options =  $this->_parseOptions();
! D& _9 f. H+ a7 \' V1 O; ^            $sql  =   $this->db->parseSql($sql,$options);" l$ S3 c8 L* o' J' g. s
        }elseif(is_array($parse)){ // SQL预处理
! E5 @$ L/ N: ?4 \, m            $sql  = vsprintf($sql,$parse);# ?/ a: @/ \3 [6 O  ~
        }else{3 @& o) e1 J5 d1 v
            $sql    =   strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>C('DB_PREFIX')));
4 S& t5 c' P2 ^6 |/ r        }
7 _) P# h( _% U( M2 r        $this->db->setModel($this->name);6 D0 \6 ]% F& O
        return $sql;3 L% V! I8 ?) D
    }7 r- w' S, y9 w( D2 K

: t' c) Z* P  d3 p/ w0 |( O验证漏洞(举例):$ U6 {# [& U8 q6 R# o# k: z
请求地址:/ a7 A+ ]' Z4 X7 S+ |% O0 n. B& i# m
http://localhost/Main?id=boo” or 1=”1
5 w9 S' q# u. q& z+ H0 ]% q# S0 q# {6 [, h3 @
http://localhost/Main?id=boo%22%20or%201=%221, z9 A- ]8 W+ z/ a  h
action代码:
1 T6 I, V- }6 z1 w1 c$model=M('Peipeidui');' z/ v! g* f  C) d
        $m=$model->query('select * from peipeidui where name="%s"',$_GET['id']);
5 L2 {" Y  S( p2 T        dump($m);exit;% X( l6 g' Y$ C( A1 E" Q$ F
或者
% s' C6 t/ @! ^: ~- n7 g- b( a$model=M('Peipeidui');
6 {6 I6 v" j' R5 K  K8 \( \        $m=$model->query('select * from peipeidui where name="%s"',array($_GET['id']));, |# \) e6 D& Z' L$ V
        dump($m);exit;$ {9 p8 r- X  C( N) L0 w
结果:
9 o* B! n7 @5 r+ j" O# ~8 R表peipeidui所有数据被列出,SQL注入语句起效.; M) r2 h. P9 u: x; g
解决办法:7 n5 ^, n, R; M1 J+ k$ V# U( D% I
将parseSql函数修改为:
3 v' G  ]) N' A4 hprotected function parseSql($sql,$parse) {
4 D! @7 t9 B5 D        // 分析表达式
6 I. t( I% j8 K  N& T* b* `& U5 ]        if(true === $parse) {( S. G6 l5 |- d; @5 V: c+ S
            $options =  $this->_parseOptions();
# S+ Q9 @6 r2 @9 X, B; l# u            $sql  =   $this->db->parseSql($sql,$options);' r& p! h" |2 \1 y* w
        }elseif(is_array($parse)){ // SQL预处理; S* k- K6 ^3 V+ D3 U& z2 z9 c
            $parse = array_map(array($this->db,'escapeString'),$parse);//此行为新增代码
. k. W8 ?8 Y  M1 b) ]  c            $sql  = vsprintf($sql,$parse);
6 J! {' a; b7 N- |, a# U        }else{3 g4 Y+ t2 s  D! L# t; B2 m
            $sql    =   strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>C('DB_PREFIX')));
/ {. g: ~- c- b4 a        }7 L) F3 v" G2 d$ Z. O0 F
        $this->db->setModel($this->name);6 g# U9 [2 G: T3 l* o* Y; q
        return $sql;% L; C0 s% l4 m
    }! U& {7 E3 f5 y5 O

8 U4 J" M! G( }8 N3 o# E1 A, I总结:
4 V7 \& X( ?6 d. e; ~5 c不要过分依赖TP的底层SQL过滤,程序员要做好安全检查
8 H. ~/ l5 H2 h不建议直接用$_GET,$_POST" O8 Y+ _# Z0 W) l/ t6 O
[/td][/tr]
' z& S) x+ v/ }# K8 \6 f6 n8 y[/table]+1$ M) L0 N8 I6 x$ L( I
) r2 B0 o% i; }. t

2 ?3 A4 C& W0 @. G* b) `( N
回复

使用道具 举报

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

本版积分规则

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