StringArrayCallsWrapper.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. 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. * @returns {TStatement[]}
  70. */
  71. protected getNodeStructure (): TStatement[] {
  72. return NodeUtils.convertCodeToStructure(this.getTemplate());
  73. }
  74. /**
  75. * @returns {string}
  76. */
  77. protected getTemplate (): string {
  78. const decodeNodeTemplate: string = this.getDecodeStringArrayTemplate();
  79. return JavaScriptObfuscator.obfuscate(
  80. this.customNodeFormatter.formatTemplate(StringArrayCallsWrapperTemplate(), {
  81. decodeNodeTemplate,
  82. stringArrayCallsWrapperName: this.stringArrayCallsWrapperName,
  83. stringArrayName: this.stringArrayName
  84. }),
  85. {
  86. ...NO_ADDITIONAL_NODES_PRESET,
  87. identifierNamesGenerator: this.options.identifierNamesGenerator,
  88. identifiersDictionary: this.options.identifiersDictionary,
  89. seed: this.randomGenerator.getRawSeed()
  90. }
  91. ).getObfuscatedCode();
  92. }
  93. /**
  94. * @returns {string}
  95. */
  96. private getDecodeStringArrayTemplate (): string {
  97. const globalVariableTemplate: string = this.options.target !== ObfuscationTarget.BrowserNoEval
  98. ? this.getGlobalVariableTemplate()
  99. : GlobalVariableNoEvalTemplate();
  100. const atobPolyfill: string = this.customNodeFormatter.formatTemplate(AtobTemplate(), { globalVariableTemplate });
  101. let decodeStringArrayTemplate: string = '';
  102. let selfDefendingCode: string = '';
  103. if (this.options.selfDefending) {
  104. selfDefendingCode = this.customNodeFormatter.formatTemplate(
  105. SelfDefendingTemplate(
  106. this.randomGenerator,
  107. this.escapeSequenceEncoder
  108. ),
  109. {
  110. stringArrayCallsWrapperName: this.stringArrayCallsWrapperName,
  111. stringArrayName: this.stringArrayName
  112. }
  113. );
  114. }
  115. switch (this.options.stringArrayEncoding) {
  116. case StringArrayEncoding.Rc4:
  117. decodeStringArrayTemplate = this.customNodeFormatter.formatTemplate(
  118. StringArrayRc4DecodeNodeTemplate(this.randomGenerator),
  119. {
  120. atobPolyfill,
  121. rc4Polyfill: Rc4Template(),
  122. selfDefendingCode,
  123. stringArrayCallsWrapperName: this.stringArrayCallsWrapperName
  124. }
  125. );
  126. break;
  127. case StringArrayEncoding.Base64:
  128. decodeStringArrayTemplate = this.customNodeFormatter.formatTemplate(
  129. StringArrayBase64DecodeNodeTemplate(this.randomGenerator),
  130. {
  131. atobPolyfill,
  132. selfDefendingCode,
  133. stringArrayCallsWrapperName: this.stringArrayCallsWrapperName
  134. }
  135. );
  136. }
  137. return decodeStringArrayTemplate;
  138. }
  139. }