StringLiteralObfuscatingReplacer.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { inject, injectable, postConstruct } from 'inversify';
  2. import { ServiceIdentifiers } from '../../../../container/ServiceIdentifiers';
  3. import * as ESTree from 'estree';
  4. import { IEscapeSequenceEncoder } from '../../../../interfaces/utils/IEscapeSequenceEncoder';
  5. import { IInitializable } from '../../../../interfaces/IInitializable';
  6. import { IOptions } from '../../../../interfaces/options/IOptions';
  7. import { IStringArrayStorage } from '../../../../interfaces/storages/string-array-storage/IStringArrayStorage';
  8. import { IStringArrayStorageAnalyzer } from '../../../../interfaces/analyzers/string-array-storage-analyzer/IStringArrayStorageAnalyzer';
  9. import { IStringArrayStorageItemData } from '../../../../interfaces/storages/string-array-storage/IStringArrayStorageItem';
  10. import { initializable } from '../../../../decorators/Initializable';
  11. import { StringArrayEncoding } from '../../../../enums/StringArrayEncoding';
  12. import { AbstractObfuscatingReplacer } from '../AbstractObfuscatingReplacer';
  13. import { NodeMetadata } from '../../../../node/NodeMetadata';
  14. import { NodeFactory } from '../../../../node/NodeFactory';
  15. import { NumberUtils } from '../../../../utils/NumberUtils';
  16. import { Utils } from '../../../../utils/Utils';
  17. @injectable()
  18. export class StringLiteralObfuscatingReplacer extends AbstractObfuscatingReplacer implements IInitializable {
  19. /**
  20. * @type {IEscapeSequenceEncoder}
  21. */
  22. private readonly escapeSequenceEncoder: IEscapeSequenceEncoder;
  23. /**
  24. * @type {Map<string, ESTree.Node>}
  25. */
  26. private readonly nodesCache: Map <string, ESTree.Node> = new Map();
  27. /**
  28. * @type {IStringArrayStorage}
  29. */
  30. private readonly stringArrayStorage: IStringArrayStorage;
  31. /**
  32. * @type {IStringArrayStorageAnalyzer}
  33. */
  34. private readonly stringArrayStorageAnalyzer: IStringArrayStorageAnalyzer;
  35. /**
  36. * @type {string}
  37. */
  38. @initializable()
  39. private stringArrayStorageCallsWrapperName!: string;
  40. /**
  41. * @param {IStringArrayStorage} stringArrayStorage
  42. * @param {IStringArrayStorageAnalyzer} stringArrayStorageAnalyzer
  43. * @param {IEscapeSequenceEncoder} escapeSequenceEncoder
  44. * @param {IOptions} options
  45. */
  46. constructor (
  47. @inject(ServiceIdentifiers.TStringArrayStorage) stringArrayStorage: IStringArrayStorage,
  48. @inject(ServiceIdentifiers.IStringArrayStorageAnalyzer) stringArrayStorageAnalyzer: IStringArrayStorageAnalyzer,
  49. @inject(ServiceIdentifiers.IEscapeSequenceEncoder) escapeSequenceEncoder: IEscapeSequenceEncoder,
  50. @inject(ServiceIdentifiers.IOptions) options: IOptions
  51. ) {
  52. super(options);
  53. this.stringArrayStorage = stringArrayStorage;
  54. this.stringArrayStorageAnalyzer = stringArrayStorageAnalyzer;
  55. this.escapeSequenceEncoder = escapeSequenceEncoder;
  56. }
  57. /**
  58. * @param {string} hexadecimalIndex
  59. * @returns {Literal}
  60. */
  61. private static getHexadecimalLiteralNode (hexadecimalIndex: string): ESTree.Literal {
  62. const hexadecimalLiteralNode: ESTree.Literal = NodeFactory.literalNode(hexadecimalIndex);
  63. NodeMetadata.set(hexadecimalLiteralNode, { replacedLiteral: true });
  64. return hexadecimalLiteralNode;
  65. }
  66. /**
  67. * @param {string} literalValue
  68. * @returns {Literal}
  69. */
  70. private static getRc4KeyLiteralNode (literalValue: string): ESTree.Literal {
  71. const rc4KeyLiteralNode: ESTree.Literal = NodeFactory.literalNode(literalValue);
  72. NodeMetadata.set(rc4KeyLiteralNode, { replacedLiteral: true });
  73. return rc4KeyLiteralNode;
  74. }
  75. @postConstruct()
  76. public initialize (): void {
  77. this.stringArrayStorageCallsWrapperName = this.stringArrayStorage.getStorageCallsWrapperName();
  78. if (this.options.shuffleStringArray) {
  79. this.stringArrayStorage.shuffleStorage();
  80. }
  81. if (this.options.rotateStringArray) {
  82. this.stringArrayStorage.rotateStorage();
  83. }
  84. }
  85. /**
  86. * @param {SimpleLiteral} literalNode
  87. * @returns {Node}
  88. */
  89. public replace (literalNode: ESTree.SimpleLiteral): ESTree.Node {
  90. const literalValue: ESTree.SimpleLiteral['value'] = literalNode.value;
  91. if (typeof literalValue !== 'string') {
  92. throw new Error('`StringLiteralObfuscatingReplacer` should accept only literals with `string` value');
  93. }
  94. const stringArrayStorageItemData: IStringArrayStorageItemData | undefined = this.stringArrayStorageAnalyzer
  95. .getItemDataForLiteralNode(literalNode);
  96. const cacheKey: string = `${literalValue}-${Boolean(stringArrayStorageItemData)}`;
  97. const useCachedValue: boolean = this.nodesCache.has(cacheKey) && this.options.stringArrayEncoding !== StringArrayEncoding.Rc4;
  98. if (useCachedValue) {
  99. return <ESTree.Node>this.nodesCache.get(cacheKey);
  100. }
  101. const resultNode: ESTree.Node = stringArrayStorageItemData
  102. ? this.replaceWithStringArrayCallNode(stringArrayStorageItemData)
  103. : this.replaceWithLiteralNode(literalValue);
  104. this.nodesCache.set(cacheKey, resultNode);
  105. return resultNode;
  106. }
  107. /**
  108. * @param {string} value
  109. * @returns {Node}
  110. */
  111. private replaceWithLiteralNode (value: string): ESTree.Node {
  112. return NodeFactory.literalNode(
  113. this.escapeSequenceEncoder.encode(value, this.options.unicodeEscapeSequence)
  114. );
  115. }
  116. /**
  117. * @param {IStringArrayStorageItemData} stringArrayStorageItemData
  118. * @returns {Node}
  119. */
  120. private replaceWithStringArrayCallNode (stringArrayStorageItemData: IStringArrayStorageItemData): ESTree.Node {
  121. const { index, decodeKey } = stringArrayStorageItemData;
  122. const hexadecimalIndex: string = `${Utils.hexadecimalPrefix}${NumberUtils.toHex(index)}`;
  123. const callExpressionArgs: (ESTree.Expression | ESTree.SpreadElement)[] = [
  124. StringLiteralObfuscatingReplacer.getHexadecimalLiteralNode(hexadecimalIndex)
  125. ];
  126. if (decodeKey) {
  127. callExpressionArgs.push(StringLiteralObfuscatingReplacer.getRc4KeyLiteralNode(
  128. this.escapeSequenceEncoder.encode(decodeKey, this.options.unicodeEscapeSequence)
  129. ));
  130. }
  131. const stringArrayIdentifierNode: ESTree.Identifier = NodeFactory.identifierNode(this.stringArrayStorageCallsWrapperName);
  132. // prevent obfuscation of this identifier
  133. NodeMetadata.set(stringArrayIdentifierNode, { renamedIdentifier: true });
  134. return NodeFactory.callExpressionNode(
  135. stringArrayIdentifierNode,
  136. callExpressionArgs
  137. );
  138. }
  139. }