LiteralObfuscator.spec.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 unicode 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 unicode 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 unicode 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 unicode array value', () => {
  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 unicode array value 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 unicode array value 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. });
  115. it('should obfuscate literal node with boolean value', () => {
  116. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  117. `var test = true;`,
  118. {
  119. ...NO_CUSTOM_NODES_PRESET,
  120. stringArray: true,
  121. stringArrayThreshold: 1
  122. }
  123. );
  124. assert.match(obfuscationResult.getObfuscatedCode(), /^var *test *= *!!\[\];$/);
  125. });
  126. it('should obfuscate literal node with number value', () => {
  127. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  128. `var test = 0;`,
  129. {
  130. ...NO_CUSTOM_NODES_PRESET,
  131. stringArray: true,
  132. stringArrayThreshold: 1
  133. }
  134. );
  135. assert.match(obfuscationResult.getObfuscatedCode(), /^var *test *= *0x0;$/);
  136. });
  137. });