|
@@ -10,6 +10,7 @@ import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscator';
|
|
|
|
|
|
describe('DeadCodeInjectionTransformer', () => {
|
|
|
const variableMatch: string = '_0x([a-f0-9]){4,6}';
|
|
|
+ const hexMatch: string = '0x[a-f0-9]';
|
|
|
|
|
|
describe('transformNode (programNode: ESTree.Program, parentNode: ESTree.Node): ESTree.Node', () => {
|
|
|
describe('variant #1 - 5 simple block statements', () => {
|
|
@@ -25,10 +26,10 @@ describe('DeadCodeInjectionTransformer', () => {
|
|
|
);
|
|
|
const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
|
|
|
const deadCodeMatch: string = `` +
|
|
|
- `if *\\(${variableMatch}\\('0x[a-z0-9]'\\) *[=|!]== *${variableMatch}\\('0x[a-z0-9]'\\)\\) *\\{`+
|
|
|
- `console\\[${variableMatch}\\('0x[a-z0-9]'\\)\\]\\(${variableMatch}\\('0x[a-z0-9]'\\)\\);` +
|
|
|
+ `if *\\(${variableMatch}\\('${hexMatch}'\\) *[=|!]== *${variableMatch}\\('${hexMatch}'\\)\\) *\\{`+
|
|
|
+ `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
|
|
|
`\\} *else *\\{`+
|
|
|
- `console\\[${variableMatch}\\('0x[a-z0-9]'\\)\\]\\(${variableMatch}\\('0x[a-z0-9]'\\)\\);` +
|
|
|
+ `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
|
|
|
`\\}` +
|
|
|
``;
|
|
|
const regexp: RegExp = new RegExp(deadCodeMatch, 'g');
|
|
@@ -54,7 +55,7 @@ describe('DeadCodeInjectionTransformer', () => {
|
|
|
const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
|
|
|
const codeMatch: string = `` +
|
|
|
`var *${variableMatch} *= *function *\\(\\) *\\{` +
|
|
|
- `console\\[${variableMatch}\\('0x[a-z0-9]'\\)\\]\\(${variableMatch}\\('0x[a-z0-9]'\\)\\);` +
|
|
|
+ `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
|
|
|
`\\};` +
|
|
|
``;
|
|
|
const regexp: RegExp = new RegExp(codeMatch, 'g');
|
|
@@ -80,7 +81,7 @@ describe('DeadCodeInjectionTransformer', () => {
|
|
|
const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
|
|
|
const codeMatch: string = `` +
|
|
|
`var *${variableMatch} *= *function *\\(\\) *\\{` +
|
|
|
- `console\\[${variableMatch}\\('0x[a-z0-9]'\\)\\]\\(${variableMatch}\\('0x[a-z0-9]'\\)\\);` +
|
|
|
+ `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
|
|
|
`\\};` +
|
|
|
``;
|
|
|
const regexp: RegExp = new RegExp(codeMatch, 'g');
|
|
@@ -91,5 +92,44 @@ describe('DeadCodeInjectionTransformer', () => {
|
|
|
assert.equal(matches.length, 5);
|
|
|
});
|
|
|
});
|
|
|
+
|
|
|
+ describe('variant #4 - break or continue statement in block statement', () => {
|
|
|
+ const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
|
|
|
+ readFileAsString(__dirname + '/fixtures/break-continue-statement.js'),
|
|
|
+ {
|
|
|
+ ...NO_CUSTOM_NODES_PRESET,
|
|
|
+ deadCodeInjection: true,
|
|
|
+ deadCodeInjectionThreshold: 1,
|
|
|
+ stringArray: true,
|
|
|
+ stringArrayThreshold: 1
|
|
|
+ }
|
|
|
+ );
|
|
|
+ const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
|
|
|
+
|
|
|
+ const functionMatch: string = `` +
|
|
|
+ `var *${variableMatch} *= *function *\\(\\) *\\{` +
|
|
|
+ `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
|
|
|
+ `\\};` +
|
|
|
+ ``;
|
|
|
+ const loopMatch: string = `` +
|
|
|
+ `for *\\(var *${variableMatch} *= *${hexMatch}; *${variableMatch} *< *${hexMatch}; *${variableMatch}\\+\\+\\) *\\{` +
|
|
|
+ `(?:continue|break);` +
|
|
|
+ `\\}` +
|
|
|
+ ``;
|
|
|
+
|
|
|
+ const functionRegExp: RegExp = new RegExp(functionMatch, 'g');
|
|
|
+ const loopRegExp: RegExp = new RegExp(loopMatch, 'g');
|
|
|
+
|
|
|
+ const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
|
|
|
+ const loopMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(loopRegExp);
|
|
|
+
|
|
|
+ it('shouldn\'t add dead code if block statement contains `continue` or `break` statements', () => {
|
|
|
+ assert.isNotNull(functionMatches);
|
|
|
+ assert.isNotNull(loopMatches);
|
|
|
+
|
|
|
+ assert.equal(functionMatches.length, 4);
|
|
|
+ assert.equal(loopMatches.length, 2);
|
|
|
+ });
|
|
|
+ });
|
|
|
});
|
|
|
});
|