h5st JS
JS: https://storage.360buyimg.com/webcontainer/js_security_v3_0.1.8.js
先进行字符串解码,分析混淆代码结构

然后通过赋值解码器a0e467aT 通过别名调用

第一步目标就是解码所有的解码器a0e467aT的值。
由于这里比较简单,就只有一个大字符串数组,可以直接把解码部分代码扣下来执行调用解码器更简单快捷。
const visitor: Parameters<typeof traverse>[1] = { // 数字类型还原16进制转10进制 NumericLiteral(path: NodePath<t.NumericLiteral>) { delete path.node.extra }, // 计算常量表达式 BinaryExpression: { exit(path: NodePath<t.BinaryExpression>) { try { const evaluated = path.evaluate(); if (evaluated.confident) { path.replaceWith(t.valueToNode(evaluated.value)); } } catch (e) { if (e instanceof RangeError) { // 太深或存在循环引用,跳过即可 // console.error("异常", e); // console.log("RangeError in", generate(path.node).code); return; } throw e; } }, }, // 处理a0e467aT解码器函数调用 CallExpression(path: NodePath<t.CallExpression>) { // 处理_1tz8d中的函数调用 if (t.isIdentifier(path.node.callee) && path.node.callee.name === "_4dn8d") { // 一个字符串参数 const args = path.node.arguments; if (args.length === 1 && t.isStringLiteral(args[0])) { const decoded = _4dn8d(args[0].value); path.replaceWith(t.valueToNode(decoded)); } return; } // 是否为解码器调用 if (!isAliasCall(path, "a0e467aT")) return; // 乱序 IIFE 内部保持动态解码 if (insideShuffleIIFE(path)) { // console.log("IIFE 内部解码器调用,保持动态解码:", generate(path.node).code); return; } // 是否仅为一个数字参数 if (path.node.arguments.length === 1 && t.isNumericLiteral(path.node.arguments[0])) { path.replaceWith(t.valueToNode(a0e467aT(path.node.arguments[0].value))) } }}需要注意的是 打乱的时候,解码器引用的字符串数组的并不固定,一直在变化。
但是后续没有打乱的方法了,就可以直接从固定中替换。

第二步,处理这个_$R对象的调用。

let decodeRObject: NodePath<t.VariableDeclarator> | null = null;const visitor2: Parameters<typeof traverse>[1] = { // 先找到解码大对象_$R定义 VariableDeclarator(path: NodePath<t.VariableDeclarator>) { if (t.isIdentifier(path.node.id) && path.node.id.name === "_$R" && t.isObjectExpression(path.node.init)) { decodeRObject = path; } }, // 处理调用形式的函数解构 _$R.LBaYM(a, _$Pv) CallExpression(path: NodePath<t.CallExpression>) { if (t.isMemberExpression(path.node.callee) && t.isIdentifier(path.node.callee.object) && path.node.callee.object.name === "_$R" && t.isIdentifier(path.node.callee.property) ) { const propertyName = path.node.callee.property.name; if (!decodeRObject?.node.init) return; if (!t.isObjectExpression(decodeRObject.node.init)) return; // 找到对应的属性节点 const propertyNode = decodeRObject.node.init.properties.find( p => t.isObjectProperty(p) && t.isStringLiteral(p.key) && p.key.value === propertyName ); if (!propertyNode || !t.isObjectProperty(propertyNode)) return; // 应该是个函数 if (!t.isFunctionExpression(propertyNode.value)) return; // 找到原始的调用关系 // 从propertyNode中获取函数体,构建一个新的函数调用节点 const returnStatement = propertyNode.value.body.body[0]; if (!t.isReturnStatement(returnStatement)) return;
const returnExpression = returnStatement.argument; // 解构替换使用真实的返回表达式,并替换为真实的参数arg1, arg2 // BinaryExpression|CallExpression|LogicalExpression if (t.isCallExpression(returnExpression)) { // 可能有1~5个参数,若为 CallExpression,第一个参数始终是 函数体(被调用者),其余为其参数 const [func, ...args] = path.node.arguments; if (!t.isExpression(func)) return; const newCallExpression = t.callExpression(func, args); path.replaceWith(newCallExpression); } else if (t.isBinaryExpression(returnExpression)) { // CallExpression|LogicalExpression if (path.node.arguments.length !== 2) return; const [arg1, arg2] = path.node.arguments; if (!t.isExpression(arg1) || !t.isExpression(arg2)) return; const newBinaryExpression = t.binaryExpression(returnExpression.operator, arg1, arg2); path.replaceWith(newBinaryExpression); } else if (t.isLogicalExpression(returnExpression)) { if (path.node.arguments.length !== 2) return; const [arg1, arg2] = path.node.arguments; if (!t.isExpression(arg1) || !t.isExpression(arg2)) return; const newLogicalExpression = t.logicalExpression(returnExpression.operator, arg1, arg2); path.replaceWith(newLogicalExpression); } else { console.log(`Warning: The return expression of the function for property ${propertyName} is not a CallExpression, BinaryExpression, or LogicalExpression. Skipping replacement.`); } }
}, // 处理成员表达式形式的函数解构 _$R.QsaBv MemberExpression: { exit(path: NodePath<t.MemberExpression>) { if (!decodeRObject) return; // 判断为_$R的成员表达式 if (t.isIdentifier(path.node.object) && path.node.object.name === "_$R") { // 获取成员类型 // 判断类型 字符串 还是函数 if (t.isIdentifier(path.node.property)) { const propertyName = path.node.property.name; if (!decodeRObject?.node.init) return; if (!t.isObjectExpression(decodeRObject.node.init)) return; // 先找对应成员的节点 const propertyNode = decodeRObject?.node.init.properties.find( p => t.isObjectProperty(p) && t.isStringLiteral(p.key) && p.key.value === propertyName ); if (!propertyNode || !t.isObjectProperty(propertyNode)) { console.log(`Warning: Property ${propertyName} not found in _$R object. Skipping replacement.`); return; } // 经过visitor处理后,CallExpression类型的PC(0x25e),应该已经是字符串了 if (t.isStringLiteral(propertyNode.value)) { // 先检查路径是否安全 if (!path.parentPath || path.removed) return; if (path.node) path.replaceWith(propertyNode.value); } } } }
}, // 处理visitor处理后的布尔常量 !0 UnaryExpression(path: NodePath<t.UnaryExpression>) { if (path.node.operator === "!") { const argument = path.node.argument; if (t.isNumericLiteral(argument)) { path.replaceWith(t.booleanLiteral(!argument.value)); } } },}使用ast解混淆, 分别输出前后两个版本,可以方便直观对比修改处。 (直接使用输入的js会存在空白、换行等格式差异,影响比较)
const output1 = generate(ast, {});fs.writeFileSync("js_security_v3_0.1.6.before.js", output1.code);
traverse(ast, visitor)traverse(ast, visitor2)
const output2 = generate(ast, {});fs.writeFileSync("js_security_v3_0.1.6.ast.js", output2.code);
还有重要的检查就是,使用解混淆后的JS,替换到浏览器后能正常使用,不会报错。 除了解析的时候一些边界情况没考虑完全之外,可能还会有一些自卫代码被检测到篡改,走了错误分支。。
滑块JS
JS:https://storage.360buyimg.com/jsresource/jcap/version/v2.8.5/1/jcap_ujb96b.js
简单解一下混淆:https://webcrack.netlify.app/
然后分析一下混淆JS的字符串混淆结构
字符串混淆分为5个部分
- 字符串大数组
- 数组旋转IIFE
- 解密内核
- 偏移包装函数
- 最终调用点
全局有多处 字符串大数组 ,就不方便一个一个扣出来了。 示例形式如下:
// 大数组function Jr() { var A = ["Ag9Z", "mtK1nZLYrLngDfG", "y29U", ...]; //一个大字符串数组 return (Jr = function () { return A; })();}// 最终解密函数function Or(A, t) { var e = Jr(); Or = function (t, n) { var r = e[t -= 103]; // ...省略 return r; }; return Or(A, t);}// 旋转数组IIFE(function (A) { function t(A, t) {} var e = A(); function n(A, t) {} while (true) {}})(Jr);// 偏移包装函数function Yr(A, t) { return Or(t - -599, A);}function e(A, t) { return Yr(t, A - 795);}var o = r[e(307, 314) + "t"]; // 调用点AST解混淆思路(由于不只一个字符串大数组,不能简单直接扣出运行计算)
- 定位字符串大数组特征,找到所有大数组,重命名(避免添加到同一上下文冲突),并添加进vm上下文。
- 找解密内核定位,重命名,添加进vm上下文
- 找旋转数组IIFE,添加进vm执行
- 找所有调用点,根据绑定移除偏移包装函数,直接调用解码内核。
- vm调用所有解码内核,获得真实字符串。
- 拼接字符串,处理未使用的函数,变量。
还有个问题,旋转数组IIFE中的调用点,里面是数组还是在动态变化过程中,会陷入死循环。
因为这只是辅助混淆的代码,不涉及业务逻辑,解码替换后便不再需要。所有解决方案有:
- 不替换,让他正常执行。
- 修改节点代码,异常不陷入死循环。
- 直接删除整个旋转数组IIFE执行代码。
一、要按照顺序,将字符串数组旋转以及解码器添加进VM上下文中,错误顺序会产生未定义的异常. 还有这个找数组、解码器判断,简单准确就行,如果不是要那么很通用的话,没必要写那么完整完善。
// 创建一个隔离的 V8 实例(可分配内存上限 32MB)const isolate = new ivm.Isolate({memoryLimit: 32 << 20});let context = isolate.createContextSync(); // 上下文const visitor = { FunctionDeclaration(path) { if (isBigStringArray(path)) { const newName = `Array_${++counter.array}`; path.scope.rename(path.node.id.name, newName); const code = generate(path.node).code; // const code = getNodeSource(path.node); // console.log(newName, code); context.evalSync(code); } }}const visitor1 = { FunctionDeclaration(path) { if (isDecryptKernel(path)) { const newName = `Decrypt_${++counter.decrypt}`; path.scope.rename(path.node.id.name, newName); const code = generate(path.node).code; // const code = getNodeSource(path.node); // console.log(newName, code); context.evalSync(code); } }}const visitor2 = { CallExpression(path) { if (isRotationIIFE(path)) { // const code = getNodeSource(path.node); const code = generate(path.node).code; // 添加自执行 console.log(++counter.rotation, "旋转数组:", code); // SyntaxError: Function statements require a function name context.evalSync('!' + code); // 删除节点 path.remove(); } }}移除中间偏移包装函数,让调用点直接调用最终的解码内核。 这一步就是比较麻烦的一点,因为它中间只能靠binding一步一步向上找,它可能存在多层包装偏移。还有多种情况要处理。 然后如果情况比较多,就可以少处理一些情况,以准确性为第一。
调用点特征:绝大部分为直接Identifier调用 i(314, 318)少部分有FunctionExpression形式 function (A, t) { return o(A - -1374, t); }(8, 172)
偏移包装函数特征:大部分为 function i(A, t) { return bA(A - 215, t); }少部分为 function e(A, t) { return function (A, t) { return pr(A - 103, t); }(A - -353, t); }
假设这里的`bA`函数就是解码内核,i(314, 318) 处理后就被替换为直接调用 `bA(314 - 215, 318)`function getFinalCallExpr(path, node, depth = 0) { // 避免异常代码导致无限递归 if (depth > 20) { throw new Error("偏移包装函数递归层数过深"); } if (!t.isCallExpression(node)) return node; let expr, params; // function (A, t) { return n(t, A - -100); }(739, 748) 形式的调用点 if (t.isFunctionExpression(node.callee)) { // 形参 params = node.callee.params; // 实际调用 expr = path.get('callee.body.body.0.argument'); } else if (t.isIdentifier(node.callee)) { // e(307, 314) 形式的调用点 const binding = path.scope.getBinding(node.callee.name); if (!binding) return; const bp = binding.path; // FunctionDeclaration // 递归出口:非偏移包装函数,返回当前节点 if (!isOffsetWrapper(bp)) return node; // 通常形式一: function e(A, t) { return y(t - 502, A); } // 部分情况二: function o(A, t) { return function (A, t) { return T(A - 720, t); }(A - -1138, t); } console.log("Wrapper call: ", getNodeSource(bp.node)); // 形参 params = bp.node.params; // 实际调用 expr = bp.get('body.body.0.argument'); } else { throw Error("不支持的节点类型:" + node); } // 替换调用点 const newExpr = replaceWrapperCall(expr, params, node.arguments); // 递归处理,直到最终调用点 return getFinalCallExpr(expr, newExpr, depth + 1);}const visitor3 = { // 处理偏移包装函数调用点 function (A, t) { return n(t, A - -100); }(739, 748) CallExpression(path) { const callee = path.node.callee; if (!t.isIdentifier(callee) && !t.isFunctionExpression(callee)) return; const args = path.node.arguments; if (args.length !== 2) return; const [arg1, arg2] = args; if (!t.isNumericLiteral(arg1) && !t.isUnaryExpression(arg1)) return; if (!t.isNumericLiteral(arg2) && !t.isUnaryExpression(arg2)) return; const newExpr = getFinalCallExpr(path, path.node); if (newExpr) { console.log("Replace wrapper call: ", generate(newExpr).code); path.replaceWith(newExpr, {scope: true}); path.skip(); // 避免替换产物被重访再展开 } }}再次就是定位找出所有的调用点,在VM中执行,获取真实的字符串。
const visitor4 = { // 处理解密调用点替换 CallExpression(path) { const callee = path.node.callee; if (!t.isIdentifier(callee)) return; const args = path.node.arguments; if (args.length !== 2) return; const [arg1, arg2] = args; // 负数 UnaryExpression, 正数 NumericLiteral if (!t.isNumericLiteral(arg1) && !t.isUnaryExpression(arg1)) return; if (!t.isNumericLiteral(arg2) && !t.isUnaryExpression(arg2)) return; const binding = path.scope.getBinding(callee.name); if (!binding) return; if (!isDecryptKernel(binding.path)) return;
const callCode = path.toString(); console.log(++counter.call, "call ", callCode); const realString = context.evalSync(callCode); // console.log(`${callCode} => ${realString}`); path.replaceWith(t.stringLiteral(realString), {scope: true}); }}最后合并所有的字符串常量拼接
const visitor5 = { BinaryExpression(path) { // 从右向左合并,避免递归时左边变化 if (path.isBinaryExpression({operator: '+'})) { let node = path.node; const values = []; // 提取右子树的字符串 while ( node.type === 'BinaryExpression' && node.operator === '+' && node.right.type === 'StringLiteral' ) { values.unshift(node.right.value); node = node.left; } // 处理最左边 if (node.type === 'StringLiteral') { values.unshift(node.value); const merged = values.join(''); path.replaceWith({ type: 'StringLiteral', value: merged, extra: {raw: JSON.stringify(merged)} }); path.skip(); // 避免继续处理替换后的节点 } } }}得到结果,整体共18个字符串大数组,解码3000个左右调用点。3117 => 2921

最后也尝试了Zcode+GLM5.3 flash, 对这两个JS文件进行自动解混淆还原。
SKILL:https://lobehub.com/skills/zhizhuodemao-ai-reverse-toolkit-ast-deobfuscate
第一个用了一个半小时,大约6000万token,还原效果还可以。
第二个用了两个多小时,接近一亿token,还是有部分字符串没能还原。