DebugProtectionCodeHelperGroup.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { inject, injectable, } from 'inversify';
  2. import { ServiceIdentifiers } from '../../../container/ServiceIdentifiers';
  3. import { TCustomCodeHelperFactory } from '../../../types/container/custom-code-helpers/TCustomCodeHelperFactory';
  4. import { TIdentifierNamesGeneratorFactory } from '../../../types/container/generators/TIdentifierNamesGeneratorFactory';
  5. import { TInitialData } from '../../../types/TInitialData';
  6. import { TNodeWithLexicalScope } from '../../../types/node/TNodeWithLexicalScope';
  7. import { TNodeWithStatements } from '../../../types/node/TNodeWithStatements';
  8. import { ICustomCodeHelper } from '../../../interfaces/custom-code-helpers/ICustomCodeHelper';
  9. import { IOptions } from '../../../interfaces/options/IOptions';
  10. import { IRandomGenerator } from '../../../interfaces/utils/IRandomGenerator';
  11. import { ICallsGraphData } from '../../../interfaces/analyzers/calls-graph-analyzer/ICallsGraphData';
  12. import { initializable } from '../../../decorators/Initializable';
  13. import { CustomCodeHelper } from '../../../enums/custom-code-helpers/CustomCodeHelper';
  14. import { ObfuscationEvent } from '../../../enums/event-emitters/ObfuscationEvent';
  15. import { AbstractCustomCodeHelperGroup } from '../../AbstractCustomCodeHelperGroup';
  16. import { CallsControllerFunctionCodeHelper } from '../../calls-controller/CallsControllerFunctionCodeHelper';
  17. import { DebugProtectionFunctionCodeHelper } from '../DebugProtectionFunctionCodeHelper';
  18. import { DebugProtectionFunctionCallCodeHelper } from '../DebugProtectionFunctionCallCodeHelper';
  19. import { DebugProtectionFunctionIntervalCodeHelper } from '../DebugProtectionFunctionIntervalCodeHelper';
  20. import { NodeAppender } from '../../../node/NodeAppender';
  21. import { NodeGuards } from '../../../node/NodeGuards';
  22. import { NodeLexicalScopeUtils } from '../../../node/NodeLexicalScopeUtils';
  23. @injectable()
  24. export class DebugProtectionCodeHelperGroup extends AbstractCustomCodeHelperGroup {
  25. /**
  26. * @type {ObfuscationEvent}
  27. */
  28. protected readonly appendEvent: ObfuscationEvent = ObfuscationEvent.BeforeObfuscation;
  29. /**
  30. * @type {Map<CustomCodeHelper, ICustomCodeHelper>}
  31. */
  32. @initializable()
  33. protected customCodeHelpers!: Map <CustomCodeHelper, ICustomCodeHelper>;
  34. /**
  35. * @type {TCustomCodeHelperFactory}
  36. */
  37. private readonly customCodeHelperFactory: TCustomCodeHelperFactory;
  38. /**
  39. * @param {TCustomCodeHelperFactory} customCodeHelperFactory
  40. * @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
  41. * @param {IRandomGenerator} randomGenerator
  42. * @param {IOptions} options
  43. */
  44. public constructor (
  45. @inject(ServiceIdentifiers.Factory__ICustomCodeHelper) customCodeHelperFactory: TCustomCodeHelperFactory,
  46. @inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
  47. identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
  48. @inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
  49. @inject(ServiceIdentifiers.IOptions) options: IOptions
  50. ) {
  51. super(identifierNamesGeneratorFactory, randomGenerator, options);
  52. this.customCodeHelperFactory = customCodeHelperFactory;
  53. }
  54. /**
  55. * @param {TNodeWithStatements} nodeWithStatements
  56. * @param {ICallsGraphData[]} callsGraphData
  57. */
  58. public appendNodes (nodeWithStatements: TNodeWithStatements, callsGraphData: ICallsGraphData[]): void {
  59. if (!this.options.debugProtection) {
  60. return;
  61. }
  62. const randomCallsGraphIndex: number = this.getRandomCallsGraphIndex(callsGraphData.length);
  63. const debugProtectionFunctionCallHostNode: TNodeWithStatements = callsGraphData.length
  64. ? NodeAppender.getOptimalBlockScope(callsGraphData, randomCallsGraphIndex)
  65. : nodeWithStatements;
  66. const callsControllerHostNode: TNodeWithStatements = callsGraphData.length
  67. ? NodeAppender.getOptimalBlockScope(callsGraphData, randomCallsGraphIndex, 1)
  68. : nodeWithStatements;
  69. const debugProtectionFunctionCallScopeNode: TNodeWithLexicalScope | null = NodeLexicalScopeUtils
  70. .getLexicalScope(debugProtectionFunctionCallHostNode) ?? null;
  71. const debugProtectionFunctionName: string = debugProtectionFunctionCallScopeNode
  72. && NodeGuards.isProgramNode(debugProtectionFunctionCallScopeNode)
  73. ? this.identifierNamesGenerator.generate(debugProtectionFunctionCallScopeNode)
  74. : this.randomGenerator.getRandomString(5);
  75. const callsControllerFunctionName: string = debugProtectionFunctionCallScopeNode
  76. && NodeGuards.isProgramNode(debugProtectionFunctionCallScopeNode)
  77. ? this.identifierNamesGenerator.generate(debugProtectionFunctionCallScopeNode)
  78. : this.randomGenerator.getRandomString(5);
  79. // debugProtectionFunctionCall helper nodes append
  80. this.appendCustomNodeIfExist(
  81. CustomCodeHelper.DebugProtectionFunctionCall,
  82. (customCodeHelper: ICustomCodeHelper<TInitialData<DebugProtectionFunctionCallCodeHelper>>) => {
  83. customCodeHelper.initialize(debugProtectionFunctionName, callsControllerFunctionName);
  84. NodeAppender.prepend(debugProtectionFunctionCallHostNode, customCodeHelper.getNode());
  85. }
  86. );
  87. // nodeCallsControllerFunction helper nodes append
  88. this.appendCustomNodeIfExist(
  89. CustomCodeHelper.CallsControllerFunction,
  90. (customCodeHelper: ICustomCodeHelper<TInitialData<CallsControllerFunctionCodeHelper>>) => {
  91. customCodeHelper.initialize(this.appendEvent, callsControllerFunctionName);
  92. NodeAppender.prepend(callsControllerHostNode, customCodeHelper.getNode());
  93. }
  94. );
  95. // debugProtectionFunction helper nodes append
  96. this.appendCustomNodeIfExist(
  97. CustomCodeHelper.DebugProtectionFunction,
  98. (customCodeHelper: ICustomCodeHelper<TInitialData<DebugProtectionFunctionCodeHelper>>) => {
  99. customCodeHelper.initialize(debugProtectionFunctionName);
  100. NodeAppender.append(nodeWithStatements, customCodeHelper.getNode());
  101. }
  102. );
  103. // debugProtectionFunctionInterval helper nodes append
  104. this.appendCustomNodeIfExist(
  105. CustomCodeHelper.DebugProtectionFunctionInterval,
  106. (customCodeHelper: ICustomCodeHelper<TInitialData<DebugProtectionFunctionIntervalCodeHelper>>) => {
  107. const programBodyLength: number = NodeGuards.isSwitchCaseNode(nodeWithStatements)
  108. ? nodeWithStatements.consequent.length
  109. : nodeWithStatements.body.length;
  110. const randomIndex: number = this.randomGenerator.getRandomInteger(0, programBodyLength);
  111. customCodeHelper.initialize(debugProtectionFunctionName);
  112. NodeAppender.insertAtIndex(nodeWithStatements, customCodeHelper.getNode(), randomIndex);
  113. }
  114. );
  115. }
  116. public initialize (): void {
  117. this.customCodeHelpers = new Map <CustomCodeHelper, ICustomCodeHelper>();
  118. if (!this.options.debugProtection) {
  119. return;
  120. }
  121. const debugProtectionFunctionCodeHelper: ICustomCodeHelper<TInitialData<DebugProtectionFunctionCodeHelper>> =
  122. this.customCodeHelperFactory(CustomCodeHelper.DebugProtectionFunction);
  123. const debugProtectionFunctionCallCodeHelper: ICustomCodeHelper<TInitialData<DebugProtectionFunctionCallCodeHelper>> =
  124. this.customCodeHelperFactory(CustomCodeHelper.DebugProtectionFunctionCall);
  125. const debugProtectionFunctionIntervalCodeHelper: ICustomCodeHelper<TInitialData<DebugProtectionFunctionIntervalCodeHelper>> =
  126. this.customCodeHelperFactory(CustomCodeHelper.DebugProtectionFunctionInterval);
  127. const callsControllerFunctionCodeHelper: ICustomCodeHelper<TInitialData<CallsControllerFunctionCodeHelper>> =
  128. this.customCodeHelperFactory(CustomCodeHelper.CallsControllerFunction);
  129. this.customCodeHelpers.set(CustomCodeHelper.DebugProtectionFunction, debugProtectionFunctionCodeHelper);
  130. this.customCodeHelpers.set(CustomCodeHelper.DebugProtectionFunctionCall, debugProtectionFunctionCallCodeHelper);
  131. if (this.options.debugProtectionInterval) {
  132. this.customCodeHelpers.set(CustomCodeHelper.DebugProtectionFunctionInterval, debugProtectionFunctionIntervalCodeHelper);
  133. }
  134. this.customCodeHelpers.set(CustomCodeHelper.CallsControllerFunction, callsControllerFunctionCodeHelper);
  135. }
  136. }