VariableDeclarationTransformer.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 { IOptions } from '../../interfaces/options/IOptions';
  7. import { IObfuscationReplacer } from '../../interfaces/node-transformers/IObfuscationReplacer';
  8. import { IObfuscationReplacerWithStorage } from '../../interfaces/node-transformers/IObfuscationReplacerWithStorage';
  9. import { IVisitor } from '../../interfaces/IVisitor';
  10. import { NodeObfuscatorsReplacers } from '../../enums/container/NodeObfuscationReplacers';
  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 {IObfuscationReplacerWithStorage}
  29. */
  30. private readonly identifierReplacer: IObfuscationReplacerWithStorage;
  31. /**
  32. * @type {Map<ESTree.Node, ESTree.Identifier[]>}
  33. */
  34. private readonly replaceableIdentifiers: Map <ESTree.Node, ESTree.Identifier[]> = new Map();
  35. /**
  36. * @param replacersFactory
  37. * @param options
  38. */
  39. constructor (
  40. @inject(ServiceIdentifiers.Factory__IObfuscatorReplacer) replacersFactory: (replacer: NodeObfuscatorsReplacers) => IObfuscationReplacer,
  41. @inject(ServiceIdentifiers.IOptions) options: IOptions
  42. ) {
  43. super(options);
  44. this.identifierReplacer = <IObfuscationReplacerWithStorage>replacersFactory(NodeObfuscatorsReplacers.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. this.replaceVariableNames(scopeNode, nodeIdentifier);
  75. return variableDeclarationNode;
  76. }
  77. /**
  78. * @param variableDeclarationNode
  79. * @param nodeIdentifier
  80. */
  81. private storeVariableNames (variableDeclarationNode: ESTree.VariableDeclaration, nodeIdentifier: number): void {
  82. variableDeclarationNode.declarations
  83. .forEach((declarationNode: ESTree.VariableDeclarator) => {
  84. if (Node.isObjectPatternNode(declarationNode.id)) {
  85. return estraverse.VisitorOption.Skip;
  86. }
  87. NodeUtils.typedTraverse(declarationNode.id, NodeType.Identifier, {
  88. enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name, nodeIdentifier)
  89. });
  90. });
  91. }
  92. /**
  93. * @param scopeNode
  94. * @param nodeIdentifier
  95. */
  96. private replaceVariableNames (scopeNode: ESTree.Node, nodeIdentifier: number): void {
  97. let replaceableIdentifiersForCurrentScope: ESTree.Identifier[];
  98. // check for cached identifiers for current scope node. If exist - loop through them.
  99. if (this.replaceableIdentifiers.has(scopeNode)) {
  100. replaceableIdentifiersForCurrentScope = <ESTree.Identifier[]>this.replaceableIdentifiers.get(scopeNode);
  101. replaceableIdentifiersForCurrentScope.forEach((replaceableIdentifier: ESTree.Identifier) => {
  102. replaceableIdentifier.name = this.identifierReplacer.replace(replaceableIdentifier.name, nodeIdentifier);
  103. });
  104. return;
  105. }
  106. replaceableIdentifiersForCurrentScope = [];
  107. estraverse.replace(scopeNode, {
  108. enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
  109. if (!node.obfuscatedNode && Node.isReplaceableIdentifierNode(node, parentNode)) {
  110. const newNodeName: string = this.identifierReplacer.replace(node.name, nodeIdentifier);
  111. if (node.name !== newNodeName) {
  112. node.name = newNodeName;
  113. } else {
  114. replaceableIdentifiersForCurrentScope.push(node);
  115. }
  116. }
  117. }
  118. });
  119. this.replaceableIdentifiers.set(scopeNode, replaceableIdentifiersForCurrentScope);
  120. }
  121. }