FunctionTransformer.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 { IIdentifierObfuscatingReplacer } from '../../interfaces/node-transformers/obfuscating-transformers/obfuscating-replacers/IIdentifierObfuscatingReplacer';
  8. import { IOptions } from '../../interfaces/options/IOptions';
  9. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  10. import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
  11. import { IdentifierObfuscatingReplacer } from '../../enums/node-transformers/obfuscating-transformers/obfuscating-replacers/IdentifierObfuscatingReplacer';
  12. import { TransformationStage } from '../../enums/node-transformers/TransformationStage';
  13. import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
  14. import { NodeGuards } from '../../node/NodeGuards';
  15. import { NodeLexicalScopeUtils } from '../../node/NodeLexicalScopeUtils';
  16. import { NodeMetadata } from '../../node/NodeMetadata';
  17. import { NodeType } from '../../enums/node/NodeType';
  18. /**
  19. * replaces:
  20. * function foo (argument1) { return argument1; };
  21. *
  22. * on:
  23. * function foo (_0x12d45f) { return _0x12d45f; };
  24. *
  25. */
  26. @injectable()
  27. export class FunctionTransformer extends AbstractNodeTransformer {
  28. /**
  29. * @type {IIdentifierObfuscatingReplacer}
  30. */
  31. private readonly identifierObfuscatingReplacer: IIdentifierObfuscatingReplacer;
  32. /**
  33. * @param {TIdentifierObfuscatingReplacerFactory} identifierObfuscatingReplacerFactory
  34. * @param {IRandomGenerator} randomGenerator
  35. * @param {IOptions} options
  36. */
  37. constructor (
  38. @inject(ServiceIdentifiers.Factory__IIdentifierObfuscatingReplacer)
  39. identifierObfuscatingReplacerFactory: TIdentifierObfuscatingReplacerFactory,
  40. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  41. @inject(ServiceIdentifiers.IOptions) options: IOptions
  42. ) {
  43. super(randomGenerator, options);
  44. this.identifierObfuscatingReplacer = identifierObfuscatingReplacerFactory(
  45. IdentifierObfuscatingReplacer.BaseIdentifierObfuscatingReplacer
  46. );
  47. }
  48. /**
  49. * @param {Node} node
  50. * @param {Node} parentNode
  51. * @returns {boolean}
  52. */
  53. private static isProhibitedIdentifierOfPropertyNode (
  54. node: ESTree.Node,
  55. parentNode: ESTree.Node | null
  56. ): node is ESTree.Identifier {
  57. return NodeGuards.isIdentifierNode(node)
  58. && !!parentNode
  59. && NodeGuards.isPropertyNode(parentNode)
  60. && parentNode.key === node;
  61. }
  62. /**
  63. * @param {Node} node
  64. * @returns {boolean}
  65. */
  66. private static isProhibitedIdentifierOfShorthandPropertyNode (
  67. node: ESTree.Node,
  68. ): node is ESTree.Property & {key: ESTree.Identifier} {
  69. return NodeGuards.isPropertyNode(node)
  70. && node.shorthand
  71. && NodeGuards.isIdentifierNode(node.key);
  72. }
  73. /**
  74. * @param {TransformationStage} transformationStage
  75. * @returns {IVisitor | null}
  76. */
  77. public getVisitor (transformationStage: TransformationStage): IVisitor | null {
  78. switch (transformationStage) {
  79. case TransformationStage.Obfuscating:
  80. return {
  81. enter: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
  82. if (parentNode && NodeGuards.isFunctionNode(node)) {
  83. return this.transformNode(node, parentNode);
  84. }
  85. }
  86. };
  87. default:
  88. return null;
  89. }
  90. }
  91. /**
  92. * @param {Function} functionNode
  93. * @param {NodeGuards} parentNode
  94. * @returns {NodeGuards}
  95. */
  96. public transformNode (functionNode: ESTree.Function, parentNode: ESTree.Node): ESTree.Node {
  97. const lexicalScopeNode: TNodeWithLexicalScope | undefined = NodeLexicalScopeUtils.getLexicalScope(functionNode);
  98. if (!lexicalScopeNode) {
  99. return functionNode;
  100. }
  101. this.storeFunctionParams(functionNode, lexicalScopeNode);
  102. this.replaceFunctionParams(functionNode, lexicalScopeNode);
  103. return functionNode;
  104. }
  105. /**
  106. * @param {Identifier} node
  107. * @param {Node} parentNode
  108. * @returns {boolean}
  109. */
  110. private isGlobalFunctionDeclarationIdentifier (node: ESTree.Identifier, parentNode: ESTree.Node): boolean {
  111. if (!NodeGuards.isFunctionDeclarationNode(parentNode) || parentNode.id !== node) {
  112. return false
  113. }
  114. const lexicalScopeNode: TNodeWithLexicalScope | undefined = NodeLexicalScopeUtils.getLexicalScopes(parentNode)[1];
  115. if (!lexicalScopeNode) {
  116. return false;
  117. }
  118. const isGlobalDeclaration: boolean = lexicalScopeNode.type === NodeType.Program;
  119. return !this.options.renameGlobals && isGlobalDeclaration;
  120. }
  121. /**
  122. * @param {Function} functionNode
  123. * @param {TNodeWithLexicalScope} lexicalScopeNode
  124. */
  125. private storeFunctionParams (functionNode: ESTree.Function, lexicalScopeNode: TNodeWithLexicalScope): void {
  126. const visitor: estraverse.Visitor = {
  127. enter: (node: ESTree.Node, parentNode: ESTree.Node | null): estraverse.VisitorOption | void => {
  128. // should check with identifier as first argument,
  129. // because prohibited identifier can be easily ignored
  130. if (FunctionTransformer.isProhibitedIdentifierOfPropertyNode(node, parentNode)) {
  131. return;
  132. }
  133. if (NodeGuards.isAssignmentPatternNode(node) && NodeGuards.isIdentifierNode(node.left)) {
  134. this.identifierObfuscatingReplacer.storeLocalName(node.left.name, lexicalScopeNode);
  135. return estraverse.VisitorOption.Skip;
  136. }
  137. if (NodeGuards.isIdentifierNode(node)) {
  138. this.identifierObfuscatingReplacer.storeLocalName(node.name, lexicalScopeNode);
  139. }
  140. }
  141. };
  142. functionNode.params.forEach((paramsNode: ESTree.Node) => {
  143. estraverse.traverse(paramsNode, visitor);
  144. });
  145. }
  146. /**
  147. * @param {Function} functionNode
  148. * @param {TNodeWithLexicalScope} lexicalScopeNode
  149. * @param {Set<string>} ignoredIdentifierNamesSet
  150. */
  151. private replaceFunctionParams (
  152. functionNode: ESTree.Function,
  153. lexicalScopeNode: TNodeWithLexicalScope,
  154. ignoredIdentifierNamesSet: Set <string> = new Set()
  155. ): void {
  156. const visitor: estraverse.Visitor = {
  157. enter: (node: ESTree.Node, parentNode: ESTree.Node | null): void | estraverse.VisitorOption => {
  158. /**
  159. * should process nested functions in different traverse loop to avoid wrong code generation
  160. */
  161. if (NodeGuards.isFunctionNode(node) && node !== functionNode) {
  162. this.replaceFunctionParams(node, lexicalScopeNode, new Set(ignoredIdentifierNamesSet));
  163. return estraverse.VisitorOption.Skip;
  164. }
  165. /**
  166. * should ignore all shorthand `key` identifiers of the `PropertyNode`
  167. */
  168. if (FunctionTransformer.isProhibitedIdentifierOfShorthandPropertyNode(node)) {
  169. ignoredIdentifierNamesSet.add(node.key.name);
  170. return;
  171. }
  172. if (
  173. parentNode
  174. && NodeGuards.isReplaceableIdentifierNode(node, parentNode)
  175. && !NodeMetadata.isRenamedIdentifier(node)
  176. && !ignoredIdentifierNamesSet.has(node.name)
  177. ) {
  178. // should ignore identifiers of global function declarations
  179. if (this.isGlobalFunctionDeclarationIdentifier(node, parentNode)) {
  180. return;
  181. }
  182. const newIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
  183. .replace(node.name, lexicalScopeNode);
  184. const newIdentifierName: string = newIdentifier.name;
  185. if (node.name !== newIdentifierName) {
  186. node.name = newIdentifierName;
  187. NodeMetadata.set(node, { renamedIdentifier: true });
  188. }
  189. }
  190. }
  191. };
  192. estraverse.replace(functionNode, visitor);
  193. }
  194. }