搜索
查看: 511|回复: 1

PHPCMS V9最新版本后台设计缺陷导致getshell

[复制链接]

1839

主题

2255

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
11913
发表于 2016-9-23 09:52:08 | 显示全部楼层 |阅读模式
0x01 背景

周末审计了下phpcms的最新版本,前台已经很难找到漏洞了,故看了看后台相关的代码,发现还是有一处可以拿shell的漏洞。由于默认安装后需要超级管理员权限,故该漏洞很鸡肋,但感觉应该会在其它cms中也存在,所以主要分享下挖掘思路~
PS:使用的测试环境是php5.6(已经移除gpc选项)
[/url]

0x02 漏洞分析

漏洞起源:
yoursite\phpsso_server\phpcms\modules\admin\system.php下的uc函数:

  1. <?php
  2. public function uc() {
  3.         if (isset($_POST['dosubmit'])) {
  4.                 $data = isset($_POST['data']) ? $_POST['data'] : '';
  5.                 $data['ucuse'] = isset($_POST['ucuse']) && intval($_POST['ucuse']) ? intval($_POST['ucuse']) : 0;
  6.                 $filepath = CACHE_PATH.'configs'.DIRECTORY_SEPARATOR.'system.php';
  7.                 $config = include $filepath;
  8.                 $uc_config = '<?php '."\ndefine('UC_CONNECT', 'mysql');\n";
  9.                 foreach ($data as $k => $v) {
  10.                         $old[] = "'$k'=>'".(isset($config[$k]) ? $config[$k] : $v)."',";
  11.                         $new[] = "'$k'=>'$v',";
  12.                         $uc_config .= "define('".strtoupper($k)."', '$v');\n";
  13.                 }
  14.                 $html = file_get_contents($filepath);
  15.                 $html = str_replace($old, $new, $html);
  16.                 $uc_config_filepath = CACHE_PATH.'configs'.DIRECTORY_SEPARATOR.'uc_config.php';
  17.                 @file_put_contents($uc_config_filepath, $uc_config);
  18.                 @file_put_contents($filepath, $html);
  19.                 $this->db->insert(array('name'=>'ucenter', 'data'=>array2string($data)), 1,1);
  20.                 showmessage(L('operation_success'), HTTP_REFERER);
  21.         }
  22.         $data = array();
  23.         $r = $this->db->get_one(array('name'=>'ucenter'));
  24.         if ($r) {
  25.                 $data = string2array($r['data']);
  26.         }
  27.         include $this->admin_tpl('system_uc');
  28. }
  29. ...
复制代码

将表单中的数据$data按照键值对遍历并以如下形式存储到$uc_config变量里:

  1. $uc_config .= "define('".strtoupper($k)."', '$v');\n";
复制代码

上面只是对$k变量进行了字母转大写处理,然后就写到yoursite\phpsso_server\caches\configs\uc_config.php中了,所以这里应该可以构造一句话木马写入到uc_config.php中,从而拿到webshell。

0x03 漏洞证明
通过观察uc_config.php,我们构造一句话木马的方法如下(审查元素或者代理改包均可):
[url=http://7xk8bu.com1.z0.glb.clouddn.com/pshell.jpg]

由于phpcms对全局会对传递的$_GET、$_POST等参数值进行addslashes转义处理,但就当前数组data来说未对key做addslashes处理,所以上面的payload就是修改data[uc_api]为data[uc_api', '');]来完成对uc_config.php中内容的闭合。经过动态调试确认未对data数组中的key进行addslashes如下图:

[/url]
成功写入一句话木马到uc_config.php中:

获取shell
[url=http://7xk8bu.com1.z0.glb.clouddn.com/pshell1.jpg]

0x04 漏洞修复

我这里引入了特殊字符的数组$array_key_safe = array(“,”, “;”, “‘“, “(“, “)”, “\“);
然后在foreach循环时对$k进行过滤如下:

  1. public function uc() {
  2. //引入$array_key_safe
  3. $array_key_safe = array(",", ";", "'", "(", ")", "\");
  4. if (isset($_POST['dosubmit'])) {
  5. $data = isset($_POST['data']) ? $_POST['data'] : '';
  6. $data['ucuse'] = isset($_POST['ucuse']) && intval($_POST['ucuse']) ? intval($_POST['ucuse']) : 0;
  7. $filepath = CACHE_PATH.'configs'.DIRECTORY_SEPARATOR.'system.php';
  8. $config = include $filepath;
  9. $uc_config = '<?php '."\ndefine('UC_CONNECT', 'mysql');\n";
  10. foreach ($data as $k => $v) {
  11. //对$k中敏感字符替换为空
  12. $k = str_replace($array_key_safe, "", $k);
  13. $old[] = "'$k'=>'".(isset($config[$k]) ? $config[$k] : $v)."',";
  14. $new[] = "'$k'=>'$v',";
  15. $uc_config .= "define('".strtoupper($k)."', '$v');\n";
  16. }
  17. $html = file_get_contents($filepath);
  18. $html = str_replace($old, $new, $html);
  19. $uc_config_filepath = CACHE_PATH.'configs'.DIRECTORY_SEPARATOR.'uc_config.php';
  20. @file_put_contents($uc_config_filepath, $uc_config);
  21. @file_put_contents($filepath, $html);
  22. $this->db->insert(array('name'=>'ucenter', 'data'=>array2string($data)), 1,1);
  23. showmessage(L('operation_success'), HTTP_REFERER);
  24. }
复制代码

当然这个修补方法很暴力~


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?Join BUC

x
过段时间可能会取消签到功能了

2

主题

52

帖子

196

积分

我是新手

Rank: 1

积分
196
发表于 2016-9-27 10:07:03 | 显示全部楼层
好                                                     
好                                                        
您需要登录后才可以回帖 登录 | Join BUC

本版积分规则

Powered by Discuz!

© 2012-2015 Baiker Union of China.

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