StringArrayCallsWrapper.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import * as format from 'string-template';
  4. import { TIdentifierNamesGeneratorFactory } from '../../types/container/generators/TIdentifierNamesGeneratorFactory';
  5. import { TStatement } from '../../types/node/TStatement';
  6. import { IEscapeSequenceEncoder } from '../../interfaces/utils/IEscapeSequenceEncoder';
  7. import { IOptions } from '../../interfaces/options/IOptions';
  8. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  9. import { ObfuscationTarget } from '../../enums/ObfuscationTarget';
  10. import { StringArrayEncoding } from '../../enums/StringArrayEncoding';
  11. import { initializable } from '../../decorators/Initializable';
  12. import { NO_CUSTOM_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 {IRandomGenerator} randomGenerator
  42. * @param {IEscapeSequenceEncoder} escapeSequenceEncoder
  43. * @param {IOptions} options
  44. */
  45. constructor (
  46. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  47. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  48. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  49. @inject(ServiceIdentifiers.IEscapeSequenceEncoder) escapeSequenceEncoder: IEscapeSequenceEncoder,
  50. @inject(ServiceIdentifiers.IOptions) options: IOptions
  51. ) {
  52. super(identifierNamesGeneratorFactory, randomGenerator, options);
  53. this.escapeSequenceEncoder = escapeSequenceEncoder;
  54. }
  55. /**
  56. * @param {string} stringArrayName
  57. * @param {string} stringArrayCallsWrapperName
  58. */
  59. public initialize (
  60. stringArrayName: string,
  61. stringArrayCallsWrapperName: string
  62. ): void {
  63. this.stringArrayName = stringArrayName;
  64. this.stringArrayCallsWrapperName = stringArrayCallsWrapperName;
  65. }
  66. /**
  67. * @returns {TStatement[]}
  68. */
  69. protected getNodeStructure (): TStatement[] {
  70. return NodeUtils.convertCodeToStructure(this.getTemplate());
  71. }
  72. /**
  73. * @returns {string}
  74. */
  75. protected getTemplate (): string {
  76. const decodeNodeTemplate: string = this.getDecodeStringArrayTemplate();
  77. return JavaScriptObfuscator.obfuscate(
  78. format(StringArrayCallsWrapperTemplate(), {
  79. decodeNodeTemplate,
  80. stringArrayCallsWrapperName: this.stringArrayCallsWrapperName,
  81. stringArrayName: this.stringArrayName
  82. }),
  83. {
  84. ...NO_CUSTOM_NODES_PRESET,
  85. identifierNamesGenerator: this.options.identifierNamesGenerator,
  86. seed: this.options.seed
  87. }
  88. ).getObfuscatedCode();
  89. }
  90. /**
  91. * @returns {string}
  92. */
  93. private getDecodeStringArrayTemplate (): string {
  94. const globalVariableTemplate: string = this.options.target !== ObfuscationTarget.Extension
  95. ? this.getGlobalVariableTemplate()
  96. : GlobalVariableNoEvalTemplate();
  97. const atobPolyfill: string = format(AtobTemplate(), { globalVariableTemplate });
  98. let decodeStringArrayTemplate: string = '',
  99. selfDefendingCode: string = '';
  100. if (this.options.selfDefending) {
  101. selfDefendingCode = format(
  102. SelfDefendingTemplate(
  103. this.randomGenerator,
  104. this.escapeSequenceEncoder
  105. ),
  106. {
  107. stringArrayCallsWrapperName: this.stringArrayCallsWrapperName,
  108. stringArrayName: this.stringArrayName
  109. }
  110. );
  111. }
  112. switch (this.options.stringArrayEncoding) {
  113. case StringArrayEncoding.Rc4:
  114. decodeStringArrayTemplate = format(
  115. StringArrayRc4DecodeNodeTemplate(this.randomGenerator),
  116. {
  117. atobPolyfill,
  118. rc4Polyfill: Rc4Template(),
  119. selfDefendingCode,
  120. stringArrayCallsWrapperName: this.stringArrayCallsWrapperName
  121. }
  122. );
  123. break;
  124. case StringArrayEncoding.Base64:
  125. decodeStringArrayTemplate = format(
  126. StringArrayBase64DecodeNodeTemplate(this.randomGenerator),
  127. {
  128. atobPolyfill,
  129. selfDefendingCode,
  130. stringArrayCallsWrapperName: this.stringArrayCallsWrapperName
  131. }
  132. );
  133. break;
  134. }
  135. return decodeStringArrayTemplate;
  136. }
  137. }