Fixed errors related to identifiers. Changed self defending helper

This commit is contained in:
sanex3339
2020-02-16 14:41:32 +03:00
parent 51523c0d5c
commit a2e9c05ce8
46 changed files with 843 additions and 306 deletions
+21 -9
View File
@@ -7,20 +7,32 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo
let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
`
const a = '123';
const b = '234';
const c = '345';
console.log(a, b, c);
(function () {
function foo () {
const f = {f: 1};
const g = {g: 2};
const h = {h: 3};
const i = {i: 4};
const j = {j: 5};
}
function bar () {
const a = 11;
const b = 12;
const c = 13;
const d = 14;
const e = 15;
}
})();
`,
{
...NO_ADDITIONAL_NODES_PRESET,
compact: false,
deadCodeInjection: true,
deadCodeInjectionThreshold: 1,
identifierNamesGenerator: 'mangled',
selfDefending: true,
stringArray: true,
stringArrayThreshold: 1,
log: true
log: true,
transformObjectKeys: true
}
).getObfuscatedCode();
@@ -1,5 +1,4 @@
import { assert } from 'chai';
import { spawn, Thread, Worker } from 'threads/dist';
import { readFileAsString } from '../../../../helpers/readFileAsString';
@@ -8,24 +7,13 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../src/options/presets/N
import { IdentifierNamesGenerator } from '../../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
import { ObfuscationTarget } from '../../../../../src/enums/ObfuscationTarget';
import { evaluateInWorker } from '../../../../helpers/evaluateInWorker';
import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFacade';
async function spawnThread(obfuscatedCode: string, threadCallback: Function, timeoutCallback: Function): Promise<void> {
const evaluationWorker = await spawn(new Worker('./workers/evaluation-worker'));
const timeout = setTimeout(() => {
Thread.terminate(evaluationWorker);
timeoutCallback();
}, 1500);
const result: string = await evaluationWorker.evaluate(obfuscatedCode);
clearTimeout(timeout);
Thread.terminate(evaluationWorker);
threadCallback(result);
}
describe('DebugProtectionFunctionCallTemplate', function () {
const evaluationTimeout: number = 1500;
this.timeout(10000);
describe('Variant #1: correctly obfuscate code with `HexadecimalIdentifierNamesGenerator``', () => {
@@ -46,7 +34,7 @@ describe('DebugProtectionFunctionCallTemplate', function () {
}
).getObfuscatedCode();
spawnThread(
evaluateInWorker(
obfuscatedCode,
(response: number) => {
evaluationResult = response;
@@ -54,7 +42,11 @@ describe('DebugProtectionFunctionCallTemplate', function () {
},
() => {
done();
}
},
() => {
done();
},
evaluationTimeout
);
});
@@ -81,7 +73,7 @@ describe('DebugProtectionFunctionCallTemplate', function () {
}
).getObfuscatedCode();
spawnThread(
evaluateInWorker(
obfuscatedCode,
(response: number) => {
evaluationResult = response;
@@ -89,7 +81,11 @@ describe('DebugProtectionFunctionCallTemplate', function () {
},
() => {
done();
}
},
() => {
done();
},
evaluationTimeout
);
});
@@ -117,7 +113,7 @@ describe('DebugProtectionFunctionCallTemplate', function () {
}
).getObfuscatedCode();
spawnThread(
evaluateInWorker(
obfuscatedCode,
(response: number) => {
evaluationResult = response;
@@ -125,7 +121,11 @@ describe('DebugProtectionFunctionCallTemplate', function () {
},
() => {
done();
}
},
() => {
done();
},
evaluationTimeout
);
});
@@ -152,7 +152,7 @@ describe('DebugProtectionFunctionCallTemplate', function () {
}
).getObfuscatedCode();
spawnThread(
evaluateInWorker(
obfuscatedCode,
(response: number) => {
evaluationResult = response;
@@ -160,7 +160,11 @@ describe('DebugProtectionFunctionCallTemplate', function () {
},
() => {
done();
}
},
() => {
done();
},
evaluationTimeout
);
});
@@ -187,7 +191,7 @@ describe('DebugProtectionFunctionCallTemplate', function () {
).getObfuscatedCode();
obfuscatedCode = obfuscatedCode.replace(/\+\+ *_0x([a-f0-9]){4,6}/, '');
spawnThread(
evaluateInWorker(
obfuscatedCode,
(response: number) => {
evaluationResult = response;
@@ -195,7 +199,11 @@ describe('DebugProtectionFunctionCallTemplate', function () {
},
() => {
done();
}
},
() => {
done();
},
evaluationTimeout
);
});
@@ -221,7 +229,7 @@ describe('DebugProtectionFunctionCallTemplate', function () {
}
).getObfuscatedCode();
spawnThread(
evaluateInWorker(
obfuscatedCode,
(response: number) => {
evaluationResult = response;
@@ -229,7 +237,11 @@ describe('DebugProtectionFunctionCallTemplate', function () {
},
() => {
done();
}
},
() => {
done();
},
evaluationTimeout
);
});
@@ -1,7 +0,0 @@
const expose = require('threads/worker').expose;
expose({
evaluate: (obfuscatedCode) => {
return eval(obfuscatedCode);
}
});
@@ -0,0 +1,180 @@
import { assert } from 'chai';
import { readFileAsString } from '../../../../helpers/readFileAsString';
import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
import { IdentifierNamesGenerator } from '../../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
import { evaluateInWorker } from '../../../../helpers/evaluateInWorker';
import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFacade';
describe('SelfDefendingTemplate', function () {
const evaluationTimeout: number = 3500;
this.timeout(10000);
describe('Variant #1: correctly obfuscate code with `HexadecimalIdentifierNamesGenerator``', () => {
const expectedEvaluationResult: number = 1;
let obfuscatedCode: string,
evaluationResult: number = 0;
beforeEach((done) => {
const code: string = readFileAsString(__dirname + '/fixtures/input.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
selfDefending: true,
identifierNamesGenerator: IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator
}
).getObfuscatedCode();
evaluateInWorker(
obfuscatedCode,
(response: number) => {
evaluationResult = response;
done();
},
() => {
done();
},
() => {
done();
},
evaluationTimeout
);
});
it('should correctly evaluate code with enabled self defending', () => {
assert.equal(evaluationResult, expectedEvaluationResult);
});
});
describe('Variant #2: correctly obfuscate code with `MangledIdentifierNamesGenerator` option', () => {
const expectedEvaluationResult: number = 1;
let obfuscatedCode: string,
evaluationResult: number = 0;
beforeEach((done) => {
const code: string = readFileAsString(__dirname + '/fixtures/input.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
selfDefending: true,
identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
}
).getObfuscatedCode();
evaluateInWorker(
obfuscatedCode,
(response: number) => {
evaluationResult = response;
done();
},
() => {
done();
},
() => {
done();
},
evaluationTimeout
);
});
it('should correctly evaluate code with enabled self defending', () => {
assert.equal(evaluationResult, expectedEvaluationResult);
});
});
describe('Variant #3: correctly obfuscate code with `DictionaryIdentifierNamesGenerator` option', () => {
const expectedEvaluationResult: number = 1;
let obfuscatedCode: string,
evaluationResult: number = 0;
beforeEach((done) => {
const code: string = readFileAsString(__dirname + '/fixtures/input.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
selfDefending: true,
identifierNamesGenerator: IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
identifiersDictionary: ['foo', 'bar', 'baz', 'bark', 'hawk', 'eagle']
}
).getObfuscatedCode();
evaluateInWorker(
obfuscatedCode,
(response: number) => {
evaluationResult = response;
done();
},
() => {
done();
},
() => {
done();
},
evaluationTimeout
);
});
it('should correctly evaluate code with enabled self defending', () => {
assert.equal(evaluationResult, expectedEvaluationResult);
});
});
describe('Variant #4: obfuscated code with beautified self defending code', () => {
const expectedEvaluationResult: number = 0;
let obfuscatedCode: string,
evaluationResult: number = 0;
beforeEach((done) => {
const code: string = readFileAsString(__dirname + '/fixtures/input.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
selfDefending: true
}
).getObfuscatedCode();
obfuscatedCode = obfuscatedCode
.replace(/function\(\){/g, 'function () {')
.replace(/=/g, ' = ')
.replace(/,/g, ', ')
.replace(/;/g, '; ')
.replace(/{/g, '{\n')
.replace(/}/g, '}\n');
evaluateInWorker(
obfuscatedCode,
(response: number) => {
evaluationResult = response;
done();
},
() => {
done();
},
() => {
done();
},
evaluationTimeout
);
});
it('should enter code in infinity loop', () => {
assert.equal(evaluationResult, expectedEvaluationResult);
});
});
});
@@ -0,0 +1,3 @@
(function () {
return 1;
})();
@@ -844,7 +844,6 @@ describe('JavaScriptObfuscator', () => {
stringArrayEncoding: StringArrayEncoding.Rc4
}
).getObfuscatedCode();
});
it('does not break on run', () => {
+35
View File
@@ -0,0 +1,35 @@
import { spawn, Thread, Worker } from 'threads/dist';
/**
* @param {string} code
* @param {(result: any, code: string) => void} resultCallback
* @param {(error: Error, code: string) => void} errorCallback
* @param {() => void} timeoutCallback
* @param {number} waitTimeout
* @returns {Promise<void>}
*/
export async function evaluateInWorker(
code: string,
resultCallback: (result: any, code: string) => void,
errorCallback: (error: Error, code: string) => void,
timeoutCallback: () => void,
waitTimeout: number
): Promise<void> {
const evaluationWorker = await spawn(new Worker('./workers/evaluation-worker'));
const timeout = setTimeout(() => {
Thread.terminate(evaluationWorker);
timeoutCallback();
}, waitTimeout);
try {
const result: string = await evaluationWorker.evaluate(code);
resultCallback(result, code);
} catch (error) {
errorCallback(error, code);
} finally {
clearTimeout(timeout);
Thread.terminate(evaluationWorker);
}
}
@@ -0,0 +1,7 @@
const expose = require('threads/worker').expose;
expose({
evaluate: (code) => {
return eval(code);
}
});
+1
View File
@@ -55,6 +55,7 @@ import './functional-tests/analyzers/calls-graph-analyzer/CallsGraphAnalyzer.spe
import './functional-tests/cli/JavaScriptObfuscatorCLI.spec';
import './functional-tests/custom-code-helpers/console-output/ConsoleOutputDisableExpressionCodeHelper.spec';
import './functional-tests/custom-code-helpers/domain-lock/DomainLockCodeHelper.spec';
import './functional-tests/custom-code-helpers/self-defending/templates/SelfDefendingTemplate.spec';
import './functional-tests/custom-code-helpers/string-array/StringArrayCallsWrapperCodeHelper.spec';
import './functional-tests/custom-code-helpers/string-array/StringArrayRotateFunctionCodeHelper.spec';
import './functional-tests/custom-code-helpers/string-array/StringArrayCodeHelper.spec';
@@ -5,6 +5,7 @@ import { TInputOptions } from '../../src/types/options/TInputOptions';
import { StringArrayEncoding } from '../../src/enums/StringArrayEncoding';
import { IdentifierNamesGenerator } from '../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
import { evaluateInWorker } from '../helpers/evaluateInWorker';
import { readFileAsString } from '../helpers/readFileAsString';
import { JavaScriptObfuscator } from '../../src/JavaScriptObfuscatorFacade';
@@ -36,7 +37,7 @@ describe('JavaScriptObfuscator runtime eval', function () {
unicodeEscapeSequence: true
};
this.timeout(100000);
this.timeout(200000);
[
{
@@ -164,5 +165,123 @@ describe('JavaScriptObfuscator runtime eval', function () {
);
});
});
describe(`Obfuscator. ${detailedDescription}`, () => {
const evaluationTimeout: number = 10000;
let evaluationResult: string;
beforeEach((done) => {
const code: string = readFileAsString(process.cwd() + '/dist/index.js');
const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
code,
{
...baseOptions,
...options
}
).getObfuscatedCode();
evaluateInWorker(
`
${getEnvironmentCode()}
${obfuscatedCode}
module.exports.obfuscate('var foo = 1;').getObfuscatedCode();
`,
(response: string) => {
evaluationResult = response;
done();
},
(error: Error) => {
evaluationResult = error.message;
done();
},
() => {
done();
},
evaluationTimeout
);
});
it('should obfuscate code without any runtime errors after obfuscation: Variant #3 obfuscator', () => {
assert.equal(
evaluationResult,
'var foo=0x1;'
);
});
});
[
{
debugProtection: false,
selfDefending: false,
stringArray: true
},
{
debugProtection: false,
selfDefending: true,
stringArray: false
},
{
debugProtection: true,
selfDefending: false,
stringArray: false
},
{
debugProtection: true,
selfDefending: true,
stringArray: false
},
{
debugProtection: true,
selfDefending: true,
stringArray: true
}
].forEach((webpackBootstrapOptions: Partial<TInputOptions>) => {
describe(`Webpack bootstrap code. ${detailedDescription}. ${JSON.stringify(webpackBootstrapOptions)}`, () => {
const evaluationTimeout: number = 10000;
let evaluationResult: string;
beforeEach((done) => {
const code: string = readFileAsString(__dirname + '/fixtures/webpack-bootstrap.js');
const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
code,
{
...baseOptions,
...options,
...webpackBootstrapOptions
}
).getObfuscatedCode();
evaluateInWorker(
`
${getEnvironmentCode()}
${obfuscatedCode}
`,
(response: string) => {
evaluationResult = response;
done();
},
(error: Error) => {
evaluationResult = error.message;
done();
},
() => {
done();
},
evaluationTimeout
);
});
it('should obfuscate code without any runtime errors after obfuscation: Variant #4 webpack bootstrap', () => {
assert.equal(
evaluationResult,
'foo'
);
});
});
});
});
});
@@ -0,0 +1,77 @@
const module =
(function(modules) {
var installedModules = {};
function __webpack_require__(moduleId) {
if (installedModules[moduleId]) {
return installedModules[moduleId].exports;
}
var module = installedModules[moduleId] = {
i: moduleId,
l: false,
exports: {}
};
modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
module.l = true;
return module.exports;
}
__webpack_require__.m = modules;
__webpack_require__.c = installedModules;
__webpack_require__.d = function(exports, name, getter) {
if (!__webpack_require__.o(exports, name)) {
Object.defineProperty(exports, name, {
enumerable: true,
get: getter
});
}
};
__webpack_require__.r = function(exports) {
if (typeof Symbol !== 'undefined' && Symbol.toStringTag) {
Object.defineProperty(exports, Symbol.toStringTag, {
value: 'Module'
});
}
Object.defineProperty(exports, '__esModule', {
value: true
});
};
__webpack_require__.t = function(value, mode) {
if (mode & 1) value = __webpack_require__(value);
if (mode & 8) return value;
if ((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
var ns = Object.create(null);
__webpack_require__.r(ns);
Object.defineProperty(ns, 'default', {
enumerable: true,
value: value
});
if (mode & 2 && typeof value != 'string')
for (var key in value) __webpack_require__.d(ns, key, function(key) {
return value[key];
}.bind(null, key));
return ns;
};
__webpack_require__.n = function(module) {
var getter = module && module.__esModule ?
function getDefault() {
return module['default'];
} :
function getModuleExports() {
return module;
};
__webpack_require__.d(getter, 'a', getter);
return getter;
};
__webpack_require__.o = function(object, property) {
return Object.prototype.hasOwnProperty.call(object, property);
};
__webpack_require__.p = "";
return __webpack_require__(__webpack_require__.s = "foo");
})
({
"foo": (function(module, exports) {
module.exports = 'foo';
})
});
module;
@@ -12,7 +12,7 @@ import { IdentifierNamesGenerator } from '../../../../src/enums/generators/ident
import { InversifyContainerFacade } from '../../../../src/container/InversifyContainerFacade';
describe('DictionaryIdentifierNamesGenerator', () => {
describe('generate', () => {
describe('generateForGlobalScope', () => {
let identifierNamesGenerator: IIdentifierNamesGenerator,
dictionaryIdentifierName: string;
@@ -33,7 +33,7 @@ describe('DictionaryIdentifierNamesGenerator', () => {
const expectedDictionaryIdentifierNameRegExp: RegExp = /[a|b]/;
beforeEach(() => {
dictionaryIdentifierName = identifierNamesGenerator.generate();
dictionaryIdentifierName = identifierNamesGenerator.generateForGlobalScope();
});
it('Match #1: should return first identifier name', () => {
@@ -49,7 +49,7 @@ describe('DictionaryIdentifierNamesGenerator', () => {
const expectedDictionaryIdentifierNameRegExp: RegExp = /[A|B]/;
beforeEach(() => {
dictionaryIdentifierName = identifierNamesGenerator.generate();
dictionaryIdentifierName = identifierNamesGenerator.generateForGlobalScope();
});
it('Match #1: should return third identifier name', () => {
@@ -79,7 +79,7 @@ describe('DictionaryIdentifierNamesGenerator', () => {
const expectedDictionaryIdentifierNameRegExp: RegExp = /[a|A]/;
beforeEach(() => {
dictionaryIdentifierName = identifierNamesGenerator.generate();
dictionaryIdentifierName = identifierNamesGenerator.generateForGlobalScope();
});
it('Match #1: should return first identifier name', () => {
@@ -112,7 +112,7 @@ describe('DictionaryIdentifierNamesGenerator', () => {
const expectedFourthIterationIdentifierName: string = 'AA';
beforeEach(() => {
dictionaryIdentifierName = identifierNamesGenerator.generate();
dictionaryIdentifierName = identifierNamesGenerator.generateForGlobalScope();
});
it('Match #1: should return first identifier name', () => {
@@ -185,7 +185,7 @@ describe('DictionaryIdentifierNamesGenerator', () => {
IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator
);
testFunc = () => identifierNamesGenerator.generate();
testFunc = () => identifierNamesGenerator.generateForGlobalScope();
});
it('Match #1: should return first identifier name', () => {
@@ -215,7 +215,7 @@ describe('DictionaryIdentifierNamesGenerator', () => {
IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator
);
testFunc = () => identifierNamesGenerator.generate();
testFunc = () => identifierNamesGenerator.generateForGlobalScope();
});
it('Should throw an error when identifiers dictionary is empty', () => {
@@ -12,7 +12,7 @@ import { IdentifierNamesGenerator } from '../../../../src/enums/generators/ident
import { InversifyContainerFacade } from '../../../../src/container/InversifyContainerFacade';
describe('HexadecimalIdentifierNamesGenerator', () => {
describe('generate', () => {
describe('generateForGlobalScope', () => {
let identifierNamesGenerator: IIdentifierNamesGenerator,
hexadecimalIdentifierName: string,
regExp: RegExp;
@@ -26,7 +26,7 @@ describe('HexadecimalIdentifierNamesGenerator', () => {
IdentifierNamesGenerator.HexadecimalIdentifierNamesGenerator
);
hexadecimalIdentifierName = identifierNamesGenerator.generate();
hexadecimalIdentifierName = identifierNamesGenerator.generateForGlobalScope();
regExp = /^_0x(\w){4,6}$/;
});
@@ -12,7 +12,7 @@ import { IdentifierNamesGenerator } from '../../../../src/enums/generators/ident
import { InversifyContainerFacade } from '../../../../src/container/InversifyContainerFacade';
describe('MangledIdentifierNamesGenerator', () => {
describe('generate', () => {
describe('generateForGlobalScope', () => {
let identifierNamesGenerator: IIdentifierNamesGenerator,
mangledIdentifierName: string;
@@ -30,7 +30,7 @@ describe('MangledIdentifierNamesGenerator', () => {
const expectedMangledIdentifierName: string = 'a';
beforeEach(() => {
mangledIdentifierName = identifierNamesGenerator.generate();
mangledIdentifierName = identifierNamesGenerator.generateForGlobalScope();
});
it('should return mangled name', () => {
@@ -44,7 +44,7 @@ describe('MangledIdentifierNamesGenerator', () => {
beforeEach(() => {
for (let i: number = 0; i <= expectedMangledIdentifierPosition; i++) {
mangledIdentifierName = identifierNamesGenerator.generate();
mangledIdentifierName = identifierNamesGenerator.generateForGlobalScope();
}
});
@@ -59,7 +59,7 @@ describe('MangledIdentifierNamesGenerator', () => {
beforeEach(() => {
for (let i: number = 0; i <= expectedMangledIdentifierPosition; i++) {
mangledIdentifierName = identifierNamesGenerator.generate();
mangledIdentifierName = identifierNamesGenerator.generateForGlobalScope();
}
});
@@ -74,7 +74,7 @@ describe('MangledIdentifierNamesGenerator', () => {
beforeEach(() => {
for (let i: number = 0; i <= expectedMangledIdentifierPosition; i++) {
mangledIdentifierName = identifierNamesGenerator.generate();
mangledIdentifierName = identifierNamesGenerator.generateForGlobalScope();
}
});
@@ -89,7 +89,7 @@ describe('MangledIdentifierNamesGenerator', () => {
beforeEach(() => {
for (let i: number = 0; i <= expectedMangledIdentifierPosition; i++) {
mangledIdentifierName = identifierNamesGenerator.generate();
mangledIdentifierName = identifierNamesGenerator.generateForGlobalScope();
}
});
@@ -109,7 +109,7 @@ describe('MangledIdentifierNamesGenerator', () => {
beforeEach(() => {
for (let i: number = 0; i <= expectedMangledIdentifierPosition2; i++) {
mangledIdentifierName = identifierNamesGenerator.generate();
mangledIdentifierName = identifierNamesGenerator.generateForGlobalScope();
if (i === expectedMangledIdentifierPosition1) {
mangledIdentifierName1 = mangledIdentifierName;
@@ -189,8 +189,8 @@ describe('MangledIdentifierNamesGenerator', () => {
IdentifierNamesGenerator.MangledIdentifierNamesGenerator
);
firstMangledIdentifierName = identifierNamesGenerator.generate();
secondMangledIdentifierName = identifierNamesGenerator.generate();
firstMangledIdentifierName = identifierNamesGenerator.generateForGlobalScope();
secondMangledIdentifierName = identifierNamesGenerator.generateForGlobalScope();
});
it('should generate first identifier', () => {
@@ -220,8 +220,8 @@ describe('MangledIdentifierNamesGenerator', () => {
IdentifierNamesGenerator.MangledIdentifierNamesGenerator
);
firstMangledIdentifierName = identifierNamesGenerator.generate();
secondMangledIdentifierName = identifierNamesGenerator.generate();
firstMangledIdentifierName = identifierNamesGenerator.generateForGlobalScope();
secondMangledIdentifierName = identifierNamesGenerator.generateForGlobalScope();
});
it('should generate first identifier', () => {