| 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 | 1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
6265x
6265x
6265x
6265x
6265x
6265x
3x
6262x
6262x
6262x
31310x
31310x
6262x
6262x
4041x
6262x
6262x
6262x
10303x
10303x
10303x
10303x
10303x
70702x
70702x
62620x
70702x
8082x
10303x
10303x
20606x
779943x
10303x
10303x
10303x
779943x
6878526x
6878526x
6878526x
6878526x
6878526x
6619572x
258954x
779943x
| import { injectable, inject } from 'inversify';
import { ServiceIdentifiers } from './container/ServiceIdentifiers';
import * as estraverse from 'estraverse';
import * as ESTree from 'estree';
import { TNodeTransformerFactory } from './types/container/TNodeTransformerFactory';
import { TVisitorDirection } from './types/TVisitorDirection';
import { TVisitorFunction } from './types/TVisitorFunction';
import { ICustomNodeGroup } from './interfaces/custom-nodes/ICustomNodeGroup';
import { IObfuscationEventEmitter } from './interfaces/event-emitters/IObfuscationEventEmitter';
import { IObfuscator } from './interfaces/IObfuscator';
import { IOptions } from './interfaces/options/IOptions';
import { IStackTraceAnalyzer } from './interfaces/stack-trace-analyzer/IStackTraceAnalyzer';
import { IStackTraceData } from './interfaces/stack-trace-analyzer/IStackTraceData';
import { IStorage } from './interfaces/storages/IStorage';
import { IVisitor } from './interfaces/IVisitor';
import { NodeTransformers } from './enums/container/NodeTransformers';
import { ObfuscationEvents } from './enums/ObfuscationEvents';
import { VisitorDirection } from './enums/VisitorDirection';
import { Node } from './node/Node';
import { NodeUtils } from './node/NodeUtils';
@injectable()
export class Obfuscator implements IObfuscator {
/**
* @type {NodeTransformers[]}
*/
private static readonly controlFlowTransformersList: NodeTransformers[] = [
NodeTransformers.BlockStatementControlFlowTransformer,
NodeTransformers.FunctionControlFlowTransformer
];
/**
* @type {NodeTransformers[]}
*/
private static readonly convertingTransformersList: NodeTransformers[] = [
NodeTransformers.MemberExpressionTransformer,
NodeTransformers.MethodDefinitionTransformer,
NodeTransformers.TemplateLiteralTransformer
];
/**
* @type {NodeTransformers[]}
*/
private static readonly obfuscatingTransformersList: NodeTransformers[] = [
NodeTransformers.CatchClauseTransformer,
NodeTransformers.FunctionDeclarationTransformer,
NodeTransformers.FunctionTransformer,
NodeTransformers.LabeledStatementTransformer,
NodeTransformers.LiteralTransformer,
NodeTransformers.ObjectExpressionTransformer,
NodeTransformers.VariableDeclarationTransformer
];
/**
* @type {IStorage<ICustomNodeGroup>}
*/
private readonly customNodeGroupStorage: IStorage<ICustomNodeGroup>;
/**
* @type {TNodeTransformerFactory}
*/
private readonly nodeTransformerFactory: TNodeTransformerFactory;
/**
* @type {IObfuscationEventEmitter}
*/
private readonly obfuscationEventEmitter: IObfuscationEventEmitter;
/**
* @type {IOptions}
*/
private readonly options: IOptions;
/**
* @type {IStackTraceAnalyzer}
*/
private readonly stackTraceAnalyzer: IStackTraceAnalyzer;
/**
* @param stackTraceAnalyzer
* @param obfuscationEventEmitter
* @param customNodeGroupStorage
* @param nodeTransformerFactory
* @param options
*/
constructor (
@inject(ServiceIdentifiers.IStackTraceAnalyzer) stackTraceAnalyzer: IStackTraceAnalyzer,
@inject(ServiceIdentifiers.IObfuscationEventEmitter) obfuscationEventEmitter: IObfuscationEventEmitter,
@inject(ServiceIdentifiers.TCustomNodeGroupStorage) customNodeGroupStorage: IStorage<ICustomNodeGroup>,
@inject(ServiceIdentifiers.Factory__INodeTransformer) nodeTransformerFactory: TNodeTransformerFactory,
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
this.stackTraceAnalyzer = stackTraceAnalyzer;
this.obfuscationEventEmitter = obfuscationEventEmitter;
this.customNodeGroupStorage = customNodeGroupStorage;
this.nodeTransformerFactory = nodeTransformerFactory;
this.options = options;
}
/**
* @param astTree
* @returns {ESTree.Program}
*/
public obfuscateAstTree (astTree: ESTree.Program): ESTree.Program {
if (Node.isProgramNode(astTree) && !astTree.body.length) {
return astTree;
}
astTree = <ESTree.Program>NodeUtils.parentize(astTree);
const stackTraceData: IStackTraceData[] = this.stackTraceAnalyzer.analyze(astTree.body);
// initialize custom node groups and configure custom nodes
this.customNodeGroupStorage
.getStorage()
.forEach((customNodeGroup: ICustomNodeGroup) => {
customNodeGroup.initialize();
this.obfuscationEventEmitter.once(
customNodeGroup.getAppendEvent(),
customNodeGroup.appendCustomNodes.bind(customNodeGroup)
);
});
this.obfuscationEventEmitter.emit(ObfuscationEvents.BeforeObfuscation, astTree, stackTraceData);
// first pass: control flow flattening
if (this.options.controlFlowFlattening) {
astTree = this.transformAstTree(astTree, Obfuscator.controlFlowTransformersList);
}
// second pass: nodes obfuscation
astTree = this.transformAstTree(astTree, [
...Obfuscator.convertingTransformersList,
...Obfuscator.obfuscatingTransformersList
]);
this.obfuscationEventEmitter.emit(ObfuscationEvents.AfterObfuscation, astTree, stackTraceData);
return astTree;
}
/**
* @param astTree
* @param nodeTransformers
*/
private transformAstTree (
astTree: ESTree.Program,
nodeTransformers: NodeTransformers[]
): ESTree.Program {
const enterVisitors: IVisitor[] = [];
const leaveVisitors: IVisitor[] = [];
const nodeTransformersLength: number = nodeTransformers.length;
let visitor: IVisitor;
for (let i: number = 0; i < nodeTransformersLength; i++) {
visitor = this.nodeTransformerFactory(nodeTransformers[i]).getVisitor();
if (visitor.enter) {
enterVisitors.push(visitor);
}
if (visitor.leave) {
leaveVisitors.push(visitor);
}
}
estraverse.replace(astTree, {
enter: this.mergeVisitorsForDirection(enterVisitors, VisitorDirection.enter),
leave: this.mergeVisitorsForDirection(leaveVisitors, VisitorDirection.leave)
});
return astTree;
}
/**
* @param visitors
* @param direction
* @return {TVisitorDirection}
*/
private mergeVisitorsForDirection (visitors: IVisitor[], direction: TVisitorDirection): TVisitorFunction {
if (!visitors.length) {
return (node: ESTree.Node, parentNode: ESTree.Node) => node;
}
const visitorsLength: number = visitors.length;
let visitor: IVisitor;
return (node: ESTree.Node, parentNode: ESTree.Node) => {
for (let i: number = 0; i < visitorsLength; i++) {
visitor = visitors[i];
const visitorFunction: TVisitorFunction | undefined = visitor[direction];
Iif (!visitorFunction) {
continue;
}
const visitorResult: ESTree.Node | void = visitorFunction(node, parentNode);
if (!visitorResult) {
continue;
}
node = visitorResult;
}
return node;
};
}
}
|