FunctionDeclarationTransformer.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import * as estraverse from 'estraverse';
  4. import * as ESTree from 'estree';
  5. import { TIdentifierObfuscatingReplacerFactory } from "../../types/container/node-transformers/TIdentifierObfuscatingReplacerFactory";
  6. import { TNodeWithLexicalScope } from '../../types/node/TNodeWithLexicalScope';
  7. import { TReplaceableIdentifiers } from '../../types/node-transformers/TReplaceableIdentifiers';
  8. import { TReplaceableIdentifiersNames } from '../../types/node-transformers/TReplaceableIdentifiersNames';
  9. import { IIdentifierObfuscatingReplacer } from '../../interfaces/node-transformers/obfuscating-transformers/obfuscating-replacers/IIdentifierObfuscatingReplacer';
  10. import { IOptions } from '../../interfaces/options/IOptions';
  11. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  12. import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
  13. import { IdentifierObfuscatingReplacer } from "../../enums/node-transformers/obfuscating-transformers/obfuscating-replacers/IdentifierObfuscatingReplacer";
  14. import { NodeType } from '../../enums/node/NodeType';
  15. import { TransformationStage } from '../../enums/node-transformers/TransformationStage';
  16. import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
  17. import { NodeGuards } from '../../node/NodeGuards';
  18. import { NodeLexicalScopeUtils } from '../../node/NodeLexicalScopeUtils';
  19. import { NodeMetadata } from '../../node/NodeMetadata';
  20. /**
  21. * replaces:
  22. * function foo () { //... };
  23. * foo();
  24. *
  25. * on:
  26. * function _0x12d45f () { //... };
  27. * _0x12d45f();
  28. */
  29. @injectable()
  30. export class FunctionDeclarationTransformer extends AbstractNodeTransformer {
  31. /**
  32. * @type {IIdentifierObfuscatingReplacer}
  33. */
  34. private readonly identifierObfuscatingReplacer: IIdentifierObfuscatingReplacer;
  35. /**
  36. * @type {Map<ESTree.Node, ESTree.Identifier[]>}
  37. */
  38. private readonly replaceableIdentifiers: TReplaceableIdentifiers = new Map();
  39. /**
  40. * @param {TIdentifierObfuscatingReplacerFactory} identifierObfuscatingReplacerFactory
  41. * @param {IRandomGenerator} randomGenerator
  42. * @param {IOptions} options
  43. */
  44. constructor (
  45. @inject(ServiceIdentifiers.Factory__IIdentifierObfuscatingReplacer)
  46. identifierObfuscatingReplacerFactory: TIdentifierObfuscatingReplacerFactory,
  47. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  48. @inject(ServiceIdentifiers.IOptions) options: IOptions
  49. ) {
  50. super(randomGenerator, options);
  51. this.identifierObfuscatingReplacer = identifierObfuscatingReplacerFactory(
  52. IdentifierObfuscatingReplacer.BaseIdentifierObfuscatingReplacer
  53. );
  54. }
  55. /**
  56. * @param {TransformationStage} transformationStage
  57. * @returns {IVisitor | null}
  58. */
  59. public getVisitor (transformationStage: TransformationStage): IVisitor | null {
  60. switch (transformationStage) {
  61. case TransformationStage.Obfuscating:
  62. return {
  63. enter: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
  64. if (
  65. parentNode
  66. && NodeGuards.isFunctionDeclarationNode(node)
  67. && !NodeGuards.isExportNamedDeclarationNode(parentNode)
  68. ) {
  69. return this.transformNode(node, parentNode);
  70. }
  71. }
  72. };
  73. default:
  74. return null;
  75. }
  76. }
  77. /**
  78. * @param {FunctionDeclaration} functionDeclarationNode
  79. * @param {NodeGuards} parentNode
  80. * @returns {NodeGuards}
  81. */
  82. public transformNode (
  83. functionDeclarationNode: ESTree.FunctionDeclaration & { id: ESTree.Identifier },
  84. parentNode: ESTree.Node
  85. ): ESTree.Node {
  86. const lexicalScopeNode: TNodeWithLexicalScope | undefined = NodeLexicalScopeUtils.getLexicalScope(parentNode);
  87. if (!lexicalScopeNode) {
  88. return functionDeclarationNode;
  89. }
  90. const isGlobalDeclaration: boolean = lexicalScopeNode.type === NodeType.Program;
  91. if (!this.options.renameGlobals && isGlobalDeclaration) {
  92. return functionDeclarationNode;
  93. }
  94. this.storeFunctionName(functionDeclarationNode, lexicalScopeNode, isGlobalDeclaration);
  95. // check for cached identifiers for current scope node. If exist - loop through them.
  96. if (this.replaceableIdentifiers.has(lexicalScopeNode)) {
  97. this.replaceScopeCachedIdentifiers(functionDeclarationNode, lexicalScopeNode);
  98. } else {
  99. this.replaceScopeIdentifiers(lexicalScopeNode);
  100. }
  101. return functionDeclarationNode;
  102. }
  103. /**
  104. * @param {FunctionDeclaration} functionDeclarationNode
  105. * @param {TNodeWithLexicalScope} lexicalScopeNode
  106. * @param {boolean} isGlobalDeclaration
  107. */
  108. private storeFunctionName (
  109. functionDeclarationNode: ESTree.FunctionDeclaration & { id: ESTree.Identifier },
  110. lexicalScopeNode: TNodeWithLexicalScope,
  111. isGlobalDeclaration: boolean
  112. ): void {
  113. if (isGlobalDeclaration) {
  114. this.identifierObfuscatingReplacer.storeGlobalName(functionDeclarationNode.id, lexicalScopeNode);
  115. } else {
  116. this.identifierObfuscatingReplacer.storeLocalName(functionDeclarationNode.id, lexicalScopeNode);
  117. }
  118. }
  119. /**
  120. * @param {FunctionDeclaration} functionDeclarationNode
  121. * @param {TNodeWithLexicalScope} lexicalScopeNode
  122. */
  123. private replaceScopeCachedIdentifiers (
  124. functionDeclarationNode: ESTree.FunctionDeclaration & { id: ESTree.Identifier },
  125. lexicalScopeNode: TNodeWithLexicalScope
  126. ): void {
  127. const cachedReplaceableIdentifiersNamesMap: TReplaceableIdentifiersNames =
  128. <TReplaceableIdentifiersNames>this.replaceableIdentifiers.get(lexicalScopeNode);
  129. const cachedReplaceableIdentifiers: ESTree.Identifier[] | undefined = cachedReplaceableIdentifiersNamesMap
  130. .get(functionDeclarationNode.id.name);
  131. if (!cachedReplaceableIdentifiers) {
  132. return;
  133. }
  134. const cachedReplaceableIdentifierLength: number = cachedReplaceableIdentifiers.length;
  135. for (let i: number = 0; i < cachedReplaceableIdentifierLength; i++) {
  136. const replaceableIdentifier: ESTree.Identifier = cachedReplaceableIdentifiers[i];
  137. const newReplaceableIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
  138. .replace(replaceableIdentifier, lexicalScopeNode);
  139. replaceableIdentifier.name = newReplaceableIdentifier.name;
  140. NodeMetadata.set(replaceableIdentifier, { renamedIdentifier: true });
  141. }
  142. }
  143. /**
  144. * @param {TNodeWithLexicalScope} lexicalScopeNode
  145. */
  146. private replaceScopeIdentifiers (lexicalScopeNode: TNodeWithLexicalScope): void {
  147. const storedReplaceableIdentifiersNamesMap: TReplaceableIdentifiersNames = new Map();
  148. estraverse.replace(lexicalScopeNode, {
  149. enter: (node: ESTree.Node, parentNode: ESTree.Node | null): void => {
  150. if (
  151. parentNode
  152. && (parentNode !== lexicalScopeNode)
  153. && NodeGuards.isReplaceableIdentifierNode(node, parentNode)
  154. && !NodeMetadata.isRenamedIdentifier(node)
  155. ) {
  156. const newIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
  157. .replace(node, lexicalScopeNode);
  158. const newIdentifierName: string = newIdentifier.name;
  159. if (node.name !== newIdentifierName) {
  160. node.name = newIdentifierName;
  161. NodeMetadata.set(node, { renamedIdentifier: true });
  162. } else {
  163. const storedReplaceableIdentifiers: ESTree.Identifier[] =
  164. storedReplaceableIdentifiersNamesMap.get(node.name) || [];
  165. storedReplaceableIdentifiers.push(node);
  166. storedReplaceableIdentifiersNamesMap.set(node.name, storedReplaceableIdentifiers);
  167. }
  168. }
  169. }
  170. });
  171. this.replaceableIdentifiers.set(lexicalScopeNode, storedReplaceableIdentifiersNamesMap);
  172. }
  173. }