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.15.4
---
* Improved `stringArray` calls wrapper code

v2.15.3
---
* Slightly improved integration between `deadCodeInjection` and `stringArrayWrappersChainedCalls` options
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.15.3",
"version": "2.15.4",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export class StringArrayCallsWrapperBase64CodeHelper extends StringArrayCallsWra
atobFunctionName,
selfDefendingCode,
stringArrayName: this.stringArrayName,
stringArrayCallsWrapperName: this.stringArrayCallsWrapperName
stringArrayCallsWrapperName: this.stringArrayCallsWrapperName,
stringArrayCacheName: this.stringArrayCacheName
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export class StringArrayCallsWrapperCodeHelper extends AbstractCustomCodeHelper
@initializable()
protected stringArrayCallsWrapperName!: string;

/**
* @type {string}
*/
@initializable()
protected stringArrayCacheName!: string;

/**
* @type {IEscapeSequenceEncoder}
*/
Expand Down Expand Up @@ -84,6 +90,8 @@ export class StringArrayCallsWrapperCodeHelper extends AbstractCustomCodeHelper
this.stringArrayName = stringArrayName;
this.stringArrayCallsWrapperName = stringArrayCallsWrapperName;
this.indexShiftAmount = indexShiftAmount;

this.stringArrayCacheName = this.randomGenerator.getRandomString(6);
}

