VariableDeclarationObfuscator.spec.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import { assert } from 'chai';
  2. import { IObfuscationResult } from '../../../../src/interfaces/IObfuscationResult';
  3. import { NO_CUSTOM_NODES_PRESET } from '../../../../src/preset-options/NoCustomNodesPreset';
  4. import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscator';
  5. describe('VariableDeclarationObfuscator', () => {
  6. it('should obfuscate `variableDeclaration` node', () => {
  7. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  8. `
  9. function foo () {
  10. var test = 'abc';
  11. console.log(test);
  12. }
  13. `,
  14. {
  15. ...NO_CUSTOM_NODES_PRESET
  16. }
  17. );
  18. assert.match(obfuscationResult.getObfuscatedCode(), /var *_0x([a-z0-9]){4,6} *= *'\\x61\\x62\\x63';/);
  19. assert.match(obfuscationResult.getObfuscatedCode(), /console\['\\x6c\\x6f\\x67'\]\(_0x([a-z0-9]){4,6}\);/);
  20. });
  21. it('should not obfuscate `variableDeclaration` node if parent block scope node is `Program` node', () => {
  22. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  23. `
  24. if (true) {
  25. var test = 10;
  26. }
  27. console.log(test);
  28. `,
  29. {
  30. ...NO_CUSTOM_NODES_PRESET
  31. }
  32. );
  33. assert.match(obfuscationResult.getObfuscatedCode(), /var *test *= *0xa;/);
  34. assert.match(obfuscationResult.getObfuscatedCode(), /console\['\\x6c\\x6f\\x67'\]\(test\);/);
  35. });
  36. it('should obfuscate variable call (`identifier` node) outside of block scope of node in which this variable was declared with `var` kind', () => {
  37. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  38. `
  39. (function () {
  40. if (true) {
  41. var test = 10;
  42. }
  43. console.log(test);
  44. })();
  45. `,
  46. {
  47. ...NO_CUSTOM_NODES_PRESET
  48. }
  49. );
  50. assert.match(obfuscationResult.getObfuscatedCode(), /console\['\\x6c\\x6f\\x67'\]\(_0x([a-z0-9]){4,6}\);/);
  51. });
  52. it('should not obfuscate variable call (`identifier` node) outside of block scope of node in which this variable was declared with `let` kind', () => {
  53. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  54. `
  55. (function () {
  56. if (true) {
  57. let test = 10;
  58. }
  59. console.log(test);
  60. })();
  61. `,
  62. {
  63. ...NO_CUSTOM_NODES_PRESET
  64. }
  65. );
  66. assert.match(obfuscationResult.getObfuscatedCode(), /console\['\\x6c\\x6f\\x67'\]\(test\);/);
  67. });
  68. describe(`variable calls before variable declaration`, () => {
  69. let obfuscationResult: IObfuscationResult;
  70. beforeEach(() => {
  71. obfuscationResult = JavaScriptObfuscator.obfuscate(
  72. `
  73. function foo () {
  74. function bar () {
  75. console.log(abc.item);
  76. }
  77. console.log(abc);
  78. var abc = {};
  79. abc.item = 15;
  80. bar();
  81. }
  82. `,
  83. {
  84. ...NO_CUSTOM_NODES_PRESET
  85. }
  86. );
  87. });
  88. it('should obfuscate variable call (`identifier` node) before variable declaration if this call is inside function body', () => {
  89. assert.match(obfuscationResult.getObfuscatedCode(), /console\['\\x6c\\x6f\\x67'\]\(_0x([a-z0-9]){4,6}\['\\x69\\x74\\x65\\x6d'\]\);/);
  90. });
  91. it('should not obfuscate variable call (`identifier` node) before variable declaration', () => {
  92. assert.match(obfuscationResult.getObfuscatedCode(), /console\['\\x6c\\x6f\\x67'\]\(_0x([a-z0-9]){4,6}\);/);
  93. });
  94. });
  95. describe(`variable calls before variable declaration when function param has the same name as variables name`, () => {
  96. let obfuscationResult: IObfuscationResult,
  97. functionParamIdentifierName: string|null,
  98. innerFunctionParamIdentifierName: string|null,
  99. constructorIdentifierName: string|null,
  100. objectIdentifierName: string|null,
  101. variableDeclarationIdentifierName: string|null;
  102. beforeEach(() => {
  103. obfuscationResult = JavaScriptObfuscator.obfuscate(
  104. `
  105. (function () {
  106. function foo (t, e) {
  107. return function () {
  108. function baz (t) {
  109. console.log(t);
  110. }
  111. return {t: t};
  112. var t;
  113. }();
  114. }
  115. })();
  116. `,
  117. {
  118. ...NO_CUSTOM_NODES_PRESET
  119. }
  120. );
  121. });
  122. it('should correct obfuscate variables inside function body', () => {
  123. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  124. const functionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  125. .match(/function *_0x[a-z0-9]{4,6} *\((_0x[a-z0-9]{4,6})\,(_0x[a-z0-9]{4,6})\) *\{/);
  126. const innerFunctionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  127. .match(/function _0x[a-z0-9]{4,6} *\((_0x[a-z0-9]{4,6})\) *\{/);
  128. const constructorIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  129. .match(/console\['\\x6c\\x6f\\x67'\]\((_0x[a-z0-9]{4,6})\)/);
  130. const objectIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  131. .match(/return\{'\\x74':(_0x[a-z0-9]{4,6})\}/);
  132. const variableDeclarationIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  133. .match(/var *(_0x[a-z0-9]{4,6});/);
  134. functionParamIdentifierName = (<RegExpMatchArray>functionParamIdentifierMatch)[1];
  135. innerFunctionParamIdentifierName = (<RegExpMatchArray>innerFunctionParamIdentifierMatch)[1];
  136. constructorIdentifierName = (<RegExpMatchArray>constructorIdentifierMatch)[1];
  137. objectIdentifierName = (<RegExpMatchArray>objectIdentifierMatch)[1];
  138. variableDeclarationIdentifierName = (<RegExpMatchArray>variableDeclarationIdentifierMatch)[1];
  139. assert.notEqual(functionParamIdentifierName, constructorIdentifierName);
  140. assert.notEqual(functionParamIdentifierName, innerFunctionParamIdentifierName);
  141. assert.equal(functionParamIdentifierName, objectIdentifierName);
  142. assert.equal(functionParamIdentifierName, variableDeclarationIdentifierName);
  143. assert.equal(innerFunctionParamIdentifierName, constructorIdentifierName);
  144. assert.equal(variableDeclarationIdentifierName, objectIdentifierName);
  145. });
  146. });
  147. describe('wrong replacement', () => {
  148. it('shouldn\'t replace property node identifier', () => {
  149. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  150. `
  151. function foo () {
  152. var test = 'abc';
  153. var object = {
  154. test: 'cde'
  155. };
  156. console.log(test);
  157. }
  158. `,
  159. {
  160. ...NO_CUSTOM_NODES_PRESET
  161. }
  162. );
  163. assert.match(obfuscationResult.getObfuscatedCode(), /var _0x([a-z0-9]){4,6} *= *\{'\\x74\\x65\\x73\\x74/);
  164. });
  165. it('shouldn\'t replace computed member expression identifier', () => {
  166. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  167. `
  168. function foo () {
  169. var test = 'abc';
  170. var object = {
  171. 'test': 'cde'
  172. };
  173. console.log(test);
  174. console.log(object.test);
  175. }
  176. `,
  177. {
  178. ...NO_CUSTOM_NODES_PRESET
  179. }
  180. );
  181. assert.match(obfuscationResult.getObfuscatedCode(), /_0x([a-z0-9]){4,6}\['\\x74\\x65\\x73\\x74'\]/);
  182. });
  183. });
  184. });