StringArrayCallsWrapperNodeTemplate.spec.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/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 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. atobFunctionName
  37. });
  38. const atobDecodeTemplate: string = format(
  39. StringArrayBase64DecodeTemplate(randomGenerator),
  40. {
  41. atobPolyfill,
  42. atobFunctionName,
  43. selfDefendingCode: '',
  44. stringArrayCallsWrapperName
  45. }
  46. );
  47. const stringArrayCallsWrapperTemplate: string = format(StringArrayCallsWrapperTemplate(), {
  48. decodeCodeHelperTemplate: atobDecodeTemplate,
  49. stringArrayCallsWrapperName,
  50. stringArrayName
  51. });
  52. decodedValue = Function(`
  53. var ${stringArrayName} = ['${cryptUtils.btoa('test1')}'];
  54. ${stringArrayCallsWrapperTemplate}
  55. return ${stringArrayCallsWrapperName}(${index});
  56. `)();
  57. });
  58. it('should correctly return decoded value', () => {
  59. assert.deepEqual(decodedValue, expectedDecodedValue);
  60. });
  61. });
  62. describe('Variant #2: `rc4` encoding', () => {
  63. const index: string = '0x0';
  64. const key: string = 'key';
  65. const expectedDecodedValue: string = 'test1';
  66. let decodedValue: string;
  67. before(() => {
  68. const atobPolyfill = format(AtobTemplate(), {
  69. atobFunctionName
  70. });
  71. const rc4Polyfill = format(Rc4Template(), {
  72. atobFunctionName
  73. });
  74. const rc4decodeCodeHelperTemplate: string = format(
  75. StringArrayRC4DecodeTemplate(randomGenerator),
  76. {
  77. atobPolyfill,
  78. rc4Polyfill,
  79. selfDefendingCode: '',
  80. stringArrayCallsWrapperName
  81. }
  82. );
  83. const stringArrayCallsWrapperTemplate: string = format(StringArrayCallsWrapperTemplate(), {
  84. decodeCodeHelperTemplate: rc4decodeCodeHelperTemplate,
  85. stringArrayCallsWrapperName,
  86. stringArrayName
  87. });
  88. decodedValue = Function(`
  89. var ${stringArrayName} = ['${cryptUtils.btoa(cryptUtils.rc4('test1', key))}'];
  90. ${stringArrayCallsWrapperTemplate}
  91. return ${stringArrayCallsWrapperName}('${index}', '${key}');
  92. `)();
  93. });
  94. it('should correctly return decoded value', () => {
  95. assert.deepEqual(decodedValue, expectedDecodedValue);
  96. });
  97. });
  98. describe('Prevailing kind of variables', () => {
  99. describe('`var` kind', () => {
  100. let obfuscatedCode: string,
  101. stringArrayCallsWrapperRegExp: RegExp = /var (_0x(\w){4}) *= *function/;
  102. beforeEach(() => {
  103. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-var.js');
  104. const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
  105. code,
  106. {
  107. ...NO_ADDITIONAL_NODES_PRESET,
  108. stringArray: true,
  109. stringArrayThreshold: 1
  110. }
  111. );
  112. obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
  113. });
  114. it('Should return correct kind of variables for string array calls wrapper', () => {
  115. assert.match(obfuscatedCode, stringArrayCallsWrapperRegExp);
  116. });
  117. it('Should does not break on obfuscating', () => {
  118. assert.doesNotThrow(() => obfuscatedCode);
  119. });
  120. });
  121. describe('`const` kind', () => {
  122. let obfuscatedCode: string,
  123. stringArrayCallsWrapperRegExp: RegExp = /const (_0x(\w){4}) *= *function/;
  124. beforeEach(() => {
  125. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
  126. const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
  127. code,
  128. {
  129. ...NO_ADDITIONAL_NODES_PRESET,
  130. stringArray: true,
  131. stringArrayThreshold: 1
  132. }
  133. );
  134. obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
  135. });
  136. it('Should return correct kind of variables for string array calls wrapper', () => {
  137. assert.match(obfuscatedCode, stringArrayCallsWrapperRegExp);
  138. });
  139. it('Should does not break on obfuscating', () => {
  140. assert.doesNotThrow(() => obfuscatedCode);
  141. });
  142. });
  143. describe('`let` kind', () => {
  144. let obfuscatedCode: string,
  145. stringArrayCallsWrapperRegExp: RegExp = /const (_0x(\w){4}) *= *function/;
  146. beforeEach(() => {
  147. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
  148. const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
  149. code,
  150. {
  151. ...NO_ADDITIONAL_NODES_PRESET,
  152. stringArray: true,
  153. stringArrayThreshold: 1
  154. }
  155. );
  156. obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
  157. });
  158. it('Should return correct kind of variables for string array calls wrapper', () => {
  159. assert.match(obfuscatedCode, stringArrayCallsWrapperRegExp);
  160. });
  161. it('Should does not break on obfuscating', () => {
  162. assert.doesNotThrow(() => obfuscatedCode);
  163. });
  164. });
  165. });
  166. });