Refactoring of custom nodes: split on custom nodes and custom code helpers

This commit is contained in:
sanex3339
2020-02-15 16:36:48 +03:00
parent dd7c1df9f9
commit 51523c0d5c
114 changed files with 1166 additions and 1105 deletions
@@ -0,0 +1,52 @@
import { assert } from 'chai';
import { NO_ADDITIONAL_NODES_PRESET } from '../../../../src/options/presets/NoCustomNodes';
import { readFileAsString } from '../../../helpers/readFileAsString';
import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade';
describe('StringArrayCallsWrapperCodeHelper', () => {
const regExp: RegExp = /_0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4,6} *- *0x0\;/;
describe('`stringArray` option is set', () => {
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
stringArray: true,
stringArrayThreshold: 1
}
).getObfuscatedCode();
});
it('should correctly append code helper into the obfuscated code', () => {
assert.match(obfuscatedCode, regExp);
});
});
describe('`stringArray` option isn\'t set', () => {
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
stringArray: false
}
).getObfuscatedCode();
});
it('shouldn\'t append code helper into the obfuscated code', () => {
assert.notMatch(obfuscatedCode, regExp);
});
});
});
@@ -0,0 +1,52 @@
import { assert } from 'chai';
import { NO_ADDITIONAL_NODES_PRESET } from '../../../../src/options/presets/NoCustomNodes';
import { readFileAsString } from '../../../helpers/readFileAsString';
import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade';
describe('StringArrayCodeHelper', () => {
const regExp: RegExp = /^var _0x([a-f0-9]){4} *= *\[/;
describe('`stringArray` option is set', () => {
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
stringArray: true,
stringArrayThreshold: 1
}
).getObfuscatedCode();
});
it('should correctly append code helper into the obfuscated code', () => {
assert.match(obfuscatedCode, regExp);
});
});
describe('`stringArray` option isn\'t set', () => {
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
stringArray: false
}
).getObfuscatedCode();
});
it('shouldn\'t append code helper into the obfuscated code', () => {
assert.notMatch(obfuscatedCode, regExp);
});
});
});
@@ -0,0 +1,55 @@
import { assert } from 'chai';
import { NO_ADDITIONAL_NODES_PRESET } from '../../../../src/options/presets/NoCustomNodes';
import { readFileAsString } from '../../../helpers/readFileAsString';
import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade';
describe('StringArrayRotateFunctionCodeHelper', () => {
const regExp: RegExp = /while *\(-- *_0x([a-f0-9]){4,6}\) *\{/;
describe('`stringArray` option is set', () => {
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
rotateStringArray: true,
stringArray: true,
stringArrayThreshold: 1
}
).getObfuscatedCode();
});
it('should correctly append code helper into the obfuscated code', () => {
assert.match(obfuscatedCode, regExp);
});
});
describe('`stringArray` option isn\'t set', () => {
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/simple-input.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
rotateStringArray: false,
stringArray: true,
stringArrayThreshold: 1
}
).getObfuscatedCode();
});
it('shouldn\'t append code helper into the obfuscated code', () => {
assert.notMatch(obfuscatedCode, regExp);
});
});
});
@@ -0,0 +1 @@
var test = 'test';
@@ -0,0 +1,202 @@
import 'reflect-metadata';
import format from 'string-template';
import { assert } from 'chai';
import { ServiceIdentifiers } from '../../../../../../src/container/ServiceIdentifiers';
import { ICryptUtils } from '../../../../../../src/interfaces/utils/ICryptUtils';
import { IInversifyContainerFacade } from '../../../../../../src/interfaces/container/IInversifyContainerFacade';
import { IObfuscatedCode } from '../../../../../../src/interfaces/source-code/IObfuscatedCode';
import { IRandomGenerator } from '../../../../../../src/interfaces/utils/IRandomGenerator';
import { AtobTemplate } from '../../../../../../src/custom-code-helpers/string-array/templates/string-array-calls-wrapper/AtobTemplate';
import { GlobalVariableTemplate1 } from '../../../../../../src/custom-code-helpers/common/templates/GlobalVariableTemplate1';
import { Rc4Template } from '../../../../../../src/custom-code-helpers/string-array/templates/string-array-calls-wrapper/Rc4Template';
import { StringArrayBase64DecodeTemplate } from '../../../../../../src/custom-code-helpers/string-array/templates/string-array-calls-wrapper/StringArrayBase64DecodeTemplate';
import { StringArrayCallsWrapperTemplate } from '../../../../../../src/custom-code-helpers/string-array/templates/string-array-calls-wrapper/StringArrayCallsWrapperTemplate';
import { StringArrayRC4DecodeTemplate } from '../../../../../../src/custom-code-helpers/string-array/templates/string-array-calls-wrapper/StringArrayRC4DecodeTemplate';
import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../../src/options/presets/NoCustomNodes';
import { InversifyContainerFacade } from '../../../../../../src/container/InversifyContainerFacade';
import { JavaScriptObfuscator } from '../../../../../../src/JavaScriptObfuscatorFacade';
import { readFileAsString } from '../../../../../helpers/readFileAsString';
describe('StringArrayCallsWrapperTemplate', () => {
const stringArrayName: string = 'stringArrayName';
const stringArrayCallsWrapperName: string = 'stringArrayCallsWrapperName';
let cryptUtils: ICryptUtils,
randomGenerator: IRandomGenerator;
before(() => {
const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
inversifyContainerFacade.load('', '', {});
cryptUtils = inversifyContainerFacade.get<ICryptUtils>(ServiceIdentifiers.ICryptUtils);
randomGenerator = inversifyContainerFacade.get<IRandomGenerator>(ServiceIdentifiers.IRandomGenerator);
});
describe('Variant #1: `base64` encoding', () => {
const index: string = '0x0';
const expectedDecodedValue: string = 'test1';
let decodedValue: string;
before(() => {
const atobPolyfill = format(AtobTemplate(), {
globalVariableTemplate: GlobalVariableTemplate1()
});
const atobDecodeTemplate: string = format(
StringArrayBase64DecodeTemplate(randomGenerator),
{
atobPolyfill,
selfDefendingCode: '',
stringArrayCallsWrapperName
}
);
const stringArrayCallsWrapperTemplate: string = format(StringArrayCallsWrapperTemplate(), {
decodeCodeHelperTemplate: atobDecodeTemplate,
stringArrayCallsWrapperName,
stringArrayName
});
decodedValue = Function(`
var ${stringArrayName} = ['${cryptUtils.btoa('test1')}'];
${stringArrayCallsWrapperTemplate}
return ${stringArrayCallsWrapperName}(${index});
`)();
});
it('should correctly return decoded value', () => {
assert.deepEqual(decodedValue, expectedDecodedValue);
});
});
describe('Variant #2: `rc4` encoding', () => {
const index: string = '0x0';
const key: string = 'key';
const expectedDecodedValue: string = 'test1';
let decodedValue: string;
before(() => {
const atobPolyfill = format(AtobTemplate(), {
globalVariableTemplate: GlobalVariableTemplate1()
});
const rc4decodeCodeHelperTemplate: string = format(
StringArrayRC4DecodeTemplate(randomGenerator),
{
atobPolyfill,
rc4Polyfill: Rc4Template(),
selfDefendingCode: '',
stringArrayCallsWrapperName
}
);
const stringArrayCallsWrapperTemplate: string = format(StringArrayCallsWrapperTemplate(), {
decodeCodeHelperTemplate: rc4decodeCodeHelperTemplate,
stringArrayCallsWrapperName,
stringArrayName
});
decodedValue = Function(`
var ${stringArrayName} = ['${cryptUtils.btoa(cryptUtils.rc4('test1', key))}'];
${stringArrayCallsWrapperTemplate}
return ${stringArrayCallsWrapperName}('${index}', '${key}');
`)();
});
it('should correctly return decoded value', () => {
assert.deepEqual(decodedValue, expectedDecodedValue);
});
});
describe('Prevailing kind of variables', () => {
describe('`var` kind', () => {
let obfuscatedCode: string,
stringArrayCallsWrapperRegExp: RegExp = /var (_0x(\w){4}) *= *function/;
beforeEach(() => {
const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-var.js');
const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
stringArray: true,
stringArrayThreshold: 1
}
);
obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
});
it('Should return correct kind of variables for string array calls wrapper', () => {
assert.match(obfuscatedCode, stringArrayCallsWrapperRegExp);
});
it('Should does not break on obfuscating', () => {
assert.doesNotThrow(() => obfuscatedCode);
});
});
describe('`const` kind', () => {
let obfuscatedCode: string,
stringArrayCallsWrapperRegExp: RegExp = /const (_0x(\w){4}) *= *function/;
beforeEach(() => {
const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
stringArray: true,
stringArrayThreshold: 1
}
);
obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
});
it('Should return correct kind of variables for string array calls wrapper', () => {
assert.match(obfuscatedCode, stringArrayCallsWrapperRegExp);
});
it('Should does not break on obfuscating', () => {
assert.doesNotThrow(() => obfuscatedCode);
});
});
describe('`let` kind', () => {
let obfuscatedCode: string,
stringArrayCallsWrapperRegExp: RegExp = /const (_0x(\w){4}) *= *function/;
beforeEach(() => {
const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
stringArray: true,
stringArrayThreshold: 1
}
);
obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
});
it('Should return correct kind of variables for string array calls wrapper', () => {
assert.match(obfuscatedCode, stringArrayCallsWrapperRegExp);
});
it('Should does not break on obfuscating', () => {
assert.doesNotThrow(() => obfuscatedCode);
});
});
});
});
@@ -0,0 +1,98 @@
import 'reflect-metadata';
import { assert } from 'chai';
import { IObfuscatedCode } from '../../../../../../src/interfaces/source-code/IObfuscatedCode';
import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../../src/options/presets/NoCustomNodes';
import { JavaScriptObfuscator } from '../../../../../../src/JavaScriptObfuscatorFacade';
import { readFileAsString } from '../../../../../helpers/readFileAsString';
describe('StringArrayRotateFunctionTemplate', () => {
describe('Prevailing kind of variables', () => {
describe('`var` kind', () => {
let obfuscatedCode: string,
stringArrayRotateFunctionRegExp: RegExp = /function\(_0x([a-f0-9]){4,6}, *_0x([a-f0-9]){4,6}\){var _0x([a-f0-9]){4,6} *= *function/;
beforeEach(() => {
const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-var.js');
const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
stringArray: true,
stringArrayThreshold: 1,
rotateStringArray: true
}
);
obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
});
it('Should return correct kind of variables for string array rotate function', () => {
assert.match(obfuscatedCode, stringArrayRotateFunctionRegExp);
});
it('Should does not break on obfuscating', () => {
assert.doesNotThrow(() => obfuscatedCode);
});
});
describe('`const` kind', () => {
let obfuscatedCode: string,
stringArrayRotateFunctionRegExp: RegExp = /function\(_0x([a-f0-9]){4,6}, *_0x([a-f0-9]){4,6}\){const _0x([a-f0-9]){4,6} *= *function/;
beforeEach(() => {
const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
stringArray: true,
stringArrayThreshold: 1,
rotateStringArray: true
}
);
obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
});
it('Should return correct kind of variables for string array rotate function', () => {
assert.match(obfuscatedCode, stringArrayRotateFunctionRegExp);
});
it('Should does not break on obfuscating', () => {
assert.doesNotThrow(() => obfuscatedCode);
});
});
describe('`let` kind', () => {
let obfuscatedCode: string,
stringArrayRotateFunctionRegExp: RegExp = /function\(_0x([a-f0-9]){4,6}, *_0x([a-f0-9]){4,6}\){const _0x([a-f0-9]){4,6} *= *function/;
beforeEach(() => {
const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
stringArray: true,
stringArrayThreshold: 1,
rotateStringArray: true
}
);
obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
});
it('Should return correct kind of variables for string array rotate function', () => {
assert.match(obfuscatedCode, stringArrayRotateFunctionRegExp);
});
it('Should does not break on obfuscating', () => {
assert.doesNotThrow(() => obfuscatedCode);
});
});
});
});
@@ -0,0 +1,95 @@
import 'reflect-metadata';
import { assert } from 'chai';
import { IObfuscatedCode } from '../../../../../../src/interfaces/source-code/IObfuscatedCode';
import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../../src/options/presets/NoCustomNodes';
import { JavaScriptObfuscator } from '../../../../../../src/JavaScriptObfuscatorFacade';
import { readFileAsString } from '../../../../../helpers/readFileAsString';
describe('StringArrayTemplate', () => {
describe('Prevailing kind of variables', () => {
describe('`var` kind', () => {
let obfuscatedCode: string,
stringArrayRegExp: RegExp = /var (_0x(\w){4}) *= *\['.*'];/;
beforeEach(() => {
const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-var.js');
const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
stringArray: true,
stringArrayThreshold: 1
}
);
obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
});
it('Should return correct kind of variables for string array', () => {
assert.match(obfuscatedCode, stringArrayRegExp);
});
it('Should does not break on obfuscating', () => {
assert.doesNotThrow(() => obfuscatedCode);
});
});
describe('`const` kind', () => {
let obfuscatedCode: string,
stringArrayRegExp: RegExp = /const (_0x(\w){4}) *= *\['.*'];/;
beforeEach(() => {
const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
stringArray: true,
stringArrayThreshold: 1
}
);
obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
});
it('Should return correct kind of variables for string array', () => {
assert.match(obfuscatedCode, stringArrayRegExp);
});
it('Should does not break on obfuscating', () => {
assert.doesNotThrow(() => obfuscatedCode);
});
});
describe('`let` kind', () => {
let obfuscatedCode: string,
stringArrayRegExp: RegExp = /const (_0x(\w){4}) *= *\['.*'];/;
beforeEach(() => {
const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
stringArray: true,
stringArrayThreshold: 1
}
);
obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
});
it('Should return correct kind of variables for string array', () => {
assert.match(obfuscatedCode, stringArrayRegExp);
});
it('Should does not break on obfuscating', () => {
assert.doesNotThrow(() => obfuscatedCode);
});
});
});
});