VariableDeclarationObfuscator.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  87. `
  88. (function () {
  89. function foo (t, e) {
  90. return function () {
  91. function baz (t) {
  92. console.log(t);
  93. }
  94. return {t: t};
  95. var t;
  96. }();
  97. }
  98. })();
  99. `,
  100. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  101. );
  102. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  103. const functionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  104. .match(/function *_0x[a-f0-9]{4,6} *\((_0x[a-f0-9]{4,6})\,(_0x[a-f0-9]{4,6})\) *\{/);
  105. const innerFunctionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  106. .match(/function _0x[a-f0-9]{4,6} *\((_0x[a-f0-9]{4,6})\) *\{/);
  107. const constructorIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  108. .match(/console\['\\x6c\\x6f\\x67'\]\((_0x[a-f0-9]{4,6})\)/);
  109. const objectIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  110. .match(/return\{'\\x74':(_0x[a-f0-9]{4,6})\}/);
  111. const variableDeclarationIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  112. .match(/var *(_0x[a-f0-9]{4,6});/);
  113. const outerFunctionParamIdentifierName: string|null = (<RegExpMatchArray>functionParamIdentifierMatch)[1];
  114. const innerFunctionParamIdentifierName: string|null = (<RegExpMatchArray>innerFunctionParamIdentifierMatch)[1];
  115. const constructorIdentifierName: string|null = (<RegExpMatchArray>constructorIdentifierMatch)[1];
  116. const objectIdentifierName: string|null = (<RegExpMatchArray>objectIdentifierMatch)[1];
  117. const variableDeclarationIdentifierName: string|null = (<RegExpMatchArray>variableDeclarationIdentifierMatch)[1];
  118. it('should\'t name variables inside inner function with names from outer function params', () => {
  119. assert.notEqual(outerFunctionParamIdentifierName, constructorIdentifierName);
  120. assert.notEqual(outerFunctionParamIdentifierName, innerFunctionParamIdentifierName);
  121. });
  122. it('should correct transform variables inside outer function body', () => {
  123. assert.equal(outerFunctionParamIdentifierName, objectIdentifierName);
  124. assert.equal(outerFunctionParamIdentifierName, variableDeclarationIdentifierName);
  125. });
  126. it('should correct transform variables inside inner function body', () => {
  127. assert.equal(innerFunctionParamIdentifierName, constructorIdentifierName);
  128. });
  129. it('should keep equal names after transformation for variables with same names', () => {
  130. assert.equal(variableDeclarationIdentifierName, objectIdentifierName);
  131. });
  132. });
  133. describe(`variable calls before variable declaration when catch clause param has the same name as variables name`, () => {
  134. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  135. `
  136. (function () {
  137. try {
  138. } catch (t) {
  139. return function () {
  140. function baz (t) {
  141. console.log(t);
  142. }
  143. return {t: t};
  144. var t;
  145. }();
  146. }
  147. })();
  148. `,
  149. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  150. );
  151. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  152. const catchClauseParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  153. .match(/catch *\((_0x[a-f0-9]{4,6})\) *\{/);
  154. const innerFunctionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  155. .match(/function _0x[a-f0-9]{4,6} *\((_0x[a-f0-9]{4,6})\) *\{/);
  156. const constructorIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  157. .match(/console\['\\x6c\\x6f\\x67'\]\((_0x[a-f0-9]{4,6})\)/);
  158. const objectIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  159. .match(/return\{'\\x74':(_0x[a-f0-9]{4,6})\}/);
  160. const variableDeclarationIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
  161. .match(/var *(_0x[a-f0-9]{4,6});/);
  162. const functionParamIdentifierName: string|null = (<RegExpMatchArray>catchClauseParamIdentifierMatch)[1];
  163. const innerFunctionParamIdentifierName: string|null = (<RegExpMatchArray>innerFunctionParamIdentifierMatch)[1];
  164. const constructorIdentifierName: string|null = (<RegExpMatchArray>constructorIdentifierMatch)[1];
  165. const objectIdentifierName: string|null = (<RegExpMatchArray>objectIdentifierMatch)[1];
  166. const variableDeclarationIdentifierName: string|null = (<RegExpMatchArray>variableDeclarationIdentifierMatch)[1];
  167. it('should\'t name variables inside inner function with names from catch clause param', () => {
  168. assert.notEqual(functionParamIdentifierName, constructorIdentifierName);
  169. assert.notEqual(functionParamIdentifierName, innerFunctionParamIdentifierName);
  170. });
  171. it('should correct transform variables inside catch clause body', () => {
  172. assert.equal(functionParamIdentifierName, objectIdentifierName);
  173. assert.equal(functionParamIdentifierName, variableDeclarationIdentifierName);
  174. });
  175. it('should correct transform variables inside inner function body', () => {
  176. assert.equal(innerFunctionParamIdentifierName, constructorIdentifierName);
  177. });
  178. it('should keep equal names after transformation for variables with same names', () => {
  179. assert.equal(variableDeclarationIdentifierName, objectIdentifierName);
  180. });
  181. });
  182. describe('wrong replacement', () => {
  183. it('shouldn\'t replace property node identifier', () => {
  184. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  185. `
  186. function foo () {
  187. var test = 'abc';
  188. var object = {
  189. test: 'cde'
  190. };
  191. console.log(test);
  192. }
  193. `,
  194. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  195. );
  196. assert.match(obfuscationResult.getObfuscatedCode(), /var _0x([a-z0-9]){4,6} *= *\{'\\x74\\x65\\x73\\x74/);
  197. });
  198. it('shouldn\'t replace computed member expression identifier', () => {
  199. let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  200. `
  201. function foo () {
  202. var test = 'abc';
  203. var object = {
  204. 'test': 'cde'
  205. };
  206. console.log(test);
  207. console.log(object.test);
  208. }
  209. `,
  210. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  211. );
  212. assert.match(obfuscationResult.getObfuscatedCode(), /_0x([a-z0-9]){4,6}\['\\x74\\x65\\x73\\x74'\]/);
  213. });
  214. });
  215. describe('object pattern as variable declarator', () => {
  216. const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
  217. `
  218. (function () {
  219. var { bar } = { bar: 'foo' };
  220. console.log(bar);
  221. })();
  222. `,
  223. Object.assign({}, NO_CUSTOM_NODES_PRESET)
  224. );
  225. const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
  226. it('shouldn\'t transform object pattern variable declarator', () => {
  227. const objectPatternVariableDeclarator: RegExp = /var *\{ *bar *\} *= *\{ *'\\x62\\x61\\x72' *: *'\\x66\\x6f\\x6f' *\};/;
  228. const variableUsageMatch: RegExp = /console\['\\x6c\\x6f\\x67'\]\(bar\);/;
  229. assert.match(obfuscatedCode, objectPatternVariableDeclarator);
  230. assert.match(obfuscatedCode, variableUsageMatch);
  231. });
  232. });
  233. });