VariableDeclarationObfuscator.spec.ts 8.5 KB

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