-
Notifications
You must be signed in to change notification settings - Fork 343
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const { parse } = require('@babel/parser') | ||
const generator = require('@babel/generator').default | ||
const traverse = require('@babel/traverse').default | ||
const t = require('@babel/types') | ||
const ivm = require('isolated-vm') | ||
|
||
const isolate = new ivm.Isolate() | ||
const globalContext = isolate.createContextSync() | ||
function virtualGlobalEval(jsStr) { | ||
return globalContext.evalSync(String(jsStr)) | ||
} | ||
|
||
function deAntiToolingCheckFunc(path) { | ||
if (path.node.params.length) { | ||
return false | ||
} | ||
const body = path.node.body | ||
if (!t.isBlockStatement(body)) { | ||
return false | ||
} | ||
if (body.body.length) { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
function deAntiToolingExtract(path, func_name) { | ||
let binding = path.scope.getBinding(func_name) | ||
for (let ref of binding.referencePaths) { | ||
if (!ref.parentPath.isCallExpression() || !ref.key === 'callee') { | ||
continue | ||
} | ||
const call = ref.parentPath | ||
if (!call.listKey === 'body') { | ||
continue | ||
} | ||
for (let node of call.node.arguments) { | ||
call.insertBefore(node) | ||
} | ||
call.remove() | ||
} | ||
binding.scope.crawl() | ||
binding = path.scope.getBinding(func_name) | ||
if (binding.references === 0) { | ||
path.remove() | ||
} | ||
} | ||
|
||
const deAntiTooling = { | ||
FunctionDeclaration(path) { | ||
const func_name = path.node.id?.name | ||
if (!func_name) { | ||
return | ||
} | ||
if (!deAntiToolingCheckFunc(path)) { | ||
return | ||
} | ||
console.log(`AntiTooling Func Name: ${func_name}`) | ||
deAntiToolingExtract(path, func_name) | ||
}, | ||
} | ||
|
||
module.exports = function (code) { | ||
let ast | ||
try { | ||
ast = parse(code, { errorRecovery: true }) | ||
} catch (e) { | ||
console.error(`Cannot parse code: ${e.reasonCode}`) | ||
return null | ||
} | ||
// AntiTooling | ||
traverse(ast, deAntiTooling) | ||
code = generator(ast, { | ||
comments: false, | ||
jsescOption: { minimal: true }, | ||
}).code | ||
return code | ||
} |