-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdeobf2.js
228 lines (203 loc) · 6.46 KB
/
deobf2.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
const fs = require("fs");
const babel = require("@babel/core");
const t = require("@babel/types");
const parser = require("@babel/parser");
const traverse = require("@babel/traverse").default;
const generate = require("@babel/generator").default;
const { readFileSync } = require("fs");
let beautify_opts = {
comments: true,
minified: false,
concise: false,
};
const [_, __, input, output] = process.argv;
const script = readFileSync(input, "utf-8");
const AST = parser.parse(script, {});
const inlineDestructuredVariables = {
VariableDeclaration(path) {
const { node, scope } = path;
node.declarations.forEach((declarator) => {
const { id, init } = declarator;
// Handle array destructuring
if (t.isArrayPattern(id) && t.isArrayExpression(init)) {
const elementsLeft = id.elements;
const elementsRight = init.elements;
// Ensure both sides have the same number of elements
if (elementsLeft.length !== elementsRight.length) return;
elementsLeft.forEach((leftElem, index) => {
const rightElem = elementsRight[index];
if (!leftElem || !rightElem) return;
if (t.isIdentifier(leftElem)) {
const varName = leftElem.name;
const binding = scope.getBinding(varName);
// Ensure the variable is not reassigned
if (binding && binding.constant) {
// Replace all references
binding.referencePaths.forEach((refPath) => {
// Resolve nested identifiers recursively
replaceWithResolvedValue(refPath, rightElem, scope);
});
// Optionally remove the declarator
// binding.path.remove();
}
}
});
}
// Handle object destructuring if needed
// (Similar logic can be applied here if your code contains object destructuring)
});
// Optionally remove the entire declaration if all variables have been inlined
if (node.declarations.every((decl) => !scope.getBinding(decl.id.name))) {
path.remove();
}
},
};
// Helper function to resolve nested identifiers and member expressions
function replaceWithResolvedValue(refPath, valueNode, scope) {
if (t.isIdentifier(valueNode)) {
const binding = scope.getBinding(valueNode.name);
if (binding && binding.constant && binding.path.node.init) {
// Recursively replace with the actual value
replaceWithResolvedValue(refPath, binding.path.node.init, scope);
} else {
refPath.replaceWith(valueNode);
}
} else if (t.isMemberExpression(valueNode)) {
// Resolve the object and property if possible
const objectNode = valueNode.object;
const propertyNode = valueNode.property;
let resolvedObjectNode = objectNode;
if (t.isIdentifier(objectNode)) {
const objectBinding = scope.getBinding(objectNode.name);
if (
objectBinding &&
objectBinding.constant &&
objectBinding.path.node.init
) {
resolvedObjectNode = objectBinding.path.node.init;
}
}
refPath.replaceWith(
t.memberExpression(resolvedObjectNode, propertyNode, valueNode.computed)
);
} else {
refPath.replaceWith(valueNode);
}
}
const evaluateMemberExpressions = {
MemberExpression(path) {
const { node } = path;
if (t.isIdentifier(node.object) && t.isStringLiteral(node.property)) {
const objectName = node.object.name;
const propertyName = node.property.value;
if (objectName === "window") {
path.replaceWithSourceString(propertyName);
}
}
},
};
const evaluateCallExpressions = {
MemberExpression(path) {
const { node } = path;
if (t.isIdentifier(node.object) && t.isStringLiteral(node.property)) {
const objectName = node.object.name;
const propertyName = node.property.value;
const validObjects = [
"document",
"crypto",
"Array",
"Uint8Array",
"TextEncoder",
"RTCPeerConnection",
"Promise",
"Math",
"String",
];
if (validObjects.includes(objectName)) {
path.replaceWithSourceString(`${objectName}.${propertyName}`);
}
}
},
};
const deadCodeElimination = {
ReturnStatement(path) {
let currentPath = path;
do {
const siblings = currentPath.getAllNextSiblings();
siblings.forEach((sibling) => sibling.remove());
currentPath = currentPath.parentPath;
} while (currentPath && t.isBlockStatement(currentPath.node));
},
};
const inlineArrayDestructuring = {
VariableDeclarator(path) {
const node = path.node;
if (t.isArrayExpression(node.init) && t.isArrayPattern(node.id)) {
const elements = node.init.elements;
const names = node.id.elements;
const newDeclarations = [];
elements.forEach((element, index) => {
const name = names[index];
if (name && element) {
newDeclarations.push(
t.variableDeclarator(t.identifier(name.name), element)
);
}
});
if (newDeclarations.length > 0) {
path.replaceWithMultiple(newDeclarations);
}
}
},
};
const inlineObjectDestructuring = {
VariableDeclaration(path) {
const node = path.node;
if (node.declarations.length > 1) {
const newDeclarations = node.declarations.map((declarator) =>
t.variableDeclaration(node.kind, [declarator])
);
path.replaceWithMultiple(newDeclarations);
}
},
};
const removeUnusedVariables = {
Program(path) {
path.traverse({
VariableDeclarator(variablePath) {
const binding = variablePath.scope.getBinding(
variablePath.node.id.name
);
if (binding && !binding.referenced) {
variablePath.remove();
}
},
});
},
};
traverse(AST, inlineDestructuredVariables);
traverse(AST, evaluateMemberExpressions);
traverse(AST, evaluateCallExpressions);
traverse(AST, deadCodeElimination);
traverse(AST, inlineArrayDestructuring);
traverse(AST, inlineObjectDestructuring);
traverse(AST, removeUnusedVariables);
const final_code = generate(AST, beautify_opts).code;
babel.transform(
final_code,
{
plugins: [
"babel-plugin-minify-constant-folding",
"babel-plugin-minify-dead-code-elimination",
"babel-plugin-transform-inline-consecutive-adds",
],
},
function (err, result) {
if (err) {
console.error(err);
} else {
const transformedCode = result.code;
fs.writeFileSync(output, transformedCode);
}
}
);