StringArrayCallsWrapperNodeTemplate.spec.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import 'reflect-metadata';
  2. import format from 'string-template';
  3. import { assert } from 'chai';
  4. import { ServiceIdentifiers } from '../../../../../src/container/ServiceIdentifiers';
  5. import { ICryptUtils } from '../../../../../src/interfaces/utils/ICryptUtils';
  6. import { IInversifyContainerFacade } from '../../../../../src/interfaces/container/IInversifyContainerFacade';
  7. import { IObfuscatedCode } from '../../../../../src/interfaces/source-code/IObfuscatedCode';
  8. import { IRandomGenerator } from '../../../../../src/interfaces/utils/IRandomGenerator';
  9. import { AtobTemplate } from '../../../../../src/templates/AtobTemplate';
  10. import { GlobalVariableTemplate1 } from '../../../../../src/templates/GlobalVariableTemplate1';
  11. import { Rc4Template } from '../../../../../src/templates/Rc4Template';
  12. import { StringArrayBase64DecodeNodeTemplate } from '../../../../../src/templates/string-array-nodes/string-array-calls-wrapper/StringArrayBase64DecodeNodeTemplate';
  13. import { StringArrayCallsWrapperTemplate } from '../../../../../src/templates/string-array-nodes/string-array-calls-wrapper/StringArrayCallsWrapperTemplate';
  14. import { StringArrayRc4DecodeNodeTemplate } from '../../../../../src/templates/string-array-nodes/string-array-calls-wrapper/StringArrayRC4DecodeNodeTemplate';
  15. import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
  16. import { InversifyContainerFacade } from '../../../../../src/container/InversifyContainerFacade';
  17. import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFacade';
  18. import { readFileAsString } from '../../../../helpers/readFileAsString';
  19. describe('StringArrayCallsWrapperNodeTemplate', () => {
  20. const stringArrayName: string = 'stringArrayName';
  21. const stringArrayCallsWrapperName: string = 'stringArrayCallsWrapperName';
  22. let cryptUtils: ICryptUtils,
  23. randomGenerator: IRandomGenerator;
  24. before(() => {
  25. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  26. inversifyContainerFacade.load('', '', {});
  27. cryptUtils = inversifyContainerFacade.get<ICryptUtils>(ServiceIdentifiers.ICryptUtils);
  28. randomGenerator = inversifyContainerFacade.get<IRandomGenerator>(ServiceIdentifiers.IRandomGenerator);
  29. });
  30. describe('Variant #1: `base64` encoding', () => {
  31. const index: string = '0x0';
  32. const expectedDecodedValue: string = 'test1';
  33. let decodedValue: string;
  34. before(() => {
  35. const atobPolyfill = format(AtobTemplate(), {
  36. globalVariableTemplate: GlobalVariableTemplate1()
  37. });
  38. const atobDecodeNodeTemplate: string = format(
  39. StringArrayBase64DecodeNodeTemplate(randomGenerator),
  40. {
  41. atobPolyfill,
  42. selfDefendingCode: '',
  43. stringArrayCallsWrapperName
  44. }
  45. );
  46. const stringArrayCallsWrapperTemplate: string = format(StringArrayCallsWrapperTemplate(), {
  47. decodeNodeTemplate: atobDecodeNodeTemplate,
  48. stringArrayCallsWrapperName,
  49. stringArrayName
  50. });
  51. decodedValue = Function(`
  52. var ${stringArrayName} = ['${cryptUtils.btoa('test1')}'];
  53. ${stringArrayCallsWrapperTemplate}
  54. return ${stringArrayCallsWrapperName}(${index});
  55. `)();
  56. });
  57. it('should correctly return decoded value', () => {
  58. assert.deepEqual(decodedValue, expectedDecodedValue);
  59. });
  60. });
  61. describe('Variant #2: `rc4` encoding', () => {
  62. const index: string = '0x0';
  63. const key: string = 'key';
  64. const expectedDecodedValue: string = 'test1';
  65. let decodedValue: string;
  66. before(() => {
  67. const atobPolyfill = format(AtobTemplate(), {
  68. globalVariableTemplate: GlobalVariableTemplate1()
  69. });
  70. const rc4DecodeNodeTemplate: string = format(
  71. StringArrayRc4DecodeNodeTemplate(randomGenerator),
  72. {
  73. atobPolyfill,
  74. rc4Polyfill: Rc4Template(),
  75. selfDefendingCode: '',
  76. stringArrayCallsWrapperName
  77. }
  78. );
  79. const stringArrayCallsWrapperTemplate: string = format(StringArrayCallsWrapperTemplate(), {
  80. decodeNodeTemplate: rc4DecodeNodeTemplate,
  81. stringArrayCallsWrapperName,
  82. stringArrayName
  83. });
  84. decodedValue = Function(`
  85. var ${stringArrayName} = ['${cryptUtils.btoa(cryptUtils.rc4('test1', key))}'];
  86. ${stringArrayCallsWrapperTemplate}
  87. return ${stringArrayCallsWrapperName}('${index}', '${key}');
  88. `)();
  89. });
  90. it('should correctly return decoded value', () => {
  91. assert.deepEqual(decodedValue, expectedDecodedValue);
  92. });
  93. });
  94. describe('Prevailing kind of variables', () => {
  95. describe('`var` kind', () => {
  96. let obfuscatedCode: string,
  97. stringArrayCallsWrapperRegExp: RegExp = /var (_0x(\w){4}) *= *function/;
  98. beforeEach(() => {
  99. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-var.js');
  100. const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
  101. code,
  102. {
  103. ...NO_ADDITIONAL_NODES_PRESET,
  104. stringArray: true,
  105. stringArrayThreshold: 1
  106. }
  107. );
  108. obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
  109. });
  110. it('Should return correct kind of variables for string array calls wrapper', () => {
  111. assert.match(obfuscatedCode, stringArrayCallsWrapperRegExp);
  112. });
  113. });
  114. describe('`const` kind', () => {
  115. let obfuscatedCode: string,
  116. stringArrayCallsWrapperRegExp: RegExp = /const (_0x(\w){4}) *= *function/;
  117. beforeEach(() => {
  118. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
  119. const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
  120. code,
  121. {
  122. ...NO_ADDITIONAL_NODES_PRESET,
  123. stringArray: true,
  124. stringArrayThreshold: 1
  125. }
  126. );
  127. obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
  128. });
  129. it('Should return correct kind of variables for string array calls wrapper', () => {
  130. assert.match(obfuscatedCode, stringArrayCallsWrapperRegExp);
  131. });
  132. });
  133. describe('`let` kind', () => {
  134. let obfuscatedCode: string,
  135. stringArrayCallsWrapperRegExp: RegExp = /const (_0x(\w){4}) *= *function/;
  136. beforeEach(() => {
  137. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
  138. const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
  139. code,
  140. {
  141. ...NO_ADDITIONAL_NODES_PRESET,
  142. stringArray: true,
  143. stringArrayThreshold: 1
  144. }
  145. );
  146. obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
  147. });
  148. it('Should return correct kind of variables for string array calls wrapper', () => {
  149. assert.match(obfuscatedCode, stringArrayCallsWrapperRegExp);
  150. });
  151. });
  152. });
  153. });