-
Notifications
You must be signed in to change notification settings - Fork 7
/
deobfuscate4.js
73 lines (65 loc) · 2.07 KB
/
deobfuscate4.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import traverse from "@babel/traverse";
import { parse } from "@babel/parser";
import generate from "@babel/generator";
import * as types from "@babel/types";
import fs from "fs";
const code = fs.readFileSync("code4.js", "utf-8");
let ast = parse(code);
traverse(ast, {
WhileStatement(path) {
const { node, scope } = path;
const { test, body } = node;
if (!types.isLiteral(test, { value: true })) return;
if (body.body.length != 2) return;
let switchNode = body.body[0],
breakNode = body.body[1];
if (
!types.isSwitchStatement(switchNode) ||
!types.isBreakStatement(breakNode)
) {
return;
}
let { discriminant, cases } = switchNode;
if (!types.isMemberExpression(discriminant)) return;
let { object, property } = discriminant;
if (!types.isIdentifier(object) || !types.isUpdateExpression(property))
return;
let arrName = object.name;
let binding = scope.getBinding(arrName);
if (!binding || !binding.path || !binding.path.isVariableDeclarator())
return;
let { init } = binding.path.node;
if (
!types.isCallExpression(init) ||
!types.isMemberExpression(init.callee) ||
!init.arguments.length > 0
) {
return;
}
object = init.callee.object;
property = init.callee.property;
let argument = init.arguments[0].value;
if (!types.isStringLiteral(object) || !types.isIdentifier(property)) {
return;
}
let arrayFlow = object.value[property.name](argument);
let resultBody = [];
arrayFlow.forEach((index) => {
let switchCases = cases.filter(
(switchCase) => switchCase.test.value == index
);
let switchCase = switchCases.length > 0 ? switchCases[0] : undefined;
if (!switchCase) {
return;
}
let caseBody = switchCase.consequent;
if (types.isContinueStatement(caseBody[caseBody.length - 1])) {
caseBody.pop();
}
resultBody = resultBody.concat(caseBody);
});
path.replaceWithMultiple(resultBody);
},
});
const { code: output } = generate(ast);
console.log(output);