Skip to content

Commit 36ea9c0

Browse files
authored
Merge pull request javascript-obfuscator#906 from javascript-obfuscator/calls-wrappers-as-function-declarations
Helpers improve
2 parents 52358ab + 85ee48b commit 36ea9c0

File tree

18 files changed

+148
-144
lines changed

18 files changed

+148
-144
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
Change Log
22

3+
v2.12.0
4+
---
5+
* `stringArray` calls wrappers now inserted as `FunctionDeclaration` nodes at random indexes
6+
37
v2.11.1
48
---
59
* **CLI**: now it's possible to specify the path to the input file after specifying the obfuscator options. Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/904

dist/index.browser.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.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.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "javascript-obfuscator",
3-
"version": "2.11.1",
3+
"version": "2.12.0",
44
"description": "JavaScript obfuscator",
55
"keywords": [
66
"obfuscator",

src/custom-code-helpers/string-array/group/StringArrayCodeHelperGroup.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { AbstractCustomCodeHelperGroup } from '../../AbstractCustomCodeHelperGro
2222
import { NodeAppender } from '../../../node/NodeAppender';
2323
import { StringArrayCallsWrapperCodeHelper } from '../StringArrayCallsWrapperCodeHelper';
2424
import { StringArrayCodeHelper } from '../StringArrayCodeHelper';
25+
import { TStatement } from '../../../types/node/TStatement';
2526

2627
@injectable()
2728
export class StringArrayCodeHelperGroup extends AbstractCustomCodeHelperGroup {
@@ -90,14 +91,22 @@ export class StringArrayCodeHelperGroup extends AbstractCustomCodeHelperGroup {
9091

9192
// stringArrayCallsWrapper helper nodes append
9293
const stringArrayEncodingsLength: number = this.options.stringArrayEncoding.length;
93-
for (let i = 0; i < stringArrayEncodingsLength; i++) {
94+
// Stating from index 1 and forward. 0 index is reserved for string array itself.
95+
let randomIndex: number = 1;
96+
for (let i = 0; i < stringArrayEncodingsLength; i++, randomIndex++) {
9497
const stringArrayEncoding: TStringArrayEncoding = this.options.stringArrayEncoding[i];
9598
const stringArrayCallsWrapperCodeHelperName: CustomCodeHelper = this.getStringArrayCallsWrapperCodeHelperName(stringArrayEncoding);
9699

100+
const scopeStatements: TStatement[] = NodeAppender.getScopeStatements(nodeWithStatements);
101+
randomIndex = this.randomGenerator.getRandomInteger(
102+
randomIndex,
103+
scopeStatements.length - 1
104+
);
105+
97106
this.appendCustomNodeIfExist(
98107
stringArrayCallsWrapperCodeHelperName,
99108
(customCodeHelper: ICustomCodeHelper<TInitialData<StringArrayCallsWrapperCodeHelper>>) => {
100-
NodeAppender.insertAtIndex(nodeWithStatements, customCodeHelper.getNode(), i + 1);
109+
NodeAppender.insertAtIndex(nodeWithStatements, customCodeHelper.getNode(), randomIndex);
101110
}
102111
);
103112
}

src/custom-code-helpers/string-array/templates/string-array-calls-wrapper/StringArrayCallsWrapperTemplate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
*/
44
export function StringArrayCallsWrapperTemplate (): string {
55
return `
6-
const {stringArrayCallsWrapperName} = function (index, key) {
6+
function {stringArrayCallsWrapperName} (index, key) {
77
index = index - {indexShiftAmount};
88
99
let value = {stringArrayName}[index];
1010
1111
{decodeCodeHelperTemplate}
1212
1313
return value;
14-
};
14+
}
1515
`;
1616
}

src/node/NodeAppender.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ export class NodeAppender {
8282
}
8383
}
8484

85+
/**
86+
* @param {TNodeWithStatements} nodeWithStatements
87+
* @returns {TStatement[]}
88+
*/
89+
public static getScopeStatements (nodeWithStatements: TNodeWithStatements): TStatement[] {
90+
if (NodeGuards.isSwitchCaseNode(nodeWithStatements)) {
91+
return nodeWithStatements.consequent;
92+
}
93+
94+
return nodeWithStatements.body;
95+
}
96+
8597
/**
8698
* @param {TNodeWithStatements} nodeWithStatements
8799
* @param {TStatement[]} statements
@@ -148,18 +160,6 @@ export class NodeAppender {
148160
]);
149161
}
150162

151-
/**
152-
* @param {TNodeWithStatements} nodeWithStatements
153-
* @returns {TStatement[]}
154-
*/
155-
private static getScopeStatements (nodeWithStatements: TNodeWithStatements): TStatement[] {
156-
if (NodeGuards.isSwitchCaseNode(nodeWithStatements)) {
157-
return nodeWithStatements.consequent;
158-
}
159-
160-
return nodeWithStatements.body;
161-
}
162-
163163
/**
164164
* @param {TNodeWithStatements} nodeWithStatements
165165
* @param {TStatement[]} statements

test/dev/dev.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
'use strict';
22

33
import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNodes';
4-
import { RenamePropertiesMode } from '../../src/enums/node-transformers/rename-properties-transformers/RenamePropertiesMode';
54

65
(function () {
76
const JavaScriptObfuscator: any = require('../../index');
87

98
let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
109
`
11-
const object = {
12-
foo: 1,
13-
bar: 2,
14-
baz: 3
15-
};
16-
17-
var excluded1 = 'bar';
18-
var excluded2 = 'baz';
19-
20-
console.log(object.foo, object['bar'], object.baz);
10+
var variable1 = '5' - 3;
11+
var variable2 = '5' + 3;
12+
var variable3 = '5' + - '2';
13+
var variable4 = ['10','10','10','10','10'].map(parseInt);
14+
var variable5 = 'foo ' + 1 + 1;
15+
console.log(variable1);
16+
console.log(variable2);
17+
console.log(variable3);
18+
console.log(variable4);
19+
console.log(variable5);
2120
`,
2221
{
2322
...NO_ADDITIONAL_NODES_PRESET,
2423
compact: false,
25-
renameProperties: true,
26-
renamePropertiesMode: RenamePropertiesMode.Safe
24+
stringArray: true,
25+
stringArrayThreshold: 1,
26+
rotateStringArray: true,
27+
stringArrayWrappersCount: 3
2728
}
2829
).getObfuscatedCode();
2930

test/functional-tests/custom-code-helpers/string-array/StringArrayCallsWrapperCodeHelper.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('StringArrayCallsWrapperCodeHelper', () => {
5555

5656
describe('Preserve string array name', () => {
5757
const callsWrapperRegExp: RegExp = new RegExp(`` +
58-
`var b *= *function *\\(c, *d\\) *{ *` +
58+
`function *b *\\(c, *d\\) *{ *` +
5959
`c *= *c *- *0x0; *` +
6060
`var e *= *a\\[c]; *` +
6161
``);

0 commit comments

Comments
 (0)