/**
Expand All @@ -107,6 +115,7 @@ export class StringArrayCallsWrapperCodeHelper extends AbstractCustomCodeHelper
decodeCodeHelperTemplate,
stringArrayCallsWrapperName: this.stringArrayCallsWrapperName,
stringArrayName: this.stringArrayName,
stringArrayCacheName: this.stringArrayCacheName,
indexShiftAmount: this.indexShiftAmount
}),
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ export class StringArrayCallsWrapperRc4CodeHelper extends StringArrayCallsWrappe
*/
protected override getDecodeStringArrayTemplate (): string {
const atobFunctionName: string = this.randomGenerator.getRandomString(6);
const rc4FunctionName: string = this.randomGenerator.getRandomString(6);

const atobPolyfill: string = this.customCodeHelperFormatter.formatTemplate(AtobTemplate(), {
atobFunctionName
});
const rc4Polyfill: string = this.customCodeHelperFormatter.formatTemplate(Rc4Template(), {
atobFunctionName
atobFunctionName,
rc4FunctionName
});

const selfDefendingCode: string = this.getSelfDefendingTemplate();
Expand All @@ -27,10 +29,12 @@ export class StringArrayCallsWrapperRc4CodeHelper extends StringArrayCallsWrappe
StringArrayRC4DecodeTemplate(this.randomGenerator),
{
atobPolyfill,
rc4FunctionName,
rc4Polyfill,
selfDefendingCode,
stringArrayName: this.stringArrayName,
stringArrayCallsWrapperName: this.stringArrayCallsWrapperName
stringArrayCallsWrapperName: this.stringArrayCallsWrapperName,
stringArrayCacheName: this.stringArrayCacheName
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
export function Rc4Template (): string {
return `
const rc4 = function (str, key) {
const {rc4FunctionName} = function (str, key) {
let s = [], j = 0, x, res = '', newStr = '';

str = {atobFunctionName}(str);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export function StringArrayBase64DecodeTemplate (
const identifierLength: number = 6;
const initializedIdentifier: string = randomGenerator.getRandomString(identifierLength);
const base64DecodeFunctionIdentifier: string = randomGenerator.getRandomString(identifierLength);
const dataIdentifier: string = randomGenerator.getRandomString(identifierLength);

return `
if ({stringArrayCallsWrapperName}.${initializedIdentifier} === undefined) {
Expand All @@ -28,20 +27,20 @@ export function StringArrayBase64DecodeTemplate (
return decodeURIComponent(newStringChars);
};

{stringArrayCallsWrapperName}.${dataIdentifier} = {};
{stringArrayCacheName} = arguments;

{stringArrayCallsWrapperName}.${initializedIdentifier} = true;
}

const firstValue = {stringArrayName}[0];
const cacheKey = index + firstValue;
const cachedValue = {stringArrayCallsWrapperName}.${dataIdentifier}[cacheKey];
if (cachedValue === undefined) {
const cachedValue = {stringArrayCacheName}[cacheKey];

if (!cachedValue) {
{selfDefendingCode}

value = {stringArrayCallsWrapperName}.${base64DecodeFunctionIdentifier}(value);
{stringArrayCallsWrapperName}.${dataIdentifier}[cacheKey] = value;
{stringArrayCacheName}[cacheKey] = value;
} else {
value = cachedValue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
/**
* The first parameter of the outer stringArrayCallsWrapperName function will be used as an initial index
* and later as a cache variable that will be captured by the inner function
*
* @returns {string}
*/
export function StringArrayCallsWrapperTemplate (): string {
return `
function {stringArrayCallsWrapperName} (index, key) {
index = index - {indexShiftAmount};
function {stringArrayCallsWrapperName} ({stringArrayCacheName}, key) {
{stringArrayCallsWrapperName} = function (index, key) {
index = index - {indexShiftAmount};

let value = {stringArrayName}[index];

{decodeCodeHelperTemplate}

let value = {stringArrayName}[index];

{decodeCodeHelperTemplate}

return value;
return value;
};

return {stringArrayCallsWrapperName}({stringArrayCacheName}, key);
}
`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,33 @@ export function StringArrayRC4DecodeTemplate (
const identifierLength: number = 6;
const initializedIdentifier: string = randomGenerator.getRandomString(identifierLength);
const rc4Identifier: string = randomGenerator.getRandomString(identifierLength);
const dataIdentifier: string = randomGenerator.getRandomString(identifierLength);
const onceIdentifier: string = randomGenerator.getRandomString(identifierLength);

return `
if ({stringArrayCallsWrapperName}.${initializedIdentifier} === undefined) {
{atobPolyfill}

{rc4Polyfill}
{stringArrayCallsWrapperName}.${rc4Identifier} = rc4;
{stringArrayCallsWrapperName}.${rc4Identifier} = {rc4FunctionName};

{stringArrayCallsWrapperName}.${dataIdentifier} = {};
{stringArrayCacheName} = arguments;

{stringArrayCallsWrapperName}.${initializedIdentifier} = true;
}

const firstValue = {stringArrayName}[0];
const cacheKey = index + firstValue;
const cachedValue = {stringArrayCallsWrapperName}.${dataIdentifier}[cacheKey];
const cachedValue = {stringArrayCacheName}[cacheKey];

if (cachedValue === undefined) {
if (!cachedValue) {
if ({stringArrayCallsWrapperName}.${onceIdentifier} === undefined) {
{selfDefendingCode}

{stringArrayCallsWrapperName}.${onceIdentifier} = true;
}

value = {stringArrayCallsWrapperName}.${rc4Identifier}(value, key);
{stringArrayCallsWrapperName}.${dataIdentifier}[cacheKey] = value;
{stringArrayCacheName}[cacheKey] = value;
} else {
value = cachedValue;
}
Expand Down
38 changes: 8 additions & 30 deletions test/dev/dev.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,24 @@
'use strict';

import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNodes';

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

let obfuscationResult = JavaScriptObfuscator.obfuscate(
`
(function(){
if (true) {
var foo = function () {
console.log('abc');
};
var bar = function () {
console.log('def');
};
var baz = function () {
console.log('ghi');
};
var bark = function () {
console.log('jkl');
};
var hawk = function () {
console.log('mno');
};

foo();
bar();
baz();
bark();
hawk();
}
})();
console.log('foo');
console.log('bar');
console.log('bar');
`,
{
...NO_ADDITIONAL_NODES_PRESET,
compact: false,
simplify: false,
stringArray: true,
stringArrayThreshold: 1,
stringArrayWrappersChainedCalls: true,
deadCodeInjection: true,
deadCodeInjectionThreshold: 1,
identifierNamesGenerator: 'mangled',
seed: 1
stringArrayEncoding: ['base64'],
identifierNamesGenerator: 'mangled'
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ describe('StringArrayCallsWrapperCodeHelper', () => {
describe('Preserve string array name', () => {
const callsWrapperRegExp: RegExp = new RegExp(`` +
`function *b *\\(c, *d\\) *{ *` +
`c *= *c *- *0x0; *` +
`var e *= *a\\[c]; *` +
`b *= *function *\\(e, *f\\) *{` +
`e *= *e *- *0x0; *` +
`var g *= *a\\[e]; *` +
``);

let obfuscatedCode: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import { swapLettersCase } from '../../../../../helpers/swapLettersCase';
describe('StringArrayCallsWrapperTemplate', () => {
const stringArrayName: string = 'stringArrayName';
const stringArrayCallsWrapperName: string = 'stringArrayCallsWrapperName';
const stringArrayCacheName: string = 'stringArrayCache';
const atobFunctionName: string = 'atob';
const rc4FunctionName: string = 'rc4';

let cryptUtilsSwappedAlphabet: ICryptUtilsStringArray,
randomGenerator: IRandomGenerator;
Expand Down Expand Up @@ -62,12 +64,14 @@ describe('StringArrayCallsWrapperTemplate', () => {
atobPolyfill,
atobFunctionName,
selfDefendingCode: '',
stringArrayCacheName,
stringArrayCallsWrapperName
}
);
const stringArrayCallsWrapperTemplate: string = format(StringArrayCallsWrapperTemplate(), {
decodeCodeHelperTemplate: atobDecodeTemplate,
indexShiftAmount,
stringArrayCacheName,
stringArrayCallsWrapperName,
stringArrayName
});
Expand Down Expand Up @@ -105,12 +109,14 @@ describe('StringArrayCallsWrapperTemplate', () => {
atobPolyfill,
atobFunctionName,
selfDefendingCode: '',
stringArrayCacheName,
stringArrayCallsWrapperName
}
);
const stringArrayCallsWrapperTemplate: string = format(StringArrayCallsWrapperTemplate(), {
decodeCodeHelperTemplate: atobDecodeTemplate,
indexShiftAmount,
stringArrayCacheName,
stringArrayCallsWrapperName,
stringArrayName
});
Expand Down Expand Up @@ -146,12 +152,14 @@ describe('StringArrayCallsWrapperTemplate', () => {
atobPolyfill,
atobFunctionName,
selfDefendingCode: '',
stringArrayCacheName,
stringArrayCallsWrapperName
}
);
const stringArrayCallsWrapperTemplate: string = format(StringArrayCallsWrapperTemplate(), {
decodeCodeHelperTemplate: atobDecodeTemplate,
indexShiftAmount,
stringArrayCacheName,
stringArrayCallsWrapperName,
stringArrayName
});
Expand Down Expand Up @@ -190,20 +198,24 @@ describe('StringArrayCallsWrapperTemplate', () => {
atobFunctionName
});
const rc4Polyfill = format(Rc4Template(), {
atobFunctionName
atobFunctionName,
rc4FunctionName
});
const rc4decodeCodeHelperTemplate: string = format(
StringArrayRC4DecodeTemplate(randomGenerator),
{
atobPolyfill,
rc4Polyfill,
rc4FunctionName,
selfDefendingCode: '',
stringArrayCacheName,
stringArrayCallsWrapperName
}
);
const stringArrayCallsWrapperTemplate: string = format(StringArrayCallsWrapperTemplate(), {
decodeCodeHelperTemplate: rc4decodeCodeHelperTemplate,
indexShiftAmount,
stringArrayCacheName,
stringArrayCallsWrapperName,
stringArrayName
});
Expand Down Expand Up @@ -237,20 +249,24 @@ describe('StringArrayCallsWrapperTemplate', () => {
atobFunctionName
});
const rc4Polyfill = format(Rc4Template(), {
atobFunctionName
atobFunctionName,
rc4FunctionName
});
const rc4decodeCodeHelperTemplate: string = format(
StringArrayRC4DecodeTemplate(randomGenerator),
{
atobPolyfill,
rc4Polyfill,
rc4FunctionName,
selfDefendingCode: '',
stringArrayCacheName,
stringArrayCallsWrapperName
}
);
const stringArrayCallsWrapperTemplate: string = format(StringArrayCallsWrapperTemplate(), {
decodeCodeHelperTemplate: rc4decodeCodeHelperTemplate,
indexShiftAmount,
stringArrayCacheName,
stringArrayCallsWrapperName,
stringArrayName
});
Expand Down
Loading