VariableDeclarationObfuscator.spec.ts 7.8 KB

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