8 Y3 O# G; D L% F8 o& s
% ~0 B, H. w- ?3 }0 m
{) |8 q, x+ E( S6 l6 E$ o$ P: ^: v7 r- Q7 o* v
前言
% r8 z. f; S+ D2 O1 q9 E! f) X K
z! s; c# D! Z
3 {! P6 Z. _& C& w 2018年9月21日,Destoon官方发布安全更新,修复了由用户“索马里的海贼”反馈的一个漏洞。
3 U4 E, A: W2 I6 ]
+ |! l# {, z( R4 x4 m+ ]) [) P& ~* Y+ R& M" v* B
f4 [9 C. n+ Z& i/ v
8 A$ d3 J& }% q" G4 x; x8 M
) e1 i( t5 m( Q( L/ E& B( X+ y$ ` 漏洞分析
2 ]3 I4 s7 q; M6 S& U$ v! u$ y1 X0 W h" c) Q7 e
' u/ n* q. \4 P7 W
根据更新消息可知漏洞发生在头像上传处。Destoon中处理头像上传的是 module/member/avatar.inc.php 文件。在会员中心处上传头像时抓包,部分内容如下:
6 H7 R6 d2 j& t0 a+ F( T2 E
9 H: v6 y& Y+ b* l" _ R6 x6 y
5 V1 Q7 r4 R: m! _ 6 E7 x1 t* X& j8 P7 w7 I2 k9 _; Z
% Z( ]: Y8 K- B+ c6 f( s# j8 X5 s; A7 d6 V
对应着avatar.inc.php代码如下:6 J( ]6 a" v4 Z: e* y# {8 e/ C
* X% V/ `2 B) r. l8 ]; n
0 X- a. S+ w* W; k+ I& y
<?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) {8 f7 `7 W, {- m8 D! }/ t
! X: _6 ~. Z, M
4 _$ f& h4 B: |# ^1 ~; R- ~ case 'upload':: V, F( m+ {/ o: ~
$ I1 w& E7 s) e9 V& A' g1 U0 \; `" }: f( f7 W+ |! c4 Q
if(!$_FILES['file']['size']) {# ~* q8 E x; C8 D9 H
' t! t9 W, ~; S8 Q
( H/ N G% T8 e, D" B if($DT_PC) dheader('?action=html&reload='.$DT_TIME);6 N& E' P! O7 h) V0 ?7 d# U
, B7 a! r5 ?6 D. C0 }1 u
3 Z% a r/ u# v' }
exit('{"error":1,"message":"Error FILE"}');' k5 m% F9 S4 }3 D: i6 I2 n
2 \ X, @* M% o: y3 k
+ a3 B/ }& y, y. ]& y. w* v0 h% E
}" Y, X4 W, X4 K& o* K) f: Q% q r
/ g( L* T* ?# X' Z7 Q$ w' R/ m
B9 ~( j! j+ D& \
require DT_ROOT.'/include/upload.class.php';
+ F5 v, _8 u8 j `! S
+ L2 p$ h7 G' K3 q
( A" U6 e) h8 R8 O0 t4 B
) K& d' a: L* e# L( V
. M' g! O2 d) K' Q7 a, |2 s: M# P
$ext = file_ext($_FILES['file']['name']);
# B9 [( G, z! W& ~0 z 1 o$ ^2 e! W& _* Q V4 x
% D9 @0 B/ \+ \6 M- x5 x! g! S3 s $name = 'avatar'.$_userid.'.'.$ext;
/ ^% b6 y2 v! G3 t4 { ( R" I9 k0 K, o/ g5 ]" `/ n3 f
( ~' N( ]5 z1 I, N4 t
$file = DT_ROOT.'/file/temp/'.$name;
- w- l( p- o- f9 B& Z E
$ m( \6 ~7 A4 i; o4 D: n, ~8 L2 ]( f
" b+ w5 M$ E; g% O3 s" W; y
) [, l n& s% }- z4 Z, K, ^7 u
8 ^9 L0 K7 v4 [
9 ]7 k! Y- N7 A9 k! U9 v/ C" m/ s V' S if(is_file($file)) file_del($file);/ m. F G+ I; M, A* L/ q
4 a/ {( f' w8 |- V- c" t
+ n8 F% V1 [9 }4 z7 F( ] $upload = new upload($_FILES, 'file/temp/', $name, 'jpg|jpeg|gif|png');; Q" ^1 F4 W4 W/ F( H6 j$ Y
- I* x6 {% \9 l) {# s& d C+ V; Q, |/ C) ?
|. _6 V7 e7 N {
5 H0 J% Z1 \5 j1 b2 g3 ?9 l& n
9 c6 Y: r" M* r, w% |. ]# G( [* o
$upload->adduserid = false;
* {/ ~: y9 O. \: a+ c+ E3 F 7 B r# [8 @& y1 b% {8 q2 {8 Y: x
8 h$ e" c& a! p1 A+ Q
( v% `2 c9 C$ P3 J- b $ j' `) d8 [7 ^' m6 a
; Z# D* O% ^% F I3 E9 v( {4 J2 H! I
if($upload->save()) {
& ^1 A4 x+ T6 A3 e! B2 S$ n* t
( ^7 K/ X2 {6 |, X N3 X4 [7 b9 I# o+ d. I& b' w
...3 O5 {9 y# v: y* O" B( [
h1 v/ {1 G: a) R
$ a$ B. Z, H% K% B/ i5 ~0 O } else {
( R, h: ?# T5 S( j. b0 x: o- G% [7 i ( k1 q7 h5 D/ N1 [; O
4 V9 a* G1 M6 _" w
...) E6 Y5 c, y/ i. u0 a) R
1 w! d9 n4 A4 J
; k2 m, `+ t, A0 X' T- f }
4 N2 }8 H6 `) W/ O, Q' S ; S. r3 W, T% L8 r
$ M% S! N5 c$ u3 ^* y6 R# v
break;& o4 i' O5 P, Q. T: N
" c Z# u& s! d+ U8 r. M
# j4 T; e, a: @! D2 U# J$ D8 a 这里通过$_FILES['file']依次获取了上传文件扩展名$ext、保存临时文件名$name、保存临时文件完整路径$file变量。之后通过new upload();创立一个upload对象,等到$upload->save()时再将文件真正写入。0 [: o& K$ u: x; v7 X7 @
3 J3 ]7 W- F1 h; ?( @3 ]# `2 r7 h$ h: v' h. T5 B+ O
upload对象构造函数如下,include/upload.class.php:25:
7 M+ O5 q# v5 ~. r; O & G! J: L! R% b( R% b/ j
e3 |, f8 r8 o8 g0 N <?phpclass upload {4 C/ {0 H3 h) W
9 {' o' P0 z% E( { I
' F P2 c/ T4 m1 d5 r, D function __construct($_file, $savepath, $savename = '', $fileformat = '') {2 L9 K; a, O% e8 G1 R) O) [
! z8 X& e1 {# E0 x/ h+ ^2 p$ L3 Q( y
global $DT, $_userid;7 [5 ]4 S7 x) _3 d
$ M3 }: c9 H% G9 j& k
$ m9 [( L( j+ u
foreach($_file as $file) {. @5 ~6 I. R/ H) g
5 i( u0 w, r+ Z- I0 p0 c. Q/ ]6 j R
6 M* G, @: w: g $this->file = $file['tmp_name'];3 s& m2 ]( x. s+ E* M
f$ @' G0 n& n |
( M; c; K! O, Q& d6 ~/ O% v
$this->file_name = $file['name'];
; d. A" g' h4 B! V9 I4 b# K
% |4 N( i7 v: D$ D# g
; N+ D( j% }+ D; a5 r' H9 Z $this->file_size = $file['size'];
! e( _# l& z0 o4 N % w$ v* D7 m- J# s& j" c
' `9 q/ w0 K) ~2 i9 f! K $this->file_type = $file['type'];# h/ P. O) T* c
+ @/ Q4 b8 u4 D
K# P/ |5 g' x* C $this->file_error = $file['error'];
$ J6 Z) ]* I$ `, s 5 a/ F) ^/ v: a: }9 V C
/ x1 }# b/ Q3 X; l* ~- g
: }) ]' D8 G! L) Z( J9 t
; i! O0 C9 ~# c& T, X& E6 W$ o
8 M# `, H" N! z0 c# k5 Z
}& A9 y) K8 M9 g2 g. a/ T
( b% k. B! u+ c3 q# a8 }
" b9 ]+ y6 z$ {9 j* a9 C $this->userid = $_userid;; J8 @- A. q# A- |4 B+ m$ k
4 B0 t5 g0 Z+ k. P* w7 Y0 T5 x* b+ I$ a( }% r8 k7 o
$this->ext = file_ext($this->file_name);3 u' q `& r9 O( T' i; [
* v3 \/ f1 L: F% ^
$ Z" {8 P' l i: S9 S $this->fileformat = $fileformat ? $fileformat : $DT['uploadtype'];
3 r( j( A$ s5 T. o) W S1 x7 n - U- _8 w" Q5 x8 I6 A8 j
3 e; o8 g* z5 l1 g0 ] $this->maxsize = $DT['uploadsize'] ? $DT['uploadsize']*1024 : 2048*1024;9 |3 H. q% L* F5 h" [- s+ V
0 S6 q9 D6 U8 I8 T. ?8 n& Z
d/ |5 o1 c9 O2 V) M/ x+ C$ V $this->savepath = $savepath;/ o0 O/ l0 b7 x! g$ J4 Z
3 b. T1 Z' Z$ ]( G/ ^
! w7 r5 o1 s5 a p7 I; B: d $this->savename = $savename;
j3 S% S0 @7 e , @/ h) d* S5 x( D7 E# u) I
+ y, m+ N) i! O9 d) T# L
}}
9 H/ U6 Z P( P 6 L: ], e/ {. r' _; d2 r
) l, I8 h3 W* `% D+ Y% v 这里通过foreach($_file as $file)来遍历初始化各项参数。而savepath、savename则是通过__construct($_file, $savepath, $savename = '', $fileformat = '')直接传入参数指定。
5 N8 j% l r. P, [
: k& s( v/ i3 L( X+ P7 R/ d2 o, B1 Y/ x
因此考虑上传了两个文件,第一个文件名是1.php,第二个文件是1.jpg,只要构造合理的表单上传(参考:https://www.cnblogs.com/DeanChopper/p/4673577.html),则在avatar.inc.php中 2 F- z' ^1 r. d' ?
; k i" {: S$ f
% @/ M! L* m* n) Z6 h' S) t# l $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.php7 C) |+ I, G, @
/ L( }' f0 Y1 S. [3 Q/ x' ]
$ `: m1 V3 T: L5 u$ z! H! ^
而在upload类中,由于多个文件上传,$this->file、$this->file_name、$this->file_type将foreach在第二次循环中被置为jpg文件。测试如下:
. g% m! g `( ?0 s2 ~ ) {) s4 g8 M. c7 E! O# w g
* V7 U4 ]4 x7 u% y9 I
U/ z1 f# F/ q( V* _- w5 U' P' N
/ e2 Q. \9 }. [0 t4 [) [& S
7 j0 ?% F5 t, H6 Q 回到avatar.inc.php,当进行文件保存时调用$upload->save(),include/upload.class.php:50:1 I' B3 k1 ~8 a5 E; ~5 Q, y# q
& @ f& m& `4 t
6 ^2 S, {) G( m/ H9 a
<?phpclass upload {0 K2 S0 h6 g0 S- L
3 X0 r" ]9 M6 \+ Z" E) H
, m8 i* l- \4 P* _: K
function save() {0 W, u' w: X& d# E* ?0 U
& S3 ^. C+ }( ^; z% _$ f9 P% T4 I; _
1 L" I, o- K" P5 u* n- v
include load('include.lang');
5 A2 i4 m+ H# u. O9 z# o 7 [7 D) L, @1 d
& G: |7 L D4 u! f4 K. F
if($this->file_error) return $this->_('Error(21)'.$L['upload_failed'].' ('.$L['upload_error_'.$this->file_error].')');' C( r' j/ P5 g0 V$ E* e3 [! C
8 }- U! D9 |. p/ b! X, i' a( w9 v9 r( H) [5 i. Y( }* e
/ ~* C7 x$ n/ C t' y ) J! }9 |$ ]6 `; o/ s9 a" z9 P
: x7 D$ l3 C' f2 T9 ^: S& L
if($this->maxsize > 0 && $this->file_size > $this->maxsize) return $this->_('Error(22)'.$L['upload_size_limit'].' ('.intval($this->maxsize/1024).'Kb)');
- r" d2 k, p* p. u a; G' V ) r% E) R& Y4 t" O: i+ o+ T
8 i% b7 }2 d: T+ Z6 h# y: O
1 W- E1 h! c4 J
F! _9 _- M0 ^. @- _
6 A8 P9 w; v8 N& F- E$ r if(!$this->is_allow()) return $this->_('Error(23)'.$L['upload_not_allow']);1 O$ H1 C2 r) I8 i5 J5 p
* Q3 o0 J6 f1 D0 g
6 g* ]1 }$ X8 V- h3 w. N# z
0 \& y' Z3 Y" {# B9 q5 m0 s4 v, D$ B
( V4 E6 P4 r) G. G% g. U' T% W- H9 w, `
$this->set_savepath($this->savepath);
- T" x* |' h* @) y
- C/ a7 A, I5 Q& u# |/ T; @8 A& ~- @# l. i5 n
$this->set_savename($this->savename);+ W6 Q2 q6 w6 N8 X) Y+ e% e! s
0 t5 n! ^. D( I# A8 x. T+ A% h' r3 X; F# G- m
+ g9 r, B- Z) F5 s: D* M) y
8 b7 c. I- Z# D; u9 P& W- P
) ^, w; N& p' `$ z8 ^ if(!is_writable(DT_ROOT.'/'.$this->savepath)) return $this->_('Error(24)'.$L['upload_unwritable']);" b3 p3 T+ y9 g. w
" {0 h' b$ t/ b: t k5 I5 O/ h
6 r4 ~8 K" R/ [. r! H' x# ^ if(!is_uploaded_file($this->file)) return $this->_('Error(25)'.$L['upload_failed']);- d) C7 T/ q1 M1 P& I& i
( P% k# t% b4 ]( @2 A3 v( ]& F3 ]" Z0 E
if(!move_uploaded_file($this->file, DT_ROOT.'/'.$this->saveto)) return $this->_('Error(26)'.$L['upload_failed']);( t5 i, f& P: r$ d. V4 E+ |
0 f/ ~8 S/ d' h# d; e# \8 v- h3 w& A/ l+ q+ z3 e! X$ r S/ W
, g/ m0 [; D, R# P
. s1 Q( Q) s8 ]' j) D
j1 f: s, Z4 B$ e5 `7 }, `9 }2 Z3 c
$this->image = $this->is_image();9 C/ L# {9 ]" e8 I1 Q1 t+ o( y
# @8 G/ d+ ~' Y. v7 ?7 w3 a/ Q2 z4 l; M/ b
if(DT_CHMOD) @chmod(DT_ROOT.'/'.$this->saveto, DT_CHMOD);
3 |3 y# w) q6 N: j
) a5 K/ @% i4 [- T# D8 l, ^* O* t( p+ `1 _) I
return true;
+ S' ?) |! O/ c5 b' g . V6 `# [8 C2 l# H0 @' U
- T6 K, A% N! c* {
}}
Z* [# I% R# k& N! h0 }* T
# E- h- S5 V3 i( ~/ Q* u3 X) n
* S6 x- I' v, e+ K4 r% v 先经过几个基本参数的检查,然后调用$this->is_allow()来进行安全检查 include/upload.class.php:72:1 m8 d" X& u L7 n1 T3 }% D
) K3 E5 q" _/ F4 Z% D" m B* `' V A" P6 Z& }( Z3 b) Y4 U% x
<?php$ e, v9 z( x. N8 _* o" z7 E
4 k4 `3 z% c/ A) P6 U- j. X `( I) j
function is_allow() {/ a% e6 Q! ]8 m- \! X8 F4 d2 V
) z2 w! b, ] z9 N$ K, Y. T. O* ]
: h) k% U2 K, Z8 U3 R
if(!$this->fileformat) return false;
8 Z4 a0 h+ ~( k/ s3 P! ^/ Y+ g1 N
D/ ]+ L0 P: w
5 m) x9 _$ i& {, t! [1 w if(!preg_match("/^(".$this->fileformat.")$/i", $this->ext)) return false;
, v+ P% P; \9 R4 A; R( Y + l0 D- B& S& s
% p8 \& S5 u# t- j$ [3 f3 h 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;$ l C$ o+ b" d& M5 m* I* @) V
& T8 f1 n+ e3 W, H6 S
0 G8 u0 P' p! Y" D5 {; n$ S return true;5 @8 y: j1 w1 R+ @+ b0 @
7 C) f* H; J4 o1 h2 c- l* z, x2 Z0 O) k
}! S7 k7 g- w. J, t2 C
$ T; k: y/ U8 t. I: Z3 g
c& G2 x, P0 Z* b& }- x4 t
可以看到这里仅仅对$this->ext进行了检查,如前此时$this->ext为jpg,检查通过。
9 f. Q: Y& m: i6 H, F. m2 E & D2 T1 e* B1 q" h8 a5 W" a
% d! y( e# N% M9 w. d& M8 b3 R
接着会进行真正的保存。通过$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文件。) d; I5 c/ m! {% [2 {4 K5 l
+ `9 n- h$ M$ @+ g1 t2 ~ v
7 b- y" ]7 m4 }, X) K7 ~- X 漏洞利用" V' q% a; A9 G2 W, Y" @
\+ c# `: E) {8 k. H P8 |% A' y( i+ h- p
综上,上传两个文件,其中第一个文件以php为结尾如1.php,用于设置后缀名为php;第二个文件为1.jpg,jpg用于绕过检测,其内容为php一句话木马(图片马)。7 H8 E! ? h6 w# j
; y4 O$ R0 N- W& y# v8 C6 Q/ j1 J! W h( M
( I, M& q' ]4 |+ P/ t% D' n9 p
2 {+ s/ x5 F3 o7 i2 ^5 Y
- t2 d' S) ?& ?/ n9 W9 ` u 然后访问http://127.0.0.1/file/temp/avatar1.php 即可。其中1是自己的_userid+ ]% n, C3 r/ ^4 Y |
3 L. O/ P/ F0 U5 y% L" q: R2 C; V) f7 r9 R4 s: T& ` y) U& v
不过实际利用上会有一定的限制。
7 A( Y! Z ]: F # }. B0 f! B9 m4 Q+ }6 p5 \
1 K9 X/ O" _$ G" t. ~- k9 y 第一点是destoon使用了伪静态规则,限制了file目录下php文件的执行。% Z2 y9 _$ {+ O3 ^* G8 [
" [. p& }) W- Q1 m, o" y- Y. z! N' A, d( J! h; [5 P
9 F+ `+ q( `. l' j/ j0 H
( x; t0 ]* R" }3 I; e$ u
( ~: a& A, ]. Q. L: H& M
第二点是avatar.inc.php中在$upload->save()后,会再次对文件进行检查,然后重命名为xx.jpg:) r/ }% p G" U+ C w7 f$ n% G
) Y- ^- ?0 y# B2 b
9 [6 K5 \; d+ t* h( _: r
省略...$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]);省略...8 U, |2 z2 ], Z
( |: H1 n* }6 g" E' H) O) @$ s6 I+ o- o W7 E% o8 a8 o' W$ t
因此要利用成功就需要条件竞争了。/ [. @. ~ L' A9 M; Z) |
, _8 v& e! T, T' Z
) A; Y6 K, {1 ] 补丁分析- D2 x& K1 n/ P
& ], j |: a1 @5 p. o
) I3 c0 \% Y! c: F" Z. W! j+ ^
+ w" V* y4 O+ k8 v: N/ I; K/ o
5 p1 I* n6 M0 r& T) J* ?( w& U0 G" r% x: r) B4 y1 N* H
在upload的一开始,就进行一次后缀名的检查。其中is_image如下:: C: h* ^' @' u0 j3 o4 r9 A
2 N% }0 C5 k; a& v
; Q5 d+ X9 u! p; c) d6 ? function is_image($file) { return preg_match("/^(jpg|jpeg|gif|png|bmp)$/i", file_ext($file));}
$ L; n9 M8 C! j0 j& c" n2 p! M
4 y1 a- z0 o" z: @: A) H& u; D6 d4 H' b$ ^9 Y
$ Q0 U8 y, Y V9 i" A! |. x% ? ~7 G
8 o) b8 d8 p% u1 x6 H
/ m- O X3 F2 e 在__construct()的foreach中使用了break,获取了第一个文件后就跳出循环。
4 K8 r0 G& O. O. X B - { x4 T( }" p6 |+ C
4 a& }; ^/ D- t/ i; d
在is_allow()中增加对$this->savename的二次检查。
0 s @8 d- v, x8 k
5 i x5 K+ j# E. t- @9 {& d. @
. R' n/ |# \ g0 }0 [& Z& Q 最后& X6 O2 w' P/ _
$ a+ A) K ~4 J) C# R* _" {
3 h6 N* \+ H8 O5 A: z, u
嘛,祝各位大师傅中秋快乐!3 _* Q% I# B/ C
2 O8 x* F2 y( V% Y" [
. F0 L- n6 \+ }# ?6 ]* H ~
0 c: q1 ~1 j/ p1 H6 R& P
3 h5 {+ O6 O1 d; k, i/ l
|