ast.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. const fs = require('fs')
  2. const parser = require('@babel/parser') // 解析 JavaScript 代码
  3. const traverse = require('@babel/traverse').default // 遍历 AST
  4. const generator = require('@babel/generator').default // 生成代码
  5. const t = require('@babel/types')
  6. // 定义文件路径
  7. const input_js = './encode.js'
  8. const output_js = './decode.js'
  9. // 读取文件内容并解析成 AST
  10. const js_code = fs.readFileSync(input_js, {encoding: 'utf-8'})
  11. const ast = parser.parse(js_code) // 解析代码为 AST
  12. // ------ 得出来 3元表达式
  13. // 只获取指纹数组的代码,全部获取还原后并不准确
  14. // 查找所有 lA()[Sr(l2)](rs, U3, pb) 和 Zb()[G9(Yx)].apply(null, [cg, N9])
  15. encode = {}
  16. const visitor_call = {
  17. ConditionalExpression(path) {
  18. js = path.toString()
  19. encode[js] = null;
  20. },
  21. CallExpression(path) {
  22. // 检查是否在三元表达式内部
  23. const isInsideTernary = path.findParent(p =>
  24. p.isConditionalExpression()
  25. );
  26. if (isInsideTernary) return;
  27. let {callee,} = path.node
  28. // let argumentsPathList = path.get('arguments')
  29. if (!t.isMemberExpression(callee)) return;
  30. // 匹配2种混淆表达式
  31. let {object, property} = callee
  32. if (!t.isCallExpression(object) && !t.isMemberExpression(object)) return;
  33. // 处理 参数中有 三元表达式的(没必要)
  34. // argumentsPathList.forEach(argPath => {
  35. // argPath.traverse({
  36. // ConditionalExpression(innerPath) {
  37. // js = innerPath.toString()
  38. // console.log('找到条件表达式完', js)
  39. // }
  40. // })
  41. // })
  42. // 更细节的处理
  43. if (t.isCallExpression(object) && t.isIdentifier(object.callee) && object.arguments.length === 0) {
  44. js = path.toString()
  45. encode[js] = null; // 要设置值为 null 否则转 json字符串时会无值,因为值为 undefind 的json转不了
  46. }
  47. if (t.isMemberExpression(object) && t.isIdentifier(property)) {
  48. // 跳过嵌套的成员表达式 只还原这种 Zb()[G9(Yx)].apply(null, [cg, N9])
  49. if (!t.isCallExpression(object.object)) return;
  50. js = path.toString()
  51. encode[js] = null;
  52. }
  53. // if(!t.isCallExpression(object) || !t.isMemberExpression(object)) return;
  54. //
  55. // if(!) return;
  56. // encode.push({key: js})
  57. },
  58. Program: {
  59. exit() {
  60. // console.log(encode)
  61. const jsonData = JSON.stringify(encode);
  62. console.log(jsonData); // 输出 JSON 字符串
  63. }
  64. }
  65. };
  66. decode = {
  67. "zK()[GQ(fp)](d5, X0)": "Object",
  68. "RP(typeof RT()[N5(Sj)], 'undefined') ? RT()[N5(xQ)].call(null, EN, BX7, Z2, OR) : RT()[N5(JI)](pN, tH7, QG, CF)": "keys",
  69. "HR(typeof pQ()[vP(b5)], 'undefined') ? pQ()[vP(TN)].apply(null, [jK, jc, B0, YU]) : pQ()[vP(OR)](DA7, Pd, cW, EN)": "map"
  70. }
  71. const visitor2 = {
  72. CallExpression(path) {
  73. let {callee,} = path.node
  74. // let argumentsPathList = path.get('arguments')
  75. if (!t.isMemberExpression(callee)) return;
  76. // 匹配2种混淆表达式
  77. let {object, property} = callee
  78. if (!t.isCallExpression(object) && !t.isMemberExpression(object)) return;
  79. // console.log(path.toString())
  80. // 更细节的处理
  81. if (t.isCallExpression(object) && t.isIdentifier(object.callee) && object.arguments.length === 0) {
  82. js = path.toString()
  83. if (decode[js] !== null) {
  84. value = decode[js]
  85. console.log(js, '=>', value)
  86. path.replaceWith(t.valueToNode(value))
  87. }
  88. }
  89. if (t.isMemberExpression(object) && t.isIdentifier(property)) {
  90. // 跳过嵌套的成员表达式 只还原这种 Zb()[G9(Yx)].apply(null, [cg, N9])
  91. if (!t.isCallExpression(object.object)) return;
  92. js = path.toString()
  93. // try {
  94. if (decode[js] !== null) {
  95. value = decode[js]
  96. console.log(js, '=>', value)
  97. path.replaceWith(t.valueToNode(value))
  98. }
  99. // } catch (e) {
  100. // console.log(e)
  101. // }
  102. }
  103. },
  104. ConditionalExpression(path) {
  105. js = path.toString()
  106. if (decode[js] !== null) {
  107. value = decode[js]
  108. console.log(js, '=>', value)
  109. path.replaceWith(t.valueToNode(value))
  110. }
  111. }
  112. };
  113. // traverse(ast, visitor_call) // 获取混淆的表达式
  114. traverse(ast, visitor2) // 还原
  115. // 使用 Babel 生成新的代码
  116. let {code} = generator(ast)
  117. // 将生成的代码写入指定的文件
  118. fs.writeFile(output_js, code, (err) => {
  119. })