LogicalExpressionControlFlowReplacer.spec.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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('LogicalExpressionControlFlowReplacer', () => {
  7. describe('replace (logicalExpressionNode: ESTree.LogicalExpression,parentNode: ESTree.Node,controlFlowStorage: IStorage <ICustomNode>)', () => {
  8. describe('variant #1 - single logical 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}\['\w{3}'\]\(!!\[\], *!\[\]\);/;
  19. it('should replace logical expression node by call to control flow storage node', () => {
  20. assert.match(obfuscatedCode, controlFlowStorageCallRegExp);
  21. });
  22. });
  23. describe('variant #2 - multiple logical expressions with threshold = 1', () => {
  24. it('should replace logical expression node by call to control flow storage node', function () {
  25. this.timeout(60000);
  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}\['\w{3}'\])\(!!\[\], *!\[\]\);/;
  41. const controlFlowStorageCallRegExp2: RegExp = /var *_0x([a-f0-9]){4,6} *= *(_0x([a-f0-9]){4,6}\['\w{3}'\])\(!\[\], *!!\[\]\);/;
  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. if (firstMatch === secondMatch) {
  49. equalsValue++;
  50. }
  51. }
  52. assert.closeTo(equalsValue / samplesCount, expectedValue, delta);
  53. });
  54. });
  55. describe('variant #3 - single logical expression with unary expression', () => {
  56. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  57. readFileAsString(__dirname + '/fixtures/input-3.js'),
  58. {
  59. ...NO_CUSTOM_NODES_PRESET,
  60. controlFlowFlattening: true,
  61. controlFlowFlatteningThreshold: 1
  62. }
  63. );
  64. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  65. const controlFlowStorageCallRegExp: RegExp = /var *_0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4,6}\['\w{3}'\]\(!_0x([a-f0-9]){4,6}, *!_0x([a-f0-9]){4,6}\);/;
  66. it('should replace logical expression node with unary expression by call to control flow storage node', () => {
  67. assert.match(obfuscatedCode, controlFlowStorageCallRegExp);
  68. });
  69. });
  70. describe('prohibited nodes variant #1', () => {
  71. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  72. readFileAsString(__dirname + '/fixtures/prohibited-nodes.js'),
  73. {
  74. ...NO_CUSTOM_NODES_PRESET,
  75. controlFlowFlattening: true,
  76. controlFlowFlatteningThreshold: .1
  77. }
  78. );
  79. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  80. const regExp: RegExp = /var *_0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4,6}\[_0x([a-f0-9]){4,6}\] *&& *!\[\];/;
  81. it('shouldn\'t replace prohibited expression nodes', () => {
  82. assert.match(obfuscatedCode, regExp);
  83. });
  84. });
  85. });
  86. });