ClassDeclarationTransformer.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. * class Foo { //... };
  23. * new Foo();
  24. *
  25. * on:
  26. * class _0x12d45f { //... };
  27. * new _0x12d45f();
  28. */
  29. @injectable()
  30. export class ClassDeclarationTransformer 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.isClassDeclarationNode(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 {ClassDeclaration} classDeclarationNode
  79. * @param {NodeGuards} parentNode
  80. * @returns {NodeGuards}
  81. */
  82. public transformNode (classDeclarationNode: ESTree.ClassDeclaration, parentNode: ESTree.Node): ESTree.Node {
  83. const lexicalScopeNode: TNodeWithLexicalScope | undefined = NodeLexicalScopeUtils.getLexicalScope(classDeclarationNode);
  84. if (!lexicalScopeNode) {
  85. return classDeclarationNode;
  86. }
  87. const isGlobalDeclaration: boolean = lexicalScopeNode.type === NodeType.Program;
  88. if (!this.options.renameGlobals && isGlobalDeclaration) {
  89. return classDeclarationNode;
  90. }
  91. this.storeClassName(classDeclarationNode, lexicalScopeNode, isGlobalDeclaration);
  92. // check for cached identifiers for current scope node. If exist - loop through them.
  93. if (this.replaceableIdentifiers.has(lexicalScopeNode)) {
  94. this.replaceScopeCachedIdentifiers(classDeclarationNode, lexicalScopeNode);
  95. } else {
  96. this.replaceScopeIdentifiers(lexicalScopeNode);
  97. }
  98. return classDeclarationNode;
  99. }
  100. /**
  101. * @param {ClassDeclaration} classDeclarationNode
  102. * @param {TNodeWithLexicalScope} lexicalScopeNode
  103. * @param {boolean} isGlobalDeclaration
  104. */
  105. private storeClassName (
  106. classDeclarationNode: ESTree.ClassDeclaration,
  107. lexicalScopeNode: TNodeWithLexicalScope,
  108. isGlobalDeclaration: boolean
  109. ): void {
  110. if (isGlobalDeclaration) {
  111. this.identifierObfuscatingReplacer.storeGlobalName(classDeclarationNode.id.name, lexicalScopeNode);
  112. } else {
  113. this.identifierObfuscatingReplacer.storeLocalName(classDeclarationNode.id.name, lexicalScopeNode);
  114. }
  115. }
  116. /**
  117. * @param {ClassDeclaration} classDeclarationNode
  118. * @param {TNodeWithLexicalScope} lexicalScopeNode
  119. */
  120. private replaceScopeCachedIdentifiers (
  121. classDeclarationNode: ESTree.ClassDeclaration,
  122. lexicalScopeNode: TNodeWithLexicalScope
  123. ): void {
  124. const cachedReplaceableIdentifiersNamesMap: TReplaceableIdentifiersNames =
  125. <TReplaceableIdentifiersNames>this.replaceableIdentifiers.get(lexicalScopeNode);
  126. const cachedReplaceableIdentifiers: ESTree.Identifier[] | undefined = cachedReplaceableIdentifiersNamesMap
  127. .get(classDeclarationNode.id.name);
  128. if (!cachedReplaceableIdentifiers) {
  129. return;
  130. }
  131. const cachedReplaceableIdentifierLength: number = cachedReplaceableIdentifiers.length;
  132. for (let i: number = 0; i < cachedReplaceableIdentifierLength; i++) {
  133. const replaceableIdentifier: ESTree.Identifier = cachedReplaceableIdentifiers[i];
  134. const newReplaceableIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
  135. .replace(replaceableIdentifier.name, lexicalScopeNode);
  136. replaceableIdentifier.name = newReplaceableIdentifier.name;
  137. NodeMetadata.set(replaceableIdentifier, { renamedIdentifier: true });
  138. }
  139. }
  140. /**
  141. * @param {TNodeWithLexicalScope} lexicalScopeNode
  142. */
  143. private replaceScopeIdentifiers (lexicalScopeNode: TNodeWithLexicalScope): void {
  144. const storedReplaceableIdentifiersNamesMap: TReplaceableIdentifiersNames = new Map();
  145. estraverse.replace(lexicalScopeNode, {
  146. enter: (node: ESTree.Node, parentNode: ESTree.Node | null): void => {
  147. if (
  148. parentNode
  149. && NodeGuards.isReplaceableIdentifierNode(node, parentNode)
  150. && !NodeMetadata.isRenamedIdentifier(node)
  151. ) {
  152. const newIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
  153. .replace(node.name, lexicalScopeNode);
  154. const newIdentifierName: string = newIdentifier.name;
  155. if (node.name !== newIdentifierName) {
  156. node.name = newIdentifierName;
  157. NodeMetadata.set(node, { renamedIdentifier: true });
  158. } else {
  159. const storedReplaceableIdentifiers: ESTree.Identifier[] =
  160. storedReplaceableIdentifiersNamesMap.get(node.name) || [];
  161. storedReplaceableIdentifiers.push(node);
  162. storedReplaceableIdentifiersNamesMap.set(node.name, storedReplaceableIdentifiers);
  163. }
  164. }
  165. }
  166. });
  167. this.replaceableIdentifiers.set(lexicalScopeNode, storedReplaceableIdentifiersNamesMap);
  168. }
  169. }