CallExpressionControlFlowReplacer.spec.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { assert } from 'chai';
  2. import { IObfuscationResult } from '../../../../../../src/interfaces/IObfuscationResult';
  3. import { NO_CUSTOM_NODES_PRESET } from '../../../../../../src/options/presets/NoCustomNodes';
  4. import { readFileAsString } from '../../../../../helpers/readFileAsString';
  5. import { JavaScriptObfuscator } from '../../../../../../src/JavaScriptObfuscator';
  6. describe('CallExpressionControlFlowReplacer', () => {
  7. describe('replace (callExpressionNode: ESTree.CallExpression,parentNode: ESTree.Node,controlFlowStorage: IStorage <ICustomNode>)', () => {
  8. describe('variant #1 - single call expression', () => {
  9. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  10. readFileAsString(__dirname + '/fixtures/input-1.js'),
  11. {
  12. ...NO_CUSTOM_NODES_PRESET,
  13. controlFlowFlattening: true,
  14. controlFlowFlatteningThreshold: 1
  15. }
  16. );
  17. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  18. const controlFlowStorageCallRegExp: RegExp = /var *_0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4,6}\['(\\x[a-f0-9]*){3}'\]\(_0x([a-f0-9]){4,6}, *0x1, *0x2\);/;
  19. it('should replace call expression node by call to control flow storage node', () => {
  20. assert.match(obfuscatedCode, controlFlowStorageCallRegExp);
  21. });
  22. });
  23. describe('variant #2 - multiple call expressions with threshold = 1', () => {
  24. it('should replace call expression node by call to control flow storage node', function () {
  25. this.timeout(15000);
  26. const samplesCount: number = 1000;
  27. const expectedValue: number = 0.5;
  28. const delta: number = 0.1;
  29. let equalsValue: number = 0;
  30. for (let i = 0; i < samplesCount; i++) {
  31. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  32. readFileAsString(__dirname + '/fixtures/input-2.js'),
  33. {
  34. ...NO_CUSTOM_NODES_PRESET,
  35. controlFlowFlattening: true,
  36. controlFlowFlatteningThreshold: 1
  37. }
  38. );
  39. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  40. const controlFlowStorageCallRegExp1: RegExp = /var *_0x([a-f0-9]){4,6} *= *(_0x([a-f0-9]){4,6}\['(\\x[a-f0-9]*){3}'\])\(_0x([a-f0-9]){4,6}, *0x1, *0x2\);/;
  41. const controlFlowStorageCallRegExp2: RegExp = /var *_0x([a-f0-9]){4,6} *= *(_0x([a-f0-9]){4,6}\['(\\x[a-f0-9]*){3}'\])\(_0x([a-f0-9]){4,6}, *0x2, *0x3\);/;
  42. const firstMatchArray: RegExpMatchArray | null = obfuscatedCode.match(controlFlowStorageCallRegExp1);
  43. const secondMatchArray: RegExpMatchArray | null = obfuscatedCode.match(controlFlowStorageCallRegExp2);
  44. const firstMatch: string | undefined = firstMatchArray ? firstMatchArray[2] : undefined;
  45. const secondMatch: string | undefined = secondMatchArray ? secondMatchArray[2] : undefined;
  46. assert.match(obfuscatedCode, controlFlowStorageCallRegExp1);
  47. assert.match(obfuscatedCode, controlFlowStorageCallRegExp2);
  48. assert.isOk(firstMatch);
  49. assert.isOk(secondMatch);
  50. if (firstMatch === secondMatch) {
  51. equalsValue++;
  52. }
  53. }
  54. assert.closeTo(equalsValue / samplesCount, expectedValue, delta);
  55. });
  56. });
  57. describe('variant #3 - callee - member expression', () => {
  58. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  59. readFileAsString(__dirname + '/fixtures/input-3.js'),
  60. {
  61. ...NO_CUSTOM_NODES_PRESET,
  62. controlFlowFlattening: true,
  63. controlFlowFlatteningThreshold: 1
  64. }
  65. );
  66. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  67. const controlFlowStorageCallRegExp: RegExp = /var *_0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4,6}\['\\x73\\x75\\x6d'\]\(0x1, *0x2\);/;
  68. it('shouldn\'t replace call expression node by call to control flow storage node if call expression callee is member expression node', () => {
  69. assert.match(obfuscatedCode, controlFlowStorageCallRegExp);
  70. });
  71. });
  72. });
  73. });