2 ?0 s' ]. T. d" {5 E. t N1 @1 Z+ {: e# k. ~. I
7 h1 b- ^* v' z& H3 z2 ~
0 u6 P7 W0 ~3 @
前言1 W: s% H; b, C' m
" C- {9 _0 ?) h K9 K" K# V9 ^% e5 ?9 F6 C; Q1 |- \0 N/ b& k
2018年9月21日,Destoon官方发布安全更新,修复了由用户“索马里的海贼”反馈的一个漏洞。) e, ~- ~8 F) c& |+ e+ k
& y, D! D# _. r$ }5 o5 y0 q2 J5 E" G$ E4 v, P
8 b0 F3 s- K0 U0 m' J' K# O/ W# X# ] ; ^5 D4 e) J9 ]) `& Y9 u
& w* b; ~/ f4 v 漏洞分析1 M$ g( a5 j% [ i0 e' I* D
' G6 H0 j: Z9 o1 X$ D: L3 `! w6 x
6 h, N3 @. U ^* r8 q 根据更新消息可知漏洞发生在头像上传处。Destoon中处理头像上传的是 module/member/avatar.inc.php 文件。在会员中心处上传头像时抓包,部分内容如下:$ B: D9 Q2 x' T2 [4 n
0 }$ E& ^# Q2 L6 `. n* U7 [
9 F% i( r/ s2 y0 {& z+ u3 r1 j! g
7 R5 K3 z9 x# _3 `5 h8 p
& b, s* o+ N6 _) }% p1 a- d) z1 c5 z
对应着avatar.inc.php代码如下:3 e8 P# F% i# U; l
; W& R+ ]2 ~2 U5 S& b# Y. X1 U: n* k
<?php defined('IN_DESTOON') or exit('Access Denied');login();require DT_ROOT.'/module/'.$module.'/common.inc.php';require DT_ROOT.'/include/post.func.php';$avatar = useravatar($_userid, 'large', 0, 2);switch($action) {
& z d: M5 [% ?1 `3 | 7 v/ [0 z3 a7 D7 V5 r0 J
# P' v8 ~& x# g
case 'upload':5 {5 Y% G+ |7 O* t g6 q5 y
o% K% l! w, o9 L# E% o O
3 b. J. l! R* D# N& ?+ j: a$ d if(!$_FILES['file']['size']) {
& J- {. r& K0 Y8 W& H& A/ o1 [: R
' q! m- Y) t8 \5 w: a" {6 Y5 Z- q6 G# q1 X/ |8 W |$ H/ i5 l A
if($DT_PC) dheader('?action=html&reload='.$DT_TIME);/ f& V9 W; P7 [% I0 t! T" |* t
; I, J' G; j8 N5 F+ d
9 v) g" p$ R! n# u) c* {! t exit('{"error":1,"message":"Error FILE"}');
. s j- H1 d+ V + _" |3 K/ N8 G' r. H3 h3 [* i+ G
9 R* y' m3 }) s" H( p# V9 p; r
}
3 L9 b4 L4 ?7 ]9 n% v
5 N9 j/ i; r" R1 U
' L, ^7 K% H* x6 [ L require DT_ROOT.'/include/upload.class.php';0 Q+ i0 U: l/ ~9 ^
; Z. M7 n u8 d8 B9 I; F
0 S7 \' s0 h: b9 ` o
+ |- \6 @; A5 V! k
% d+ q0 I7 C( W6 c" G ]0 l5 \6 }9 M4 Z
$ext = file_ext($_FILES['file']['name']);
, Z. Z% d# L+ Q
0 ^7 s# l2 ?# C1 F0 s2 L) u% j; n7 c) Y9 M% ?4 H
$name = 'avatar'.$_userid.'.'.$ext;
4 h0 H& H- d* g1 I
& J: L! P3 h2 D8 C1 }) D
1 B' [2 z6 \, G. O/ S $file = DT_ROOT.'/file/temp/'.$name;$ z3 x. _* K5 Y5 k7 A
5 |- O6 V; v, P: G# n1 b: G$ x4 l
, g5 f" h1 o9 M" u& A7 o1 B
- x! }/ j5 F+ @* k; Z4 ^# Y" T/ W
) b' e/ M7 a% ^; K# z5 d$ O0 [7 I0 n
if(is_file($file)) file_del($file);3 X: m. L: G: K% G
9 |2 {" S. C0 Z3 C# q" H- }5 z
$upload = new upload($_FILES, 'file/temp/', $name, 'jpg|jpeg|gif|png');
4 N: U& a* u; x/ [* } ]# N! M" m
7 B* M0 ^3 m4 G8 o, E# `6 V V8 j+ `9 X" Y( T) S. c; E* ?' B$ o: n* V" s {
# A3 `# S6 f. {7 V/ q
) ~' _" s! S l! J8 f) j4 \ G
' z8 L5 m) n7 K; p. H: m
$upload->adduserid = false;+ j" z9 B) `; Z4 f! c
& r$ x6 W8 u; @8 J3 }
6 Y: n5 M4 x" z$ `5 e# r c$ F9 F, K
! P; ?) s6 A# o# h; {3 ]
& k% O9 L B$ V6 a: Z8 R0 z0 B; @0 V, |4 C3 v# h
if($upload->save()) {/ Y/ Z$ j1 N7 r- _ g/ g$ e3 s
# \: N& }2 b" a; ~7 ~9 O
1 d" {3 T. L& L1 Y/ E8 ?( \ ...
/ s$ k# d5 h K, u; { % Z' q4 L! ^0 s, S, Q- v4 ~0 L6 [
8 m, T" {; h6 s5 V8 @) D
} else {6 ~; e5 ?+ i6 o; ^% F; S
% i/ d F( m/ O3 }9 \7 `: w6 R
) Q% l$ S4 S! e ...5 G5 ]7 p1 r9 }& {' d9 e
0 A" k# U t- z- d$ H0 \
$ ^) _! _3 o- d' }3 K
}
Y& ~) u- Z( a
: g8 g- Y1 I. F+ r u0 l7 `8 _& n# b! V7 C, X
break;
3 |8 c% d" a2 ~% A# U8 T 5 I. k0 [* ?6 D1 J2 B* C
1 G% t2 o- ]# w) n 这里通过$_FILES['file']依次获取了上传文件扩展名$ext、保存临时文件名$name、保存临时文件完整路径$file变量。之后通过new upload();创立一个upload对象,等到$upload->save()时再将文件真正写入。
/ |! B. B- i" k& \5 K4 v8 ?+ p2 p
7 N3 G, a" }) `8 @7 a) ?* A0 H+ P9 A! T
upload对象构造函数如下,include/upload.class.php:25:
3 N5 g# f( t3 f- j0 d) A , d, A$ o6 \2 V+ b0 V+ x5 ]
: W7 p% o- }6 \& b
<?phpclass upload {
3 ]1 d5 @6 P& g" a J! }
5 x) Z0 ]) N, T3 z
& p8 q4 J" u1 H* U5 x function __construct($_file, $savepath, $savename = '', $fileformat = '') {. M& N1 q8 e( e
( m6 `4 {9 v, S \0 c; n' K3 n: K/ E* `0 C
global $DT, $_userid;3 y4 a5 Y8 A$ x2 j& C
% k. V3 a0 `# R6 p
3 s- S6 Q. `) r1 U9 w5 l! f foreach($_file as $file) {
! l+ X: u& z) ~$ N F; ~. Y; B# R' D
, Y+ o9 p! `1 n7 N. V$ X
( |! ? G; A* X9 o+ w0 [ $this->file = $file['tmp_name'];8 M r' l4 ^0 h' g- [( X
7 a# G- v+ H8 G& B3 {" N$ p" X8 u+ J' I, G/ |2 S
$this->file_name = $file['name'];
' B3 y2 v' i! F; r- g4 K# v$ F + d9 t7 B( i4 N% n
" {# e* P8 [' S3 @
$this->file_size = $file['size'];4 Q' O" M( ^+ x; X6 A5 Y
% K0 u5 ]- a' `+ c) c9 A
' J6 o) d+ S7 C+ f: @: s3 [ $this->file_type = $file['type'];
# h$ \) N- V8 X8 A r9 ]# x 6 u- Q# w+ o$ S/ r
, L6 F! }" {1 u* [; t' g $this->file_error = $file['error'];" Q1 E1 _4 Z% [
6 y; O+ U( K3 m$ a
. Q1 c, |/ C' X- b' p$ o
6 f. O% m5 J# h4 \; |) b, B
' p" ~2 ^: G7 Q, C9 C
5 a2 D$ ^5 G$ \ G
}
% i4 c- V$ V: a1 o
. B& W+ r& S" t
6 O2 c* j* X P $this->userid = $_userid;
0 L* f1 z% k) y S# A
. e) m" s5 n4 I" f4 k/ S0 G7 h# G' ^9 R, U: H2 N. q
$this->ext = file_ext($this->file_name);
d& K" d: B, H Y' V
/ Z* ]' m5 z& q" ?" x' D" U( C" h) r1 t; N2 _6 h
$this->fileformat = $fileformat ? $fileformat : $DT['uploadtype'];
0 ^5 Q* T6 ^: Z7 x* s* n
: @; \2 J& |6 ~) G, [8 z+ Y1 C( s2 d8 Z6 o
$this->maxsize = $DT['uploadsize'] ? $DT['uploadsize']*1024 : 2048*1024;# P6 d# N; E3 }; G
- V, A3 b$ f7 I
Z, K1 o0 V- A; T8 X( v" k o' X $this->savepath = $savepath;# e1 r& S" D2 g# X2 ]) _8 a
; [" U. M/ j" N# v" ^* k* E' b4 w
: A' o# y& _; p* s $this->savename = $savename;+ L. p- T8 I5 `6 I" c
- H' V$ c N) r( C0 C/ s* |3 }6 k, q3 d" K6 N; G* A* N0 c7 B; p8 a
}}
6 P) @" }- H! I4 v6 C4 h . S/ |' D6 I# U- `; Z' N
8 |) f: l2 c3 n4 L" g) i
这里通过foreach($_file as $file)来遍历初始化各项参数。而savepath、savename则是通过__construct($_file, $savepath, $savename = '', $fileformat = '')直接传入参数指定。
! N7 f6 i3 M, X3 U, V) E8 v
6 f6 ]5 v5 `0 _5 d' k) O, Y: d& o4 w4 G; Z5 c ?
因此考虑上传了两个文件,第一个文件名是1.php,第二个文件是1.jpg,只要构造合理的表单上传(参考:https://www.cnblogs.com/DeanChopper/p/4673577.html),则在avatar.inc.php中 0 n- \1 Y, }% u" O& ~
: t4 D$ O B0 Q/ I+ q/ H; I
( C% z, k! F3 t* V4 \- c $ext = file_ext($_FILES['file']['name']); // `$ext`即为`php` $name = 'avatar'.$_userid.'.'.$ext; // $name 为 'avatar'.$_userid.'.'php'$file = DT_ROOT.'/file/temp/'.$name; // $file 即为 xx/xx/xx/xx.php/ ~3 g. E8 d1 B6 L
# h7 i! {% w5 g
2 z& |4 {' p1 O; X2 K7 Y
而在upload类中,由于多个文件上传,$this->file、$this->file_name、$this->file_type将foreach在第二次循环中被置为jpg文件。测试如下:
( d t8 l# @) h3 n5 Q: s y; B \: x- l8 E 6 E5 `6 D8 |4 a$ X& @
& u9 s( r6 ]( q |# o3 e: n
) A) ?" r) d9 q: Z* A( l
8 Q1 i1 z( X% ]' Z3 S$ u* W+ ~8 D1 S
回到avatar.inc.php,当进行文件保存时调用$upload->save(),include/upload.class.php:50:* S" A' O) D8 y4 O8 t$ {
" z+ ]! t, o* p% t$ r
+ x9 {8 n0 e* I& p! Y
<?phpclass upload {# i( d+ h& e# e
; w) q3 J9 |/ d* G. r
- ~: \* I7 A' Y) H function save() {6 \/ o' P* h& l! l" G `
. b+ M+ j# c) J5 e" n
3 l$ T* V, Q# a. u& c4 y" P
include load('include.lang');7 }* u8 D$ q' `3 ?+ l, }1 ^
- P6 D* t: N; p: x S6 X5 P
& I7 s: Y* ^% @( {0 { G \ if($this->file_error) return $this->_('Error(21)'.$L['upload_failed'].' ('.$L['upload_error_'.$this->file_error].')');* K- D+ ~' k4 t
; G1 T# F4 X/ [) l& C; Y
* F [: m- w4 S4 C" h$ y& B6 ?1 H/ d2 S
) T5 E* B) K2 i7 j8 y
% T B j- @8 d" W# S! ]% |' b
9 ]8 I# {: i5 g, R+ W5 ?6 _ if($this->maxsize > 0 && $this->file_size > $this->maxsize) return $this->_('Error(22)'.$L['upload_size_limit'].' ('.intval($this->maxsize/1024).'Kb)');3 v+ s o# H6 D8 g" r7 C) F
! G S1 _2 E- m$ i/ H: V& V( P% U+ }+ {; S: J( i
8 j. r8 P3 f4 y9 G+ c6 W
: \! G9 ~0 ]7 Z" m7 Q2 ]
w7 C6 q; T! D! l, c
if(!$this->is_allow()) return $this->_('Error(23)'.$L['upload_not_allow']);
% w: W' k" _* t6 j) F! ], {& N' i & R4 v: G; b2 M2 s2 U" [6 y; {
( A( D2 }/ k, ?3 d + u1 T3 b% m2 s# \
. L2 F, ?9 N$ _+ Q9 |+ g
1 {2 T0 y$ }/ v0 t6 F9 `7 N/ i $this->set_savepath($this->savepath);
$ w) s; O' c- P$ d, z+ D
4 @* r; m, H" C7 Y* v! l; V9 G* z; Y' T: [8 V2 h
$this->set_savename($this->savename);
- R3 }/ h3 G- l% I! D - m: A6 P G0 d! X" R5 D7 A
$ F: b3 i1 n6 o$ a3 W. T" A
5 P1 _, G. ~) W' J& E/ D , {/ Q; g2 E$ a I: K
, u, d5 J, ~+ K+ C if(!is_writable(DT_ROOT.'/'.$this->savepath)) return $this->_('Error(24)'.$L['upload_unwritable']);
# D. Y# n7 H) }6 E: @/ v Y% r9 r' J
2 N: S T; ]! {$ l) g! F1 I5 t, Y( k: D5 P! l
if(!is_uploaded_file($this->file)) return $this->_('Error(25)'.$L['upload_failed']);' M' v; m' U! h, T
. ]! Y( W6 v( u0 d
4 P! S0 M/ t6 o, i if(!move_uploaded_file($this->file, DT_ROOT.'/'.$this->saveto)) return $this->_('Error(26)'.$L['upload_failed']);
0 B" c% l, X9 r! _1 A+ t |+ H
% O# M* O3 ~! m4 T: c4 F
" m$ i8 `- I8 Y6 | a
9 ]! G3 N G+ o2 s1 o
: A7 y' @6 y2 k% g/ H5 `
/ E: E% E: N# F, C* C% M0 e' c $this->image = $this->is_image();" D4 ^4 B2 M7 ^% T9 O E, j
+ s8 H+ p, M) R! p
- s6 q! h) m( k: Y2 t
if(DT_CHMOD) @chmod(DT_ROOT.'/'.$this->saveto, DT_CHMOD);
, V/ R) Y) q7 l; F- e# R9 b , d$ ?0 W+ R& @$ I0 k6 k; s
( u+ O0 o) `# @" T# B/ j" F
return true;( B* L4 _% ~9 M/ O! G
$ U& c$ ^. h. ^9 r. U i6 o- B2 e2 K/ I5 R4 i
}}
+ x }* `+ Z1 P: |3 s5 p- M - x5 E3 @4 i+ _+ q# O2 A
& G* P+ y" N3 y- B1 R) D
先经过几个基本参数的检查,然后调用$this->is_allow()来进行安全检查 include/upload.class.php:72:0 `, U+ Q! C1 c) ]4 {$ p1 `5 Z% u0 r
( ?+ `( ^' |$ b5 j- f
% t! z! Z/ T! C+ A- |2 x0 Q" Z4 A% r
<?php$ s3 W% L, p$ D' K: T- u3 V
5 A$ w q: q/ k+ V) e/ t
7 F- y( N. j/ E function is_allow() {
5 x9 t5 s6 ]+ P3 h4 V- e& R0 ~ ; A1 D( c9 m: ~* [$ _3 y: g
: v/ V0 l$ Y$ A, D9 ?7 E8 H
if(!$this->fileformat) return false; h2 x! Y) N# a. ]2 ~& i9 z0 z
$ Z# Z+ H$ F) U8 C
" u1 o7 C& h9 q6 w/ G% u if(!preg_match("/^(".$this->fileformat.")$/i", $this->ext)) return false;
2 Z- R1 p3 m" L$ a
# C, i7 g9 c) r: y. S% ?% I
7 V' t6 a6 j% w3 C1 S. l$ d" t' x0 q if(preg_match("/^(php|phtml|php3|php4|jsp|exe|dll|cer|shtml|shtm|asp|asa|aspx|asax|ashx|cgi|fcgi|pl)$/i", $this->ext)) return false;( U1 u _, V1 r, C+ F% L
( j: _" u3 F* j9 h$ \* R/ b1 E6 G6 d; t; }3 F. Q& o1 _2 @
return true;2 W- Y$ A. q% h- w; J( ?
, o( @/ x9 @7 H. l
1 e" }. P2 J; g3 V2 b, v) ]' C" k
}6 z- U+ B% C p4 n
" h# c5 @( v) n* Q
- F$ c) p! Q& {) l 可以看到这里仅仅对$this->ext进行了检查,如前此时$this->ext为jpg,检查通过。
* E, M5 W; E2 I9 U& _ 5 N+ W* }5 r/ K0 R
5 |& a! [6 @- L& k5 E
接着会进行真正的保存。通过$this->set_savepath($this->savepath); $this->set_savename($this->savename);设置了$this->saveto,然后通过move_uploaded_file($this->file, DT_ROOT.'/'.$this->saveto)将file保存到$this->saveto ,注意此时的savepath、savename、saveto均以php为后缀,而$this->file实际指的是第二个jpg文件。9 v( K9 l. ?' Y0 ?3 B- i
3 I0 Q$ l0 q7 y1 V
H6 v/ X: k. m- h5 m" K 漏洞利用4 B2 y/ v m" K
b( R8 |% c! ]" @6 K- d$ Q g
1 A; {" {* H0 L5 K 综上,上传两个文件,其中第一个文件以php为结尾如1.php,用于设置后缀名为php;第二个文件为1.jpg,jpg用于绕过检测,其内容为php一句话木马(图片马)。; H \# w& X' E& F
0 T" J- G, H* {+ E6 }) [; h7 P6 l0 i6 L {9 N& F
" z3 u: L7 ~$ Q8 `- J + D6 T1 l/ v+ M4 l
' v: t* L, B# k. _
然后访问http://127.0.0.1/file/temp/avatar1.php 即可。其中1是自己的_userid
, x# h% h/ v+ t) v& }
6 a; p5 u" I% {0 B/ O, P0 [ |9 C
5 `- s4 p( A) f0 r 不过实际利用上会有一定的限制。
. l# R/ _9 o+ c" O4 O( n! n9 P0 r9 w
* P4 I& P1 D( J+ C3 G9 E
+ @0 I u6 v& I. e 第一点是destoon使用了伪静态规则,限制了file目录下php文件的执行。7 r3 |: D- [7 i
( \, v1 M; I! x+ O. }3 t6 `" X/ W4 K
# v2 J7 ~" `( m8 _. a; [) y( D6 }0 |1 n
{4 ]; n3 B( {* d: E# X" n0 U$ @
* V+ Q5 e1 n1 B7 h' L/ ]5 R" G5 D
第二点是avatar.inc.php中在$upload->save()后,会再次对文件进行检查,然后重命名为xx.jpg:3 o0 @( L% m% z2 I/ f% _8 S# q
& \$ D3 _! S+ Z$ G$ @; [4 U* D
$ T4 d5 R5 v+ k8 |) |, Z2 p 省略...$img = array();$img[1] = $dir.'.jpg';$img[2] = $dir.'x48.jpg';$img[3] = $dir.'x20.jpg';$md5 = md5($_username);$dir = DT_ROOT.'/file/avatar/'.substr($md5, 0, 2).'/'.substr($md5, 2, 2).'/_'.$_username;$img[4] = $dir.'.jpg';$img[5] = $dir.'x48.jpg';$img[6] = $dir.'x20.jpg';file_copy($file, $img[1]);file_copy($file, $img[4]);省略...
+ j- \6 j/ `1 N4 F7 p 6 g5 R4 F7 t$ [5 ~* }6 c$ L$ I
( ?1 d& }& b# M* y
因此要利用成功就需要条件竞争了。8 F6 n$ g2 r* A0 u/ A
6 c( I" n; w9 E2 P
' Q; H, j! O0 Y- b( a* G \ 补丁分析. |3 V* H% f+ h$ o7 `
$ k: I3 G+ |1 M
) ~* W: X+ B0 ^. G% L" W0 [
$ h5 Z6 F; k, g" A* c
3 k b P7 y: ], O5 ?2 y# ?
; q: I ^8 b( x* x 在upload的一开始,就进行一次后缀名的检查。其中is_image如下:
- O* h8 H& P# v7 \5 b) T4 y , O7 J9 z( ]2 t6 o4 ^
0 f3 g8 E( R z( w9 o! n function is_image($file) { return preg_match("/^(jpg|jpeg|gif|png|bmp)$/i", file_ext($file));}
" \) V' f- V$ c+ O& V
3 p1 t7 g/ M2 {+ i. B3 [2 i
( U# o* m5 \+ @% t ) w2 y4 w4 d! I# {0 s
0 \+ @2 z7 B$ E& V S
8 t2 u, f) d# a3 ]( A 在__construct()的foreach中使用了break,获取了第一个文件后就跳出循环。
+ s5 z3 M. M M' Q: d & R, q2 T1 \( Q$ Q; I+ E+ ^! c
& _& P2 q' c* W: S6 J' n4 E 在is_allow()中增加对$this->savename的二次检查。. m1 e: c6 O! l. ^2 P
# E7 [8 o2 D. ^) |, e |( Z' Q3 b9 e, _7 A. ] o3 `. j
最后) G J- [6 y( ` x7 j0 N
1 c) |, Y7 D( v7 Y0 L# p9 z( a" V
4 i0 k* `# @ A, L6 u0 p 嘛,祝各位大师傅中秋快乐!
1 y6 f) A/ x9 L$ O# m, t/ P* W7 m1 A
7 s7 k6 @, t7 }4 `, f) b3 ^) @; U! f* V
6 [# ^ f1 D: q8 o3 {
" u2 s8 X% w4 h: {. P ?, [
|