LiteralObfuscator.spec.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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('LiteralObfuscator', () => {
  7. describe('obfuscation of literal node with string value', () => {
  8. it('should replace literal node value with unicode escape sequence', () => {
  9. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  10. `var test = 'test';`,
  11. {
  12. ...NO_CUSTOM_NODES_PRESET
  13. }
  14. );
  15. assert.match(obfuscationResult.getObfuscatedCode(), /^var *test *= *'\\x74\\x65\\x73\\x74';$/);
  16. });
  17. it('should replace literal node value with unicode escape sequence from string array', () => {
  18. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  19. `var test = 'test';`,
  20. {
  21. ...NO_CUSTOM_NODES_PRESET,
  22. stringArray: true,
  23. stringArrayThreshold: 1
  24. }
  25. );
  26. assert.match(
  27. obfuscationResult.getObfuscatedCode(),
  28. /^var *_0x([a-z0-9]){4} *= *\['\\x74\\x65\\x73\\x74'\];/
  29. );
  30. assert.match(obfuscationResult.getObfuscatedCode(), /var *test *= *_0x([a-z0-9]){4}\('0x0'\);/);
  31. });
  32. it('should replace literal node value with raw value from string array if `unicodeEscapeSequence` is disabled', () => {
  33. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  34. `var test = 'test';`,
  35. {
  36. ...NO_CUSTOM_NODES_PRESET,
  37. stringArray: true,
  38. stringArrayThreshold: 1,
  39. unicodeEscapeSequence: false
  40. }
  41. );
  42. assert.match(
  43. obfuscationResult.getObfuscatedCode(),
  44. /^var *_0x([a-z0-9]){4} *= *\['test'\];/
  45. );
  46. assert.match(obfuscationResult.getObfuscatedCode(), /var *test *= *_0x([a-z0-9]){4}\('0x0'\);/);
  47. });
  48. it('should replace literal node value with raw value from string array if `unicodeEscapeSequence` and `stringArray` are disabled', () => {
  49. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  50. `var test = 'test';`,
  51. {
  52. ...NO_CUSTOM_NODES_PRESET,
  53. unicodeEscapeSequence: false
  54. }
  55. );
  56. assert.match(
  57. obfuscationResult.getObfuscatedCode(),
  58. /^var *test *= *'test';/
  59. );
  60. });
  61. it('should\'t throw an error when string contains non-latin and non-digit characters and `unicodeEscapeSequence` is disabled', () => {
  62. assert.doesNotThrow(() => JavaScriptObfuscator.obfuscate(
  63. readFileAsString('./test/fixtures/node-transformers/node-obfuscators/literal-obfuscator/literal-obfuscator-unicode-sequence.js'),
  64. {
  65. ...NO_CUSTOM_NODES_PRESET,
  66. stringArray: true,
  67. stringArrayThreshold: 1,
  68. unicodeEscapeSequence: false
  69. }
  70. ));
  71. });
  72. it('shouldn\'t replace short literal node value with value from string array', () => {
  73. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  74. `var test = 'te';`,
  75. {
  76. ...NO_CUSTOM_NODES_PRESET,
  77. stringArray: true,
  78. stringArrayThreshold: 1
  79. }
  80. );
  81. assert.match(obfuscationResult.getObfuscatedCode(), /var *test *= *'\\x74\\x65';/);
  82. });
  83. it('should replace literal node value with value from string array encoded using base64', () => {
  84. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  85. `var test = 'test';`,
  86. {
  87. ...NO_CUSTOM_NODES_PRESET,
  88. stringArray: true,
  89. stringArrayEncoding: 'base64',
  90. stringArrayThreshold: 1
  91. }
  92. );
  93. assert.match(
  94. obfuscationResult.getObfuscatedCode(),
  95. /^var *_0x([a-z0-9]){4} *= *\['\\x64\\x47\\x56\\x7a\\x64\\x41\\x3d\\x3d'\];/
  96. );
  97. assert.match(obfuscationResult.getObfuscatedCode(), /var *test *= *_0x([a-z0-9]){4}\('0x0'\);/);
  98. });
  99. it('should replace literal node value with value from string array encoded using rc4', () => {
  100. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  101. `var test = 'test';`,
  102. {
  103. ...NO_CUSTOM_NODES_PRESET,
  104. stringArray: true,
  105. stringArrayEncoding: 'rc4',
  106. stringArrayThreshold: 1
  107. }
  108. );
  109. assert.match(
  110. obfuscationResult.getObfuscatedCode(),
  111. /var *test *= *_0x([a-z0-9]){4}\('0x0', '(\\x[a-f0-9]*){4}'\);/
  112. );
  113. });
  114. it('should replace literal node value with value from string array with `stringArrayThreshold` chance', () => {
  115. const samples: number = 1000;
  116. const stringArrayThreshold: number = 0.5;
  117. const delta: number = 0.1;
  118. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  119. `var test = 'test';\n`.repeat(samples),
  120. {
  121. ...NO_CUSTOM_NODES_PRESET,
  122. stringArray: true,
  123. stringArrayThreshold: stringArrayThreshold
  124. }
  125. );
  126. const regExp1: RegExp = /var *test *= *_0x([a-z0-9]){4}\('0x0'\);/g;
  127. const regExp2: RegExp = /var *test *= *'\\x74\\x65\\x73\\x74';/g;
  128. const stringArrayMatchesLength = obfuscationResult.getObfuscatedCode().match(regExp1)!.length;
  129. const noStringArrayMatchesLength = obfuscationResult.getObfuscatedCode().match(regExp2)!.length;
  130. assert.closeTo(stringArrayMatchesLength / samples, stringArrayThreshold, delta);
  131. assert.closeTo(noStringArrayMatchesLength / samples, stringArrayThreshold, delta);
  132. });
  133. });
  134. it('should obfuscate literal node with boolean value', () => {
  135. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  136. `var test = true;`,
  137. {
  138. ...NO_CUSTOM_NODES_PRESET,
  139. stringArray: true,
  140. stringArrayThreshold: 1
  141. }
  142. );
  143. assert.match(obfuscationResult.getObfuscatedCode(), /^var *test *= *!!\[\];$/);
  144. });
  145. it('should obfuscate literal node with number value', () => {
  146. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  147. `var test = 0;`,
  148. {
  149. ...NO_CUSTOM_NODES_PRESET,
  150. stringArray: true,
  151. stringArrayThreshold: 1
  152. }
  153. );
  154. assert.match(obfuscationResult.getObfuscatedCode(), /^var *test *= *0x0;$/);
  155. });
  156. });