StringArrayCallsWrapper.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 { IEscapeSequenceEncoder } from '../../interfaces/utils/IEscapeSequenceEncoder';
  6. import { IOptions } from '../../interfaces/options/IOptions';
  7. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  8. import { ICustomNodeFormatter } from '../../interfaces/custom-nodes/ICustomNodeFormatter';
  9. import { ObfuscationTarget } from '../../enums/ObfuscationTarget';
  10. import { StringArrayEncoding } from '../../enums/StringArrayEncoding';
  11. import { initializable } from '../../decorators/Initializable';
  12. import { NO_ADDITIONAL_NODES_PRESET } from '../../options/presets/NoCustomNodes';
  13. import { AtobTemplate } from '../../templates/AtobTemplate';
  14. import { GlobalVariableNoEvalTemplate } from '../../templates/GlobalVariableNoEvalTemplate';
  15. import { Rc4Template } from '../../templates/Rc4Template';
  16. import { SelfDefendingTemplate } from '../../templates/string-array-nodes/string-array-calls-wrapper/SelfDefendingTemplate';
  17. import { StringArrayBase64DecodeNodeTemplate } from '../../templates/string-array-nodes/string-array-calls-wrapper/StringArrayBase64DecodeNodeTemplate';
  18. import { StringArrayCallsWrapperTemplate } from '../../templates/string-array-nodes/string-array-calls-wrapper/StringArrayCallsWrapperTemplate';
  19. import { StringArrayRc4DecodeNodeTemplate } from '../../templates/string-array-nodes/string-array-calls-wrapper/StringArrayRC4DecodeNodeTemplate';
  20. import { AbstractCustomNode } from '../AbstractCustomNode';
  21. import { JavaScriptObfuscator } from '../../JavaScriptObfuscatorFacade';
  22. import { NodeUtils } from '../../node/NodeUtils';
  23. @injectable()
  24. export class StringArrayCallsWrapper extends AbstractCustomNode {
  25. /**
  26. * @type {IEscapeSequenceEncoder}
  27. */
  28. private readonly escapeSequenceEncoder: IEscapeSequenceEncoder;
  29. /**
  30. * @type {string}
  31. */
  32. @initializable()
  33. private stringArrayName!: string;
  34. /**
  35. * @type {string}
  36. */
  37. @initializable()
  38. private stringArrayCallsWrapperName!: string;
  39. /**
  40. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  41. * @param {ICustomNodeFormatter} customNodeFormatter
  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.ICustomNodeFormatter) customNodeFormatter: ICustomNodeFormatter,
  50. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  51. @inject(ServiceIdentifiers.IOptions) options: IOptions,
  52. @inject(ServiceIdentifiers.IEscapeSequenceEncoder) escapeSequenceEncoder: IEscapeSequenceEncoder
  53. ) {
  54. super(identifierNamesGeneratorFactory, customNodeFormatter, randomGenerator, options);
  55. this.escapeSequenceEncoder = escapeSequenceEncoder;
  56. }
  57. /**
  58. * @param {string} stringArrayName
  59. * @param {string} stringArrayCallsWrapperName
  60. */
  61. public initialize (
  62. stringArrayName: string,
  63. stringArrayCallsWrapperName: string
  64. ): void {
  65. this.stringArrayName = stringArrayName;
  66. this.stringArrayCallsWrapperName = stringArrayCallsWrapperName;
  67. }
  68. /**
  69. * @param {string} nodeTemplate
  70. * @returns {TStatement[]}
  71. */
  72. protected getNodeStructure (nodeTemplate: string): TStatement[] {
  73. return NodeUtils.convertCodeToStructure(nodeTemplate);
  74. }
  75. /**
  76. * @returns {string}
  77. */
  78. protected getNodeTemplate (): string {
  79. const decodeNodeTemplate: string = this.getDecodeStringArrayTemplate();
  80. return JavaScriptObfuscator.obfuscate(
  81. this.customNodeFormatter.formatTemplate(StringArrayCallsWrapperTemplate(), {
  82. decodeNodeTemplate,
  83. stringArrayCallsWrapperName: this.stringArrayCallsWrapperName,
  84. stringArrayName: this.stringArrayName
  85. }),
  86. {
  87. ...NO_ADDITIONAL_NODES_PRESET,
  88. identifierNamesGenerator: this.options.identifierNamesGenerator,
  89. identifiersDictionary: this.options.identifiersDictionary,
  90. seed: this.randomGenerator.getRawSeed()
  91. }
  92. ).getObfuscatedCode();
  93. }
  94. /**
  95. * @returns {string}
  96. */
  97. private getDecodeStringArrayTemplate (): string {
  98. const globalVariableTemplate: string = this.options.target !== ObfuscationTarget.BrowserNoEval
  99. ? this.getGlobalVariableTemplate()
  100. : GlobalVariableNoEvalTemplate();
  101. const atobPolyfill: string = this.customNodeFormatter.formatTemplate(AtobTemplate(), { globalVariableTemplate });
  102. let decodeStringArrayTemplate: string = '';
  103. let selfDefendingCode: string = '';
  104. if (this.options.selfDefending) {
  105. selfDefendingCode = this.customNodeFormatter.formatTemplate(
  106. SelfDefendingTemplate(
  107. this.randomGenerator,
  108. this.escapeSequenceEncoder
  109. ),
  110. {
  111. stringArrayCallsWrapperName: this.stringArrayCallsWrapperName,
  112. stringArrayName: this.stringArrayName
  113. }
  114. );
  115. }
  116. switch (this.options.stringArrayEncoding) {
  117. case StringArrayEncoding.Rc4:
  118. decodeStringArrayTemplate = this.customNodeFormatter.formatTemplate(
  119. StringArrayRc4DecodeNodeTemplate(this.randomGenerator),
  120. {
  121. atobPolyfill,
  122. rc4Polyfill: Rc4Template(),
  123. selfDefendingCode,
  124. stringArrayCallsWrapperName: this.stringArrayCallsWrapperName
  125. }
  126. );
  127. break;
  128. case StringArrayEncoding.Base64:
  129. decodeStringArrayTemplate = this.customNodeFormatter.formatTemplate(
  130. StringArrayBase64DecodeNodeTemplate(this.randomGenerator),
  131. {
  132. atobPolyfill,
  133. selfDefendingCode,
  134. stringArrayCallsWrapperName: this.stringArrayCallsWrapperName
  135. }
  136. );
  137. }
  138. return decodeStringArrayTemplate;
  139. }
  140. }