FunctionDeclarationTransformer.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 { NodeUtils } from '../../node/NodeUtils';
  19. /**
  20. * replaces:
  21. * function foo () { //... };
  22. * foo();
  23. *
  24. * on:
  25. * function _0x12d45f () { //... };
  26. * _0x12d45f();
  27. */
  28. @injectable()
  29. export class FunctionDeclarationTransformer extends AbstractNodeTransformer {
  30. /**
  31. * @type {IIdentifierObfuscatingReplacer}
  32. */
  33. private readonly identifierObfuscatingReplacer: IIdentifierObfuscatingReplacer;
  34. /**
  35. * @type {Map<ESTree.Node, ESTree.Identifier[]>}
  36. */
  37. private readonly replaceableIdentifiers: TReplaceableIdentifiers = new Map();
  38. /**
  39. * @param {TIdentifierObfuscatingReplacerFactory} identifierObfuscatingReplacerFactory
  40. * @param {IRandomGenerator} randomGenerator
  41. * @param {IOptions} options
  42. */
  43. constructor (
  44. @inject(ServiceIdentifiers.Factory__IIdentifierObfuscatingReplacer)
  45. identifierObfuscatingReplacerFactory: TIdentifierObfuscatingReplacerFactory,
  46. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  47. @inject(ServiceIdentifiers.IOptions) options: IOptions
  48. ) {
  49. super(randomGenerator, options);
  50. this.identifierObfuscatingReplacer = identifierObfuscatingReplacerFactory(
  51. IdentifierObfuscatingReplacer.BaseIdentifierObfuscatingReplacer
  52. );
  53. }
  54. /**
  55. * @param {TransformationStage} transformationStage
  56. * @returns {IVisitor | null}
  57. */
  58. public getVisitor (transformationStage: TransformationStage): IVisitor | null {
  59. switch (transformationStage) {
  60. case TransformationStage.Obfuscating:
  61. return {
  62. enter: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
  63. if (parentNode && NodeGuards.isFunctionDeclarationNode(node)) {
  64. return this.transformNode(node, parentNode);
  65. }
  66. }
  67. };
  68. default:
  69. return null;
  70. }
  71. }
  72. /**
  73. * @param {FunctionDeclaration} functionDeclarationNode
  74. * @param {NodeGuards} parentNode
  75. * @returns {NodeGuards}
  76. */
  77. public transformNode (functionDeclarationNode: ESTree.FunctionDeclaration, parentNode: ESTree.Node): ESTree.Node {
  78. const nodeIdentifier: number = this.nodeIdentifier++;
  79. const blockScopeNode: TNodeWithBlockScope = NodeUtils.getBlockScopesOfNode(functionDeclarationNode)[0];
  80. const isGlobalDeclaration: boolean = blockScopeNode.type === NodeType.Program;
  81. if (!this.options.renameGlobals && isGlobalDeclaration) {
  82. return functionDeclarationNode;
  83. }
  84. this.storeFunctionName(functionDeclarationNode, nodeIdentifier);
  85. // check for cached identifiers for current scope node. If exist - loop through them.
  86. if (this.replaceableIdentifiers.has(blockScopeNode)) {
  87. this.replaceScopeCachedIdentifiers(functionDeclarationNode, blockScopeNode, nodeIdentifier);
  88. } else {
  89. this.replaceScopeIdentifiers(blockScopeNode, nodeIdentifier);
  90. }
  91. return functionDeclarationNode;
  92. }
  93. /**
  94. * @param {FunctionDeclaration} functionDeclarationNode
  95. * @param {number} nodeIdentifier
  96. */
  97. private storeFunctionName (functionDeclarationNode: ESTree.FunctionDeclaration, nodeIdentifier: number): void {
  98. this.identifierObfuscatingReplacer.storeNames(functionDeclarationNode.id.name, nodeIdentifier);
  99. }
  100. /**
  101. * @param {FunctionDeclaration} functionDeclarationNode
  102. * @param {TNodeWithBlockScope} blockScopeNode
  103. * @param {number} nodeIdentifier
  104. */
  105. private replaceScopeCachedIdentifiers (
  106. functionDeclarationNode: ESTree.FunctionDeclaration,
  107. blockScopeNode: TNodeWithBlockScope,
  108. nodeIdentifier: number
  109. ): void {
  110. const cachedReplaceableIdentifiersNamesMap: TReplaceableIdentifiersNames | undefined = this.replaceableIdentifiers.get(blockScopeNode);
  111. if (!cachedReplaceableIdentifiersNamesMap) {
  112. return;
  113. }
  114. const cachedReplaceableIdentifiers: ESTree.Identifier[] | undefined = cachedReplaceableIdentifiersNamesMap
  115. .get(functionDeclarationNode.id.name);
  116. if (!cachedReplaceableIdentifiers) {
  117. return;
  118. }
  119. const cachedReplaceableIdentifierLength: number = cachedReplaceableIdentifiers.length;
  120. for (let i: number = 0; i < cachedReplaceableIdentifierLength; i++) {
  121. const replaceableIdentifier: ESTree.Identifier = cachedReplaceableIdentifiers[i];
  122. const newReplaceableIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
  123. .replace(replaceableIdentifier.name, nodeIdentifier);
  124. replaceableIdentifier.name = newReplaceableIdentifier.name;
  125. }
  126. }
  127. /**
  128. * @param {TNodeWithBlockScope} blockScopeNode
  129. * @param {number} nodeIdentifier
  130. */
  131. private replaceScopeIdentifiers (blockScopeNode: TNodeWithBlockScope, nodeIdentifier: number): void {
  132. const storedReplaceableIdentifiersNamesMap: TReplaceableIdentifiersNames = new Map();
  133. estraverse.replace(blockScopeNode, {
  134. enter: (node: ESTree.Node, parentNode: ESTree.Node | null): any => {
  135. if (parentNode && NodeGuards.isReplaceableIdentifierNode(node, parentNode)) {
  136. const newIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
  137. .replace(node.name, nodeIdentifier);
  138. const newIdentifierName: string = newIdentifier.name;
  139. if (node.name !== newIdentifierName) {
  140. node.name = newIdentifierName;
  141. } else {
  142. const storedReplaceableIdentifiers: ESTree.Identifier[] = storedReplaceableIdentifiersNamesMap.get(node.name) || [];
  143. storedReplaceableIdentifiers.push(node);
  144. storedReplaceableIdentifiersNamesMap.set(node.name, storedReplaceableIdentifiers);
  145. }
  146. }
  147. }
  148. });
  149. this.replaceableIdentifiers.set(blockScopeNode, storedReplaceableIdentifiersNamesMap);
  150. }
  151. }