下面是摘自thinkphp官方的一个公告,官方直接贴出这些东西是非常不负责的行为,跟上次apache公开的Struts2的代码执行一样的行为,会造成很多用户被黑。建议类似的厂商不要再做这种蠢事。. s" ~5 x7 F' d9 L1 Z k: v
ThinkPHP 3.1.3及之前的版本存在一个SQL注入漏洞,漏洞存在于ThinkPHP/Lib/Core/Model.class.php 文件! A9 ~- W: t# I f! f
根据官方文档对”防止SQL注入”的方法解释(见http://doc.thinkphp.cn/manual/sql_injection.html)( q* m ?! g3 r' k% t
使用查询条件预处理可以防止SQL注入,没错,当使用如下代码时可以起到效果:3 Q" {2 _, G3 ]1 E+ s
$Model->where("id=%d and username='%s' and xx='%f'",array($id,$username,$xx))->select();* p+ m9 C* l4 U Q/ k$ e
G: A/ X: B+ [& g* G, Q 或者
; X7 ?& j0 n P: @0 O3 d$Model->where("id=%d and username='%s' and xx='%f'",$id,$username,$xx)->select();9 j/ V3 |2 M: W+ @" B/ x9 z, a' R
" [! L, s& y2 `0 w# Q3 m
但是,当你使用如下代码时,却没有”防止SQL注入”效果(而官方文档却说可以防止SQL注入):1 h& U1 D' q$ r: X
$model->query('select * from user where id=%d and status=%s',$id,$status);
5 S* X/ z4 k# i# V9 S' M t5 G0 w! U+ {
- s+ ^. J: [/ d, z或者" U0 R7 z# U" c! H+ i
$model->query('select * from user where id=%d and status=%s',array($id,$status));) |9 e% W/ @! R+ y0 E2 H& s( H
+ _# h0 g0 ]- G( c 原因:
& x6 A6 r0 C% r$ bThinkPHP/Lib/Core/Model.class.php 文件里的parseSql函数没有实现SQL过滤.
7 D9 `. H5 k' P0 A0 P# X- i原函数:
% C) I: z, Y, _: J6 zprotected function parseSql($sql,$parse) {
# G0 e# i5 i' c& s" y ^9 y // 分析表达式
$ ?, P+ w, R* ~$ {/ b4 e9 C9 j if(true === $parse) {
% |4 G; q2 G" ]8 o $options = $this->_parseOptions();
' t8 }: q$ d1 {% `; B9 _8 b $sql = $this->db->parseSql($sql,$options);% ]" K# T# x, `, h4 w& r# J
}elseif(is_array($parse)){ // SQL预处理8 u4 d. D- f6 S; P2 F% j/ H! C
$sql = vsprintf($sql,$parse);
, i; ?$ E$ D1 G' l }else{, }" f ?5 n! ]/ R5 g" }
$sql = strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>C('DB_PREFIX')));; C6 P( o( G$ d* k
}! b0 T: I/ ]8 h* ]4 V) a' b# M
$this->db->setModel($this->name);
; r- Y# \. V6 ~ return $sql;
; I2 _$ N [$ ~ S/ G5 B }
# D) Y9 j7 F2 h! t, c' \: [5 x$ b' A8 e1 }6 d; G+ X F. @8 P$ e. [7 E: | l
验证漏洞(举例):
. \. \( c$ D6 A- f7 D9 N请求地址:: v! X/ J: I2 M+ a9 K9 [" l0 {
http://localhost/Main?id=boo” or 1=”11 D/ g' m' c3 Y3 Q
或% V/ h, _+ L- N8 m
http://localhost/Main?id=boo%22%20or%201=%221
$ I/ i* o" @& `* ]action代码:
% L: Q2 G- o5 }3 @9 Y$model=M('Peipeidui');5 E6 E' x* Z q: a
$m=$model->query('select * from peipeidui where name="%s"',$_GET['id']);2 j- l* [; l" O4 s9 j: }/ n
dump($m);exit;' R9 N! A/ j, j
或者' h$ w9 x+ {# o" r, @
$model=M('Peipeidui');% z, F+ x; L( o
$m=$model->query('select * from peipeidui where name="%s"',array($_GET['id']));
" W4 {# D/ a% z dump($m);exit;$ T, ]- c& t: p, j
结果:& o3 t, T% [6 c! Z, Z
表peipeidui所有数据被列出,SQL注入语句起效.
5 T# r, x! b. U1 G9 Y解决办法:
& [' w) ]/ l" \0 n* K将parseSql函数修改为:6 w0 G( X6 C4 B( a& u
protected function parseSql($sql,$parse) {
1 j0 T( M! k) f" h; f // 分析表达式, i& P' E# s; k* i$ n
if(true === $parse) {0 [/ D8 N5 |4 @' w, c
$options = $this->_parseOptions();' N, p6 E$ \8 d* r
$sql = $this->db->parseSql($sql,$options);
0 x6 k8 n" E) `- H7 v9 J2 G6 f }elseif(is_array($parse)){ // SQL预处理
n2 v& A9 G. y4 S o, k $parse = array_map(array($this->db,'escapeString'),$parse);//此行为新增代码
* Y( ^4 [# a3 W2 w1 y3 x $sql = vsprintf($sql,$parse);: K$ e k" K3 \) ]: b
}else{
$ X& u) i5 }$ m: I+ F" B $sql = strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>C('DB_PREFIX')));
. g V3 f# M$ M; y$ P9 M( a, i }
8 k- W& b1 A% @/ e9 }! [ $this->db->setModel($this->name);6 f7 k! z b2 U5 O' U2 B: @
return $sql;0 z8 g" m2 p, S; m+ ~
}! {# m0 e \0 ?" G* `* B0 I4 s
, P4 \3 y9 J$ z$ }$ a
总结:
2 @+ `2 x6 L, m P+ Z不要过分依赖TP的底层SQL过滤,程序员要做好安全检查
( t& R- g7 K, f& c0 Y" Y2 X. G不建议直接用$_GET,$_POST
* @+ N" s7 j; ]" H[/td][/tr]$ v; Q! V. |0 Q/ O+ o M n
[/table]+1
1 A8 Y( L5 g& x! f( |# t$ _
' t; [$ }! o" d/ l O9 @
9 Z/ P+ s- h j' f8 }/ ~ |