DebugProtectionCustomNodeGroup.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
  3. import { TCustomNodeFactory } from '../../../types/container/custom-nodes/TCustomNodeFactory';
  4. import { TIdentifierNamesGeneratorFactory } from '../../../types/container/generators/TIdentifierNamesGeneratorFactory';
  5. import { TInitialData } from '../../../types/TInitialData';
  6. import { TNodeWithStatements } from '../../../types/node/TNodeWithStatements';
  7. import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
  8. import { IOptions } from '../../../interfaces/options/IOptions';
  9. import { IRandomGenerator } from '../../../interfaces/utils/IRandomGenerator';
  10. import { ICallsGraphData } from '../../../interfaces/analyzers/calls-graph-analyzer/ICallsGraphData';
  11. import { initializable } from '../../../decorators/Initializable';
  12. import { CustomNode } from '../../../enums/custom-nodes/CustomNode';
  13. import { ObfuscationEvent } from '../../../enums/event-emitters/ObfuscationEvent';
  14. import { AbstractCustomNodeGroup } from '../../AbstractCustomNodeGroup';
  15. import { DebugProtectionFunctionNode } from '../DebugProtectionFunctionNode';
  16. import { DebugProtectionFunctionCallNode } from '../DebugProtectionFunctionCallNode';
  17. import { DebugProtectionFunctionIntervalNode } from '../DebugProtectionFunctionIntervalNode';
  18. import { NodeAppender } from '../../../node/NodeAppender';
  19. import { NodeCallsControllerFunctionNode } from '../../node-calls-controller-nodes/NodeCallsControllerFunctionNode';
  20. import { NodeGuards } from '../../../node/NodeGuards';
  21. @injectable()
  22. export class DebugProtectionCustomNodeGroup extends AbstractCustomNodeGroup {
  23. /**
  24. * @type {ObfuscationEvent}
  25. */
  26. protected readonly appendEvent: ObfuscationEvent = ObfuscationEvent.BeforeObfuscation;
  27. /**
  28. * @type {Map<CustomNode, ICustomNode>}
  29. */
  30. @initializable()
  31. protected customNodes!: Map <CustomNode, ICustomNode>;
  32. /**
  33. * @type {TCustomNodeFactory}
  34. */
  35. private readonly customNodeFactory: TCustomNodeFactory;
  36. /**
  37. * @param {TCustomNodeFactory} customNodeFactory
  38. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  39. * @param {IRandomGenerator} randomGenerator
  40. * @param {IOptions} options
  41. */
  42. public constructor (
  43. @inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
  44. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  45. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  46. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  47. @inject(ServiceIdentifiers.IOptions) options: IOptions
  48. ) {
  49. super(identifierNamesGeneratorFactory, randomGenerator, options);
  50. this.customNodeFactory = customNodeFactory;
  51. }
  52. /**
  53. * @param {TNodeWithStatements} nodeWithStatements
  54. * @param {ICallsGraphData[]} callsGraphData
  55. */
  56. public appendCustomNodes (nodeWithStatements: TNodeWithStatements, callsGraphData: ICallsGraphData[]): void {
  57. const randomCallsGraphIndex: number = this.getRandomCallsGraphIndex(callsGraphData.length);
  58. // debugProtectionFunctionCallNode append
  59. this.appendCustomNodeIfExist(CustomNode.DebugProtectionFunctionCallNode, (customNode: ICustomNode) => {
  60. NodeAppender.appendToOptimalBlockScope(
  61. callsGraphData,
  62. nodeWithStatements,
  63. customNode.getNode(),
  64. randomCallsGraphIndex
  65. );
  66. });
  67. // debugProtectionFunctionNode append
  68. this.appendCustomNodeIfExist(CustomNode.DebugProtectionFunctionNode, (customNode: ICustomNode) => {
  69. NodeAppender.append(nodeWithStatements, customNode.getNode());
  70. });
  71. // debugProtectionFunctionIntervalNode append
  72. this.appendCustomNodeIfExist(CustomNode.DebugProtectionFunctionIntervalNode, (customNode: ICustomNode) => {
  73. const programBodyLength: number = NodeGuards.isSwitchCaseNode(nodeWithStatements)
  74. ? nodeWithStatements.consequent.length
  75. : nodeWithStatements.body.length;
  76. const randomIndex: number = this.randomGenerator.getRandomInteger(0, programBodyLength);
  77. NodeAppender.insertAtIndex(nodeWithStatements, customNode.getNode(), randomIndex);
  78. });
  79. // nodeCallsControllerFunctionNode append
  80. this.appendCustomNodeIfExist(CustomNode.NodeCallsControllerFunctionNode, (customNode: ICustomNode) => {
  81. const targetNodeWithStatements: TNodeWithStatements = callsGraphData.length
  82. ? NodeAppender.getOptimalBlockScope(callsGraphData, randomCallsGraphIndex, 1)
  83. : nodeWithStatements;
  84. NodeAppender.prepend(targetNodeWithStatements, customNode.getNode());
  85. });
  86. }
  87. public initialize (): void {
  88. this.customNodes = new Map <CustomNode, ICustomNode>();
  89. if (!this.options.debugProtection) {
  90. return;
  91. }
  92. const debugProtectionFunctionName: string = this.identifierNamesGenerator.generate();
  93. const callsControllerFunctionName: string = this.identifierNamesGenerator.generate();
  94. const debugProtectionFunctionNode: ICustomNode<TInitialData<DebugProtectionFunctionNode>> =
  95. this.customNodeFactory(CustomNode.DebugProtectionFunctionNode);
  96. const debugProtectionFunctionCallNode: ICustomNode<TInitialData<DebugProtectionFunctionCallNode>> =
  97. this.customNodeFactory(CustomNode.DebugProtectionFunctionCallNode);
  98. const debugProtectionFunctionIntervalNode: ICustomNode<TInitialData<DebugProtectionFunctionIntervalNode>> =
  99. this.customNodeFactory(CustomNode.DebugProtectionFunctionIntervalNode);
  100. const nodeCallsControllerFunctionNode: ICustomNode<TInitialData<NodeCallsControllerFunctionNode>> =
  101. this.customNodeFactory(CustomNode.NodeCallsControllerFunctionNode);
  102. debugProtectionFunctionNode.initialize(debugProtectionFunctionName);
  103. debugProtectionFunctionCallNode.initialize(debugProtectionFunctionName, callsControllerFunctionName);
  104. debugProtectionFunctionIntervalNode.initialize(debugProtectionFunctionName);
  105. nodeCallsControllerFunctionNode.initialize(this.appendEvent, callsControllerFunctionName);
  106. this.customNodes.set(CustomNode.DebugProtectionFunctionNode, debugProtectionFunctionNode);
  107. this.customNodes.set(CustomNode.DebugProtectionFunctionCallNode, debugProtectionFunctionCallNode);
  108. if (this.options.debugProtectionInterval) {
  109. this.customNodes.set(CustomNode.DebugProtectionFunctionIntervalNode, debugProtectionFunctionIntervalNode);
  110. }
  111. this.customNodes.set(CustomNode.NodeCallsControllerFunctionNode, nodeCallsControllerFunctionNode);
  112. }
  113. }