Fixed #123 and #125, added tests for debug protection template

This commit is contained in:
sanex3339
2017-11-23 01:42:55 +03:00
parent f5b9c22f11
commit e52fb4c84c
14 changed files with 274 additions and 12 deletions
@@ -178,7 +178,60 @@ describe('DeadCodeInjectionTransformer', () => {
});
});
describe('variant #5 - chance of `IfStatement` variant', () => {
describe('variant #5 - await expression in block statement', () => {
const functionRegExp: RegExp = new RegExp(
`var *${variableMatch} *= *function *\\(\\) *\\{` +
`console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
`\\};`,
'g'
);
const awaitExpressionRegExp: RegExp = new RegExp(
`await *${variableMatch}\\(\\)`,
'g'
);
const expectedFunctionMatchesLength: number = 4;
const expectedAwaitExpressionMatchesLength: number = 1;
let functionMatchesLength: number = 0,
awaitExpressionMatchesLength: number = 0;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/await-expression.js');
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
code,
{
...NO_CUSTOM_NODES_PRESET,
deadCodeInjection: true,
deadCodeInjectionThreshold: 1,
stringArray: true,
stringArrayThreshold: 1
}
);
const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
const awaitExpressionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(awaitExpressionRegExp);
console.log(obfuscatedCode);
if (functionMatches) {
functionMatchesLength = functionMatches.length;
}
if (awaitExpressionMatches) {
awaitExpressionMatchesLength = awaitExpressionMatches.length;
}
});
it('match #1: shouldn\'t add dead code', () => {
assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
});
it('match #2: shouldn\'t add dead code', () => {
assert.equal(awaitExpressionMatchesLength, expectedAwaitExpressionMatchesLength);
});
});
describe('variant #6 - chance of `IfStatement` variant', () => {
const samplesCount: number = 1000;
const delta: number = 0.1;
const expectedDistribution: number = 0.25;
@@ -280,7 +333,7 @@ describe('DeadCodeInjectionTransformer', () => {
});
});
describe('variant #6 - block scope of block statement is `ProgramNode`', () => {
describe('variant #7 - block scope of block statement is `ProgramNode`', () => {
const regExp: RegExp = new RegExp(
`if *\\(!!\\[\\]\\) *{` +
`console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
@@ -310,7 +363,7 @@ describe('DeadCodeInjectionTransformer', () => {
});
});
describe('variant #7 - correct obfuscation of dead-code block statements', () => {
describe('variant #8 - correct obfuscation of dead-code block statements', () => {
const variableName: string = 'importantVariableName';
let obfuscatedCode: string;
@@ -0,0 +1,25 @@
(async 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');
};
if (true) {
await foo();
}
foo();
bar();
baz();
bark();
}
})();
@@ -0,0 +1,138 @@
import { assert } from 'chai';
import { spawn } from 'threads';
import { IObfuscationResult } from '../../../../../src/interfaces/IObfuscationResult';
import { readFileAsString } from '../../../../helpers/readFileAsString';
import { NO_CUSTOM_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFacade';
function spawnThread(inputCallback: Function, threadCallback: Function, timeoutCallback: Function): void {
const thread = spawn<string, number>((input: string, postMessage: Function) => {
postMessage(eval(input));
});
const timeout = setTimeout(() => {
thread.kill();
timeoutCallback();
}, 500);
thread
.send(inputCallback())
.on('message', (response: number) => {
clearTimeout(timeout);
thread.kill();
threadCallback(response);
});
}
describe('DebugProtectionFunctionCallTemplate (): string', () => {
describe('variant #1: correctly obfuscated code`', () => {
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_CUSTOM_NODES_PRESET,
debugProtection: true
}
);
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 #2: correctly obfuscated code with enabled `mangle` option', () => {
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_CUSTOM_NODES_PRESET,
debugProtection: true,
mangle: true
}
);
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 #3: obfuscated code with removed debug protection function call', () => {
const expectedEvaluationResult: number = 0;
let obfuscatedCode: string,
evaluationResult: number = 0;
beforeEach((done) => {
const code: string = readFileAsString(__dirname + '/fixtures/input.js');
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
code,
{
...NO_CUSTOM_NODES_PRESET,
debugProtection: true
}
);
obfuscatedCode = obfuscationResult.getObfuscatedCode();
obfuscatedCode = obfuscatedCode.replace(/\+\+ *_0x([a-f0-9]){4,6}/, '++abc');
spawnThread(
() => obfuscatedCode,
(response: number) => {
evaluationResult = response;
done();
},
() => {
done();
}
);
});
it('should enter code in infinity loop', () => {
assert.equal(evaluationResult, expectedEvaluationResult);
});
});
});
@@ -0,0 +1,3 @@
(function () {
return 1;
})();
+1
View File
@@ -62,6 +62,7 @@ import './functional-tests/node-transformers/obfuscating-transformers/object-exp
import './functional-tests/node-transformers/obfuscating-transformers/variable-declaration-transformer/VariableDeclarationTransformer.spec';
import './functional-tests/node-transformers/parentizing-transformers/obfuscating-guards/black-list-obfuscating-guard/BlackListObfuscatingGuard.spec';
import './functional-tests/node-transformers/parentizing-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';