StringLiteralObfuscatingReplacer.ts 5.8 KB

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