下面是摘自thinkphp官方的一个公告,官方直接贴出这些东西是非常不负责的行为,跟上次apache公开的Struts2的代码执行一样的行为,会造成很多用户被黑。建议类似的厂商不要再做这种蠢事。3 Q. M6 S% [& A! I5 H
ThinkPHP 3.1.3及之前的版本存在一个SQL注入漏洞,漏洞存在于ThinkPHP/Lib/Core/Model.class.php 文件
# i2 x" C; ~1 }" o" }根据官方文档对”防止SQL注入”的方法解释(见http://doc.thinkphp.cn/manual/sql_injection.html)
: m E K: `2 Z# v使用查询条件预处理可以防止SQL注入,没错,当使用如下代码时可以起到效果:5 ^. [) |3 d6 l
$Model->where("id=%d and username='%s' and xx='%f'",array($id,$username,$xx))->select();
, R V% ^+ s% E% T/ v/ N: n; Z0 z2 e7 f" Q/ U# D
或者
7 J3 E. V2 J: H1 {( a" q, Z+ v; X$Model->where("id=%d and username='%s' and xx='%f'",$id,$username,$xx)->select();
" J8 Q4 Y |+ c, o: M3 c R5 H% \
但是,当你使用如下代码时,却没有”防止SQL注入”效果(而官方文档却说可以防止SQL注入):/ J" Y8 g9 p# v/ ~
$model->query('select * from user where id=%d and status=%s',$id,$status);3 J" u3 ?7 i y8 G; J$ m" C
7 e% x/ l" @/ a4 x- h3 C, X8 J: b5 m1 p
或者
3 t2 P3 M9 }4 J. t3 G! V$model->query('select * from user where id=%d and status=%s',array($id,$status));% Z0 u$ z. B# v- j7 b9 c& ~+ Y
, q: V- K6 U( O$ N- _( |8 g. b 原因:
% x/ r/ Z! z& k' nThinkPHP/Lib/Core/Model.class.php 文件里的parseSql函数没有实现SQL过滤.
3 o8 Q/ J) |( |原函数:4 I. W* ~9 @ X7 l
protected function parseSql($sql,$parse) {
! ], K, x" x9 C, ]5 U0 E$ P // 分析表达式8 y/ t( _0 C" ^3 i3 G- J# n# L) i
if(true === $parse) {
4 ?" ^ S( H0 v7 ] $options = $this->_parseOptions();7 h1 G$ u! o8 w: M" m( U3 A0 W$ M
$sql = $this->db->parseSql($sql,$options);: a5 A, T0 `( [2 E4 i7 F4 M ]
}elseif(is_array($parse)){ // SQL预处理/ k$ j$ ?2 h6 Z- W9 e; t
$sql = vsprintf($sql,$parse);
$ o2 d+ f* t: w) ]* p% c" l( V& P }else{
l( y) c8 o( m' M' D4 I $sql = strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>C('DB_PREFIX')));( S! Z) U# Q2 y' A5 O& ?0 \
}
8 M4 j* R' ^5 ^6 _ $this->db->setModel($this->name);
j& Z' d+ H D v. R# E' | return $sql;0 A, g* T: w5 h1 T0 L, p. J: `
}, }1 U) Z1 b0 e! X
" |! K$ U! X& B. n3 j验证漏洞(举例):" a" ?9 z& w, {2 q$ [/ f. J* y) j% U
请求地址:
# [1 A, V, d3 h6 X8 Yhttp://localhost/Main?id=boo” or 1=”1' K5 e4 r. |$ X- V* V8 U
或
. O- ^; B; I5 Y t+ H& m* ahttp://localhost/Main?id=boo%22%20or%201=%221
9 _( n0 ]3 T: v0 ]( Paction代码:6 P9 V6 ]) ?: X6 Z1 \
$model=M('Peipeidui');9 U% E* U( S! m n, C% P
$m=$model->query('select * from peipeidui where name="%s"',$_GET['id']);
' i Y }6 @; z0 F dump($m);exit;
/ V8 Y# j7 r: ^5 g或者
" s+ P3 Y d7 C$model=M('Peipeidui');
% Z( C( A, i$ U0 i& e3 [8 E) w $m=$model->query('select * from peipeidui where name="%s"',array($_GET['id'])); |/ e4 f9 t: |$ P; ]" ^
dump($m);exit;
% E& q) k1 N- o结果:. K* M/ @" b# o! |
表peipeidui所有数据被列出,SQL注入语句起效.' K7 q" u, w1 }) A
解决办法:3 V+ _" V5 D+ p' M
将parseSql函数修改为:6 i7 g. w. P* y( t i
protected function parseSql($sql,$parse) {
) }% t+ O( D: W( o K // 分析表达式
; W0 F# b) e. D5 w) n if(true === $parse) {
4 A# \1 _+ L4 s$ g' S $options = $this->_parseOptions();, U; U7 n7 u! z: E
$sql = $this->db->parseSql($sql,$options);" n% o4 O* G6 f
}elseif(is_array($parse)){ // SQL预处理
- J o; H: D. P: v$ {/ E $parse = array_map(array($this->db,'escapeString'),$parse);//此行为新增代码
+ L- `/ O( w& b- F: M% `# A1 Q/ Q $sql = vsprintf($sql,$parse);
' |8 h7 p g5 p7 o, J6 O8 V }else{! G8 e4 [. g" ?6 X9 ^& N! U
$sql = strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>C('DB_PREFIX')));
- z' e8 B- w+ A; v& X5 ^4 R }- @" J, n4 n& u T& A$ C
$this->db->setModel($this->name);1 Q: H: {9 x U/ M2 ?
return $sql;
; }7 b: |8 ~ @ Z% l! X8 r }# o/ [1 e. A+ Q8 ^- z& y: g
. d9 W! N' F# r/ W0 }
总结:
! I0 F. h' e. h X不要过分依赖TP的底层SQL过滤,程序员要做好安全检查/ m% w% f' T% i7 _$ z7 ?
不建议直接用$_GET,$_POST2 p- R/ p9 j0 a* D9 x- S
[/td][/tr]
: T! e# P! p% d9 S[/table]+1
5 P! N/ N M+ f9 H# L T2 `& W$ N
p# u( ?$ C+ p0 J* `. J |