|
| 1 | +const { parse } = require('@babel/parser') |
| 2 | +const generator = require('@babel/generator').default |
| 3 | +const traverse = require('@babel/traverse').default |
| 4 | +const t = require('@babel/types') |
| 5 | +const ivm = require('isolated-vm') |
| 6 | + |
| 7 | +const isolate = new ivm.Isolate() |
| 8 | +const globalContext = isolate.createContextSync() |
| 9 | +function virtualGlobalEval(jsStr) { |
| 10 | + return globalContext.evalSync(String(jsStr)) |
| 11 | +} |
| 12 | + |
| 13 | +function deAntiToolingCheckFunc(path) { |
| 14 | + if (path.node.params.length) { |
| 15 | + return false |
| 16 | + } |
| 17 | + const body = path.node.body |
| 18 | + if (!t.isBlockStatement(body)) { |
| 19 | + return false |
| 20 | + } |
| 21 | + if (body.body.length) { |
| 22 | + return false |
| 23 | + } |
| 24 | + return true |
| 25 | +} |
| 26 | + |
| 27 | +function deAntiToolingExtract(path, func_name) { |
| 28 | + let binding = path.scope.getBinding(func_name) |
| 29 | + for (let ref of binding.referencePaths) { |
| 30 | + if (!ref.parentPath.isCallExpression() || !ref.key === 'callee') { |
| 31 | + continue |
| 32 | + } |
| 33 | + const call = ref.parentPath |
| 34 | + if (!call.listKey === 'body') { |
| 35 | + continue |
| 36 | + } |
| 37 | + for (let node of call.node.arguments) { |
| 38 | + call.insertBefore(node) |
| 39 | + } |
| 40 | + call.remove() |
| 41 | + } |
| 42 | + binding.scope.crawl() |
| 43 | + binding = path.scope.getBinding(func_name) |
| 44 | + if (binding.references === 0) { |
| 45 | + path.remove() |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +const deAntiTooling = { |
| 50 | + FunctionDeclaration(path) { |
| 51 | + const func_name = path.node.id?.name |
| 52 | + if (!func_name) { |
| 53 | + return |
| 54 | + } |
| 55 | + if (!deAntiToolingCheckFunc(path)) { |
| 56 | + return |
| 57 | + } |
| 58 | + console.log(`AntiTooling Func Name: ${func_name}`) |
| 59 | + deAntiToolingExtract(path, func_name) |
| 60 | + }, |
| 61 | +} |
| 62 | + |
| 63 | +module.exports = function (code) { |
| 64 | + let ast |
| 65 | + try { |
| 66 | + ast = parse(code, { errorRecovery: true }) |
| 67 | + } catch (e) { |
| 68 | + console.error(`Cannot parse code: ${e.reasonCode}`) |
| 69 | + return null |
| 70 | + } |
| 71 | + // AntiTooling |
| 72 | + traverse(ast, deAntiTooling) |
| 73 | + code = generator(ast, { |
| 74 | + comments: false, |
| 75 | + jsescOption: { minimal: true }, |
| 76 | + }).code |
| 77 | + return code |
| 78 | +} |
0 commit comments