BinaryExpressionControlFlowReplacer.spec.ts 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { assert } from 'chai';
  2. import { IObfuscationResult } from '../../../../../src/interfaces/IObfuscationResult';
  3. import { NO_CUSTOM_NODES_PRESET } from '../../../../../src/preset-options/NoCustomNodesPreset';
  4. import { readFileAsString } from '../../../../helpers/readFileAsString';
  5. import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscator';
  6. describe('BinaryExpressionControlFlowReplacer', () => {
  7. describe('replace (binaryExpressionNode: ESTree.BinaryExpression,parentNode: ESTree.Node,controlFlowStorage: IStorage <ICustomNode>)', () => {
  8. describe('variant #1 - single binary expression', () => {
  9. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  10. readFileAsString(
  11. './test/fixtures/node-transformers/node-control-flow-transformers/control-flow-replacers/binary-expression-control-flow-replacer-1.js'
  12. ),
  13. {
  14. ...NO_CUSTOM_NODES_PRESET,
  15. controlFlowFlattening: true,
  16. controlFlowFlatteningThreshold: 1
  17. }
  18. );
  19. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  20. const controlFlowStorageCallRegExp: RegExp = /var *_0x([a-z0-9]){4,6} *= *_0x([a-z0-9]){4,6}\['(\\x[a-f0-9]*){3}'\]\(0x1, *0x2\);/;
  21. it('should replace binary expression node by call to control flow storage node', () => {
  22. assert.match(obfuscatedCode, controlFlowStorageCallRegExp);
  23. });
  24. });
  25. describe('variant #2 - multiple binary expressions with threshold = 1', () => {
  26. it('should replace binary expression node by call to control flow storage node', () => {
  27. const samplesCount: number = 50;
  28. let equalsValue: number = 0;
  29. for (let i = 0; i < samplesCount; i++) {
  30. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  31. readFileAsString(
  32. './test/fixtures/node-transformers/node-control-flow-transformers/control-flow-replacers/binary-expression-control-flow-replacer-2.js'
  33. ),
  34. {
  35. ...NO_CUSTOM_NODES_PRESET,
  36. controlFlowFlattening: true,
  37. controlFlowFlatteningThreshold: 1
  38. }
  39. );
  40. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  41. const controlFlowStorageCallRegExp1: RegExp = /var *_0x([a-z0-9]){4,6} *= *(_0x([a-z0-9]){4,6}\['(\\x[a-f0-9]*){3}'\])\(0x1, *0x2\);/;
  42. const controlFlowStorageCallRegExp2: RegExp = /var *_0x([a-z0-9]){4,6} *= *(_0x([a-z0-9]){4,6}\['(\\x[a-f0-9]*){3}'\])\(0x2, *0x3\);/;
  43. const firstMatchArray: RegExpMatchArray | null = obfuscatedCode.match(controlFlowStorageCallRegExp1);
  44. const secondMatchArray: RegExpMatchArray | null = obfuscatedCode.match(controlFlowStorageCallRegExp2);
  45. const firstMatch: string | undefined = firstMatchArray ? firstMatchArray[2] : undefined;
  46. const secondMatch: string | undefined = secondMatchArray ? secondMatchArray[2] : undefined;
  47. assert.match(obfuscatedCode, controlFlowStorageCallRegExp1);
  48. assert.match(obfuscatedCode, controlFlowStorageCallRegExp2);
  49. assert.isOk(firstMatch);
  50. assert.isOk(secondMatch);
  51. if (firstMatch === secondMatch) {
  52. equalsValue++;
  53. }
  54. }
  55. assert.closeTo(equalsValue / samplesCount, 0.5, 0.05);
  56. });
  57. });
  58. });
  59. });