8 g5 \' T5 u! @( t0 l" Z Z
+ e' d. h) Z: D; g' i
! C, T% g. L/ s
' O/ r5 o; [. P( T% y* L% {$ C 前言
$ q8 j8 H% k, ?1 d( U3 n+ J/ n5 M5 H! x _/ o" i% L
; |0 f1 B6 }1 ?+ d 2018年9月21日,Destoon官方发布安全更新,修复了由用户“索马里的海贼”反馈的一个漏洞。
2 ]8 D: O* m1 o& v, X " h: }6 |! Z4 J* f `: \2 } D
; Q, j( K# R5 {7 y: ~
( F( i: e. U# X
5 ?8 e& `1 e0 c2 G$ H$ |1 |, X' }+ P ^( @
漏洞分析, Y4 |4 Q* U; G$ r8 a q7 w
3 w4 _1 d9 s# M6 u7 |
$ ^ q) V4 E6 ^5 ~5 b& v, ~ 根据更新消息可知漏洞发生在头像上传处。Destoon中处理头像上传的是 module/member/avatar.inc.php 文件。在会员中心处上传头像时抓包,部分内容如下:
) h. f! M( l/ F2 O% \ }9 y& \ ( t- P K& p( E" A& \& I. D
2 e% [& L: N0 ~! c! ?4 S * d6 e9 t: b# Y4 F* Y
" O4 L v, P# r' e5 B& o, G
+ f5 M" u6 X6 \! q1 K4 ~) E1 Y 对应着avatar.inc.php代码如下:8 q" Z6 h7 f+ }. p
4 [2 x3 g- }5 O* s+ e. a
! C! @) @5 H, `$ O) {! B. J1 b <?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) {
+ b8 r1 K: S% M
4 N: d( t1 \6 @ I1 L" I' p% G2 M$ [4 s+ R- D2 Y
case 'upload':
6 `3 ~( a. j. g6 m" ?6 W7 L" E , `6 i) V* X. y" c3 D! _, {
' Y8 C( H% W0 c/ Z
if(!$_FILES['file']['size']) {$ ^" c9 e2 V# ~& G
, X7 L4 n- [# r' _4 F1 Y2 F
6 O3 n- `# Z/ [6 [. |6 U if($DT_PC) dheader('?action=html&reload='.$DT_TIME);8 E: o; P$ o1 b/ _- p$ s% i7 c- S' ^
, E9 x" D2 p( r9 n1 G- Z. }. d, \- G2 u6 P6 A+ A+ s) [
exit('{"error":1,"message":"Error FILE"}');/ e" W5 F7 w- P0 I6 f5 g' L1 ~/ u
& f9 a+ B4 x8 W+ x# u) c$ R5 ?, n) Q T8 Q8 w: q; E9 h4 g
}
3 e( K3 w7 V; P7 w( h4 g- G
, z6 N. I' H& l9 g- e& k$ u4 z4 x$ ^2 p/ S" u& S$ e
require DT_ROOT.'/include/upload.class.php';, X7 e5 O, w- \, L
* k# n: {) s2 ^
7 D" {& y/ \6 n) A: U2 I6 l# @# K
0 n2 ] l; E) t4 E( s9 i
2 ?" s2 Z. d) j# F- J! y# ?( H0 V j7 X" x3 s [
$ext = file_ext($_FILES['file']['name']);# O( ?3 q2 v/ \
- @/ w0 y$ h+ e9 n) G
3 B9 O* O9 p3 H' \4 p A $name = 'avatar'.$_userid.'.'.$ext;
x8 E$ I4 L1 Z: k' d8 Q# @! c/ ] 9 y8 s; `4 d+ _+ M: w! H* K* ? }
* R8 k& O' M7 a$ X' f( }. F* N $file = DT_ROOT.'/file/temp/'.$name;
4 p1 z6 i; `: \+ ~- ^7 j % O" N9 u3 }9 H7 W7 m* |
0 T: Q( _: ]% a" y6 e4 X: S" F 7 i3 z+ P& N, \7 ?+ U( t, Z' ^
$ q5 P i- `! |- D, D8 G% g$ X, h; r; O$ [+ Q& k" F- P; \
if(is_file($file)) file_del($file);) m5 T$ ~. e7 ~4 f5 N U
% T( {2 j$ |: ]' V& P
2 J3 F! d3 ?1 w* Y: M* R $upload = new upload($_FILES, 'file/temp/', $name, 'jpg|jpeg|gif|png');
- Q' R1 i) b& ~1 _8 C
# m! M. Q( n3 j0 G# S1 [1 {( Y
! f) h! x. H( i8 a0 k * T- | A2 ~/ h( o
; g# D( f: c' N6 Y, e7 d: U
/ t2 c6 s" T3 l" n9 I: ?
$upload->adduserid = false;, Y0 ~/ B/ ^( } M6 a/ p
+ E! @, r4 ~7 z i h9 `
& t0 Z3 i0 `" g! J$ D5 B
+ ^* d* k# j! U6 J& d3 m1 ^4 G7 i
( v& l; H9 U1 W" i1 c; J2 n$ ]- l8 m7 V% y9 m" i
if($upload->save()) {
- U. ]! |1 s% L# t5 a$ c" n' _ 0 Z" {9 l$ i: G. w/ W- p" A
& v q( {& \* @6 G1 k ...* X& K$ N5 H( x* J* s# O9 M
7 [0 s% q& G' N
1 i5 F8 i1 l* h' x' t } else {6 {( F$ V4 G. B
3 F0 G: b& }. v3 b+ U6 n
! V+ ~1 N2 m5 {; Y/ P ...
' c+ l" T9 l0 Y; |7 s( m( `' U, f ; k$ k4 f4 ] n! v" p
! q2 n6 _/ }- @$ i }
* R5 W3 `4 o# x! b5 U 3 W k- @+ g& w0 \4 G! M. s- Q7 x# j: U
7 {8 }2 \$ P& h; _0 X
break;+ u- [# n2 ]! K- I/ v2 D: o/ r! Z
8 o' y2 ]4 E7 t* K
! _! s9 W4 o9 v/ n" m. s 这里通过$_FILES['file']依次获取了上传文件扩展名$ext、保存临时文件名$name、保存临时文件完整路径$file变量。之后通过new upload();创立一个upload对象,等到$upload->save()时再将文件真正写入。' L! w# V+ W) |" U. [9 e" F4 E
0 p% G/ ~9 b& a w Z# l, L& M! |2 V
" p% H2 U. ?* ~' A5 A( I1 @! S* {
upload对象构造函数如下,include/upload.class.php:25:
- V8 S3 d& D# A! m+ [: K# {7 n
& O2 k4 V O' [6 }
# h1 P4 ?; \5 z# S) d/ N7 E <?phpclass upload { M6 e' j( a* s: M
S9 h! U7 d5 _4 s6 M- A! r
( s- j" t# N! e
function __construct($_file, $savepath, $savename = '', $fileformat = '') {
8 T! |- H5 U6 `7 M' b 5 ?( {- a( g$ A0 b1 x! M
( s: l% `3 x* R1 l4 P global $DT, $_userid;
( @ x6 }# j2 U* R4 K& S, I' ?4 N+ F
4 b0 d; {+ f% U& r& T1 l/ ^7 R% f4 Z9 i4 J( M/ G+ ?% i
foreach($_file as $file) {& F! C( [/ T: p0 r' h& e$ R7 |
' J( O0 z- m" c% _1 E. [
( a, [& C) A7 b- | $this->file = $file['tmp_name'];$ C$ y9 V g3 ]
6 ]9 w$ a8 ]! N7 l. n; f Y
* L z( O j2 T0 P
$this->file_name = $file['name'];% v$ \( B0 ~/ ?- M: {- o6 V
- t" u& S7 `' C3 ?5 P- s, d
6 i9 R' {3 k+ O% ^
$this->file_size = $file['size'];2 l1 ]/ v4 g# G. F) n
2 C8 ^( Z5 n0 u. p2 s$ m, _- |7 e' L1 b/ x2 B& `
$this->file_type = $file['type'];
/ e' t m) u- M [* \1 W 7 x* R9 a1 [5 |% ~8 A# z% J+ p
- T* M- P( `8 x2 V
$this->file_error = $file['error'];5 r( ]# ^; d1 D2 ]$ _2 {
: b. ~3 [& _5 \4 R( |: J
" Z! |1 M- e9 D) f, @9 s2 | / _8 D4 W4 e1 R. v) p
! |% x/ Z/ R& W5 n6 t
- K, r& K/ X4 y x
}
2 l' k2 N3 h& `* {8 e2 Z
2 g k& b# v% t/ y, C3 l) B/ b0 S2 ]
$this->userid = $_userid;
6 V- J' p" i0 H: v ! J& @4 S! K3 d9 {
) P2 W: K- p" T2 z' @, _ $this->ext = file_ext($this->file_name);
& |' V+ K. x, J+ P7 v. v' ~2 v
, z, B. ~, E7 e( w& V. t) s2 a f+ i) d$ O. v$ c
$this->fileformat = $fileformat ? $fileformat : $DT['uploadtype'];
/ K0 P1 @: R" w" K3 ~& D" f
# v# A; D" ^6 |9 O9 l
4 E$ ?5 I* p% I, t $this->maxsize = $DT['uploadsize'] ? $DT['uploadsize']*1024 : 2048*1024;
% V1 |% P) X+ d
5 Y8 {# w& b2 o$ t) p
, h; y$ v7 i# Z3 g0 g $this->savepath = $savepath;
3 t5 p0 Z4 h3 z! c" M6 l
# o5 q9 B4 d- G Y# g
# ^! i* f3 R! N& a/ s $this->savename = $savename;- U- x, Q) |; d, L1 H/ h6 N
5 x' t+ D* T, c! G, }: V6 e. d8 V4 w: C; E4 O
}}: g; [7 w0 c j# ^. [! c) h
0 L/ O' g5 a- \( @( G9 }2 u! _
5 D: V' {* D5 j2 Q
这里通过foreach($_file as $file)来遍历初始化各项参数。而savepath、savename则是通过__construct($_file, $savepath, $savename = '', $fileformat = '')直接传入参数指定。
g. D: {1 C( x3 h' V4 b, H
5 W' w0 r1 U! v/ |" _. x& R" ]
# }7 ]' U* m3 T9 g 因此考虑上传了两个文件,第一个文件名是1.php,第二个文件是1.jpg,只要构造合理的表单上传(参考:https://www.cnblogs.com/DeanChopper/p/4673577.html),则在avatar.inc.php中 ( F3 e. l4 }' d' p, Z+ D
- O& U6 h: _. f& R! D! k7 e7 U4 R& f: A( i. c& p! l% z
$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
4 Y& L* ^8 n$ X7 n
, D3 t$ f8 V" Z1 f @ s M+ p8 X; F$ U1 r0 }
而在upload类中,由于多个文件上传,$this->file、$this->file_name、$this->file_type将foreach在第二次循环中被置为jpg文件。测试如下:1 K" w. p$ }5 R$ h O
" k+ [5 r X, A$ @% a0 A
8 J) {5 s/ F2 ?
# P3 g! r% T) w7 b 1 ^& n* ~6 v# g/ h9 n" a7 c+ {/ I Q
" L& Y K5 @/ G- o4 M) _ 回到avatar.inc.php,当进行文件保存时调用$upload->save(),include/upload.class.php:50:
; r+ V2 b" K6 x2 r1 U
4 q2 `% Z( i6 U+ R2 t, \! I2 n# w1 J4 O/ q2 Z- |; g8 [1 C
<?phpclass upload {+ l: l5 p: D% I6 b- f
, t% z/ Q( n* T& U' |" G) @' Z( M! _
; \) F n- n: B6 h1 P* i: ^" [: Q
function save() {
( G" s, p) K! Q' ~/ J1 U$ ?* ^; b1 i ! i8 K) k i" H4 j. ?4 v
0 e+ K* y( V( z+ R o include load('include.lang');
, ~9 p: K3 t, l( J- T# z' s
6 M5 o( E1 s" g7 g3 {% v; u! s9 L l6 }% l$ v' m4 R
if($this->file_error) return $this->_('Error(21)'.$L['upload_failed'].' ('.$L['upload_error_'.$this->file_error].')');
# F9 E/ o" ^; y: B9 l. S" U7 ? * V* p0 u: @3 ~- Q7 H( A
9 x5 |) s5 I _& t; X2 L( P 9 N6 q3 J1 ~9 o, K7 \
, j. ?* U$ i0 `6 m0 }
0 E1 C! I# ~3 T1 V" K% U4 G. r- t if($this->maxsize > 0 && $this->file_size > $this->maxsize) return $this->_('Error(22)'.$L['upload_size_limit'].' ('.intval($this->maxsize/1024).'Kb)');
& Y, M3 g5 r* U% ]! _' S
& [2 c, P3 m+ [& w& l+ S/ b* S W+ m8 k* j1 K
* X2 O% B: ~) A9 r6 \* ?# @
$ P+ I* n* o5 f, ?
4 c, P5 O1 j+ g6 q/ X3 z8 z) I if(!$this->is_allow()) return $this->_('Error(23)'.$L['upload_not_allow']);* Z: G% \ H* \7 `! G |
7 q) ]! z. H0 ^% @0 ~ P- T
$ M5 @( p0 K$ m( D# t
5 G( c9 `) H% @" ]$ ^! _* D% N
& V6 a3 E5 W$ D0 H" b ?9 w- Q: K3 i4 j9 y& \3 V' \
$this->set_savepath($this->savepath);- L0 n( G) ~$ B" k/ C. m/ _2 m( S
% _# s! l( m2 b
: c# \, G" f; @: j/ k3 N $this->set_savename($this->savename);5 b1 }" d. l+ n; K: D& V
0 E( t: r- T7 A( x" O6 s
! M- X' o+ V' f& E; V 7 W4 N7 W: G! R
K7 v( e; l- d8 \5 h$ d6 U& I
, ]$ K/ u" y/ X2 K6 c' m) F" [: _
if(!is_writable(DT_ROOT.'/'.$this->savepath)) return $this->_('Error(24)'.$L['upload_unwritable']);
# \, S+ r# |1 V2 X 2 C; P! x% H6 ], I
2 {+ C1 A: L+ N% j8 i: ?
if(!is_uploaded_file($this->file)) return $this->_('Error(25)'.$L['upload_failed']);) K/ Q n4 r( C) _2 \0 u9 C! H
) _* ^! @- Z4 z5 }" K" A0 n0 \4 k0 B# Y" D
if(!move_uploaded_file($this->file, DT_ROOT.'/'.$this->saveto)) return $this->_('Error(26)'.$L['upload_failed']);' _( Y0 q4 w d4 w2 z8 P9 j
1 l2 M( K" C4 ]0 s0 I. }2 q$ T$ i
1 C' [. E9 R- ~: K2 u5 I2 R8 _ : C, y2 l9 T/ O5 D; K; {6 y
6 [+ Y4 t9 G$ s$ k( |
& U5 b4 S- {* D; C+ e; B $this->image = $this->is_image();
' A: j7 S0 B( i6 w1 C$ K7 r
% W. u |8 C) H P# J" r0 T# p9 |) u; @$ V) J7 x6 `
if(DT_CHMOD) @chmod(DT_ROOT.'/'.$this->saveto, DT_CHMOD);; F! ~, W, i F/ n0 S( W
; E: S$ P8 k5 e7 y: Y5 U) g% J9 A |6 c$ f$ W
return true;
' i9 t: D6 X5 u( w
& {; ]$ L( o6 z( O! W, J1 M
$ q g7 H7 l* J1 q& E' C* T8 U& o }}
+ m2 V, k$ d. t* j8 r8 K5 p
( P9 X7 G' t. o# d O
: E& \1 f1 Q0 B6 p 先经过几个基本参数的检查,然后调用$this->is_allow()来进行安全检查 include/upload.class.php:72:" ^' |( G& ?2 G1 v# x
: o- m; W2 b l1 ]2 Y
^7 m0 i0 @1 y
<?php
; T% [. B4 `/ a8 P* r* F
/ ~ Y1 I: H' O. c' L, y
; E, J5 p' C. ?2 K, C8 N function is_allow() {
4 D( z5 ^' O% ^. {7 d
6 n% ?6 }/ B8 w% t" o4 f
1 I) J. n$ b, `8 F) w3 c1 t if(!$this->fileformat) return false;
@" r8 g! [5 _5 U9 f# Z : J" q; r% d2 m7 M: _6 U. s, q$ @. b L' B
1 m o6 ]7 O9 m: {8 b: Q- m Q" f if(!preg_match("/^(".$this->fileformat.")$/i", $this->ext)) return false;
1 ]1 M$ Y$ {6 f2 o
" d; F0 u0 r0 T$ z2 C/ p, b6 l3 P2 c: R% h/ p
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;) G! z% X' W8 \ A) x4 Z: ]
' z3 e* A$ ]3 a5 N0 ?" Y: z0 N$ e! d" I
return true;
( e- ~+ J5 T o5 O+ j9 K2 h # X1 P& p: `7 i' G6 B1 G0 ~
: o, j: i y" M
}
) v* S( z* K2 S. J
% c* j' _' o4 T! ~
' q0 d! t! V/ p* k: {/ @ 可以看到这里仅仅对$this->ext进行了检查,如前此时$this->ext为jpg,检查通过。
: h' D5 @% P: P7 [4 h
% X! `9 G, y* @9 }8 X4 Z4 `
" X% A3 o# a; ~% W) u4 \' W 接着会进行真正的保存。通过$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 j) p8 U' Y& [' ~1 ]* J
: h2 c+ T7 v8 Y3 E2 \- N
. p& f" Q/ j/ A; n* ?/ U4 G) G. k* y
漏洞利用) r5 R9 k/ I7 X/ r }$ b, Z
; P" D, N# F9 z) q0 |- R# P: |
7 k. d- e9 r8 m; @, Q& k( c
综上,上传两个文件,其中第一个文件以php为结尾如1.php,用于设置后缀名为php;第二个文件为1.jpg,jpg用于绕过检测,其内容为php一句话木马(图片马)。# i+ O7 j# p, T' U) j! R! ?9 q+ Z
- Y3 S5 v3 F: i: y
4 i) y2 v0 {- L0 C" n; H" B7 c7 g% Y ' e. t8 \" Y' f( f+ v5 _
- g7 z8 K/ n' E( I" @" J. K
, U7 W/ T4 L7 b# p$ K/ Z) I
然后访问http://127.0.0.1/file/temp/avatar1.php 即可。其中1是自己的_userid n1 r9 B* i8 j
4 R9 D2 _, [5 m6 o% l0 V- m$ e3 V% n& r$ j1 ^- v6 K
不过实际利用上会有一定的限制。+ v% d3 G3 U: y5 L3 T& v
$ V2 [# X, b, G$ T5 z, ]/ j; O6 \& w' h4 e6 ?* D, {9 m# ^
第一点是destoon使用了伪静态规则,限制了file目录下php文件的执行。, Y2 y4 e; \2 @; N& B; y O0 b
1 o& U; c9 G% S# V
5 D& S* E4 n! T% ^
+ Q; {+ D4 g+ \7 b7 V9 n# K( s' i 5 G6 j: X/ t6 D9 Y. _
! I( d4 W- N1 Q; d/ P0 W 第二点是avatar.inc.php中在$upload->save()后,会再次对文件进行检查,然后重命名为xx.jpg:
; E' e# }: T7 ]& z, ]) M$ n ' k8 V9 X: l3 C# u% o. L- V" U
' j+ @$ l4 m$ Z
省略...$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]);省略...
$ E: y( C5 G' D- e: Z: ]( |
2 ? q+ k4 L& m
# g/ F1 b8 ]9 p, U* m 因此要利用成功就需要条件竞争了。
! W% ~ l% N3 {3 t4 k
% e, ]$ d2 V0 C/ d' t. c1 s. O0 f0 m7 }
补丁分析9 ^7 R4 `( T6 a4 q9 n3 C
3 V( l8 c8 g) t1 e9 z3 q& r9 B
h8 U; z7 u* J: y) k" f$ N
" G2 k8 v) S1 I
( a7 q1 k; }2 t
3 C" A5 G4 E0 v4 k3 Q+ @6 u 在upload的一开始,就进行一次后缀名的检查。其中is_image如下:
+ |7 e2 P! d0 u5 U; s* s( R
" N/ }0 t7 c$ |& E6 A* D7 T5 K" `" E' l/ Y$ @
function is_image($file) { return preg_match("/^(jpg|jpeg|gif|png|bmp)$/i", file_ext($file));}$ N. f5 s& g1 L% l
2 g0 b* d; B% F: R# J! i
2 y6 V; f9 z# s0 x; u5 p: b o, j * l# t2 ?! Q8 C7 c
$ K4 u# z6 [6 s" t @; C$ n h* B! A5 q& G" u
在__construct()的foreach中使用了break,获取了第一个文件后就跳出循环。3 n1 G8 x- c; I% D- Y
) ?# }! d$ i5 o' Z+ I8 M7 W# a" N
5 M7 H" k1 K9 s
在is_allow()中增加对$this->savename的二次检查。* ]; b: G8 k8 |
* }( m5 A. X$ n. S9 U
8 i$ y- r: u# ?7 c/ H, Y 最后0 e7 `3 g! R0 o) E* o! E
" k! F- Q; r5 u9 g
7 O' }0 K0 |( S' L
嘛,祝各位大师傅中秋快乐!- D3 f% J/ Q" i& a
; H, l7 \' b' G) e6 C! J) `' A4 Z" O5 v
% V+ ]0 o$ y2 h, V6 q
/ i U# T! Y. M. c9 W. J1 L1 T
|