Skip to content

Commit 9dde522

Browse files
author
sanex3339
committed
Custom nodes refactoring. CustomNodeObfuscator class.
1 parent 837e743 commit 9dde522

File tree

74 files changed

+430
-27569
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+430
-27569
lines changed

dist/index.browser.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.cli.js

Lines changed: 1 addition & 14045 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 1 addition & 13348 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/container/ServiceIdentifiers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export enum ServiceIdentifiers {
2222
ICustomNodeGroup = 'ICustomNodeGroup',
2323
IControlFlowReplacer = 'IControlFlowReplacer',
2424
ICustomNodeFormatter = 'ICustomNodeFormatter',
25+
ICustomNodeObfuscator = 'ICustomNodeObfuscator',
2526
IEscapeSequenceEncoder = 'IEscapeSequenceEncoder',
2627
IIdentifierNamesGenerator = 'IIdentifierNamesGenerator',
2728
IIdentifierObfuscatingReplacer = 'IIdentifierObfuscatingReplacer',
@@ -41,7 +42,6 @@ export enum ServiceIdentifiers {
4142
IRandomGenerator = 'IRandomGenerator',
4243
IScopeIdentifiersTraverser = 'IScopeIdentifiersTraverser',
4344
ISourceCode = 'ISourceCode',
44-
ISourceMapCorrector = 'ISourceMapCorrector',
4545
IScopeAnalyzer = 'IScopeAnalyzer',
4646
IStringArrayStorage = 'IStringArrayStorage',
4747
IStringArrayStorageAnalyzer = 'IStringArrayStorageAnalyzer',

src/container/modules/custom-nodes/CustomNodesModule.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ import { StringArrayNode } from '../../../custom-nodes/string-array-nodes/String
4040
import { StringArrayRotateFunctionNode } from '../../../custom-nodes/string-array-nodes/StringArrayRotateFunctionNode';
4141
import { StringLiteralControlFlowStorageCallNode } from '../../../custom-nodes/control-flow-flattening-nodes/control-flow-storage-nodes/StringLiteralControlFlowStorageCallNode';
4242
import { StringLiteralNode } from '../../../custom-nodes/control-flow-flattening-nodes/StringLiteralNode';
43+
import { ICustomNodeObfuscator } from '../../../interfaces/custom-nodes/ICustomNodeObfuscator';
44+
import { CustomNodeObfuscator } from '../../../custom-nodes/CustomNodeObfuscator';
4345

4446
export const customNodesModule: interfaces.ContainerModule = new ContainerModule((bind: interfaces.Bind) => {
4547
// custom nodes
@@ -163,6 +165,7 @@ export const customNodesModule: interfaces.ContainerModule = new ContainerModule
163165
ServiceIdentifiers.Newable__ICustomNode,
164166
ServiceIdentifiers.Factory__IIdentifierNamesGenerator,
165167
ServiceIdentifiers.ICustomNodeFormatter,
168+
ServiceIdentifiers.ICustomNodeObfuscator,
166169
ServiceIdentifiers.IRandomGenerator,
167170
ServiceIdentifiers.IOptions,
168171
ServiceIdentifiers.IPrevailingKindOfVariablesAnalyzer
@@ -175,6 +178,7 @@ export const customNodesModule: interfaces.ContainerModule = new ContainerModule
175178
ServiceIdentifiers.Newable__ICustomNode,
176179
ServiceIdentifiers.Factory__IIdentifierNamesGenerator,
177180
ServiceIdentifiers.ICustomNodeFormatter,
181+
ServiceIdentifiers.ICustomNodeObfuscator,
178182
ServiceIdentifiers.IRandomGenerator,
179183
ServiceIdentifiers.IOptions
180184
));
@@ -186,6 +190,7 @@ export const customNodesModule: interfaces.ContainerModule = new ContainerModule
186190
ServiceIdentifiers.Newable__ICustomNode,
187191
ServiceIdentifiers.Factory__IIdentifierNamesGenerator,
188192
ServiceIdentifiers.ICustomNodeFormatter,
193+
ServiceIdentifiers.ICustomNodeObfuscator,
189194
ServiceIdentifiers.IRandomGenerator,
190195
ServiceIdentifiers.IOptions,
191196
ServiceIdentifiers.IPrevailingKindOfVariablesAnalyzer
@@ -200,4 +205,9 @@ export const customNodesModule: interfaces.ContainerModule = new ContainerModule
200205
bind<ICustomNodeFormatter>(ServiceIdentifiers.ICustomNodeFormatter)
201206
.to(CustomNodeFormatter)
202207
.inSingletonScope();
208+
209+
// custom node obfuscator
210+
bind<ICustomNodeObfuscator>(ServiceIdentifiers.ICustomNodeObfuscator)
211+
.to(CustomNodeObfuscator)
212+
.inSingletonScope();
203213
});

