LogicalExpressionControlFlowReplacer.spec.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { assert } from 'chai';
  2. import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../../src/options/presets/NoCustomNodes';
  3. import { readFileAsString } from '../../../../../helpers/readFileAsString';
  4. import { JavaScriptObfuscator } from '../../../../../../src/JavaScriptObfuscatorFacade';
  5. describe('LogicalExpressionControlFlowReplacer', function () {
  6. this.timeout(100000);
  7. describe('replace', () => {
  8. describe('Variant #1 - single logical expression', () => {
  9. const controlFlowStorageCallRegExp: RegExp = /var _0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4,6}\['\w{5}'\]\(!!\[\], *!\[\]\);/;
  10. let obfuscatedCode: string;
  11. before(() => {
  12. const code: string = readFileAsString(__dirname + '/fixtures/input-1.js');
  13. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  14. code,
  15. {
  16. ...NO_ADDITIONAL_NODES_PRESET,
  17. controlFlowFlattening: true,
  18. controlFlowFlatteningThreshold: 1
  19. }
  20. ).getObfuscatedCode();
  21. });
  22. it('should replace logical expression node with call to control flow storage node', () => {
  23. assert.match(obfuscatedCode, controlFlowStorageCallRegExp);
  24. });
  25. });
  26. describe('Variant #2 - multiple logical expressions with threshold = 1', () => {
  27. const expectedMatchErrorsCount: number = 0;
  28. const expectedChance: number = 0.5;
  29. const samplesCount: number = 1000;
  30. const delta: number = 0.1;
  31. const controlFlowStorageCallRegExp1: RegExp = /var _0x(?:[a-f0-9]){4,6} *= *(_0x([a-f0-9]){4,6}\['\w{5}'\])\(!!\[\], *!\[\]\);/;
  32. const controlFlowStorageCallRegExp2: RegExp = /var _0x(?:[a-f0-9]){4,6} *= *(_0x([a-f0-9]){4,6}\['\w{5}'\])\(!\[\], *!!\[\]\);/;
  33. let matchErrorsCount: number = 0,
  34. usingExistingIdentifierChance: number;
  35. before(() => {
  36. const code: string = readFileAsString(__dirname + '/fixtures/input-2.js');
  37. let obfuscatedCode: string,
  38. firstMatchArray: RegExpMatchArray | null,
  39. secondMatchArray: RegExpMatchArray | null,
  40. firstMatch: string | undefined,
  41. secondMatch: string | undefined,
  42. equalsValue: number = 0;
  43. for (let i = 0; i < samplesCount; i++) {
  44. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  45. code,
  46. {
  47. ...NO_ADDITIONAL_NODES_PRESET,
  48. controlFlowFlattening: true,
  49. controlFlowFlatteningThreshold: 1
  50. }
  51. ).getObfuscatedCode();
  52. firstMatchArray = obfuscatedCode.match(controlFlowStorageCallRegExp1);
  53. secondMatchArray = obfuscatedCode.match(controlFlowStorageCallRegExp2);
  54. if (!firstMatchArray || !secondMatchArray) {
  55. matchErrorsCount++;
  56. continue;
  57. }
  58. firstMatch = firstMatchArray ? firstMatchArray[1] : undefined;
  59. secondMatch = secondMatchArray ? secondMatchArray[1] : undefined;
  60. if (firstMatch === secondMatch) {
  61. equalsValue++;
  62. }
  63. }
  64. usingExistingIdentifierChance = equalsValue / samplesCount;
  65. });
  66. it('should replace logical expression node by call to control flow storage node', () => {
  67. assert.equal(matchErrorsCount, expectedMatchErrorsCount);
  68. });
  69. it('should use existing identifier for control flow storage with expected chance', () => {
  70. assert.closeTo(usingExistingIdentifierChance, expectedChance, delta);
  71. });
  72. });
  73. describe('Variant #3 - single logical expression with unary expression', () => {
  74. const controlFlowStorageCallRegExp: RegExp = /var _0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4,6}\['\w{5}'\]\(!_0x([a-f0-9]){4,6}, *!_0x([a-f0-9]){4,6}\);/;
  75. let obfuscatedCode: string;
  76. before(() => {
  77. const code: string = readFileAsString(__dirname + '/fixtures/input-3.js');
  78. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  79. code,
  80. {
  81. ...NO_ADDITIONAL_NODES_PRESET,
  82. controlFlowFlattening: true,
  83. controlFlowFlatteningThreshold: 1
  84. }
  85. ).getObfuscatedCode();
  86. });
  87. it('should replace logical unary expression with call to control flow storage node', () => {
  88. assert.match(obfuscatedCode, controlFlowStorageCallRegExp);
  89. });
  90. });
  91. describe('prohibited nodes Variant #1', () => {
  92. const regExp: RegExp = /var _0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4,6}\[_0x([a-f0-9]){4,6}\] *&& *!\[\];/;
  93. let obfuscatedCode: string;
  94. before(() => {
  95. const code: string = readFileAsString(__dirname + '/fixtures/prohibited-nodes.js');
  96. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  97. code,
  98. {
  99. ...NO_ADDITIONAL_NODES_PRESET,
  100. controlFlowFlattening: true,
  101. controlFlowFlatteningThreshold: .1
  102. }
  103. ).getObfuscatedCode();
  104. });
  105. it('shouldn\'t replace prohibited expression nodes', () => {
  106. assert.match(obfuscatedCode, regExp);
  107. });
  108. });
  109. });
  110. });