FunctionDeclarationTransformer.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 { TNodeWithBlockScope } from '../../types/node/TNodeWithBlockScope';
  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 { NodeMetadata } from '../../node/NodeMetadata';
  19. import { NodeUtils } from '../../node/NodeUtils';
  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 (functionDeclarationNode: ESTree.FunctionDeclaration, parentNode: ESTree.Node): ESTree.Node {
  83. const blockScopeNode: TNodeWithBlockScope = NodeUtils.getBlockScopeOfNode(functionDeclarationNode);
  84. const isGlobalDeclaration: boolean = blockScopeNode.type === NodeType.Program;
  85. if (!this.options.renameGlobals && isGlobalDeclaration) {
  86. return functionDeclarationNode;
  87. }
  88. this.storeFunctionName(functionDeclarationNode, blockScopeNode, isGlobalDeclaration);
  89. // check for cached identifiers for current scope node. If exist - loop through them.
  90. if (this.replaceableIdentifiers.has(blockScopeNode)) {
  91. this.replaceScopeCachedIdentifiers(functionDeclarationNode, blockScopeNode);
  92. } else {
  93. this.replaceScopeIdentifiers(blockScopeNode);
  94. }
  95. return functionDeclarationNode;
  96. }
  97. /**
  98. * @param {FunctionDeclaration} functionDeclarationNode
  99. * @param {TNodeWithBlockScope} blockScopeNode
  100. * @param {boolean} isGlobalDeclaration
  101. */
  102. private storeFunctionName (
  103. functionDeclarationNode: ESTree.FunctionDeclaration,
  104. blockScopeNode: TNodeWithBlockScope,
  105. isGlobalDeclaration: boolean
  106. ): void {
  107. if (isGlobalDeclaration) {
  108. this.identifierObfuscatingReplacer.storeGlobalName(functionDeclarationNode.id.name, blockScopeNode);
  109. } else {
  110. this.identifierObfuscatingReplacer.storeLocalName(functionDeclarationNode.id.name, blockScopeNode);
  111. }
  112. }
  113. /**
  114. * @param {FunctionDeclaration} functionDeclarationNode
  115. * @param {TNodeWithBlockScope} blockScopeNode
  116. */
  117. private replaceScopeCachedIdentifiers (
  118. functionDeclarationNode: ESTree.FunctionDeclaration,
  119. blockScopeNode: TNodeWithBlockScope
  120. ): void {
  121. const cachedReplaceableIdentifiersNamesMap: TReplaceableIdentifiersNames =
  122. <TReplaceableIdentifiersNames>this.replaceableIdentifiers.get(blockScopeNode);
  123. const cachedReplaceableIdentifiers: ESTree.Identifier[] | undefined = cachedReplaceableIdentifiersNamesMap
  124. .get(functionDeclarationNode.id.name);
  125. if (!cachedReplaceableIdentifiers) {
  126. return;
  127. }
  128. const cachedReplaceableIdentifierLength: number = cachedReplaceableIdentifiers.length;
  129. for (let i: number = 0; i < cachedReplaceableIdentifierLength; i++) {
  130. const replaceableIdentifier: ESTree.Identifier = cachedReplaceableIdentifiers[i];
  131. const newReplaceableIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
  132. .replace(replaceableIdentifier.name, blockScopeNode);
  133. replaceableIdentifier.name = newReplaceableIdentifier.name;
  134. NodeMetadata.set(replaceableIdentifier, { renamedIdentifier: true });
  135. }
  136. }
  137. /**
  138. * @param {TNodeWithBlockScope} blockScopeNode
  139. */
  140. private replaceScopeIdentifiers (blockScopeNode: TNodeWithBlockScope): void {
  141. const storedReplaceableIdentifiersNamesMap: TReplaceableIdentifiersNames = new Map();
  142. estraverse.replace(blockScopeNode, {
  143. enter: (node: ESTree.Node, parentNode: ESTree.Node | null): void => {
  144. if (
  145. parentNode
  146. && NodeGuards.isReplaceableIdentifierNode(node, parentNode)
  147. && !NodeMetadata.isRenamedIdentifier(node)
  148. ) {
  149. const newIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
  150. .replace(node.name, blockScopeNode);
  151. const newIdentifierName: string = newIdentifier.name;
  152. if (node.name !== newIdentifierName) {
  153. node.name = newIdentifierName;
  154. NodeMetadata.set(node, { renamedIdentifier: true });
  155. } else {
  156. const storedReplaceableIdentifiers: ESTree.Identifier[] =
  157. storedReplaceableIdentifiersNamesMap.get(node.name) || [];
  158. storedReplaceableIdentifiers.push(node);
  159. storedReplaceableIdentifiersNamesMap.set(node.name, storedReplaceableIdentifiers);
  160. }
  161. }
  162. }
  163. });
  164. this.replaceableIdentifiers.set(blockScopeNode, storedReplaceableIdentifiersNamesMap);
  165. }
  166. }