StringArrayCallsWrapperNodeTemplate.spec.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 { GlobalVariableTemplate1 } from '../../../../../../src/custom-code-helpers/common/templates/GlobalVariableTemplate1';
  11. import { Rc4Template } from '../../../../../../src/custom-code-helpers/string-array/templates/string-array-calls-wrapper/Rc4Template';
  12. import { StringArrayBase64DecodeTemplate } from '../../../../../../src/custom-code-helpers/string-array/templates/string-array-calls-wrapper/StringArrayBase64DecodeTemplate';
  13. import { StringArrayCallsWrapperTemplate } from '../../../../../../src/custom-code-helpers/string-array/templates/string-array-calls-wrapper/StringArrayCallsWrapperTemplate';
  14. import { StringArrayRC4DecodeTemplate } from '../../../../../../src/custom-code-helpers/string-array/templates/string-array-calls-wrapper/StringArrayRC4DecodeTemplate';
  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('StringArrayCallsWrapperTemplate', () => {
  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 atobDecodeTemplate: string = format(
  39. StringArrayBase64DecodeTemplate(randomGenerator),
  40. {
  41. atobPolyfill,
  42. selfDefendingCode: '',
  43. stringArrayCallsWrapperName
  44. }
  45. );
  46. const stringArrayCallsWrapperTemplate: string = format(StringArrayCallsWrapperTemplate(), {
  47. decodeCodeHelperTemplate: atobDecodeTemplate,
  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 rc4decodeCodeHelperTemplate: string = format(
  71. StringArrayRC4DecodeTemplate(randomGenerator),
  72. {
  73. atobPolyfill,
  74. rc4Polyfill: Rc4Template(),
  75. selfDefendingCode: '',
  76. stringArrayCallsWrapperName
  77. }
  78. );
  79. const stringArrayCallsWrapperTemplate: string = format(StringArrayCallsWrapperTemplate(), {
  80. decodeCodeHelperTemplate: rc4decodeCodeHelperTemplate,
  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. it('Should does not break on obfuscating', () => {
  114. assert.doesNotThrow(() => obfuscatedCode);
  115. });
  116. });
  117. describe('`const` kind', () => {
  118. let obfuscatedCode: string,
  119. stringArrayCallsWrapperRegExp: RegExp = /const (_0x(\w){4}) *= *function/;
  120. beforeEach(() => {
  121. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-const.js');
  122. const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
  123. code,
  124. {
  125. ...NO_ADDITIONAL_NODES_PRESET,
  126. stringArray: true,
  127. stringArrayThreshold: 1
  128. }
  129. );
  130. obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
  131. });
  132. it('Should return correct kind of variables for string array calls wrapper', () => {
  133. assert.match(obfuscatedCode, stringArrayCallsWrapperRegExp);
  134. });
  135. it('Should does not break on obfuscating', () => {
  136. assert.doesNotThrow(() => obfuscatedCode);
  137. });
  138. });
  139. describe('`let` kind', () => {
  140. let obfuscatedCode: string,
  141. stringArrayCallsWrapperRegExp: RegExp = /const (_0x(\w){4}) *= *function/;
  142. beforeEach(() => {
  143. const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-let.js');
  144. const obfuscatedCodeObject: IObfuscatedCode = JavaScriptObfuscator.obfuscate(
  145. code,
  146. {
  147. ...NO_ADDITIONAL_NODES_PRESET,
  148. stringArray: true,
  149. stringArrayThreshold: 1
  150. }
  151. );
  152. obfuscatedCode = obfuscatedCodeObject.getObfuscatedCode();
  153. });
  154. it('Should return correct kind of variables for string array calls wrapper', () => {
  155. assert.match(obfuscatedCode, stringArrayCallsWrapperRegExp);
  156. });
  157. it('Should does not break on obfuscating', () => {
  158. assert.doesNotThrow(() => obfuscatedCode);
  159. });
  160. });
  161. });
  162. });