CallExpressionControlFlowReplacer.spec.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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('CallExpressionControlFlowReplacer', function () {
  6. this.timeout(100000);
  7. describe('replace', () => {
  8. describe('Variant #1 - single call expression', () => {
  9. const controlFlowStorageCallRegExp: RegExp = /var _0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4,6}\['\w{5}'\]\(_0x([a-f0-9]){4,6}, *0x1, *0x2\);/;
  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 call expression node with call to control flow storage node', () => {
  23. assert.match(obfuscatedCode, controlFlowStorageCallRegExp);
  24. });
  25. });
  26. describe('Variant #2 - multiple call 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}'\])\(_0x([a-f0-9]){4,6}, *0x1, *0x2\);/;
  32. const controlFlowStorageCallRegExp2: RegExp = /var _0x(?:[a-f0-9]){4,6} *= *(_0x([a-f0-9]){4,6}\['\w{5}'\])\(_0x([a-f0-9]){4,6}, *0x2, *0x3\);/;
  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 call 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 - call expression callee is member expression node', () => {
  74. const regExp: RegExp = /var _0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4,6}\['sum'\]\(0x1, *0x2\);/;
  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('shouldn\'t replace call expression node with call to control flow storage node', () => {
  88. assert.match(obfuscatedCode, regExp);
  89. });
  90. });
  91. describe('Variant #4 - rest as start call argument', () => {
  92. const controlFlowStorageCallRegExp: RegExp = /_0x([a-f0-9]){4,6}\['\w{5}']\(_0x([a-f0-9]){4,6}, *\.\.\._0x([a-f0-9]){4,6}, *_0x([a-f0-9]){4,6}\);/;
  93. const controlFlowStorageNodeRegExp: RegExp = new RegExp(`` +
  94. `'\\w{5}' *: *function *\\(_0x([a-f0-9]){4,6}, *\.\.\._0x([a-f0-9]){4,6}\\) *\\{` +
  95. `return *_0x([a-f0-9]){4,6}\\(\.\.\._0x([a-f0-9]){4,6}\\);` +
  96. `\\}` +
  97. ``);
  98. let obfuscatedCode: string;
  99. before(() => {
  100. const code: string = readFileAsString(__dirname + '/fixtures/rest-as-start-call-argument.js');
  101. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  102. code,
  103. {
  104. ...NO_ADDITIONAL_NODES_PRESET,
  105. controlFlowFlattening: true,
  106. controlFlowFlatteningThreshold: 1
  107. }
  108. ).getObfuscatedCode();
  109. });
  110. it('should replace call expression node with call to control flow storage node', () => {
  111. assert.match(obfuscatedCode, controlFlowStorageCallRegExp);
  112. });
  113. it('should keep rest parameter and rest call argument, but remove all function parameters after rest parameter', () => {
  114. assert.match(obfuscatedCode, controlFlowStorageNodeRegExp);
  115. });
  116. });
  117. describe('Variant #5 - rest as middle call argument', () => {
  118. const controlFlowStorageCallRegExp: RegExp = /_0x([a-f0-9]){4,6}\['\w{5}']\(_0x([a-f0-9]){4,6}, *_0x([a-f0-9]){4,6}, *\.\.\._0x([a-f0-9]){4,6}, *_0x([a-f0-9]){4,6}\);/;
  119. const controlFlowStorageNodeRegExp: RegExp = new RegExp(`` +
  120. `'\\w{5}' *: *function *\\(_0x([a-f0-9]){4,6}, *_0x([a-f0-9]){4,6}, *\.\.\._0x([a-f0-9]){4,6}\\) *\\{` +
  121. `return *_0x([a-f0-9]){4,6}\\(_0x([a-f0-9]){4,6}, *\.\.\._0x([a-f0-9]){4,6}\\);` +
  122. `\\}` +
  123. ``);
  124. let obfuscatedCode: string;
  125. before(() => {
  126. const code: string = readFileAsString(__dirname + '/fixtures/rest-as-middle-call-argument.js');
  127. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  128. code,
  129. {
  130. ...NO_ADDITIONAL_NODES_PRESET,
  131. controlFlowFlattening: true,
  132. controlFlowFlatteningThreshold: 1
  133. }
  134. ).getObfuscatedCode();
  135. });
  136. it('should replace call expression node with call to control flow storage node', () => {
  137. assert.match(obfuscatedCode, controlFlowStorageCallRegExp);
  138. });
  139. it('should keep rest parameter and rest call argument, but remove all function parameters after rest parameter', () => {
  140. assert.match(obfuscatedCode, controlFlowStorageNodeRegExp);
  141. });
  142. });
  143. describe('Variant #6 - rest as last call argument', () => {
  144. const controlFlowStorageCallRegExp: RegExp = /_0x([a-f0-9]){4,6}\['\w{5}']\(_0x([a-f0-9]){4,6}, *_0x([a-f0-9]){4,6}, *\.\.\._0x([a-f0-9]){4,6}\);/;
  145. const controlFlowStorageNodeRegExp: RegExp = new RegExp(`` +
  146. `'\\w{5}' *: *function *\\(_0x([a-f0-9]){4,6}, *_0x([a-f0-9]){4,6}, *\.\.\._0x([a-f0-9]){4,6}\\) *\\{` +
  147. `return *_0x([a-f0-9]){4,6}\\(_0x([a-f0-9]){4,6}, *\.\.\._0x([a-f0-9]){4,6}\\);` +
  148. `\\}` +
  149. ``);
  150. let obfuscatedCode: string;
  151. before(() => {
  152. const code: string = readFileAsString(__dirname + '/fixtures/rest-as-last-call-argument.js');
  153. obfuscatedCode = JavaScriptObfuscator.obfuscate(
  154. code,
  155. {
  156. ...NO_ADDITIONAL_NODES_PRESET,
  157. controlFlowFlattening: true,
  158. controlFlowFlatteningThreshold: 1
  159. }
  160. ).getObfuscatedCode();
  161. });
  162. it('should replace call expression node with call to control flow storage node', () => {
  163. assert.match(obfuscatedCode, controlFlowStorageCallRegExp);
  164. });
  165. it('should keep rest parameter and rest call argument inside control flow storage node function', () => {
  166. assert.match(obfuscatedCode, controlFlowStorageNodeRegExp);
  167. });
  168. });
  169. });
  170. });