VariableDeclarationTransformer.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { injectable, inject } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import * as estraverse from 'estraverse';
  4. import * as ESTree from 'estree';
  5. import { TNodeWithBlockStatement } from '../../types/node/TNodeWithBlockStatement';
  6. import { TObfuscationReplacerFactory } from '../../types/container/TObfuscationReplacerFactory';
  7. import { IOptions } from '../../interfaces/options/IOptions';
  8. import { IIdentifierReplacer } from '../../interfaces/node-transformers/IIdentifierReplacer';
  9. import { IVisitor } from '../../interfaces/IVisitor';
  10. import { ObfuscationReplacers } from '../../enums/container/ObfuscationReplacers';
  11. import { NodeType } from '../../enums/NodeType';
  12. import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
  13. import { Node } from '../../node/Node';
  14. import { NodeUtils } from '../../node/NodeUtils';
  15. /**
  16. * replaces:
  17. * var variable = 1;
  18. * variable++;
  19. *
  20. * on:
  21. * var _0x12d45f = 1;
  22. * _0x12d45f++;
  23. *
  24. */
  25. @injectable()
  26. export class VariableDeclarationTransformer extends AbstractNodeTransformer {
  27. /**
  28. * @type {IIdentifierReplacer}
  29. */
  30. private readonly identifierReplacer: IIdentifierReplacer;
  31. /**
  32. * @type {Map<ESTree.Node, ESTree.Identifier[]>}
  33. */
  34. private readonly replaceableIdentifiers: Map <ESTree.Node, ESTree.Identifier[]> = new Map();
  35. /**
  36. * @param obfuscatingReplacerFactory
  37. * @param options
  38. */
  39. constructor (
  40. @inject(ServiceIdentifiers.Factory__IObfuscationReplacer) obfuscatingReplacerFactory: TObfuscationReplacerFactory,
  41. @inject(ServiceIdentifiers.IOptions) options: IOptions
  42. ) {
  43. super(options);
  44. this.identifierReplacer = <IIdentifierReplacer>obfuscatingReplacerFactory(ObfuscationReplacers.IdentifierReplacer);
  45. }
  46. /**
  47. * @return {IVisitor}
  48. */
  49. public getVisitor (): IVisitor {
  50. return {
  51. enter: (node: ESTree.Node, parentNode: ESTree.Node) => {
  52. if (Node.isVariableDeclarationNode(node)) {
  53. return this.transformNode(node, parentNode);
  54. }
  55. }
  56. };
  57. }
  58. /**
  59. * @param variableDeclarationNode
  60. * @param parentNode
  61. * @returns {ESTree.Node}
  62. */
  63. public transformNode (variableDeclarationNode: ESTree.VariableDeclaration, parentNode: ESTree.Node): ESTree.Node {
  64. const blockScopeOfVariableDeclarationNode: TNodeWithBlockStatement = NodeUtils
  65. .getBlockScopesOfNode(variableDeclarationNode)[0];
  66. if (blockScopeOfVariableDeclarationNode.type === NodeType.Program) {
  67. return variableDeclarationNode;
  68. }
  69. const nodeIdentifier: number = this.nodeIdentifier++;
  70. const scopeNode: ESTree.Node = variableDeclarationNode.kind === 'var'
  71. ? blockScopeOfVariableDeclarationNode
  72. : parentNode;
  73. this.storeVariableNames(variableDeclarationNode, nodeIdentifier);
  74. // check for cached identifiers for current scope node. If exist - loop through them.
  75. if (this.replaceableIdentifiers.has(scopeNode)) {
  76. this.replaceScopeCachedIdentifiers(scopeNode, nodeIdentifier);
  77. } else {
  78. this.replaceScopeIdentifiers(scopeNode, nodeIdentifier);
  79. }
  80. return variableDeclarationNode;
  81. }
  82. /**
  83. * @param variableDeclarationNode
  84. * @param nodeIdentifier
  85. */
  86. private storeVariableNames (variableDeclarationNode: ESTree.VariableDeclaration, nodeIdentifier: number): void {
  87. variableDeclarationNode.declarations
  88. .forEach((declarationNode: ESTree.VariableDeclarator) => {
  89. if (Node.isObjectPatternNode(declarationNode.id)) {
  90. return estraverse.VisitorOption.Skip;
  91. }
  92. NodeUtils.typedTraverse(declarationNode.id, NodeType.Identifier, {
  93. enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name, nodeIdentifier)
  94. });
  95. });
  96. }
  97. /**
  98. * @param scopeNode
  99. * @param nodeIdentifier
  100. */
  101. private replaceScopeCachedIdentifiers (scopeNode: ESTree.Node, nodeIdentifier: number): void {
  102. const cachedReplaceableIdentifiers: ESTree.Identifier[] = <ESTree.Identifier[]>this.replaceableIdentifiers.get(scopeNode);
  103. cachedReplaceableIdentifiers.forEach((replaceableIdentifier: ESTree.Identifier) => {
  104. const newReplaceableIdentifier: ESTree.Identifier = this.identifierReplacer.replace(replaceableIdentifier.name, nodeIdentifier);
  105. replaceableIdentifier.name = newReplaceableIdentifier.name;
  106. });
  107. }
  108. /**
  109. * @param scopeNode
  110. * @param nodeIdentifier
  111. */
  112. private replaceScopeIdentifiers (scopeNode: ESTree.Node, nodeIdentifier: number): void {
  113. const storedReplaceableIdentifiers: ESTree.Identifier[] = [];
  114. estraverse.replace(scopeNode, {
  115. enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
  116. if (!node.obfuscatedNode && Node.isReplaceableIdentifierNode(node, parentNode)) {
  117. const newIdentifier: ESTree.Identifier = this.identifierReplacer.replace(node.name, nodeIdentifier);
  118. if (node.name === newIdentifier.name) {
  119. storedReplaceableIdentifiers.push(node);
  120. return node;
  121. }
  122. return newIdentifier;
  123. }
  124. }
  125. });
  126. this.replaceableIdentifiers.set(scopeNode, storedReplaceableIdentifiers);
  127. }
  128. }