SelfDefendingCustomNodeGroup.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
  3. import { TCustomNodeFactory } from '../../../types/container/custom-nodes/TCustomNodeFactory';
  4. import { TIdentifierNameGeneratorFactory } from '../../../types/container/generators/TIdentifierNameGeneratorFactory';
  5. import { TNodeWithBlockStatement } from '../../../types/node/TNodeWithBlockStatement';
  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 SelfDefendingCustomNodeGroup extends AbstractCustomNodeGroup {
  17. /**
  18. * @type {ObfuscationEvent}
  19. */
  20. protected appendEvent: ObfuscationEvent = ObfuscationEvent.AfterObfuscation;
  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 {TIdentifierNameGeneratorFactory} identifierNameGeneratorFactory
  33. * @param {IRandomGenerator} randomGenerator
  34. * @param {IOptions} options
  35. */
  36. constructor (
  37. @inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
  38. @inject(ServiceIdentifiers.Factory__IIdentifierNameGenerator)
  39. identifierNameGeneratorFactory: TIdentifierNameGeneratorFactory,
  40. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  41. @inject(ServiceIdentifiers.IOptions) options: IOptions
  42. ) {
  43. super(identifierNameGeneratorFactory, randomGenerator, options);
  44. this.customNodeFactory = customNodeFactory;
  45. }
  46. /**
  47. * @param {TNodeWithBlockStatement} blockScopeNode
  48. * @param {IStackTraceData[]} stackTraceData
  49. */
  50. public appendCustomNodes (blockScopeNode: TNodeWithBlockStatement, stackTraceData: IStackTraceData[]): void {
  51. const randomStackTraceIndex: number = this.getRandomStackTraceIndex(stackTraceData.length);
  52. // selfDefendingUnicodeNode append
  53. this.appendCustomNodeIfExist(CustomNode.SelfDefendingUnicodeNode, (customNode: ICustomNode) => {
  54. NodeAppender.appendNodeToOptimalBlockScope(
  55. stackTraceData,
  56. blockScopeNode,
  57. customNode.getNode(),
  58. randomStackTraceIndex
  59. );
  60. });
  61. // nodeCallsControllerFunctionNode append
  62. this.appendCustomNodeIfExist(CustomNode.NodeCallsControllerFunctionNode, (customNode: ICustomNode) => {
  63. let targetBlockScope: TNodeWithBlockStatement;
  64. if (stackTraceData.length) {
  65. targetBlockScope = NodeAppender.getOptimalBlockScope(stackTraceData, randomStackTraceIndex, 1);
  66. } else {
  67. targetBlockScope = blockScopeNode;
  68. }
  69. NodeAppender.prependNode(targetBlockScope, customNode.getNode());
  70. });
  71. }
  72. public initialize (): void {
  73. this.customNodes = new Map <CustomNode, ICustomNode>();
  74. if (!this.options.selfDefending) {
  75. return;
  76. }
  77. const callsControllerFunctionName: string = this.identifierNameGenerator.generate(6);
  78. const selfDefendingUnicodeNode: ICustomNode = this.customNodeFactory(CustomNode.SelfDefendingUnicodeNode);
  79. const nodeCallsControllerFunctionNode: ICustomNode = this.customNodeFactory(CustomNode.NodeCallsControllerFunctionNode);
  80. selfDefendingUnicodeNode.initialize(callsControllerFunctionName);
  81. nodeCallsControllerFunctionNode.initialize(this.appendEvent, callsControllerFunctionName);
  82. this.customNodes.set(CustomNode.SelfDefendingUnicodeNode, selfDefendingUnicodeNode);
  83. this.customNodes.set(CustomNode.NodeCallsControllerFunctionNode, nodeCallsControllerFunctionNode);
  84. }
  85. }