-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathpack.ts
201 lines (175 loc) · 5.99 KB
/
pack.ts
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
import * as t from "@babel/types";
import Obfuscator from "../obfuscator";
import Template from "../templates/template";
import {
isDefiningIdentifier,
isModifiedIdentifier,
isVariableIdentifier,
} from "../utils/ast-utils";
import {
GEN_NODE,
NodeSymbol,
reservedIdentifiers,
variableFunctionName,
WITH_STATEMENT,
} from "../constants";
import { PluginArg, PluginObject } from "./plugin";
import { Order } from "../order";
import { computeProbabilityMap } from "../probability";
export default function pack({ Plugin }: PluginArg): PluginObject {
const me = Plugin(Order.Pack, {
changeData: {
globals: 0,
},
});
const objectName = me.obfuscator.nameGen.generate();
const mappings = new Map<string, string>();
const setterPropsNeeded = new Set<string>();
const typeofMappings = new Map<string, string>();
const prependNodes: t.Statement[] = [];
return {
// Transform identifiers, preserve import statements
visitor: {
ImportDeclaration(path) {
prependNodes.push(path.node);
path.remove();
// Ensure bindings are removed -> variable becomes a global -> added to mappings object
path.scope.crawl();
},
Program(path) {
path.scope.crawl();
},
Identifier: {
exit(path) {
if (!isVariableIdentifier(path)) return;
if (isDefiningIdentifier(path)) return;
if ((path.node as NodeSymbol)[GEN_NODE]) return;
if ((path.node as NodeSymbol)[WITH_STATEMENT]) return;
const identifierName = path.node.name;
if (reservedIdentifiers.has(identifierName)) return;
if (me.obfuscator.options.globalVariables.has(identifierName)) return;
if (identifierName === variableFunctionName) return;
if (identifierName === objectName) return;
if (!path.scope.hasGlobal(identifierName)) return;
if (path.scope.hasBinding(identifierName)) return;
// Check user's custom implementation
if (!computeProbabilityMap(me.options.pack, identifierName)) return;
if (
path.key === "argument" &&
path.parentPath.isUnaryExpression({ operator: "typeof" })
) {
const unaryExpression = path.parentPath;
let propertyName = typeofMappings.get(identifierName);
if (!propertyName) {
propertyName = me.obfuscator.nameGen.generate();
typeofMappings.set(identifierName, propertyName);
}
unaryExpression.replaceWith(
t.memberExpression(
t.identifier(objectName),
t.stringLiteral(propertyName),
true
)
);
return;
}
let propertyName = mappings.get(identifierName);
if (!propertyName) {
propertyName = me.obfuscator.nameGen.generate();
mappings.set(identifierName, propertyName);
}
// Only add setter if the identifier is modified
if (isModifiedIdentifier(path)) {
setterPropsNeeded.add(identifierName);
}
path.replaceWith(
t.memberExpression(
t.identifier(objectName),
t.stringLiteral(propertyName),
true
)
);
},
},
},
// Final AST handler
// Very last step in the obfuscation process
finalASTHandler(ast) {
// Create object expression
// Very similar to flatten, maybe refactor to use the same code
const objectProperties: t.ObjectMethod[] = [];
me.changeData.globals = mappings.size;
for (const [identifierName, propertyName] of mappings) {
// get identifier() { return identifier; }
objectProperties.push(
t.objectMethod(
"get",
t.stringLiteral(propertyName),
[],
t.blockStatement([t.returnStatement(t.identifier(identifierName))])
)
);
// Only add setter if the identifier is modified
if (setterPropsNeeded.has(identifierName)) {
// set identifier(value) { return identifier = value; }
objectProperties.push(
t.objectMethod(
"set",
t.stringLiteral(propertyName),
[t.identifier(objectName)],
t.blockStatement([
t.returnStatement(
t.assignmentExpression(
"=",
t.identifier(identifierName),
t.identifier(objectName)
)
),
])
)
);
}
}
// Add typeof mappings
for (const [identifierName, propertyName] of typeofMappings) {
// get typeof identifier() { return typeof identifier; }
objectProperties.push(
t.objectMethod(
"get",
t.stringLiteral(propertyName),
[],
t.blockStatement([
t.returnStatement(
t.unaryExpression("typeof", t.identifier(identifierName))
),
])
)
);
}
const objectExpression = t.objectExpression(objectProperties);
// Convert last expression to return statement
// This preserves the last expression in the packed code
var lastStatement = ast.program.body.at(-1);
if (lastStatement && t.isExpressionStatement(lastStatement)) {
Object.assign(
lastStatement,
t.returnStatement(lastStatement.expression)
);
}
const outputCode = Obfuscator.generateCode(ast, {
...me.obfuscator.options,
compact: true,
});
var newAST = new Template(`
{prependNodes}
Function({objectName}, {outputCode})({objectExpression});
`).file({
objectName: () => t.stringLiteral(objectName),
outputCode: () => t.stringLiteral(outputCode),
objectExpression: objectExpression,
prependNodes: prependNodes,
});
return newAST;
},
};
}