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
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,10 @@ export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGene
private static generateNewMangledName (previousMangledName: string): string {
const generateNewMangledName: (name: string) => string = (name: string): string => {
const nameSequence: string[] = MangledIdentifierNamesGenerator.nameSequence;
const zeroSequenceCache: string[] = [];
const nameLength: number = name.length;

const zeroSequence: (num: number) => string = (num: number): string => {
let result: string = zeroSequenceCache[num];

if (result !== undefined) {
return result;
}

result = '0'.repeat(num);
zeroSequenceCache[num] = result;

return result;
return '0'.repeat(num);
};

let index: number = nameLength - 1;
Expand All @@ -78,7 +68,8 @@ export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGene
if (indexInSequence !== lastNameSequenceIndex) {
const previousNamePart: string = name.substring(0, index);
const nextCharacter: string = nameSequence[indexInSequence + 1];
const zeroSequenceCharacters: string = zeroSequence(nameLength - (index + 1));
const zeroSequenceLength: number = nameLength - (index + 1);
const zeroSequenceCharacters: string = zeroSequence(zeroSequenceLength);

return previousNamePart + nextCharacter + zeroSequenceCharacters;
}
Expand Down
22 changes: 0 additions & 22 deletions src/node/NodeAppender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ export class NodeAppender {
* @param {TStatement[]} scopeStatements
*/
public static appendNode (scopeNode: TNodeWithScope, scopeStatements: TStatement[]): void {
if (!NodeAppender.validateScopeStatements(scopeStatements)) {
scopeStatements = [];
}

scopeStatements = NodeAppender.parentizeScopeStatementsBeforeAppend(scopeNode, scopeStatements);

NodeAppender.setScopeNodeStatements(scopeNode, [
Expand Down Expand Up @@ -112,10 +108,6 @@ export class NodeAppender {
* @param {number} index
*/
public static insertNodeAtIndex (scopeNode: TNodeWithScope, scopeStatements: TStatement[], index: number): void {
if (!NodeAppender.validateScopeStatements(scopeStatements)) {
scopeStatements = [];
}

scopeStatements = NodeAppender.parentizeScopeStatementsBeforeAppend(scopeNode, scopeStatements);

NodeAppender.setScopeNodeStatements(scopeNode, [
Expand All @@ -130,10 +122,6 @@ export class NodeAppender {
* @param {TStatement[]} scopeStatements
*/
public static prependNode (scopeNode: TNodeWithScope, scopeStatements: TStatement[]): void {
if (!NodeAppender.validateScopeStatements(scopeStatements)) {
scopeStatements = [];
}

scopeStatements = NodeAppender.parentizeScopeStatementsBeforeAppend(scopeNode, scopeStatements);

NodeAppender.setScopeNodeStatements(scopeNode, [
Expand Down Expand Up @@ -180,14 +168,4 @@ export class NodeAppender {

scopeNode.body = statements;
}

/**
* @param {TStatement[]} scopeStatement
* @returns {boolean}
*/
private static validateScopeStatements (scopeStatement: TStatement[]): boolean {
return scopeStatement.every((statementNode: TStatement) => {
return !!statementNode && statementNode.hasOwnProperty('type');
});
}
}
27 changes: 0 additions & 27 deletions src/node/Nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,6 @@ export class Nodes {
return breakStatementNode;
}

/**
* @param {Statement[]} body
* @returns {CatchClause}
*/
public static getCatchClauseNode (body: ESTree.Statement[] = []): ESTree.CatchClause {
return {
type: NodeType.CatchClause,
param: Nodes.getIdentifierNode('err'),
body: Nodes.getBlockStatementNode(body),
obfuscatedNode: false
};
}

/**
* @param {Expression} callee
* @param {(Expression | SpreadElement)[]} args
Expand Down Expand Up @@ -231,20 +218,6 @@ export class Nodes {
};
}

/**
* @param {Identifier} label
* @param {Statement} body
* @returns {LabeledStatement}
*/
public static getLabeledStatement (label: ESTree.Identifier, body: ESTree.Statement): ESTree.LabeledStatement {
return {
type: NodeType.LabeledStatement,
label,
body,
obfuscatedNode: false
};
}

/**
* @param {boolean | number | string} value
* @param {string} raw
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import 'reflect-metadata';

import * as format from 'string-template';

import { assert } from 'chai';

import { GlobalVariableNoEvalTemplate } from '../../../src/templates/GlobalVariableNoEvalTemplate';

describe('GlobalVariableNoEvalTemplate (): string', () => {
describe('variant #1: simple', () => {
const expectedGlobalObject: NodeJS.Global = global;

let globalObject: NodeJS.Global;

before(() => {
const globalVariableNoEvalTemplate: string = format(GlobalVariableNoEvalTemplate());

globalObject = Function(`
${globalVariableNoEvalTemplate}

return that;
`)();
});

it('should correctly return global object', () => {
assert.deepEqual(globalObject, expectedGlobalObject);
});
});

describe('variant #2: call inside function', () => {
const expectedGlobalObject: NodeJS.Global = global;

let globalObject: NodeJS.Global;

before(() => {
const globalVariableNoEvalTemplate: string = format(GlobalVariableNoEvalTemplate());

globalObject = Function(`
return (function () {
${globalVariableNoEvalTemplate}

return that;
})();
`)();
});

it('should correctly return global object', () => {
assert.deepEqual(globalObject, expectedGlobalObject);
});
});

describe('variant #3: return `window`', () => {
const expectedGlobalObject: {} = {
document: {}
};

let globalObject: NodeJS.Global;

before(() => {
const globalVariableNoEvalTemplate: string = format(GlobalVariableNoEvalTemplate());

globalObject = Function(`
this.window = {
document: {}
};

${globalVariableNoEvalTemplate}

return that;
`)();
});

it('should correctly return `window` object', () => {
assert.deepEqual(globalObject, expectedGlobalObject);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { assert } from 'chai';
import { spawn } from 'threads';

import { IObfuscationResult } from '../../../../../src/interfaces/IObfuscationResult';
import { IObfuscationResult } from '../../../../src/interfaces/IObfuscationResult';

import { readFileAsString } from '../../../../helpers/readFileAsString';
import { readFileAsString } from '../../../helpers/readFileAsString';

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

import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFacade';
import { ObfuscationTarget } from '../../../../src/enums/ObfuscationTarget';

import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade';

function spawnThread(inputCallback: Function, threadCallback: Function, timeoutCallback: Function): void {
const thread = spawn<string, number>((input: string, postMessage: Function) => {
Expand Down Expand Up @@ -100,7 +102,43 @@ describe('DebugProtectionFunctionCallTemplate (): string', () => {
});
});

describe('variant #3: obfuscated code with removed debug protection code', () => {
describe('variant #3: correctly obfuscated code with target `extension`', () => {
const expectedEvaluationResult: number = 1;

let obfuscatedCode: string,
evaluationResult: number = 0;

beforeEach((done) => {
const code: string = readFileAsString(__dirname + '/fixtures/input.js');
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
debugProtection: true,
target: ObfuscationTarget.Extension
}
);

obfuscatedCode = obfuscationResult.getObfuscatedCode();

spawnThread(
() => obfuscatedCode,
(response: number) => {
evaluationResult = response;
done();
},
() => {
done();
}
);
});

it('should correctly evaluate code with enabled debug protection', () => {
assert.equal(evaluationResult, expectedEvaluationResult);
});
});

describe('variant #4: obfuscated code with removed debug protection code', () => {
const expectedEvaluationResult: number = 0;

let obfuscatedCode: string,
Expand Down Expand Up @@ -136,7 +174,7 @@ describe('DebugProtectionFunctionCallTemplate (): string', () => {
});
});

describe('variant #4: single call of debug protection code', () => {
describe('variant #5: single call of debug protection code', () => {
const expectedEvaluationResult: number = 1;

let obfuscatedCode: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import * as format from 'string-template';

import { assert } from 'chai';

import { ServiceIdentifiers } from '../../../../../src/container/ServiceIdentifiers';
import { ServiceIdentifiers } from '../../../../src/container/ServiceIdentifiers';

import { ICryptUtils } from '../../../../../src/interfaces/utils/ICryptUtils';
import { IInversifyContainerFacade } from '../../../../../src/interfaces/container/IInversifyContainerFacade';
import { ICryptUtils } from '../../../../src/interfaces/utils/ICryptUtils';
import { IInversifyContainerFacade } from '../../../../src/interfaces/container/IInversifyContainerFacade';

import { DomainLockNodeTemplate } from '../../../../../src/templates/domain-lock-nodes/domain-lock-node/DomainLockNodeTemplate';
import { GlobalVariableTemplate1 } from '../../../../../src/templates/GlobalVariableTemplate1';
import { DomainLockNodeTemplate } from '../../../../src/templates/domain-lock-nodes/domain-lock-node/DomainLockNodeTemplate';
import { GlobalVariableTemplate1 } from '../../../../src/templates/GlobalVariableTemplate1';

import { InversifyContainerFacade } from '../../../../../src/container/InversifyContainerFacade';
import { InversifyContainerFacade } from '../../../../src/container/InversifyContainerFacade';

/**
* @param templateData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import * as format from 'string-template';

import { assert } from 'chai';

import { ServiceIdentifiers } from '../../../../../src/container/ServiceIdentifiers';
import { ServiceIdentifiers } from '../../../../src/container/ServiceIdentifiers';

import { ICryptUtils } from '../../../../../src/interfaces/utils/ICryptUtils';
import { IInversifyContainerFacade } from '../../../../../src/interfaces/container/IInversifyContainerFacade';
import { IRandomGenerator } from '../../../../../src/interfaces/utils/IRandomGenerator';
import { ICryptUtils } from '../../../../src/interfaces/utils/ICryptUtils';
import { IInversifyContainerFacade } from '../../../../src/interfaces/container/IInversifyContainerFacade';
import { IRandomGenerator } from '../../../../src/interfaces/utils/IRandomGenerator';

import { AtobTemplate } from '../../../../../src/templates/AtobTemplate';
import { GlobalVariableTemplate1 } from '../../../../../src/templates/GlobalVariableTemplate1';
import { Rc4Template } from '../../../../../src/templates/Rc4Template';
import { StringArrayBase64DecodeNodeTemplate } from '../../../../../src/templates/string-array-nodes/string-array-calls-wrapper/StringArrayBase64DecodeNodeTemplate';
import { StringArrayCallsWrapperTemplate } from '../../../../../src/templates/string-array-nodes/string-array-calls-wrapper/StringArrayCallsWrapperTemplate';
import { StringArrayRc4DecodeNodeTemplate } from '../../../../../src/templates/string-array-nodes/string-array-calls-wrapper/StringArrayRC4DecodeNodeTemplate';
import { AtobTemplate } from '../../../../src/templates/AtobTemplate';
import { GlobalVariableTemplate1 } from '../../../../src/templates/GlobalVariableTemplate1';
import { Rc4Template } from '../../../../src/templates/Rc4Template';
import { StringArrayBase64DecodeNodeTemplate } from '../../../../src/templates/string-array-nodes/string-array-calls-wrapper/StringArrayBase64DecodeNodeTemplate';
import { StringArrayCallsWrapperTemplate } from '../../../../src/templates/string-array-nodes/string-array-calls-wrapper/StringArrayCallsWrapperTemplate';
import { StringArrayRc4DecodeNodeTemplate } from '../../../../src/templates/string-array-nodes/string-array-calls-wrapper/StringArrayRC4DecodeNodeTemplate';

import { InversifyContainerFacade } from '../../../../../src/container/InversifyContainerFacade';
import { InversifyContainerFacade } from '../../../../src/container/InversifyContainerFacade';

describe('StringArrayCallsWrapperNodeTemplate (): string', () => {
const stringArrayName: string = 'stringArrayName';
Expand Down
7 changes: 4 additions & 3 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ import './functional-tests/node-transformers/preparing-transformers/eval-call-ex
import './functional-tests/node-transformers/preparing-transformers/comments-transformer/CommentsTransformer.spec';
import './functional-tests/node-transformers/preparing-transformers/obfuscating-guards/black-list-obfuscating-guard/BlackListObfuscatingGuard.spec';
import './functional-tests/node-transformers/preparing-transformers/obfuscating-guards/conditional-comment-obfuscating-guard/ConditionalCommentObfuscatingGuard.spec';
import './functional-tests/templates/custom-nodes/debug-protection-nodes/DebufProtectionFunctionCallTemplate.spec';
import './functional-tests/templates/custom-nodes/domain-lock-nodes/DomainLockNodeTemplate.spec';
import './functional-tests/templates/custom-nodes/string-array-nodes/StringArrayCallsWrapperNodeTemplate.spec';
import './functional-tests/templates/debug-protection-nodes/DebugProtectionFunctionCallTemplate.spec';
import './functional-tests/templates/domain-lock-nodes/DomainLockNodeTemplate.spec';
import './functional-tests/templates/GlobalVariableNoEvalTemplate.spec';
import './functional-tests/templates/string-array-nodes/StringArrayCallsWrapperNodeTemplate.spec';

/**
* Performance tests
Expand Down
Loading