DebugProtectionCustomNodeGroup.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { injectable, inject } from 'inversify';
  2. import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
  3. import { TCustomNodeFactory } from '../../../types/container/TCustomNodeFactory';
  4. import { TNodeWithBlockStatement } from '../../../types/node/TNodeWithBlockStatement';
  5. import { TObfuscationEvent } from '../../../types/event-emitters/TObfuscationEvent';
  6. import { ICustomNode } from '../../../interfaces/custom-nodes/ICustomNode';
  7. import { IObfuscationEventEmitter } from '../../../interfaces/event-emitters/IObfuscationEventEmitter';
  8. import { IOptions } from '../../../interfaces/options/IOptions';
  9. import { IStackTraceData } from '../../../interfaces/stack-trace-analyzer/IStackTraceData';
  10. import { initializable } from '../../../decorators/Initializable';
  11. import { CustomNodes } from '../../../enums/container/CustomNodes';
  12. import { ObfuscationEvents } from '../../../enums/ObfuscationEvents';
  13. import { AbstractCustomNodeGroup } from '../../AbstractCustomNodeGroup';
  14. import { NodeAppender } from '../../../node/NodeAppender';
  15. import { RandomGeneratorUtils } from '../../../utils/RandomGeneratorUtils';
  16. @injectable()
  17. export class DebugProtectionCustomNodeGroup extends AbstractCustomNodeGroup {
  18. /**
  19. * @type {TObfuscationEvent}
  20. */
  21. protected readonly appendEvent: TObfuscationEvent = ObfuscationEvents.BeforeObfuscation;
  22. /**
  23. * @type {Map<CustomNodes, ICustomNode>}
  24. */
  25. @initializable()
  26. protected customNodes: Map <CustomNodes, ICustomNode>;
  27. /**
  28. * @type {TCustomNodeFactory}
  29. */
  30. private readonly customNodeFactory: TCustomNodeFactory;
  31. /**
  32. * @type {IObfuscationEventEmitter}
  33. */
  34. private readonly obfuscationEventEmitter: IObfuscationEventEmitter;
  35. /**
  36. * @param customNodeFactory
  37. * @param obfuscationEventEmitter
  38. * @param options
  39. */
  40. constructor (
  41. @inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
  42. @inject(ServiceIdentifiers.IObfuscationEventEmitter) obfuscationEventEmitter: IObfuscationEventEmitter,
  43. @inject(ServiceIdentifiers.IOptions) options: IOptions
  44. ) {
  45. super(options);
  46. this.customNodeFactory = customNodeFactory;
  47. this.obfuscationEventEmitter = obfuscationEventEmitter;
  48. }
  49. /**
  50. * @param blockScopeNode
  51. * @param stackTraceData
  52. */
  53. public appendCustomNodes (blockScopeNode: TNodeWithBlockStatement, stackTraceData: IStackTraceData[]): void {
  54. // debugProtectionFunctionNode append
  55. this.appendCustomNodeIfExist(CustomNodes.DebugProtectionFunctionNode, (customNode: ICustomNode) => {
  56. NodeAppender.appendNode(blockScopeNode, customNode.getNode());
  57. });
  58. // debugProtectionFunctionCallNode append
  59. this.appendCustomNodeIfExist(CustomNodes.DebugProtectionFunctionCallNode, (customNode: ICustomNode) => {
  60. NodeAppender.appendNode(blockScopeNode, customNode.getNode());
  61. });
  62. // debugProtectionFunctionIntervalNode append
  63. this.appendCustomNodeIfExist(CustomNodes.DebugProtectionFunctionIntervalNode, (customNode: ICustomNode) => {
  64. const programBodyLength: number = blockScopeNode.body.length;
  65. const randomIndex: number = RandomGeneratorUtils.getRandomInteger(0, programBodyLength);
  66. NodeAppender.insertNodeAtIndex(blockScopeNode, customNode.getNode(), randomIndex);
  67. });
  68. }
  69. public initialize (): void {
  70. this.customNodes = new Map <CustomNodes, ICustomNode> ();
  71. if (!this.options.debugProtection) {
  72. return;
  73. }
  74. const debugProtectionFunctionName: string = RandomGeneratorUtils.getRandomVariableName();
  75. const debugProtectionFunctionNode: ICustomNode = this.customNodeFactory(CustomNodes.DebugProtectionFunctionNode);
  76. const debugProtectionFunctionCallNode: ICustomNode = this.customNodeFactory(CustomNodes.DebugProtectionFunctionCallNode);
  77. const debugProtectionFunctionIntervalNode: ICustomNode = this.customNodeFactory(CustomNodes.DebugProtectionFunctionIntervalNode);
  78. debugProtectionFunctionNode.initialize(debugProtectionFunctionName);
  79. debugProtectionFunctionCallNode.initialize(debugProtectionFunctionName);
  80. debugProtectionFunctionIntervalNode.initialize(debugProtectionFunctionName);
  81. this.customNodes.set(CustomNodes.DebugProtectionFunctionNode, debugProtectionFunctionNode);
  82. this.customNodes.set(CustomNodes.DebugProtectionFunctionCallNode, debugProtectionFunctionCallNode);
  83. if (this.options.debugProtectionInterval) {
  84. this.customNodes.set(CustomNodes.DebugProtectionFunctionIntervalNode, debugProtectionFunctionIntervalNode);
  85. }
  86. }
  87. }