SelfDefendingCustomNodeGroup.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 { NodeAppender } from '../../../node/NodeAppender';
  16. import { NodeCallsControllerFunctionNode } from '../../node-calls-controller-nodes/NodeCallsControllerFunctionNode';
  17. import { SelfDefendingUnicodeNode } from '../SelfDefendingUnicodeNode';
  18. @injectable()
  19. export class SelfDefendingCustomNodeGroup extends AbstractCustomNodeGroup {
  20. /**
  21. * @type {ObfuscationEvent}
  22. */
  23. protected appendEvent: ObfuscationEvent = ObfuscationEvent.AfterObfuscation;
  24. /**
  25. * @type {Map<CustomNode, ICustomNode>}
  26. */
  27. @initializable()
  28. protected customNodes!: Map <CustomNode, ICustomNode>;
  29. /**
  30. * @type {TCustomNodeFactory}
  31. */
  32. private readonly customNodeFactory: TCustomNodeFactory;
  33. /**
  34. * @param {TCustomNodeFactory} customNodeFactory
  35. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  36. * @param {IRandomGenerator} randomGenerator
  37. * @param {IOptions} options
  38. */
  39. public constructor (
  40. @inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
  41. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  42. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  43. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  44. @inject(ServiceIdentifiers.IOptions) options: IOptions
  45. ) {
  46. super(identifierNamesGeneratorFactory, randomGenerator, options);
  47. this.customNodeFactory = customNodeFactory;
  48. }
  49. /**
  50. * @param {TNodeWithStatements} nodeWithStatements
  51. * @param {ICallsGraphData[]} callsGraphData
  52. */
  53. public appendCustomNodes (nodeWithStatements: TNodeWithStatements, callsGraphData: ICallsGraphData[]): void {
  54. const randomCallsGraphIndex: number = this.getRandomCallsGraphIndex(callsGraphData.length);
  55. // selfDefendingUnicodeNode append
  56. this.appendCustomNodeIfExist(CustomNode.SelfDefendingUnicodeNode, (customNode: ICustomNode) => {
  57. NodeAppender.appendToOptimalBlockScope(
  58. callsGraphData,
  59. nodeWithStatements,
  60. customNode.getNode(),
  61. randomCallsGraphIndex
  62. );
  63. });
  64. // nodeCallsControllerFunctionNode append
  65. this.appendCustomNodeIfExist(CustomNode.NodeCallsControllerFunctionNode, (customNode: ICustomNode) => {
  66. const targetNodeWithStatements: TNodeWithStatements = callsGraphData.length
  67. ? NodeAppender.getOptimalBlockScope(callsGraphData, randomCallsGraphIndex, 1)
  68. : nodeWithStatements;
  69. NodeAppender.prepend(targetNodeWithStatements, 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.identifierNamesGenerator.generate();
  78. const selfDefendingUnicodeNode: ICustomNode<TInitialData<SelfDefendingUnicodeNode>> =
  79. this.customNodeFactory(CustomNode.SelfDefendingUnicodeNode);
  80. const nodeCallsControllerFunctionNode: ICustomNode<TInitialData<NodeCallsControllerFunctionNode>> =
  81. this.customNodeFactory(CustomNode.NodeCallsControllerFunctionNode);
  82. selfDefendingUnicodeNode.initialize(callsControllerFunctionName);
  83. nodeCallsControllerFunctionNode.initialize(this.appendEvent, callsControllerFunctionName);
  84. this.customNodes.set(CustomNode.SelfDefendingUnicodeNode, selfDefendingUnicodeNode);
  85. this.customNodes.set(CustomNode.NodeCallsControllerFunctionNode, nodeCallsControllerFunctionNode);
  86. }
  87. }