使用ast解ob混淆时关于types的总结
在AST解OB混淆过程中,@babel/types模块(简称types
)是核心操作工具,以下是典型应用场景及具体代码示例:
一、字符串解密场景
场景:OB混淆常将字符串存储为十六进制或Unicode编码,需还原为明文
types应用:
traverse(ast, {// 遍历Literal节点替换加密字符串Literal(path) {if (path.node.extra?.raw) {// 将\x68\x65转为明文"he"const decodedValue = decodeHexString(path.node.extra.raw);path.replaceWith(types.stringLiteral(decodedValue)); // }}
});
通过types.stringLiteral
直接生成明文字符串节点替换加密节点。
二、大数组元素还原
场景:混淆代码将关键字符串存储在大数组中,通过索引调用
types应用:
const arrayMap = new Map(); // 存储数组变量名与值的映射
traverse(ast, {VariableDeclarator(path) {if (path.node.init?.elements) {arrayMap.set(path.node.id.name, path.node.init.elements.map(e => e.value)); // }},MemberExpression(path) {if (path.node.object.type === "Identifier" && arrayMap.has(path.node.object.name)) {const index = path.node.property.value;const realValue = arrayMap.get(path.node.object.name)[index];path.replaceWith(types.valueToNode(realValue)); // }}
});
通过types.valueToNode
将数组索引引用直接替换为实际值。
三、控制流反扁平化
场景:混淆代码将逻辑拆分为多个switch-case块
types应用:
traverse(ast, {SwitchCase(path) {const testValue = path.node.test.value;const consequent = path.node.consequent;if (types.isReturnStatement(consequent[0])) { // // 提取真实逻辑代码替换switch结构path.parentPath.replaceWithMultiple(consequent);}}
});
使用types.isReturnStatement
判断节点类型,直接替换控制流结构。
四、函数调用间接化处理
场景:通过中间变量调用关键函数
types应用:
traverse(ast, {CallExpression(path) {if (types.isIdentifier(path.node.callee, {name: "_0x5e920f"})) { // // 解析加密函数参数并替换为实际值const args = path.node.arguments.map(arg => evalNode(arg));path.replaceWith(types.stringLiteral(decryptFunc(args))); // }}
});
通过types.isIdentifier
精准定位加密函数调用节点。
五、数值常量折叠优化
场景:将运算表达式(如1+2
)替换为计算结果
types应用:
traverse(ast, {BinaryExpression(path) {if (types.isNumericLiteral(path.node.left) && types.isNumericLiteral(path.node.right)) { // const result = eval(`${path.node.left.value}${path.node.operator}${path.node.right.value}`);path.replaceWith(types.numericLiteral(result)); // }}
});
通过types.numericLiteral
生成计算结果节点。
对比总结(核心方法应用)
功能 | 方法示例 | 典型场景 |
---|---|---|
节点替换 | replaceWith(types.xxx) | 字符串解密、控制流还原 |
类型判断 | types.isIdentifier/isLiteral | 加密函数定位 |
值转换 | types.valueToNode | 数组元素替换 |
表达式构造 | types.binaryExpression | 运算折叠优化 |
动态节点生成 | types.stringLiteral | 明文替换加密内容 |
参考来源:
通过合理组合这些方法,可系统化解构OB混淆的字符串加密、数组索引、控制流混淆等特征。