| 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 | 1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
2621x
2621x
2621x
3x
2618x
2618x
2618x
2618x
2618x
2618x
2618x
2618x
3618x
2588x
2618x
3618x
1030x
2618x
2618x
13090x
13090x
11538x
1552x
2618x
495377x
495377x
333686x
161691x
163725x
2618x
495377x
| import * as estraverse from 'estraverse';
import * as ESTree from 'estree';
import { TNodeGroup } from './types/TNodeGroup';
import { TNodeObfuscator } from './types/TNodeObfuscator';
import { ICustomNode } from './interfaces/custom-nodes/ICustomNode';
import { IObfuscator } from './interfaces/IObfuscator';
import { IOptions } from './interfaces/IOptions';
import { IStackTraceData } from './interfaces/stack-trace-analyzer/IStackTraceData';
import { AppendState } from './enums/AppendState';
import { NodeType } from './enums/NodeType';
import { CatchClauseObfuscator } from './node-obfuscators/CatchClauseObfuscator';
import { ConsoleOutputNodesGroup } from './node-groups/ConsoleOutputNodesGroup';
import { DebugProtectionNodesGroup } from './node-groups/DebugProtectionNodesGroup';
import { DomainLockNodesGroup } from './node-groups/DomainLockNodesGroup';
import { FunctionDeclarationObfuscator } from './node-obfuscators/FunctionDeclarationObfuscator';
import { FunctionObfuscator } from './node-obfuscators/FunctionObfuscator';
import { LabeledStatementObfuscator } from './node-obfuscators/LabeledStatementObfuscator';
import { LiteralObfuscator } from './node-obfuscators/LiteralObfuscator';
import { MemberExpressionObfuscator } from './node-obfuscators/MemberExpressionObfuscator';
import { MethodDefinitionObfuscator } from './node-obfuscators/MethodDefinitionObfuscator';
import { Node } from './node/Node';
import { NodeUtils } from './node/NodeUtils';
import { ObjectExpressionObfuscator } from './node-obfuscators/ObjectExpressionObfuscator';
import { SelfDefendingNodesGroup } from './node-groups/SelfDefendingNodesGroup';
import { StackTraceAnalyzer } from './stack-trace-analyzer/StackTraceAnalyzer';
import { StringArrayNodesGroup } from './node-groups/StringArrayNodesGroup';
import { VariableDeclarationObfuscator } from './node-obfuscators/VariableDeclarationObfuscator';
export class Obfuscator implements IObfuscator {
/**
* @type {TNodeGroup[]}
*/
private static nodeGroups: TNodeGroup[] = [
DomainLockNodesGroup,
SelfDefendingNodesGroup,
ConsoleOutputNodesGroup,
DebugProtectionNodesGroup,
StringArrayNodesGroup
];
/**
* @type {Map<string, TNodeObfuscator[]>}
*/
private static nodeObfuscators: Map <string, TNodeObfuscator[]> = new Map <string, TNodeObfuscator[]> ([
[NodeType.ArrowFunctionExpression, [FunctionObfuscator]],
[NodeType.ClassDeclaration, [FunctionDeclarationObfuscator]],
[NodeType.CatchClause, [CatchClauseObfuscator]],
[NodeType.FunctionDeclaration, [
FunctionDeclarationObfuscator,
FunctionObfuscator
]],
[NodeType.FunctionExpression, [FunctionObfuscator]],
[NodeType.MemberExpression, [MemberExpressionObfuscator]],
[NodeType.MethodDefinition, [MethodDefinitionObfuscator]],
[NodeType.ObjectExpression, [ObjectExpressionObfuscator]],
[NodeType.VariableDeclaration, [VariableDeclarationObfuscator]],
[NodeType.LabeledStatement, [LabeledStatementObfuscator]],
[NodeType.Literal, [LiteralObfuscator]]
]);
/**
* @type {Map<string, AbstractCustomNode>}
*/
private customNodes: Map <string, ICustomNode> = new Map <string, ICustomNode> ();
/**
* @type {IOptions}
*/
private options: IOptions;
/**
* @param options
*/
constructor (options: IOptions) {
this.options = options;
}
/**
* @param node
* @returns {ESTree.Node}
*/
public obfuscateNode (node: ESTree.Program): ESTree.Node {
if (Node.isProgramNode(node) && !node.body.length) {
return node;
}
NodeUtils.parentize(node);
const stackTraceData: IStackTraceData[] = new StackTraceAnalyzer(node.body).analyze();
this.initializeCustomNodes(stackTraceData);
this.beforeObfuscation(node);
this.obfuscate(node);
this.afterObfuscation(node);
return node;
}
/**
* @param astTree
*/
private afterObfuscation (astTree: ESTree.Node): void {
this.customNodes.forEach((node: ICustomNode) => {
if (node.getAppendState() === AppendState.AfterObfuscation) {
node.appendNode(astTree);
}
});
}
/**
* @param astTree
*/
private beforeObfuscation (astTree: ESTree.Node): void {
this.customNodes.forEach((node: ICustomNode) => {
if (node.getAppendState() === AppendState.BeforeObfuscation) {
node.appendNode(astTree);
}
});
};
/**
* @param stackTraceData
*/
private initializeCustomNodes (stackTraceData: IStackTraceData[]): void {
let customNodes: [string, ICustomNode][] = [];
Obfuscator.nodeGroups.forEach((nodeGroupConstructor: TNodeGroup) => {
const nodeGroupNodes: Map <string, ICustomNode> | undefined = new nodeGroupConstructor(
stackTraceData, this.options
).getNodes();
if (!nodeGroupNodes) {
return;
}
customNodes.push(...nodeGroupNodes);
});
this.customNodes = new Map <string, ICustomNode> (customNodes);
}
/**
* @param node
* @param parentNode
*/
private initializeNodeObfuscators (node: ESTree.Node, parentNode: ESTree.Node): void {
let nodeObfuscators: TNodeObfuscator[] | undefined = Obfuscator.nodeObfuscators.get(node.type);
if (!nodeObfuscators) {
return;
}
nodeObfuscators.forEach((obfuscator: TNodeObfuscator) => {
new obfuscator(this.customNodes, this.options).obfuscateNode(node, parentNode);
});
}
/**
* @param node
*/
private obfuscate (node: ESTree.Node): void {
estraverse.traverse(node, {
enter: (node: ESTree.Node, parentNode: ESTree.Node): void => {
this.initializeNodeObfuscators(node, parentNode);
}
});
}
}
|