FunctionControlFlowTransformer.spec.ts 3.9 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('FunctionControlFlowTransformer', () => {
  7. const variableMatch: string = '_0x([a-z0-9]){4,6}';
  8. const rootControlFlowStorageNodeMatch: string = `` +
  9. `var *${variableMatch} *= *\\{` +
  10. `'(\\\\x[a-f0-9]*){3}' *: *function *${variableMatch} *\\(${variableMatch}, *${variableMatch}\\) *\\{` +
  11. `return *${variableMatch} *\\+ *${variableMatch};` +
  12. `\\}` +
  13. `\\};` +
  14. ``;
  15. const innerControlFlowStorageNodeMatch: string = `` +
  16. `var *${variableMatch} *= *\\{` +
  17. `'(\\\\x[a-f0-9]*){3}' *: *function *${variableMatch} *\\(${variableMatch}, *${variableMatch}\\) *\\{` +
  18. `return *${variableMatch}\\['(\\\\x[a-f0-9]*){3}'\\]\\(${variableMatch}, *${variableMatch}\\);` +
  19. `\\}` +
  20. `\\};` +
  21. ``;
  22. describe('transformNode (functionNode: ESTree.Function): ESTree.Node', () => {
  23. describe('variant #1 - single `control flow storage` node with single item', () => {
  24. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  25. readFileAsString(
  26. './test/fixtures/node-transformers/node-control-flow-transformers/function-control-flow-transformer-1.js'
  27. ),
  28. {
  29. ...NO_CUSTOM_NODES_PRESET,
  30. controlFlowFlattening: true,
  31. controlFlowFlatteningThreshold: 1
  32. }
  33. );
  34. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  35. const regexp: RegExp = new RegExp(rootControlFlowStorageNodeMatch);
  36. it('should add `control flow storage` node to the obfuscated code', () => {
  37. assert.match(obfuscatedCode, regexp);
  38. });
  39. });
  40. describe('variant #2 - two `control flow storage` nodes: root and inner', () => {
  41. const samplesCount: number = 150;
  42. const delta: number = 0.1;
  43. const expectedValue: number = 0.5;
  44. const regExp1: RegExp = new RegExp(
  45. `\\(function\\(\\) *\\{ *${rootControlFlowStorageNodeMatch}`,
  46. 'g'
  47. );
  48. const regExp2: RegExp = new RegExp(
  49. `function *${variableMatch} *\\(\\) *\\{ *${innerControlFlowStorageNodeMatch}`,
  50. 'g'
  51. );
  52. let totalValue: number = 0;
  53. for (let i = 0; i < samplesCount; i++) {
  54. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  55. readFileAsString(
  56. './test/fixtures/node-transformers/node-control-flow-transformers/function-control-flow-transformer-2.js'
  57. ),
  58. {
  59. ...NO_CUSTOM_NODES_PRESET,
  60. controlFlowFlattening: true,
  61. controlFlowFlatteningThreshold: 1
  62. }
  63. );
  64. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  65. if (regExp1.test(obfuscatedCode)) {
  66. totalValue += obfuscatedCode.match(regExp1)!.length;
  67. if (regExp2.test(obfuscatedCode)) {
  68. totalValue += obfuscatedCode.match(regExp2)!.length;
  69. }
  70. }
  71. }
  72. it('should add two `control flow storage` nodes (root and inner) to the obfuscated code in different scopes', () => {
  73. assert.closeTo((totalValue - samplesCount) / samplesCount, expectedValue, delta);
  74. });
  75. });
  76. });
  77. });