下面是摘自thinkphp官方的一个公告,官方直接贴出这些东西是非常不负责的行为,跟上次apache公开的Struts2的代码执行一样的行为,会造成很多用户被黑。建议类似的厂商不要再做这种蠢事。9 v; u: T; f, m" I& H& R; t
ThinkPHP 3.1.3及之前的版本存在一个SQL注入漏洞,漏洞存在于ThinkPHP/Lib/Core/Model.class.php 文件% ~0 k+ t$ l/ F9 K1 r
根据官方文档对”防止SQL注入”的方法解释(见http://doc.thinkphp.cn/manual/sql_injection.html)
$ z; N; M, S# t9 y: d# n使用查询条件预处理可以防止SQL注入,没错,当使用如下代码时可以起到效果:3 _. ^1 Y% \7 ^% ]5 R. R
$Model->where("id=%d and username='%s' and xx='%f'",array($id,$username,$xx))->select();5 u$ Y: V4 Q6 e' t6 K8 }
6 [4 y5 X& D9 V1 x# I, L. c 或者
m3 h& z* U, U1 I/ J" H$Model->where("id=%d and username='%s' and xx='%f'",$id,$username,$xx)->select();
: s# X& X% k, T0 j q' y, r% T& O: [+ a- @
但是,当你使用如下代码时,却没有”防止SQL注入”效果(而官方文档却说可以防止SQL注入):
: Z( u. I* W( N( a" N$model->query('select * from user where id=%d and status=%s',$id,$status);7 O& c+ ?8 I+ X; D& N2 t5 L1 f
: i m6 N' v8 z' x; E. w0 c
或者: E5 L1 l! Y/ i
$model->query('select * from user where id=%d and status=%s',array($id,$status));
, R0 u" N, e3 Z& |4 l% e+ Q
9 a, r( \; ~5 J$ \5 l 原因:% i! A' y3 g7 f
ThinkPHP/Lib/Core/Model.class.php 文件里的parseSql函数没有实现SQL过滤.
' Z6 O8 t% K' |' @0 G原函数:" o6 u9 ?( ?& g2 J5 C9 A u
protected function parseSql($sql,$parse) {
9 U+ A% Y+ G$ ] I' T5 h // 分析表达式9 k! \$ E: {2 U" U6 E$ J# P
if(true === $parse) {6 E9 M/ P; W1 Y; z0 i, K8 K
$options = $this->_parseOptions();! t. U0 H+ ~. ]/ U& @# @/ I
$sql = $this->db->parseSql($sql,$options);2 t9 h3 O' N6 p$ |2 Q+ m5 {6 h- ?
}elseif(is_array($parse)){ // SQL预处理/ l8 w* y5 y2 I) |+ ~# r
$sql = vsprintf($sql,$parse);
; Y8 M+ O7 X2 n6 m# X3 T7 j }else{
# X: L9 o6 ?- X/ ?3 D5 N $sql = strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>C('DB_PREFIX')));
: g/ ?; F% B0 {6 U2 J+ e: w& Q$ u }: ]( G k3 U" l3 w: {, _
$this->db->setModel($this->name);& A4 ~+ ~6 t: i/ t" t
return $sql;
) p# z5 g4 ?' D1 m }" {7 @- C# t$ V: c' m8 o4 _
) h, Y3 e( S: X8 c验证漏洞(举例):0 G# T" D9 f4 a; x
请求地址:
8 h7 f* N; c6 X6 y" Q rhttp://localhost/Main?id=boo” or 1=”1
* G* \" q. N* t% v2 d或
+ B' D. J f2 }, F* g5 nhttp://localhost/Main?id=boo%22%20or%201=%221
/ E' U6 |/ X5 d k9 z+ s7 faction代码:
m4 C u5 w6 a6 b5 B9 B$model=M('Peipeidui');
- ?9 }' W) R- y& w7 H0 q4 P% V' ] $m=$model->query('select * from peipeidui where name="%s"',$_GET['id']);+ ]3 B2 _3 q* L! c$ Q+ x2 A1 n
dump($m);exit;6 ^: p" t$ r5 r; A2 z; h
或者
& l/ R T' M8 @/ X E! o$ M$model=M('Peipeidui');
6 a$ s, O. @+ g% m1 _4 D n' T! X $m=$model->query('select * from peipeidui where name="%s"',array($_GET['id']));
2 w5 ^3 a& |+ m0 {! H8 R+ x dump($m);exit;
4 \1 `5 K4 ?$ z: f! ^结果:* P0 m2 j2 V3 }" z$ }/ G4 G/ o
表peipeidui所有数据被列出,SQL注入语句起效.
% f2 H! f2 R3 x3 S8 ^" b+ e解决办法:
8 m9 ~1 I, H! k) l0 H7 m# q将parseSql函数修改为:
2 b2 q9 H+ P4 e- V4 f+ o+ k: R5 t3 \protected function parseSql($sql,$parse) {2 q# [. L6 b3 Z( B
// 分析表达式; }' X5 F, u" W6 s7 P% n6 |: l; Q0 B3 A
if(true === $parse) {3 h! K2 E; |* y( C: Z
$options = $this->_parseOptions();0 ^6 D D' v. [7 Z. J
$sql = $this->db->parseSql($sql,$options);
" H& a2 W) Q/ W1 a/ @$ M& y# l/ a }elseif(is_array($parse)){ // SQL预处理' f( ^3 q \9 V+ ?4 `
$parse = array_map(array($this->db,'escapeString'),$parse);//此行为新增代码. s, V& j3 n, a( F/ K4 p
$sql = vsprintf($sql,$parse);
( E$ L* {7 g" \( u ^ N' | }else{9 \2 }' H* S) |4 d$ Y! o
$sql = strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>C('DB_PREFIX')));( r4 B+ ?! {0 A! r( M$ _) C
}' J4 B9 Z& c% } K
$this->db->setModel($this->name);. u9 O/ m" o0 N, M5 u
return $sql;5 b0 O" T. n: p% d% Z
}6 g8 X% g7 l% D* a3 Z% a
0 p Y/ {3 ?& \ G5 @/ m8 Z9 ]总结:
. R5 g3 D* `) q( |5 v; g7 v! q2 z' e不要过分依赖TP的底层SQL过滤,程序员要做好安全检查
+ W8 N% e8 b: H3 S+ B: c不建议直接用$_GET,$_POST
" ?/ U) ]0 c0 d3 U: I) w[/td][/tr]
! U2 E* ?6 j4 [+ k" w[/table]+1
_$ Y. I3 i9 \7 u0 K- h8 ^* ^
# ]5 h& D+ s' g. Z; t( j/ T3 j. i( z' h' I( {0 S6 B! D) D
|