Skip to content

Commit 002697c

Browse files
author
sanex3339
committed
Improved mangled identifier names generator. Improved variable preserving.
1 parent 029d595 commit 002697c

File tree

27 files changed

+255
-164
lines changed

27 files changed

+255
-164
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Change Log
22

33
v0.25.0
44
---
5+
* Improved `mangled` identifier names generator logic
56
* Fixed conflicts between generated names and names from untouched identifiers from source code. Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/550. Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/549
67
* Prevented transformation of object keys in sequence expression that has `super` call
78

dist/index.browser.js

Lines changed: 5 additions & 5 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 & 1 deletion
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 & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/custom-nodes/AbstractCustomNode.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ 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';
56
import { TStatement } from '../types/node/TStatement';
67

78
import { ICustomNode } from '../interfaces/custom-nodes/ICustomNode';
@@ -13,6 +14,10 @@ import { ICustomNodeFormatter } from '../interfaces/custom-nodes/ICustomNodeForm
1314
import { GlobalVariableTemplate1 } from '../templates/GlobalVariableTemplate1';
1415
import { GlobalVariableTemplate2 } from '../templates/GlobalVariableTemplate2';
1516

17+
import { NO_ADDITIONAL_NODES_PRESET } from '../options/presets/NoCustomNodes';
18+
19+
import { JavaScriptObfuscator } from '../JavaScriptObfuscatorFacade';
20+
1621
@injectable()
1722
export abstract class AbstractCustomNode <
1823
TInitialData extends any[] = any[]
@@ -100,16 +105,39 @@ export abstract class AbstractCustomNode <
100105
return '';
101106
}
102107

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+
103129
/**
104130
* @param {string[]} additionalNames
105131
* @returns {string[]}
106132
*/
107-
protected getPreservedNames (additionalNames: string[]): string[] {
108-
return Array.from(new Set([
109-
...Array.from(this.identifierNamesGenerator.getPreservedNames().values()),
110-
...additionalNames
111-
]).values())
112-
.map((preservedName: string) => `^${preservedName}$`);
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}$`);
113141
}
114142

115143
/**

src/custom-nodes/node-calls-controller-nodes/NodeCallsControllerFunctionNode.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
44
import { TIdentifierNamesGeneratorFactory } from '../../types/container/generators/TIdentifierNamesGeneratorFactory';
55
import { TStatement } from '../../types/node/TStatement';
66

7+
import { ICustomNodeFormatter } from '../../interfaces/custom-nodes/ICustomNodeFormatter';
78
import { IOptions } from '../../interfaces/options/IOptions';
89
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
910

@@ -13,12 +14,8 @@ import { initializable } from '../../decorators/Initializable';
1314

1415
import { SingleNodeCallControllerTemplate } from '../../templates/SingleNodeCallControllerTemplate';
1516

16-
import { NO_ADDITIONAL_NODES_PRESET } from '../../options/presets/NoCustomNodes';
17-
1817
import { AbstractCustomNode } from '../AbstractCustomNode';
19-
import { JavaScriptObfuscator } from '../../JavaScriptObfuscatorFacade';
2018
import { NodeUtils } from '../../node/NodeUtils';
21-
import { ICustomNodeFormatter } from '../../interfaces/custom-nodes/ICustomNodeFormatter';
2219

2320
@injectable()
2421
export class NodeCallsControllerFunctionNode extends AbstractCustomNode {
@@ -72,17 +69,11 @@ export class NodeCallsControllerFunctionNode extends AbstractCustomNode {
7269
*/
7370
protected getNodeTemplate (): string {
7471
if (this.appendEvent === ObfuscationEvent.AfterObfuscation) {
75-
return JavaScriptObfuscator.obfuscate(
72+
return this.obfuscateTemplate(
7673
this.customNodeFormatter.formatTemplate(SingleNodeCallControllerTemplate(), {
7774
singleNodeCallControllerFunctionName: this.callsControllerFunctionName
78-
}),
79-
{
80-
...NO_ADDITIONAL_NODES_PRESET,
81-
identifierNamesGenerator: this.options.identifierNamesGenerator,
82-
identifiersDictionary: this.options.identifiersDictionary,
83-
seed: this.options.seed
84-
}
85-
).getObfuscatedCode();
75+
})
76+
);
8677
}
8778

8879
return this.customNodeFormatter.formatTemplate(SingleNodeCallControllerTemplate(), {

src/custom-nodes/object-expression-keys-transformer-nodes/ObjectExpressionVariableDeclarationHostNode.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
44
import * as ESTree from 'estree';
55

66
import { TIdentifierNamesGeneratorFactory } from '../../types/container/generators/TIdentifierNamesGeneratorFactory';
7+
import { TNodeWithLexicalScope } from '../../types/node/TNodeWithLexicalScope';
78
import { TStatement } from '../../types/node/TStatement';
89

910
import { IOptions } from '../../interfaces/options/IOptions';
@@ -15,6 +16,10 @@ import { NodeFactory } from '../../node/NodeFactory';
1516

1617
@injectable()
1718
export class ObjectExpressionVariableDeclarationHostNode extends AbstractCustomNode {
19+
/**
20+
* @type {TNodeWithLexicalScope}
21+
*/
22+
private lexicalScopeNode!: TNodeWithLexicalScope;
1823
/**
1924
* @ type {Property}
2025
*/
@@ -36,7 +41,8 @@ export class ObjectExpressionVariableDeclarationHostNode extends AbstractCustomN
3641
super(identifierNamesGeneratorFactory, customNodeFormatter, randomGenerator, options);
3742
}
3843

39-
public initialize (properties: ESTree.Property[]): void {
44+
public initialize (lexicalScopeNode: TNodeWithLexicalScope, properties: ESTree.Property[]): void {
45+
this.lexicalScopeNode = lexicalScopeNode;
4046
this.properties = properties;
4147
}
4248

@@ -49,7 +55,7 @@ export class ObjectExpressionVariableDeclarationHostNode extends AbstractCustomN
4955
[
5056
NodeFactory.variableDeclaratorNode(
5157
NodeFactory.identifierNode(
52-
this.identifierNamesGenerator.generate()
58+
this.identifierNamesGenerator.generateForLexicalScope(this.lexicalScopeNode)
5359
),
5460
NodeFactory.objectExpressionNode(this.properties)
5561
)

src/custom-nodes/self-defending-nodes/SelfDefendingUnicodeNode.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@ import { ICustomNodeFormatter } from '../../interfaces/custom-nodes/ICustomNodeF
1111

1212
import { initializable } from '../../decorators/Initializable';
1313

14-
import { NO_ADDITIONAL_NODES_PRESET } from '../../options/presets/NoCustomNodes';
15-
1614
import { SelfDefendingTemplate } from '../../templates/self-defending-nodes/self-defending-unicode-node/SelfDefendingTemplate';
1715

1816
import { AbstractCustomNode } from '../AbstractCustomNode';
19-
import { JavaScriptObfuscator } from '../../JavaScriptObfuscatorFacade';
2017
import { NodeUtils } from '../../node/NodeUtils';
2118

2219
@injectable()
@@ -71,18 +68,14 @@ export class SelfDefendingUnicodeNode extends AbstractCustomNode {
7168
* @returns {string}
7269
*/
7370
protected getNodeTemplate (): string {
74-
return JavaScriptObfuscator.obfuscate(
71+
return this.obfuscateTemplate(
7572
this.customNodeFormatter.formatTemplate(SelfDefendingTemplate(this.escapeSequenceEncoder), {
7673
selfDefendingFunctionName: this.identifierNamesGenerator.generate(),
7774
singleNodeCallControllerFunctionName: this.callsControllerFunctionName
7875
}),
7976
{
80-
...NO_ADDITIONAL_NODES_PRESET,
81-
identifierNamesGenerator: this.options.identifierNamesGenerator,
82-
identifiersDictionary: this.options.identifiersDictionary,
83-
seed: this.options.seed,
8477
unicodeEscapeSequence: true
8578
}
86-
).getObfuscatedCode();
79+
);
8780
}
8881
}

src/custom-nodes/string-array-nodes/StringArrayCallsWrapper.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import { StringArrayEncoding } from '../../enums/StringArrayEncoding';
1414

1515
import { initializable } from '../../decorators/Initializable';
1616

17-
import { NO_ADDITIONAL_NODES_PRESET } from '../../options/presets/NoCustomNodes';
18-
1917
import { AtobTemplate } from '../../templates/AtobTemplate';
2018
import { GlobalVariableNoEvalTemplate } from '../../templates/GlobalVariableNoEvalTemplate';
2119
import { Rc4Template } from '../../templates/Rc4Template';
@@ -25,7 +23,6 @@ import { StringArrayCallsWrapperTemplate } from '../../templates/string-array-no
2523
import { StringArrayRc4DecodeNodeTemplate } from '../../templates/string-array-nodes/string-array-calls-wrapper/StringArrayRC4DecodeNodeTemplate';
2624

2725
import { AbstractCustomNode } from '../AbstractCustomNode';
28-
import { JavaScriptObfuscator } from '../../JavaScriptObfuscatorFacade';
2926
import { NodeUtils } from '../../node/NodeUtils';
3027

3128
@injectable()
@@ -93,24 +90,18 @@ export class StringArrayCallsWrapper extends AbstractCustomNode {
9390
protected getNodeTemplate (): string {
9491
const decodeNodeTemplate: string = this.getDecodeStringArrayTemplate();
9592

96-
const preservedNames: string[] = this.getPreservedNames([this.stringArrayName]);
93+
const preservedNames: string[] = [this.stringArrayName];
9794

98-
return JavaScriptObfuscator.obfuscate(
95+
return this.obfuscateTemplate(
9996
this.customNodeFormatter.formatTemplate(StringArrayCallsWrapperTemplate(), {
10097
decodeNodeTemplate,
10198
stringArrayCallsWrapperName: this.stringArrayCallsWrapperName,
10299
stringArrayName: this.stringArrayName
103100
}),
104101
{
105-
...NO_ADDITIONAL_NODES_PRESET,
106-
identifierNamesGenerator: this.options.identifierNamesGenerator,
107-
identifiersDictionary: this.options.identifiersDictionary,
108-
reservedNames: [
109-
...preservedNames
110-
],
111-
seed: this.randomGenerator.getRawSeed()
102+
reservedNames: preservedNames
112103
}
113-
).getObfuscatedCode();
104+
);
114105
}
115106

116107
/**

src/custom-nodes/string-array-nodes/StringArrayRotateFunctionNode.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,10 @@ import { ICustomNodeFormatter } from '../../interfaces/custom-nodes/ICustomNodeF
1111

1212
import { initializable } from '../../decorators/Initializable';
1313

14-
import { NO_ADDITIONAL_NODES_PRESET } from '../../options/presets/NoCustomNodes';
15-
1614
import { SelfDefendingTemplate } from '../../templates/string-array-nodes/string-array-rotate-function-node/SelfDefendingTemplate';
1715
import { StringArrayRotateFunctionTemplate } from '../../templates/string-array-nodes/string-array-rotate-function-node/StringArrayRotateFunctionTemplate';
1816

1917
import { AbstractCustomNode } from '../AbstractCustomNode';
20-
import { JavaScriptObfuscator } from '../../JavaScriptObfuscatorFacade';
2118
import { NodeUtils } from '../../node/NodeUtils';
2219
import { NumberUtils } from '../../utils/NumberUtils';
2320

@@ -86,6 +83,7 @@ export class StringArrayRotateFunctionNode extends AbstractCustomNode {
8683
protected getNodeTemplate (): string {
8784
const timesName: string = this.identifierNamesGenerator.generate();
8885
const whileFunctionName: string = this.identifierNamesGenerator.generate();
86+
const preservedNames: string[] = [this.stringArrayName];
8987

9088
let code: string = '';
9189

@@ -98,20 +96,17 @@ export class StringArrayRotateFunctionNode extends AbstractCustomNode {
9896
code = `${whileFunctionName}(++${timesName})`;
9997
}
10098

101-
return JavaScriptObfuscator.obfuscate(
99+
return this.obfuscateTemplate(
102100
this.customNodeFormatter.formatTemplate(StringArrayRotateFunctionTemplate(), {
103101
code,
104102
timesName,
103+
whileFunctionName,
105104
stringArrayName: this.stringArrayName,
106-
stringArrayRotationAmount: NumberUtils.toHex(this.stringArrayRotationAmount),
107-
whileFunctionName
105+
stringArrayRotationAmount: NumberUtils.toHex(this.stringArrayRotationAmount)
108106
}),
109107
{
110-
...NO_ADDITIONAL_NODES_PRESET,
111-
identifierNamesGenerator: this.options.identifierNamesGenerator,
112-
identifiersDictionary: this.options.identifiersDictionary,
113-
seed: this.randomGenerator.getRawSeed()
108+
reservedNames: preservedNames
114109
}
115-
).getObfuscatedCode();
110+
);
116111
}
117112
}

0 commit comments

Comments
 (0)