StringArrayCustomNodeGroup.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { injectable, inject } from 'inversify';
  2. import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
  3. import { TCustomNodeFactory } from '../../../types/container/custom-nodes/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 { IStorage } from '../../../interfaces/storages/IStorage';
  11. import { initializable } from '../../../decorators/Initializable';
  12. import { CustomNode } from '../../../enums/container/custom-nodes/CustomNode';
  13. import { ObfuscationEvent } from '../../../enums/event-emitters/ObfuscationEvent';
  14. import { AbstractCustomNodeGroup } from '../../AbstractCustomNodeGroup';
  15. import { NodeAppender } from '../../../node/NodeAppender';
  16. import { RandomGeneratorUtils } from '../../../utils/RandomGeneratorUtils';
  17. import { Utils } from '../../../utils/Utils';
  18. @injectable()
  19. export class StringArrayCustomNodeGroup extends AbstractCustomNodeGroup {
  20. /**
  21. * @type {TObfuscationEvent}
  22. */
  23. protected appendEvent: TObfuscationEvent = 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. * @type {IObfuscationEventEmitter}
  35. */
  36. private readonly obfuscationEventEmitter: IObfuscationEventEmitter;
  37. /**
  38. * @type {IStorage <string>}
  39. */
  40. @initializable()
  41. private stringArrayStorage: IStorage <string>;
  42. /**
  43. * @param customNodeFactory
  44. * @param obfuscationEventEmitter
  45. * @param stringArrayStorage
  46. * @param options
  47. */
  48. constructor (
  49. @inject(ServiceIdentifiers.Factory__ICustomNode) customNodeFactory: TCustomNodeFactory,
  50. @inject(ServiceIdentifiers.IObfuscationEventEmitter) obfuscationEventEmitter: IObfuscationEventEmitter,
  51. @inject(ServiceIdentifiers.TStringArrayStorage) stringArrayStorage: IStorage<string>,
  52. @inject(ServiceIdentifiers.IOptions) options: IOptions
  53. ) {
  54. super(options);
  55. this.customNodeFactory = customNodeFactory;
  56. this.obfuscationEventEmitter = obfuscationEventEmitter;
  57. this.stringArrayStorage = stringArrayStorage;
  58. }
  59. /**
  60. * @param blockScopeNode
  61. * @param stackTraceData
  62. */
  63. public appendCustomNodes (blockScopeNode: TNodeWithBlockStatement, stackTraceData: IStackTraceData[]): void {
  64. if (!this.stringArrayStorage.getLength()) {
  65. return;
  66. }
  67. // stringArrayNode append
  68. this.appendCustomNodeIfExist(CustomNode.StringArrayNode, (customNode: ICustomNode) => {
  69. NodeAppender.prependNode(blockScopeNode, customNode.getNode());
  70. });
  71. // stringArrayCallsWrapper append
  72. this.appendCustomNodeIfExist(CustomNode.StringArrayCallsWrapper, (customNode: ICustomNode) => {
  73. NodeAppender.insertNodeAtIndex(blockScopeNode, customNode.getNode(), 1);
  74. });
  75. // stringArrayRotateFunctionNode append
  76. this.appendCustomNodeIfExist(CustomNode.StringArrayRotateFunctionNode, (customNode: ICustomNode) => {
  77. NodeAppender.insertNodeAtIndex(blockScopeNode, customNode.getNode(), 1);
  78. });
  79. }
  80. public initialize (): void {
  81. this.customNodes = new Map <CustomNode, ICustomNode> ();
  82. if (!this.options.stringArray) {
  83. return;
  84. }
  85. const stringArrayNode: ICustomNode = this.customNodeFactory(CustomNode.StringArrayNode);
  86. const stringArrayCallsWrapper: ICustomNode = this.customNodeFactory(CustomNode.StringArrayCallsWrapper);
  87. const stringArrayRotateFunctionNode: ICustomNode = this.customNodeFactory(CustomNode.StringArrayRotateFunctionNode);
  88. const stringArrayStorageId: string = this.stringArrayStorage.getStorageId();
  89. const stringArrayName: string = `_${Utils.hexadecimalPrefix}${stringArrayStorageId}`;
  90. const stringArrayCallsWrapperName: string = `_${Utils.hexadecimalPrefix}${Utils.stringRotate(stringArrayStorageId, 1)}`;
  91. let stringArrayRotateValue: number;
  92. if (this.options.rotateStringArray) {
  93. stringArrayRotateValue = RandomGeneratorUtils.getRandomInteger(100, 500);
  94. } else {
  95. stringArrayRotateValue = 0;
  96. }
  97. stringArrayNode.initialize(this.stringArrayStorage, stringArrayName, stringArrayRotateValue);
  98. stringArrayCallsWrapper.initialize(this.stringArrayStorage, stringArrayName, stringArrayCallsWrapperName);
  99. stringArrayRotateFunctionNode.initialize(this.stringArrayStorage, stringArrayName, stringArrayRotateValue);
  100. this.customNodes.set(CustomNode.StringArrayNode, stringArrayNode);
  101. this.customNodes.set(CustomNode.StringArrayCallsWrapper, stringArrayCallsWrapper);
  102. if (this.options.rotateStringArray) {
  103. this.customNodes.set(CustomNode.StringArrayRotateFunctionNode, stringArrayRotateFunctionNode);
  104. }
  105. }
  106. }