VariableDeclarationObfuscator.spec.ts 6.4 KB

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