下面是摘自thinkphp官方的一个公告,官方直接贴出这些东西是非常不负责的行为,跟上次apache公开的Struts2的代码执行一样的行为,会造成很多用户被黑。建议类似的厂商不要再做这种蠢事。4 w" E$ o p+ g n4 N
ThinkPHP 3.1.3及之前的版本存在一个SQL注入漏洞,漏洞存在于ThinkPHP/Lib/Core/Model.class.php 文件
3 U6 @! |3 A% T根据官方文档对”防止SQL注入”的方法解释(见http://doc.thinkphp.cn/manual/sql_injection.html)5 L3 C, H3 N: s& [
使用查询条件预处理可以防止SQL注入,没错,当使用如下代码时可以起到效果:5 d# C: R G- p! F1 F8 `/ t
$Model->where("id=%d and username='%s' and xx='%f'",array($id,$username,$xx))->select();
6 T. G/ W, b1 D2 c k* t/ a$ m; T0 c% O% U# |" l6 h
或者
! j5 Z3 v% p: S$ `% T$ n% d7 A$Model->where("id=%d and username='%s' and xx='%f'",$id,$username,$xx)->select();% I: G1 }* U- y& l) [. d
2 F g/ b4 i0 \# p3 ]1 `; z
但是,当你使用如下代码时,却没有”防止SQL注入”效果(而官方文档却说可以防止SQL注入):1 \6 \ E: P2 |% l1 a0 A
$model->query('select * from user where id=%d and status=%s',$id,$status);# Z$ P$ b) J; r; E
l0 q9 x& {( R* A- l或者
4 R7 s6 P9 F+ p" |$model->query('select * from user where id=%d and status=%s',array($id,$status));
5 B7 @# c0 V' F( \# l
m7 ~$ X/ _6 [ 原因:
+ E% q) d/ e8 B4 p# T! R, ~) TThinkPHP/Lib/Core/Model.class.php 文件里的parseSql函数没有实现SQL过滤.3 d! S" u7 [- U+ V; Z
原函数:
. k- a8 }6 P; C( W* P. i$ Uprotected function parseSql($sql,$parse) {
5 B1 }. C, `- p# N! k6 U w // 分析表达式( k9 p, u8 k5 M
if(true === $parse) {! @, ~ @5 Y- T9 m+ q; U# G
$options = $this->_parseOptions();
' x8 L2 X5 t, h; m $sql = $this->db->parseSql($sql,$options);
. m! k1 [: b" W1 W }elseif(is_array($parse)){ // SQL预处理
; d0 g% t4 |3 y/ C" {9 i $sql = vsprintf($sql,$parse);
k; K) N% A M7 X. I5 G. V }else{& i m3 s5 | [2 ^
$sql = strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>C('DB_PREFIX')));
) U) w* S3 c4 x' K A/ J& J }
1 `1 i' l5 _8 p& B- H6 I7 l3 C $this->db->setModel($this->name);8 Y! h# B5 X# |# n! i/ _
return $sql;! `2 m( P. [0 u, L4 I! G
}
- y% y2 f% F$ e% t- V2 b1 S: G8 |: [
验证漏洞(举例):
* E# f5 `" S, V请求地址:
! W& t& b( p) E7 Vhttp://localhost/Main?id=boo” or 1=”1
) C v' v3 g! r( Q6 k7 z8 r) }或
3 C U6 a) |" E. |% E! X/ w( Thttp://localhost/Main?id=boo%22%20or%201=%221- u0 k5 O5 q" A( ]
action代码:
% W* ]- P% H1 Z6 Y0 m, _9 m$model=M('Peipeidui');
$ u6 @0 _! j# k# o! { $m=$model->query('select * from peipeidui where name="%s"',$_GET['id']);
- @8 L0 R( ^ d# U dump($m);exit;" c* {. l8 E- L4 K$ I
或者5 k+ D% r& I: b6 n0 O5 B& q
$model=M('Peipeidui');
v- V( A1 }. H# h7 K5 F9 M' D $m=$model->query('select * from peipeidui where name="%s"',array($_GET['id']));
2 H: V/ o4 m$ n7 o# `+ A dump($m);exit;0 c( E9 s/ v: W2 F+ {
结果:
& F' i1 K2 ~) m% ^9 t4 A/ E( o) B表peipeidui所有数据被列出,SQL注入语句起效.
! W$ }$ q0 q/ N$ O解决办法:7 }) m, N' z$ `4 S. ?9 ?5 i
将parseSql函数修改为:
1 o$ s% e. p' ^6 hprotected function parseSql($sql,$parse) { m$ P& i3 W d& `! y+ y
// 分析表达式4 S' O: ?' ~9 v% |6 n
if(true === $parse) {
# z0 H+ D. e% P6 D, H0 y $options = $this->_parseOptions();4 ^; R+ J1 x# r, Y8 Q
$sql = $this->db->parseSql($sql,$options); W9 a0 F0 k! d! N5 \+ C6 E5 v
}elseif(is_array($parse)){ // SQL预处理* i5 ?% w9 O- }6 u' E2 e8 m
$parse = array_map(array($this->db,'escapeString'),$parse);//此行为新增代码
$ Q3 d8 R6 K" p+ m4 | $sql = vsprintf($sql,$parse);
5 B: N; I5 q1 b/ o. v- C6 b }else{9 I% ^; x m2 ^9 U
$sql = strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>C('DB_PREFIX')));$ Z5 d! B9 G$ `( u- t
}- o4 b( a: O8 `5 v9 D* y+ T+ C
$this->db->setModel($this->name);$ w. W, K: ?# E& |, v
return $sql;
& t4 X; k4 J2 @) K0 @! b% n& |( ` }
" }6 ^% z+ U/ }/ y' [! }, h
) [0 \" ~- J2 c( v8 H总结:
3 [- R# c8 t5 Z( k0 K# s* o不要过分依赖TP的底层SQL过滤,程序员要做好安全检查; x6 ]! F- w# a3 H1 T' t6 T5 @
不建议直接用$_GET,$_POST0 E; ~: W5 ~! t5 J+ I6 ?
[/td][/tr]4 F3 I# ?* M- ^* \* Y
[/table]+10 e4 M: ^( K+ K
/ q; V! f9 a- O$ _: n( G% d! c& l8 l4 K. R" N) \. e
|