StringArrayCallsWrapperTemplate.spec.ts 8.6 KB

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