Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Change Log

v2.12.0
---
* `stringArray` calls wrappers now inserted as `FunctionDeclaration` nodes at random indexes

v2.11.1
---
* **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
Expand Down
2 changes: 1 addition & 1 deletion dist/index.browser.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.cli.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "2.11.1",
"version": "2.12.0",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { AbstractCustomCodeHelperGroup } from '../../AbstractCustomCodeHelperGro
import { NodeAppender } from '../../../node/NodeAppender';
import { StringArrayCallsWrapperCodeHelper } from '../StringArrayCallsWrapperCodeHelper';
import { StringArrayCodeHelper } from '../StringArrayCodeHelper';
import { TStatement } from '../../../types/node/TStatement';

@injectable()
export class StringArrayCodeHelperGroup extends AbstractCustomCodeHelperGroup {
Expand Down Expand Up @@ -90,14 +91,22 @@ export class StringArrayCodeHelperGroup extends AbstractCustomCodeHelperGroup {

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

const scopeStatements: TStatement[] = NodeAppender.getScopeStatements(nodeWithStatements);
randomIndex = this.randomGenerator.getRandomInteger(
randomIndex,
scopeStatements.length - 1
);

this.appendCustomNodeIfExist(
stringArrayCallsWrapperCodeHelperName,
(customCodeHelper: ICustomCodeHelper<TInitialData<StringArrayCallsWrapperCodeHelper>>) => {
NodeAppender.insertAtIndex(nodeWithStatements, customCodeHelper.getNode(), i + 1);
NodeAppender.insertAtIndex(nodeWithStatements, customCodeHelper.getNode(), randomIndex);
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
*/
export function StringArrayCallsWrapperTemplate (): string {
return `
const {stringArrayCallsWrapperName} = function (index, key) {
function {stringArrayCallsWrapperName} (index, key) {
index = index - {indexShiftAmount};

let value = {stringArrayName}[index];

{decodeCodeHelperTemplate}

return value;
};
}
`;
}
24 changes: 12 additions & 12 deletions src/node/NodeAppender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ export class NodeAppender {
}
}

/**
* @param {TNodeWithStatements} nodeWithStatements
* @returns {TStatement[]}
*/
public static getScopeStatements (nodeWithStatements: TNodeWithStatements): TStatement[] {
if (NodeGuards.isSwitchCaseNode(nodeWithStatements)) {
return nodeWithStatements.consequent;
}

return nodeWithStatements.body;
}

/**
* @param {TNodeWithStatements} nodeWithStatements
* @param {TStatement[]} statements
Expand Down Expand Up @@ -148,18 +160,6 @@ export class NodeAppender {
]);
}

/**
* @param {TNodeWithStatements} nodeWithStatements
* @returns {TStatement[]}
*/
private static getScopeStatements (nodeWithStatements: TNodeWithStatements): TStatement[] {
if (NodeGuards.isSwitchCaseNode(nodeWithStatements)) {
return nodeWithStatements.consequent;
}

return nodeWithStatements.body;
}

/**
* @param {TNodeWithStatements} nodeWithStatements
* @param {TStatement[]} statements
Expand Down
27 changes: 14 additions & 13 deletions test/dev/dev.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
'use strict';

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

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

let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
`
const object = {
foo: 1,
bar: 2,
baz: 3
};

var excluded1 = 'bar';
var excluded2 = 'baz';

console.log(object.foo, object['bar'], object.baz);
var variable1 = '5' - 3;
var variable2 = '5' + 3;
var variable3 = '5' + - '2';
var variable4 = ['10','10','10','10','10'].map(parseInt);
var variable5 = 'foo ' + 1 + 1;
console.log(variable1);
console.log(variable2);
console.log(variable3);
console.log(variable4);
console.log(variable5);
`,
{
...NO_ADDITIONAL_NODES_PRESET,
compact: false,
renameProperties: true,
renamePropertiesMode: RenamePropertiesMode.Safe
stringArray: true,
stringArrayThreshold: 1,
rotateStringArray: true,
stringArrayWrappersCount: 3
}
).getObfuscatedCode();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('StringArrayCallsWrapperCodeHelper', () => {

describe('Preserve string array name', () => {
const callsWrapperRegExp: RegExp = new RegExp(`` +
`var b *= *function *\\(c, *d\\) *{ *` +
`function *b *\\(c, *d\\) *{ *` +
`c *= *c *- *0x0; *` +
`var e *= *a\\[c]; *` +
``);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFac

describe('StringArrayCodeHelperGroup', () => {
const regExp: RegExp = new RegExp(
'var b *= *function\\(\\w, *\\w\\) *{.*return \\w;}; *' +
'var c *= *function\\(\\w, *\\w\\) *{.*return \\w;}; *' +
'var d *= *function\\(\\w, *\\w\\) *{.*return \\w;};'
'function *b *\\(\\w, *\\w\\) *{.*return \\w;}.*' +
'function *c *\\(\\w, *\\w\\) *{.*return \\w;}.*' +
'function *d *\\(\\w, *\\w\\) *{.*return \\w;}'
);

describe('StringArrayCallsWrapper code helper names', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ describe('StringArrayCallsWrapperTemplate', () => {
describe('Prevailing kind of variables', () => {
describe('`var` kind', () => {
let obfuscatedCode: string,
stringArrayCallsWrapperRegExp: RegExp = /var (_0x(\w){4}) *= *function/;
stringArrayCallsWrapperVariableRegExp: RegExp = /var (_0x(\w){4,6}) *= *(_0x(\w){4,6})\[(_0x(\w){4,6})];/;

beforeEach(() => {
const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-var.js');
Expand All @@ -289,8 +289,8 @@ describe('StringArrayCallsWrapperTemplate', () => {
obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
});

it('Should return correct kind of variables for string array calls wrapper', () => {
assert.match(obfuscatedCode, stringArrayCallsWrapperRegExp);
it('Should return correct kind of variables for string array calls wrapper code', () => {
assert.match(obfuscatedCode, stringArrayCallsWrapperVariableRegExp);
});

it('Should does not break on obfuscating', () => {
Expand All @@ -300,7 +300,7 @@ describe('StringArrayCallsWrapperTemplate', () => {

describe('`const` kind', () => {
let obfuscatedCode: string,
stringArrayCallsWrapperRegExp: RegExp = /const (_0x(\w){4}) *= *function/;
stringArrayCallsWrapperVariableRegExp: RegExp = /let (_0x(\w){4,6}) *= *(_0x(\w){4,6})\[(_0x(\w){4,6})];/;

beforeEach(() => {
const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
Expand All @@ -316,8 +316,8 @@ describe('StringArrayCallsWrapperTemplate', () => {
obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
});

it('Should return correct kind of variables for string array calls wrapper', () => {
assert.match(obfuscatedCode, stringArrayCallsWrapperRegExp);
it('Should return correct kind of variables for string array calls wrapper code', () => {
assert.match(obfuscatedCode, stringArrayCallsWrapperVariableRegExp);
});

it('Should does not break on obfuscating', () => {
Expand All @@ -327,7 +327,7 @@ describe('StringArrayCallsWrapperTemplate', () => {

describe('`let` kind', () => {
let obfuscatedCode: string,
stringArrayCallsWrapperRegExp: RegExp = /const (_0x(\w){4}) *= *function/;
stringArrayCallsWrapperVariableRegExp: RegExp = /let (_0x(\w){4,6}) *= *(_0x(\w){4,6})\[(_0x(\w){4,6})];/;

beforeEach(() => {
const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
Expand All @@ -343,8 +343,8 @@ describe('StringArrayCallsWrapperTemplate', () => {
obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
});

it('Should return correct kind of variables for string array calls wrapper', () => {
assert.match(obfuscatedCode, stringArrayCallsWrapperRegExp);
it('Should return correct kind of variables for string array calls wrapper code', () => {
assert.match(obfuscatedCode, stringArrayCallsWrapperVariableRegExp);
});

it('Should does not break on obfuscating', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('DirectivePlacementTransformer', function () {
const directiveRegExp: RegExp = new RegExp(
'^var _0x([a-f0-9]){4} *= *\\[\'test\', *\'use\\\\x20strict\']; *' +
'.*?' +
'var test *= *_0x([a-f0-9]){4}\\(0x0\\); *' +
'var test *= *_0x([a-f0-9]){4}\\(0x0\\);.*' +
'_0x([a-f0-9]){4}\\(0x1\\);'
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,10 @@ describe('EscapeSequenceTransformer', function () {

describe('Variant #6: `stringArrayWrappersCount` option enabled', () => {
const stringArrayCallRegExp: RegExp = new RegExp(
'return e;' +
'};' +
'const f *= *b;' +
'const foo *= *f\\(\'\\\\x30\\\\x78\\\\x30\'\\);' +
'const bar *= *f\\(\'\\\\x30\\\\x78\\\\x31\'\\);' +
'const baz *= *f\\(\'\\\\x30\\\\x78\\\\x32\'\\);' +
'const f *= *b;.*' +
'const foo *= *f\\(\'\\\\x30\\\\x78\\\\x30\'\\);.*' +
'const bar *= *f\\(\'\\\\x30\\\\x78\\\\x31\'\\);.*' +
'const baz *= *f\\(\'\\\\x30\\\\x78\\\\x32\'\\);.*' +
'function test\\( *\\) *{' +
'const g *= *f;' +
'const c *= *g\\(\'\\\\x30\\\\x78\\\\x33\'\\);' +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('ForceTransformStringObfuscatingGuard', () => {
describe('check', () => {
describe('`forceTransformStrings` option is enabled', () => {
const obfuscatingGuardRegExp: RegExp = new RegExp(
'var foo *= *\'foo\';' +
'var foo *= *\'foo\';.*' +
'var bar *= *b\\(0x0\\);'
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ describe('IgnoredRequireImportObfuscatingGuard', () => {
describe('check', () => {
describe('`ignoreRequireImports` option is enabled', () => {
const obfuscatingGuardRegExp: RegExp = new RegExp(
'const foo *= *require\\(\'\\./foo\'\\); *' +
'import _0x(?:[a-f0-9]){4,6} from *\'\\./bar\'; *' +
'const foo *= *require\\(\'\\./foo\'\\);.*' +
'import _0x(?:[a-f0-9]){4,6} from *\'\\./bar\';.*' +
'const baz *= *_0x(?:[a-f0-9]){4,6}\\(0x0\\);'
);

Expand All @@ -38,8 +38,8 @@ describe('IgnoredRequireImportObfuscatingGuard', () => {

describe('`ignoreRequireImports` option is disabled', () => {
const obfuscatingGuardRegExp: RegExp = new RegExp(
'const foo *= *require\\(_0x(?:[a-f0-9]){4,6}\\(0x0\\)\\); *' +
'import _0x(?:[a-f0-9]){4,6} from *\'\\./bar\'; *' +
'const foo *= *require\\(_0x(?:[a-f0-9]){4,6}\\(0x0\\)\\);.*' +
'import _0x(?:[a-f0-9]){4,6} from *\'\\./bar\';.*' +
'const baz *= *_0x(?:[a-f0-9]){4,6}\\(0x1\\);'
);

Expand Down
Loading