12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094 |
- import { assert } from 'chai';
- import { NO_ADDITIONAL_NODES_PRESET } from '../../../../src/options/presets/NoCustomNodes';
- import { IdentifierNamesGenerator } from '../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
- import { getRegExpMatch } from '../../../helpers/getRegExpMatch';
- import { readFileAsString } from '../../../helpers/readFileAsString';
- import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscatorFacade';
- describe('DeadCodeInjectionTransformer', () => {
- const variableMatch: string = '_0x([a-f0-9]){4,6}';
- const hexMatch: string = '0x[a-f0-9]';
- const stringArrayCallMatch: string = `${variableMatch}\\(${hexMatch}\\)`;
- describe('transformNode', function () {
- this.timeout(100000);
- describe('Variant #1 - 5 simple block statements', () => {
- const regExp: RegExp = new RegExp(
- `if *\\(${variableMatch}\\(${hexMatch}\\) *[=|!]== *${variableMatch}\\(${hexMatch}\\)\\) *\\{`+
- `(?:console|${variableMatch})\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
- `\\} *else *\\{`+
- `(?:console|${variableMatch})\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
- `\\}`,
- 'g'
- );
- const expectedMatchesLength: number = 5;
- let matchesLength: number = 0;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/input-1.js');
- const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1,
- stringArray: true,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- const matches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
- if (matches) {
- matchesLength = matches.length;
- }
- });
- it('should replace block statements with condition with original block statements and dead code', () => {
- assert.equal(matchesLength, expectedMatchesLength);
- });
- });
- describe('Variant #2 - block statements count is less than `5`', () => {
- const regexp: RegExp = new RegExp(
- `var ${variableMatch} *= *function *\\(\\) *\\{` +
- `console\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
- `\\};`,
- 'g'
- );
- const expectedMatchesLength: number = 4;
- let matchesLength: number = 0;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/block-statements-min-count.js');
- const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1,
- stringArray: true,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- const matches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regexp);
- if (matches) {
- matchesLength = matches.length;
- }
- });
- it('shouldn\'t add dead code', () => {
- assert.equal(matchesLength, expectedMatchesLength);
- });
- });
- describe('Variant #3 - deadCodeInjectionThreshold: 0', () => {
- const regexp: RegExp = new RegExp(
- `var ${variableMatch} *= *function *\\(\\) *\\{` +
- `console\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
- `\\};`,
- 'g'
- );
- const expectedMatchesLength: number = 5;
- let matchesLength: number = 0;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/input-1.js');
- const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 0,
- stringArray: true,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- const matches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regexp);
- if (matches) {
- matchesLength = matches.length;
- }
- });
- it('shouldn\'t add dead code', () => {
- assert.equal(matchesLength, expectedMatchesLength);
- });
- });
- describe('Variant #4 - prohibited node inside collected block statement', () => {
- describe('Variant #1 - function declaration in block statement', () => {
- const functionRegExp: RegExp = new RegExp(
- `var ${variableMatch} *= *function *\\(\\) *\\{` +
- `console\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
- `\\};`,
- 'g'
- );
- const functionDeclarationRegExp: RegExp = new RegExp(
- `function *${variableMatch} *\\(${variableMatch}\\) *{}`,
- 'g'
- );
- const expectedFunctionMatchesLength: number = 4;
- const expectedFunctionDeclarationMatchesLength: number = 1;
- let functionMatchesLength: number = 0,
- functionDeclarationMatchesLength: number = 0;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/function-declaration-inside-block-statement.js');
- const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1,
- stringArray: true,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
- const loopMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionDeclarationRegExp);
- if (functionMatches) {
- functionMatchesLength = functionMatches.length;
- }
- if (loopMatches) {
- functionDeclarationMatchesLength = loopMatches.length;
- }
- });
- it('match #1: shouldn\'t add dead code', () => {
- assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
- });
- it('match #2: shouldn\'t add dead code', () => {
- assert.equal(functionDeclarationMatchesLength, expectedFunctionDeclarationMatchesLength);
- });
- });
- describe('Variant #2 - break or continue statement in block statement', () => {
- describe('Variant #1', () => {
- const functionRegExp: RegExp = new RegExp(
- `var ${variableMatch} *= *function *\\(\\) *\\{` +
- `console\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
- `\\};`,
- 'g'
- );
- const loopRegExp: RegExp = new RegExp(
- `for *\\(var ${variableMatch} *= *${hexMatch}; *${variableMatch} *< *${hexMatch}; *${variableMatch}\\+\\+\\) *\\{` +
- `(?:continue|break);` +
- `\\}`,
- 'g'
- );
- const expectedFunctionMatchesLength: number = 4;
- const expectedLoopMatchesLength: number = 2;
- let functionMatchesLength: number = 0,
- loopMatchesLength: number = 0;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/break-continue-statement-1.js');
- const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1,
- stringArray: true,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
- const loopMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(loopRegExp);
- if (functionMatches) {
- functionMatchesLength = functionMatches.length;
- }
- if (loopMatches) {
- loopMatchesLength = loopMatches.length;
- }
- });
- it('match #1: shouldn\'t add dead code', () => {
- assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
- });
- it('match #2: shouldn\'t add dead code', () => {
- assert.equal(loopMatchesLength, expectedLoopMatchesLength);
- });
- });
- describe('Variant #2', () => {
- const functionRegExp: RegExp = new RegExp(
- `var ${variableMatch} *= *function *\\(\\) *\\{` +
- `console\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
- `\\};`,
- 'g'
- );
- const loopRegExp: RegExp = new RegExp(
- `for *\\(var ${variableMatch} *= *${hexMatch}; *${variableMatch} *< *${hexMatch}; *${variableMatch}\\+\\+\\) *` +
- `(?:continue|break);`,
- 'g'
- );
- const expectedFunctionMatchesLength: number = 4;
- const expectedLoopMatchesLength: number = 2;
- let functionMatchesLength: number = 0,
- loopMatchesLength: number = 0;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/break-continue-statement-2.js');
- const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1,
- stringArray: true,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
- const loopMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(loopRegExp);
- if (functionMatches) {
- functionMatchesLength = functionMatches.length;
- }
- if (loopMatches) {
- loopMatchesLength = loopMatches.length;
- }
- });
- it('match #1: shouldn\'t add dead code', () => {
- assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
- });
- it('match #2: shouldn\'t add dead code', () => {
- assert.equal(loopMatchesLength, expectedLoopMatchesLength);
- });
- });
- });
- describe('Variant #3 - 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 obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1,
- stringArray: true,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
- const awaitExpressionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(awaitExpressionRegExp);
- 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 #4 - yield expression in block statement', () => {
- const functionRegExp: RegExp = new RegExp(
- `var ${variableMatch} *= *function *\\(\\) *\\{` +
- `console\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
- `\\};`,
- 'g'
- );
- const yieldExpressionRegExp: RegExp = new RegExp(
- `yield *${variableMatch}\\(\\)`,
- 'g'
- );
- const expectedFunctionMatchesLength: number = 4;
- const expectedAwaitExpressionMatchesLength: number = 1;
- let functionMatchesLength: number = 0,
- yieldExpressionMatchesLength: number = 0;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/yield-expression.js');
- const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1,
- stringArray: true,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
- const yieldExpressionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(yieldExpressionRegExp);
- if (functionMatches) {
- functionMatchesLength = functionMatches.length;
- }
- if (yieldExpressionMatches) {
- yieldExpressionMatchesLength = yieldExpressionMatches.length;
- }
- });
- it('match #1: shouldn\'t add dead code', () => {
- assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
- });
- it('match #2: shouldn\'t add dead code', () => {
- assert.equal(yieldExpressionMatchesLength, expectedAwaitExpressionMatchesLength);
- });
- });
- describe('Variant #5 - super expression in block statement', () => {
- const functionRegExp: RegExp = new RegExp(
- `var ${variableMatch} *= *function *\\(\\) *\\{` +
- `console\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
- `\\};`,
- 'g'
- );
- const superExpressionRegExp: RegExp = new RegExp(
- `super *\\(\\);`,
- 'g'
- );
- const expectedFunctionMatchesLength: number = 4;
- const expectedSuperExpressionMatchesLength: number = 1;
- let functionMatchesLength: number = 0,
- superExpressionMatchesLength: number = 0;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/super-expression.js');
- const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1,
- stringArray: true,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
- const superExpressionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(superExpressionRegExp);
- if (functionMatches) {
- functionMatchesLength = functionMatches.length;
- }
- if (superExpressionMatches) {
- superExpressionMatchesLength = superExpressionMatches.length;
- }
- });
- it('match #1: shouldn\'t add dead code', () => {
- assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
- });
- it('match #2: shouldn\'t add dead code', () => {
- assert.equal(superExpressionMatchesLength, expectedSuperExpressionMatchesLength);
- });
- });
- describe('Variant #6 - for-await expression in block statement', () => {
- const functionRegExp: RegExp = new RegExp(
- `var ${variableMatch} *= *function *\\(\\) *\\{` +
- `console\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
- `\\};`,
- 'g'
- );
- const awaitExpressionRegExp: RegExp = new RegExp(
- `for await *\\(const ${variableMatch} of *\\[]\\){}`,
- 'g'
- );
- const expectedFunctionMatchesLength: number = 4;
- const expectedAwaitExpressionMatchesLength: number = 1;
- let functionMatchesLength: number = 0,
- awaitExpressionMatchesLength: number = 0;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/for-await-expression.js');
- const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1,
- stringArray: true,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
- const awaitExpressionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(awaitExpressionRegExp);
- 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 #7 - private identifier in block statement', () => {
- const functionRegExp: RegExp = new RegExp(
- `var ${variableMatch} *= *function *\\(\\) *\\{` +
- `console\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
- `\\};`,
- 'g'
- );
- const privateIdentifierRegExp: RegExp = new RegExp(
- `this\.#private *= *0x1;`,
- 'g'
- );
- const expectedFunctionMatchesLength: number = 4;
- const expectedPrivateIdentifierMatchesLength: number = 1;
- let functionMatchesLength: number = 0,
- privateIdentifierMatchesLength: number = 0;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/private-identifier.js');
- const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1,
- stringArray: true,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
- const privateIdentifierMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(privateIdentifierRegExp);
- if (functionMatches) {
- functionMatchesLength = functionMatches.length;
- }
- if (privateIdentifierMatches) {
- privateIdentifierMatchesLength = privateIdentifierMatches.length;
- }
- });
- it('match #1: shouldn\'t add dead code', () => {
- assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
- });
- it('match #2: shouldn\'t add dead code', () => {
- assert.equal(privateIdentifierMatchesLength, expectedPrivateIdentifierMatchesLength);
- });
- });
- });
- describe('Variant #5 - chance of `IfStatement` variant', () => {
- const samplesCount: number = 1000;
- const delta: number = 0.1;
- const expectedDistribution: number = 0.25;
- const ifMatch: string = `if *\\(!!\\[\\]\\) *\\{`;
- const functionMatch: string = `var ${variableMatch} *= *function *\\(\\) *\\{`;
- const match1: string = `` +
- `if *\\(${stringArrayCallMatch} *=== *${stringArrayCallMatch}\\) *\\{` +
- `console\\[${stringArrayCallMatch}]\\(${stringArrayCallMatch}\\);` +
- `\\} *else *\\{` +
- `${variableMatch}\\(${stringArrayCallMatch}\\);` +
- `\\}` +
- ``;
- const match2: string = `` +
- `if *\\(${stringArrayCallMatch} *!== *${stringArrayCallMatch}\\) *\\{` +
- `console\\[${stringArrayCallMatch}]\\(${stringArrayCallMatch}\\);` +
- `\\} *else *\\{` +
- `${variableMatch}\\(${stringArrayCallMatch}\\);` +
- `\\}` +
- ``;
- const match3: string = `` +
- `if *\\(${stringArrayCallMatch} *=== *${stringArrayCallMatch}\\) *\\{` +
- `${variableMatch}\\(${stringArrayCallMatch}\\);` +
- `\\} *else *\\{` +
- `console\\[${stringArrayCallMatch}]\\(${stringArrayCallMatch}\\);` +
- `\\}` +
- ``;
- const match4: string = `` +
- `if *\\(${stringArrayCallMatch} *!== *${stringArrayCallMatch}\\) *\\{` +
- `${variableMatch}\\(${stringArrayCallMatch}\\);` +
- `\\} *else *\\{` +
- `console\\[${stringArrayCallMatch}]\\(${stringArrayCallMatch}\\);` +
- `\\}` +
- ``;
- let distribution1: number = 0,
- distribution2: number = 0,
- distribution3: number = 0,
- distribution4: number = 0;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/if-statement-variants-distribution.js');
- const regExp1: RegExp = new RegExp(`${ifMatch}${functionMatch}${match1}`);
- const regExp2: RegExp = new RegExp(`${ifMatch}${functionMatch}${match2}`);
- const regExp3: RegExp = new RegExp(`${ifMatch}${functionMatch}${match3}`);
- const regExp4: RegExp = new RegExp(`${ifMatch}${functionMatch}${match4}`);
- let count1: number = 0;
- let count2: number = 0;
- let count3: number = 0;
- let count4: number = 0;
- for (let i = 0; i < samplesCount; i++) {
- const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1,
- stringArray: true,
- stringArrayThreshold: 1
- }
- ).getObfuscatedCode();
- if (regExp1.test(obfuscatedCode)) {
- count1++;
- } else if (regExp2.test(obfuscatedCode)) {
- count2++;
- } else if (regExp3.test(obfuscatedCode)) {
- count3++;
- } else if (regExp4.test(obfuscatedCode)) {
- count4++;
- }
- }
- distribution1 = count1 / samplesCount;
- distribution2 = count2 / samplesCount;
- distribution3 = count3 / samplesCount;
- distribution4 = count4 / samplesCount;
- });
- it('Variant #1: `IfStatement` variant should have distribution close to `0.25`', () => {
- assert.closeTo(distribution1, expectedDistribution, delta);
- });
- it('Variant #2: `IfStatement` variant should have distribution close to `0.25`', () => {
- assert.closeTo(distribution2, expectedDistribution, delta);
- });
- it('Variant #3: `IfStatement` variant should have distribution close to `0.25`', () => {
- assert.closeTo(distribution3, expectedDistribution, delta);
- });
- it('Variant #4: `IfStatement` variant should have distribution close to `0.25`', () => {
- assert.closeTo(distribution4, expectedDistribution, delta);
- });
- });
- describe('Variant #6 - block scope of block statement is `ProgramNode`', () => {
- const regExp: RegExp = new RegExp(
- `if *\\(!!\\[\\]\\) *{` +
- `console\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
- `\\}`
- );
- let obfuscatedCode: string;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/block-scope-is-program-node.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- stringArray: true,
- stringArrayThreshold: 1,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1
- }
- ).getObfuscatedCode();
- });
- it('shouldn\'t add dead code in block statements with `ProgramNode` block scope', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- describe('Variant #7 - correct obfuscation of dead-code block statements', () => {
- const variableName: string = 'importantVariableName';
- let obfuscatedCode: string;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/obfuscation-of-dead-code-block-statements.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1,
- debugProtection: true
- }
- ).getObfuscatedCode();
- });
- it('should correctly obfuscate dead-code block statements and prevent any exposing of internal variable names', () => {
- assert.notInclude(obfuscatedCode, variableName);
- });
- });
- describe('Variant #8 - unique names for dead code identifiers', () => {
- /**
- * Code:
- *
- * (function(variable){
- * function foo () {
- * return variable.push(1);
- * }
- *
- * function bar () {
- * var variable = 1;
- * }
- *
- * function baz() {
- * var variable = 2;
- * }
- *
- * function bark() {
- * var variable = 3;
- * }
- *
- * function hawk() {
- * var variable = 4;
- * }
- * })([]);
- *
- * With this code, dead code can be added to the first function `foo`
- * If dead code won't be renamed before add - identifier name inside dead code block statement can be
- * the same as identifier name inside transformed block statement:
- *
- * (function(variable){
- * function foo () {
- * if (1 !== 1) {
- * var variable = 1; // <- overwriting value of function parameter
- * } else {
- * return variable.push(1);
- * }
- * }
- *
- * function bar () {
- * var variable = 1;
- * }
- *
- * function baz() {
- * var variable = 2;
- * }
- *
- * function bark() {
- * var variable = 3;
- * }
- *
- * function hawk() {
- * var variable = 4;
- * }
- * })([]);
- *
- * So, added dead code variable declaration will overwrite a value of function parameter.
- * This should never happen.
- */
- describe('Variant #1', () => {
- const functionParameterMatch: string = `` +
- `\\(function\\((\\w)\\){` +
- ``;
- const deadCodeMatch: string = `` +
- `function \\w *\\(\\w\\) *{` +
- `if *\\(.{0,30}\\) *{` +
- `var (\\w).*?;` +
- `} *else *{` +
- `return *(\\w).*?;` +
- `}` +
- `}` +
- ``;
- const functionParameterRegExp: RegExp = new RegExp(functionParameterMatch);
- const deadCodeRegExp: RegExp = new RegExp(deadCodeMatch);
- let result: boolean = false,
- functionIdentifierName: string | null,
- returnIdentifierName: string | null,
- variableDeclarationIdentifierName: string | null,
- obfuscatedCode: string;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/unique-names-for-dead-code-identifiers.js');
- for (let i: number = 0; i < 100; i++) {
- while (true) {
- try {
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1,
- identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
- }
- ).getObfuscatedCode();
- functionIdentifierName = getRegExpMatch(obfuscatedCode, functionParameterRegExp, 0);
- variableDeclarationIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 0);
- returnIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 1);
- break;
- } catch {}
- }
- if (
- // variable declaration from dead code is affects original code
- functionIdentifierName === variableDeclarationIdentifierName &&
- returnIdentifierName === variableDeclarationIdentifierName
- ) {
- result = false;
- break;
- }
- result = true;
- }
- });
- it('should generate separate identifiers for common AST and dead code', () => {
- assert.isOk(result, 'wrong identifier names');
- });
- });
- describe('Variant #2', () => {
- const functionParameterMatch: string = `` +
- `\\(function\\((\\w)\\){` +
- ``;
- const deadCodeMatch: string = `` +
- `function \\w *\\(\\w\\) *{` +
- `if *\\(.{0,30}\\) *{` +
- `return *(\\w).{0,40};` +
- `} *else *{` +
- `var (\\w).*?;` +
- `}` +
- `}` +
- ``;
- const functionParameterRegExp: RegExp = new RegExp(functionParameterMatch);
- const deadCodeRegExp: RegExp = new RegExp(deadCodeMatch);
- let result: boolean = false,
- functionIdentifierName: string | null,
- returnIdentifierName: string | null,
- variableDeclarationIdentifierName: string | null,
- obfuscatedCode: string;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/unique-names-for-dead-code-identifiers.js');
- for (let i: number = 0; i < 100; i++) {
- while (true) {
- try {
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1,
- identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
- }
- ).getObfuscatedCode();
- functionIdentifierName = getRegExpMatch(obfuscatedCode, functionParameterRegExp, 0);
- returnIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 0);
- variableDeclarationIdentifierName = getRegExpMatch(obfuscatedCode, deadCodeRegExp, 1);
- break;
- } catch {}
- }
- if (
- // variable declaration from dead code is affects original code
- functionIdentifierName === variableDeclarationIdentifierName &&
- returnIdentifierName === variableDeclarationIdentifierName
- ) {
- console.log(obfuscatedCode);
- result = false;
- break;
- }
- result = true;
- }
- });
- it('should generate separate identifiers for common AST and dead code', () => {
- assert.isOk(result, 'wrong identifier names');
- });
- });
- });
- describe('Variant #9 - block statements with empty body', () => {
- const regExp: RegExp = new RegExp(
- `function *${variableMatch} *\\(\\) *{ *} *` +
- `${variableMatch} *\\(\\); *`,
- 'g'
- );
- const expectedMatchesLength: number = 5;
- let matchesLength: number = 0;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/block-statement-empty-body.js');
- const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- stringArray: true,
- stringArrayThreshold: 1,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1
- }
- ).getObfuscatedCode();
- const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
- if (functionMatches) {
- matchesLength = functionMatches.length;
- }
- });
- it('shouldn\'t add dead code conditions to the block empty block statements', () => {
- assert.isAtLeast(matchesLength, expectedMatchesLength);
- });
- });
- describe('Variant #10 - block statement with scope-hoisting', () => {
- describe('Variant #1: collecting of block statements', () => {
- const regExp: RegExp = new RegExp(
- `${variableMatch} *\\(\\); *` +
- `var ${variableMatch} *= *0x2; *` +
- `function *${variableMatch} *\\(\\) *{ *} *`,
- 'g'
- );
- const expectedMatchesLength: number = 5;
- let matchesLength: number = 0;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/block-statement-with-scope-hoisting-1.js');
- const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- stringArray: true,
- stringArrayThreshold: 1,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1
- }
- ).getObfuscatedCode();
- const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
- if (functionMatches) {
- matchesLength = functionMatches.length;
- }
- });
- it('shouldn\'t collect block statements with scope-hoisting', () => {
- assert.equal(matchesLength, expectedMatchesLength);
- });
- });
- describe('Variant #2: wrapping of block statements in dead code conditions', () => {
- const regExp: RegExp = new RegExp(
- `function *${variableMatch} *\\(\\) *{ *` +
- `var ${variableMatch} *= *0x1; *` +
- `${variableMatch} *\\(\\); *` +
- `var ${variableMatch} *= *0x2; *` +
- `function *${variableMatch} *\\(\\) *{ *} *` +
- `var ${variableMatch} *= *0x3; *` +
- `}`,
- 'g'
- );
- let obfuscatedCode: string;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/block-statement-with-scope-hoisting-2.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- stringArray: true,
- stringArrayThreshold: 1,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1
- }
- ).getObfuscatedCode();
- });
- it('shouldn\'t wrap block statements in dead code conditions', () => {
- assert.match(obfuscatedCode, regExp);
- });
- });
- });
- describe('Variant #11 - prevailing kind of variables of inserted code', () => {
- describe('Variant #1: base', () => {
- const variableDeclarationsRegExp: RegExp = new RegExp(
- `const ${variableMatch} *= *\\[\\]; *` +
- `var ${variableMatch} *= *\\[\\]; *`,
- 'g'
- );
- const invalidVariableDeclarationsRegExp: RegExp = new RegExp(
- `var ${variableMatch} *= *\\[\\]; *` +
- `var ${variableMatch} *= *\\[\\]; *`,
- 'g'
- );
- const forLoopRegExp: RegExp = new RegExp(
- `for *\\(const ${variableMatch} of ${variableMatch}\\) *{`,
- 'g'
- );
- const invalidForLoopRegExp: RegExp = new RegExp(
- `for *\\(var ${variableMatch} of ${variableMatch}\\) *{`,
- 'g'
- );
- let obfuscatedCode: string;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-1.js');
- obfuscatedCode = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1
- }
- ).getObfuscatedCode();
- });
- it('Match #1: shouldn\'t replace kinds of variables of inserted original code', () => {
- assert.match(obfuscatedCode, variableDeclarationsRegExp);
- });
- it('Match #2: shouldn\'t replace kinds of variables of inserted original code', () => {
- assert.notMatch(obfuscatedCode, invalidVariableDeclarationsRegExp);
- });
- it('Match #3: shouldn\'t replace kinds of variables of inserted original code', () => {
- assert.match(obfuscatedCode, forLoopRegExp);
- });
- it('Match #4: shouldn\'t replace kinds of variables of inserted original code', () => {
- assert.notMatch(obfuscatedCode, invalidForLoopRegExp);
- });
- });
- });
- describe('Variant #12 - correct integration with `stringArrayWrappersChainedCalls` option', () => {
- const regExp: RegExp = new RegExp(
- `var ${variableMatch} *= *${variableMatch}; *` +
- `if *\\(${variableMatch}\\(${hexMatch}\\) *[=|!]== *${variableMatch}\\(${hexMatch}\\)\\) *\\{`+
- `(?:console|${variableMatch})\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
- `\\} *else *\\{`+
- `(?:console|${variableMatch})\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
- `\\}`,
- 'g'
- );
- const expectedMatchesLength: number = 5;
- let matchesLength: number = 0;
- before(() => {
- const code: string = readFileAsString(__dirname + '/fixtures/input-1.js');
- const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
- code,
- {
- ...NO_ADDITIONAL_NODES_PRESET,
- deadCodeInjection: true,
- deadCodeInjectionThreshold: 1,
- stringArray: true,
- stringArrayThreshold: 1,
- stringArrayWrappersCount: 1,
- stringArrayWrappersChainedCalls: true
- }
- ).getObfuscatedCode();
- const matches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
- if (matches) {
- matchesLength = matches.length;
- }
- });
- it('should unwrap dead code injection root AST host node before the string array transformer', () => {
- assert.equal(matchesLength, expectedMatchesLength);
- });
- });
- });
- });
|