src/custom-nodes/AbstractCustomNode.ts

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@ import { inject, injectable } from 'inversify';
22
import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
33

44
import { TIdentifierNamesGeneratorFactory } from '../types/container/generators/TIdentifierNamesGeneratorFactory';
5-
import { TInputOptions } from '../types/options/TInputOptions';
65
import { TStatement } from '../types/node/TStatement';
76

87
import { ICustomNode } from '../interfaces/custom-nodes/ICustomNode';
8+
import { ICustomNodeFormatter } from '../interfaces/custom-nodes/ICustomNodeFormatter';
9+
import { ICustomNodeObfuscator } from '../interfaces/custom-nodes/ICustomNodeObfuscator';
910
import { IIdentifierNamesGenerator } from '../interfaces/generators/identifier-names-generators/IIdentifierNamesGenerator';
1011
import { IOptions } from '../interfaces/options/IOptions';
1112
import { IRandomGenerator } from '../interfaces/utils/IRandomGenerator';
12-
import { ICustomNodeFormatter } from '../interfaces/custom-nodes/ICustomNodeFormatter';
13-
14-
import { GlobalVariableTemplate1 } from '../templates/GlobalVariableTemplate1';
15-
import { GlobalVariableTemplate2 } from '../templates/GlobalVariableTemplate2';
16-
17-
import { NO_ADDITIONAL_NODES_PRESET } from '../options/presets/NoCustomNodes';
1813

19-
import { JavaScriptObfuscator } from '../JavaScriptObfuscatorFacade';
14+
import { GlobalVariableTemplate1 } from './common/templates/GlobalVariableTemplate1';
15+
import { GlobalVariableTemplate2 } from './common/templates/GlobalVariableTemplate2';
2016

2117
@injectable()
2218
export abstract class AbstractCustomNode <
@@ -35,6 +31,16 @@ export abstract class AbstractCustomNode <
3531
*/
3632
protected cachedNode: TStatement[] | null = null;
3733

34+
/**
35+
* @type {ICustomNodeFormatter}
36+
*/
37+
protected readonly customNodeFormatter: ICustomNodeFormatter;
38+
39+
/**
40+
* @type {ICustomNodeObfuscator}
41+
*/
42+
protected readonly customNodeObfuscator: ICustomNodeObfuscator;
43+
3844
/**
3945
* @type {IIdentifierNamesGenerator}
4046
*/
@@ -50,26 +56,24 @@ export abstract class AbstractCustomNode <
5056
*/
5157
protected readonly randomGenerator: IRandomGenerator;
5258

53-
/**
54-
* @type {ICustomNodeFormatter}
55-
*/
56-
protected readonly customNodeFormatter: ICustomNodeFormatter;
57-
5859
/**
5960
* @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
6061
* @param {ICustomNodeFormatter} customNodeFormatter
62+
* @param {ICustomNodeObfuscator} customNodeObfuscator
6163
* @param {IRandomGenerator} randomGenerator
6264
* @param {IOptions} options
6365
*/
6466
protected constructor (
6567
@inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
6668
identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
6769
@inject(ServiceIdentifiers.ICustomNodeFormatter) customNodeFormatter: ICustomNodeFormatter,
70+
@inject(ServiceIdentifiers.ICustomNodeObfuscator) customNodeObfuscator: ICustomNodeObfuscator,
6871
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
6972
@inject(ServiceIdentifiers.IOptions) options: IOptions
7073
) {
7174
this.identifierNamesGenerator = identifierNamesGeneratorFactory(options);
7275
this.customNodeFormatter = customNodeFormatter;
76+
this.customNodeObfuscator = customNodeObfuscator;
7377
this.randomGenerator = randomGenerator;
7478
this.options = options;
7579
}
@@ -105,41 +109,6 @@ export abstract class AbstractCustomNode <
105109
return '';
106110
}
107111

