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