CustomCodeHelpersTransformer.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
  3. import * as ESTree from 'estree';
  4. import { TCustomCodeHelperGroupStorage } from '../../types/storages/TCustomCodeHelperGroupStorage';
  5. import { ICustomCodeHelperGroup } from '../../interfaces/custom-code-helpers/ICustomCodeHelperGroup';
  6. import { IObfuscationEventEmitter } from '../../interfaces/event-emitters/IObfuscationEventEmitter';
  7. import { IOptions } from '../../interfaces/options/IOptions';
  8. import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
  9. import { ICallsGraphAnalyzer } from '../../interfaces/analyzers/calls-graph-analyzer/ICallsGraphAnalyzer';
  10. import { ICallsGraphData } from '../../interfaces/analyzers/calls-graph-analyzer/ICallsGraphData';
  11. import { IPrevailingKindOfVariablesAnalyzer } from '../../interfaces/analyzers/calls-graph-analyzer/IPrevailingKindOfVariablesAnalyzer';
  12. import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
  13. import { NodeTransformer } from '../../enums/node-transformers/NodeTransformer';
  14. import { ObfuscationEvent } from '../../enums/event-emitters/ObfuscationEvent';
  15. import { NodeTransformationStage } from '../../enums/node-transformers/NodeTransformationStage';
  16. import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
  17. import { NodeGuards } from '../../node/NodeGuards';
  18. /**
  19. * Analyzing AST-tree and appending custom code helpers
  20. */
  21. @injectable()
  22. export class CustomCodeHelpersTransformer extends AbstractNodeTransformer {
  23. /**
  24. * @type {NodeTransformer.ParentificationTransformer[]}
  25. */
  26. public readonly runAfter: NodeTransformer[] = [
  27. NodeTransformer.ParentificationTransformer,
  28. NodeTransformer.VariablePreserveTransformer
  29. ];
  30. /**
  31. * @type {TCustomCodeHelperGroupStorage}
  32. */
  33. private readonly customCodeHelperGroupStorage: TCustomCodeHelperGroupStorage;
  34. /**
  35. * @type {IObfuscationEventEmitter}
  36. */
  37. private readonly obfuscationEventEmitter: IObfuscationEventEmitter;
  38. /**
  39. * @type {ICallsGraphAnalyzer}
  40. */
  41. private readonly callsGraphAnalyzer: ICallsGraphAnalyzer;
  42. /**
  43. * @type {ICallsGraphData[]}
  44. */
  45. private callsGraphData: ICallsGraphData[] = [];
  46. /**
  47. * @type {IPrevailingKindOfVariablesAnalyzer}
  48. */
  49. private readonly prevailingKindOfVariablesAnalyzer: IPrevailingKindOfVariablesAnalyzer;
  50. /**
  51. * @param {ICallsGraphAnalyzer} callsGraphAnalyzer
  52. * @param {IPrevailingKindOfVariablesAnalyzer} prevailingKindOfVariablesAnalyzer
  53. * @param {IObfuscationEventEmitter} obfuscationEventEmitter
  54. * @param {TCustomCodeHelperGroupStorage} customCodeHelperGroupStorage
  55. * @param {IRandomGenerator} randomGenerator
  56. * @param {IOptions} options
  57. */
  58. public constructor (
  59. @inject(ServiceIdentifiers.ICallsGraphAnalyzer) callsGraphAnalyzer: ICallsGraphAnalyzer,
  60. @inject(ServiceIdentifiers.IPrevailingKindOfVariablesAnalyzer)
  61. prevailingKindOfVariablesAnalyzer: IPrevailingKindOfVariablesAnalyzer,
  62. @inject(ServiceIdentifiers.IObfuscationEventEmitter) obfuscationEventEmitter: IObfuscationEventEmitter,
  63. @inject(ServiceIdentifiers.TCustomNodeGroupStorage) customCodeHelperGroupStorage: TCustomCodeHelperGroupStorage,
  64. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  65. @inject(ServiceIdentifiers.IOptions) options: IOptions
  66. ) {
  67. super(randomGenerator, options);
  68. this.callsGraphAnalyzer = callsGraphAnalyzer;
  69. this.prevailingKindOfVariablesAnalyzer = prevailingKindOfVariablesAnalyzer;
  70. this.obfuscationEventEmitter = obfuscationEventEmitter;
  71. this.customCodeHelperGroupStorage = customCodeHelperGroupStorage;
  72. }
  73. /**
  74. * @param {NodeTransformationStage} nodeTransformationStage
  75. * @returns {IVisitor | null}
  76. */
  77. public getVisitor (nodeTransformationStage: NodeTransformationStage): IVisitor | null {
  78. switch (nodeTransformationStage) {
  79. case NodeTransformationStage.Preparing:
  80. return {
  81. leave: (node: ESTree.Node, parentNode: ESTree.Node | null): ESTree.Node | undefined => {
  82. if (NodeGuards.isProgramNode(node)) {
  83. this.prepareNode(node, parentNode);
  84. this.appendCustomNodesBeforeObfuscation(node, parentNode);
  85. return this.transformNode(node, parentNode);
  86. }
  87. }
  88. };
  89. case NodeTransformationStage.Finalizing:
  90. return {
  91. leave: (node: ESTree.Node, parentNode: ESTree.Node | null): void => {
  92. if (NodeGuards.isProgramNode(node)) {
  93. this.appendCustomNodesAfterObfuscation(node, parentNode);
  94. }
  95. }
  96. };
  97. default:
  98. return null;
  99. }
  100. }
  101. /**
  102. * @param {Program} node
  103. * @param {Node | null} parentNode
  104. */
  105. public prepareNode (node: ESTree.Program, parentNode: ESTree.Node | null): void {
  106. this.callsGraphData = this.callsGraphAnalyzer.analyze(node);
  107. this.prevailingKindOfVariablesAnalyzer.analyze(node);
  108. }
  109. /**
  110. * @param {Program} node
  111. * @param {Node | null} parentNode
  112. * @returns {Node}
  113. */
  114. public transformNode (node: ESTree.Program, parentNode: ESTree.Node | null): ESTree.Node {
  115. return node;
  116. }
  117. /**
  118. * @param {Program} node
  119. * @param {Node | null} parentNode
  120. */
  121. private appendCustomNodesBeforeObfuscation (node: ESTree.Program, parentNode: ESTree.Node | null): void {
  122. this.customCodeHelperGroupStorage
  123. .getStorage()
  124. .forEach((customCodeHelperGroup: ICustomCodeHelperGroup) => {
  125. customCodeHelperGroup.initialize();
  126. this.obfuscationEventEmitter.once(
  127. customCodeHelperGroup.getAppendEvent(),
  128. customCodeHelperGroup.appendNodes.bind(customCodeHelperGroup)
  129. );
  130. });
  131. this.obfuscationEventEmitter.emit(ObfuscationEvent.BeforeObfuscation, node, this.callsGraphData);
  132. }
  133. /**
  134. * @param {Program} node
  135. * @param {Node | null} parentNode
  136. */
  137. private appendCustomNodesAfterObfuscation (node: ESTree.Program, parentNode: ESTree.Node | null): void {
  138. this.obfuscationEventEmitter.emit(ObfuscationEvent.AfterObfuscation, node, this.callsGraphData);
  139. }
  140. }