108-
/**
109-
* @param {string} template
110-
* @param {TInputOptions} options
111-
* @returns {string}
112-
*/
113-
protected obfuscateTemplate (template: string, options: TInputOptions = {}): string {
114-
const reservedNames: string[] = this.getPreservedNames(options.reservedNames);
115-
116-
return JavaScriptObfuscator.obfuscate(
117-
template,
118-
{
119-
...NO_ADDITIONAL_NODES_PRESET,
120-
identifierNamesGenerator: this.options.identifierNamesGenerator,
121-
identifiersDictionary: this.options.identifiersDictionary,
122-
seed: this.randomGenerator.getRawSeed(),
123-
...options,
124-
reservedNames
125-
}
126-
).getObfuscatedCode();
127-
}
128-
129-
/**
130-
* @param {string[]} additionalNames
131-
* @returns {string[]}
132-
*/
133-
private getPreservedNames (additionalNames: string[] = []): string[] {
134-
return Array
135-
.from(new Set([
136-
...Array.from(this.identifierNamesGenerator.getPreservedNames().values()),
137-
...additionalNames
138-
])
139-
.values())
140-
.map((preservedName: string) => `^${preservedName}$`);
141-
}
142-
143112
/**
144113
* @param {TInitialData} args
145114
*/

src/custom-nodes/AbstractCustomNodeGroup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
44
import { TIdentifierNamesGeneratorFactory } from '../types/container/generators/TIdentifierNamesGeneratorFactory';
55
import { TNodeWithStatements } from '../types/node/TNodeWithStatements';
66

7+
import { ICallsGraphData } from '../interfaces/analyzers/calls-graph-analyzer/ICallsGraphData';
78
import { ICustomNode } from '../interfaces/custom-nodes/ICustomNode';
89
import { ICustomNodeGroup } from '../interfaces/custom-nodes/ICustomNodeGroup';
910
import { IIdentifierNamesGenerator } from '../interfaces/generators/identifier-names-generators/IIdentifierNamesGenerator';
1011
import { IOptions } from '../interfaces/options/IOptions';
1112
import { IRandomGenerator } from '../interfaces/utils/IRandomGenerator';
12-
import { ICallsGraphData } from '../interfaces/analyzers/calls-graph-analyzer/ICallsGraphData';
1313

1414
import { CustomNode } from '../enums/custom-nodes/CustomNode';
1515
import { ObfuscationEvent } from '../enums/event-emitters/ObfuscationEvent';
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { inject, injectable } from 'inversify';
2+
import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
3+
4+
import { TInputOptions } from '../types/options/TInputOptions';
5+
6+
import { ICustomNodeObfuscator } from '../interfaces/custom-nodes/ICustomNodeObfuscator';
7+
import { IOptions } from '../interfaces/options/IOptions';
8+
import { IRandomGenerator } from '../interfaces/utils/IRandomGenerator';
9+
10+
import { IdentifierNamesGenerator } from '../enums/generators/identifier-names-generators/IdentifierNamesGenerator';
11+
12+
import { NO_ADDITIONAL_NODES_PRESET } from '../options/presets/NoCustomNodes';
13+
14+
import { JavaScriptObfuscator } from '../JavaScriptObfuscatorFacade';
15+
16+
@injectable()
17+
export class CustomNodeObfuscator implements ICustomNodeObfuscator {
18+
/**
19+
* @type {IOptions}
20+
*/
21+
private readonly options: IOptions;
22+
23+
/**
24+
* @type {IRandomGenerator}
25+
*/
26+
private readonly randomGenerator: IRandomGenerator;
27+
28+
/**
29+
* @param {IRandomGenerator} randomGenerator
30+
* @param {IOptions} options
31+
*/
32+
public constructor (
33+
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
34+
@inject(ServiceIdentifiers.IOptions) options: IOptions
35+
) {
36+
this.randomGenerator = randomGenerator;
37+
this.options = options;
38+
}
39+
40+
/**
41+
* @param {string} template
42+
* @param {TInputOptions} additionalOptions
43+
* @returns {string}
44+
*/
45+
public obfuscateTemplate (template: string, additionalOptions: TInputOptions = {}): string {
46+
/**
47+
* During the first pass we should rename all identifiers to a random one
48+
*/
49+
const firstPassObfuscatedCode = JavaScriptObfuscator.obfuscate(
50+
template,
51+
{
52+
...NO_ADDITIONAL_NODES_PRESET,
53+
identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator
54+
}
55+
).getObfuscatedCode();
56+
57+
/**
58+
* During the seconds pass we should obfuscate template to the same format as the main code
59+
* Also, we should add additional transformations that depends on the custom node
60+
*/
61+
const secondPassObfuscatedCode = JavaScriptObfuscator.obfuscate(
62+
firstPassObfuscatedCode,
63+
{
64+
...NO_ADDITIONAL_NODES_PRESET,
65+
identifierNamesGenerator: this.options.identifierNamesGenerator,
66+
identifiersDictionary: this.options.identifiersDictionary,
67+
seed: this.randomGenerator.getRawSeed(),
68+
...additionalOptions
69+
}
70+
).getObfuscatedCode();
71+
72+
return secondPassObfuscatedCode;
73+
}
74+
}

0 commit comments

Comments
 (0)