LogicalExpressionControlFlowReplacer.spec.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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}\['(\\x[a-f0-9]*){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(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}'\])\(!!\[\], *!\[\]\);/;
  41. const controlFlowStorageCallRegExp2: RegExp = /var *_0x([a-f0-9]){4,6} *= *(_0x([a-f0-9]){4,6}\['(\\x[a-f0-9]*){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. console.log(equalsValue / samplesCount);
  54. });
  55. });
  56. describe('variant #3 - single logical expression with unary expression', () => {
  57. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  58. readFileAsString(__dirname + '/fixtures/input-3.js'),
  59. {
  60. ...NO_CUSTOM_NODES_PRESET,
  61. controlFlowFlattening: true,
  62. controlFlowFlatteningThreshold: 1
  63. }
  64. );
  65. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  66. 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}, *!_0x([a-f0-9]){4,6}\);/;
  67. it('should replace logical expression node with unary expression by call to control flow storage node', () => {
  68. assert.match(obfuscatedCode, controlFlowStorageCallRegExp);
  69. });
  70. });
  71. describe('prohibited nodes variant #1', () => {
  72. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  73. readFileAsString(__dirname + '/fixtures/prohibited-nodes.js'),
  74. {
  75. ...NO_CUSTOM_NODES_PRESET,
  76. controlFlowFlattening: true,
  77. controlFlowFlatteningThreshold: .1
  78. }
  79. );
  80. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  81. const regExp: RegExp = /var *_0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4,6}\[_0x([a-f0-9]){4,6}\] *&& *!\[\];/;
  82. it('shouldn\'t replace prohibited expression nodes', () => {
  83. assert.match(obfuscatedCode, regExp);
  84. });
  85. });
  86. });
  87. });