DebugProtectionCustomNodeGroup.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 { TNodeWithBlockScope } from '../../../types/node/TNodeWithBlockScope';
  6. import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
  7. import { IOptions } from '../../../interfaces/options/IOptions';
  8. import { IRandomGenerator } from '../../../interfaces/utils/IRandomGenerator';
  9. import { IStackTraceData } from '../../../interfaces/analyzers/stack-trace-analyzer/IStackTraceData';
  10. import { initializable } from '../../../decorators/Initializable';
  11. import { CustomNode } from '../../../enums/custom-nodes/CustomNode';
  12. import { ObfuscationEvent } from '../../../enums/event-emitters/ObfuscationEvent';
  13. import { AbstractCustomNodeGroup } from '../../AbstractCustomNodeGroup';
  14. import { NodeAppender } from '../../../node/NodeAppender';
  15. @injectable()
  16. export class DebugProtectionCustomNodeGroup extends AbstractCustomNodeGroup {
  17. /**
  18. * @type {ObfuscationEvent}
  19. */
  20. protected readonly appendEvent: ObfuscationEvent = ObfuscationEvent.BeforeObfuscation;
  21. /**
  22. * @type {Map<CustomNode, ICustomNode>}
  23. */
  24. @initializable()
  25. protected customNodes!: Map <CustomNode, ICustomNode>;
  26. /**
  27. * @type {TCustomNodeFactory}
  28. */
  29. private readonly customNodeFactory: TCustomNodeFactory;
  30. /**
  31. * @param {TCustomNodeFactory} customNodeFactory
  32. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  33. * @param {IRandomGenerator} randomGenerator
  34. * @param {IOptions} options
  35. */
  36. constructor (
  37. @inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
  38. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  39. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  40. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  41. @inject(ServiceIdentifiers.IOptions) options: IOptions
  42. ) {
  43. super(identifierNamesGeneratorFactory, randomGenerator, options);
  44. this.customNodeFactory = customNodeFactory;
  45. }
  46. /**
  47. * @param {TNodeWithBlockScope} blockScopeNode
  48. * @param {IStackTraceData[]} stackTraceData
  49. */
  50. public appendCustomNodes (blockScopeNode: TNodeWithBlockScope, stackTraceData: IStackTraceData[]): void {
  51. const randomStackTraceIndex: number = this.getRandomStackTraceIndex(stackTraceData.length);
  52. // debugProtectionFunctionCallNode append
  53. this.appendCustomNodeIfExist(CustomNode.DebugProtectionFunctionCallNode, (customNode: ICustomNode) => {
  54. NodeAppender.appendNodeToOptimalBlockScope(
  55. stackTraceData,
  56. blockScopeNode,
  57. customNode.getNode(),
  58. randomStackTraceIndex
  59. );
  60. });
  61. // debugProtectionFunctionNode append
  62. this.appendCustomNodeIfExist(CustomNode.DebugProtectionFunctionNode, (customNode: ICustomNode) => {
  63. NodeAppender.appendNode(blockScopeNode, customNode.getNode());
  64. });
  65. // debugProtectionFunctionIntervalNode append
  66. this.appendCustomNodeIfExist(CustomNode.DebugProtectionFunctionIntervalNode, (customNode: ICustomNode) => {
  67. const programBodyLength: number = blockScopeNode.body.length;
  68. const randomIndex: number = this.randomGenerator.getRandomInteger(0, programBodyLength);
  69. NodeAppender.insertNodeAtIndex(blockScopeNode, customNode.getNode(), randomIndex);
  70. });
  71. // nodeCallsControllerFunctionNode append
  72. this.appendCustomNodeIfExist(CustomNode.NodeCallsControllerFunctionNode, (customNode: ICustomNode) => {
  73. let targetBlockScope: TNodeWithBlockScope;
  74. if (stackTraceData.length) {
  75. targetBlockScope = NodeAppender.getOptimalBlockScope(stackTraceData, randomStackTraceIndex, 1);
  76. } else {
  77. targetBlockScope = blockScopeNode;
  78. }
  79. NodeAppender.prependNode(targetBlockScope, customNode.getNode());
  80. });
  81. }
  82. public initialize (): void {
  83. this.customNodes = new Map <CustomNode, ICustomNode>();
  84. if (!this.options.debugProtection) {
  85. return;
  86. }
  87. const debugProtectionFunctionName: string = this.identifierNamesGenerator.generate();
  88. const callsControllerFunctionName: string = this.identifierNamesGenerator.generate();
  89. const debugProtectionFunctionNode: ICustomNode = this.customNodeFactory(CustomNode.DebugProtectionFunctionNode);
  90. const debugProtectionFunctionCallNode: ICustomNode = this.customNodeFactory(CustomNode.DebugProtectionFunctionCallNode);
  91. const debugProtectionFunctionIntervalNode: ICustomNode = this.customNodeFactory(CustomNode.DebugProtectionFunctionIntervalNode);
  92. const nodeCallsControllerFunctionNode: ICustomNode = this.customNodeFactory(CustomNode.NodeCallsControllerFunctionNode);
  93. debugProtectionFunctionNode.initialize(debugProtectionFunctionName);
  94. debugProtectionFunctionCallNode.initialize(debugProtectionFunctionName, callsControllerFunctionName);
  95. debugProtectionFunctionIntervalNode.initialize(debugProtectionFunctionName);
  96. nodeCallsControllerFunctionNode.initialize(this.appendEvent, callsControllerFunctionName);
  97. this.customNodes.set(CustomNode.DebugProtectionFunctionNode, debugProtectionFunctionNode);
  98. this.customNodes.set(CustomNode.DebugProtectionFunctionCallNode, debugProtectionFunctionCallNode);
  99. if (this.options.debugProtectionInterval) {
  100. this.customNodes.set(CustomNode.DebugProtectionFunctionIntervalNode, debugProtectionFunctionIntervalNode);
  101. }
  102. this.customNodes.set(CustomNode.NodeCallsControllerFunctionNode, nodeCallsControllerFunctionNode);
  103. }
  104. }