StringArrayCallsWrapperCodeHelper.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import { TIdentifierNamesGeneratorFactory } from '../../types/container/generators/TIdentifierNamesGeneratorFactory';
  4. import { TStatement } from '../../types/node/TStatement';
  5. import { ICustomCodeHelperFormatter } from '../../interfaces/custom-code-helpers/ICustomCodeHelperFormatter';
  6. import { ICustomCodeHelperObfuscator } from '../../interfaces/custom-code-helpers/ICustomCodeHelperObfuscator';
  7. import { IEscapeSequenceEncoder } from '../../interfaces/utils/IEscapeSequenceEncoder';
  8. import { IOptions } from '../../interfaces/options/IOptions';
  9. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  10. import { ObfuscationTarget } from '../../enums/ObfuscationTarget';
  11. import { StringArrayEncoding } from '../../enums/StringArrayEncoding';
  12. import { initializable } from '../../decorators/Initializable';
  13. import { AtobTemplate } from './templates/string-array-calls-wrapper/AtobTemplate';
  14. import { GlobalVariableNoEvalTemplate } from '../common/templates/GlobalVariableNoEvalTemplate';
  15. import { Rc4Template } from './templates/string-array-calls-wrapper/Rc4Template';
  16. import { SelfDefendingTemplate } from './templates/string-array-calls-wrapper/SelfDefendingTemplate';
  17. import { StringArrayBase64DecodeTemplate } from './templates/string-array-calls-wrapper/StringArrayBase64DecodeTemplate';
  18. import { StringArrayCallsWrapperTemplate } from './templates/string-array-calls-wrapper/StringArrayCallsWrapperTemplate';
  19. import { StringArrayRC4DecodeTemplate } from './templates/string-array-calls-wrapper/StringArrayRC4DecodeTemplate';
  20. import { AbstractCustomCodeHelper } from '../AbstractCustomCodeHelper';
  21. import { NodeUtils } from '../../node/NodeUtils';
  22. @injectable()
  23. export class StringArrayCallsWrapperCodeHelper extends AbstractCustomCodeHelper {
  24. /**
  25. * @type {IEscapeSequenceEncoder}
  26. */
  27. private readonly escapeSequenceEncoder: IEscapeSequenceEncoder;
  28. /**
  29. * @type {string}
  30. */
  31. @initializable()
  32. private stringArrayName!: string;
  33. /**
  34. * @type {string}
  35. */
  36. @initializable()
  37. private stringArrayCallsWrapperName!: string;
  38. /**
  39. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  40. * @param {ICustomCodeHelperFormatter} customCodeHelperFormatter
  41. * @param {ICustomCodeHelperObfuscator} customCodeHelperObfuscator
  42. * @param {IRandomGenerator} randomGenerator
  43. * @param {IOptions} options
  44. * @param {IEscapeSequenceEncoder} escapeSequenceEncoder
  45. */
  46. public constructor (
  47. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  48. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  49. @inject(ServiceIdentifiers.ICustomCodeHelperFormatter) customCodeHelperFormatter: ICustomCodeHelperFormatter,
  50. @inject(ServiceIdentifiers.ICustomCodeHelperObfuscator) customCodeHelperObfuscator: ICustomCodeHelperObfuscator,
  51. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  52. @inject(ServiceIdentifiers.IOptions) options: IOptions,
  53. @inject(ServiceIdentifiers.IEscapeSequenceEncoder) escapeSequenceEncoder: IEscapeSequenceEncoder
  54. ) {
  55. super(
  56. identifierNamesGeneratorFactory,
  57. customCodeHelperFormatter,
  58. customCodeHelperObfuscator,
  59. randomGenerator,
  60. options
  61. );
  62. this.escapeSequenceEncoder = escapeSequenceEncoder;
  63. }
  64. /**
  65. * @param {string} stringArrayName
  66. * @param {string} stringArrayCallsWrapperName
  67. */
  68. public initialize (
  69. stringArrayName: string,
  70. stringArrayCallsWrapperName: string
  71. ): void {
  72. this.stringArrayName = stringArrayName;
  73. this.stringArrayCallsWrapperName = stringArrayCallsWrapperName;
  74. }
  75. /**
  76. * @param {string} codeHelperTemplate
  77. * @returns {TStatement[]}
  78. */
  79. protected getNodeStructure (codeHelperTemplate: string): TStatement[] {
  80. return NodeUtils.convertCodeToStructure(codeHelperTemplate);
  81. }
  82. /**
  83. * @returns {string}
  84. */
  85. protected getCodeHelperTemplate (): string {
  86. const decodeCodeHelperTemplate: string = this.getDecodeStringArrayTemplate();
  87. const preservedNames: string[] = [this.stringArrayName];
  88. return this.customCodeHelperObfuscator.obfuscateTemplate(
  89. this.customCodeHelperFormatter.formatTemplate(StringArrayCallsWrapperTemplate(), {
  90. decodeCodeHelperTemplate,
  91. stringArrayCallsWrapperName: this.stringArrayCallsWrapperName,
  92. stringArrayName: this.stringArrayName
  93. }),
  94. {
  95. reservedNames: preservedNames
  96. }
  97. );
  98. }
  99. /**
  100. * @returns {string}
  101. */
  102. private getDecodeStringArrayTemplate (): string {
  103. const globalVariableTemplate: string = this.options.target !== ObfuscationTarget.BrowserNoEval
  104. ? this.getGlobalVariableTemplate()
  105. : GlobalVariableNoEvalTemplate();
  106. const atobPolyfill: string = this.customCodeHelperFormatter.formatTemplate(AtobTemplate(), { globalVariableTemplate });
  107. let decodeStringArrayTemplate: string = '';
  108. let selfDefendingCode: string = '';
  109. if (this.options.selfDefending) {
  110. selfDefendingCode = this.customCodeHelperFormatter.formatTemplate(
  111. SelfDefendingTemplate(
  112. this.randomGenerator,
  113. this.escapeSequenceEncoder
  114. ),
  115. {
  116. stringArrayCallsWrapperName: this.stringArrayCallsWrapperName,
  117. stringArrayName: this.stringArrayName
  118. }
  119. );
  120. }
  121. switch (this.options.stringArrayEncoding) {
  122. case StringArrayEncoding.Rc4:
  123. decodeStringArrayTemplate = this.customCodeHelperFormatter.formatTemplate(
  124. StringArrayRC4DecodeTemplate(this.randomGenerator),
  125. {
  126. atobPolyfill,
  127. selfDefendingCode,
  128. rc4Polyfill: Rc4Template(),
  129. stringArrayCallsWrapperName: this.stringArrayCallsWrapperName
  130. }
  131. );
  132. break;
  133. case StringArrayEncoding.Base64:
  134. decodeStringArrayTemplate = this.customCodeHelperFormatter.formatTemplate(
  135. StringArrayBase64DecodeTemplate(this.randomGenerator),
  136. {
  137. atobPolyfill,
  138. selfDefendingCode,
  139. stringArrayCallsWrapperName: this.stringArrayCallsWrapperName
  140. }
  141. );
  142. }
  143. return decodeStringArrayTemplate;
  144. }
  145. }