UnicodeArrayCallsWrapper.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import 'format-unicorn';
  2. import { TNodeWithBlockStatement } from '../../types/TNodeWithBlockStatement';
  3. import { TStatement } from '../../types/TStatement';
  4. import { IOptions } from '../../interfaces/IOptions';
  5. import { AppendState } from '../../enums/AppendState';
  6. import { UnicodeArrayEncoding } from '../../enums/UnicodeArrayEncoding';
  7. import { NO_CUSTOM_NODES_PRESET } from '../../preset-options/NoCustomNodesPreset';
  8. import { AtobTemplate } from '../../templates/custom-nodes/AtobTemplate';
  9. import { Rc4Template } from '../../templates/custom-nodes/Rc4Template';
  10. import { SelfDefendingTemplate } from '../../templates/custom-nodes/unicode-array-nodes/unicode-array-calls-wrapper/SelfDefendingTemplate';
  11. import { UnicodeArrayBase64DecodeNodeTemplate } from '../../templates/custom-nodes/unicode-array-nodes/unicode-array-calls-wrapper/UnicodeArrayBase64DecodeNodeTemplate';
  12. import { UnicodeArrayCallsWrapperTemplate } from '../../templates/custom-nodes/unicode-array-nodes/unicode-array-calls-wrapper/UnicodeArrayCallsWrapperTemplate';
  13. import { UnicodeArrayRc4DecodeNodeTemplate } from '../../templates/custom-nodes/unicode-array-nodes/unicode-array-calls-wrapper/UnicodeArrayRC4DecodeNodeTemplate';
  14. import { AbstractCustomNode } from '../AbstractCustomNode';
  15. import { JavaScriptObfuscator } from '../../JavaScriptObfuscator';
  16. import { NodeAppender } from '../../NodeAppender';
  17. import { NodeUtils } from '../../NodeUtils';
  18. import { UnicodeArray } from '../../UnicodeArray';
  19. export class UnicodeArrayCallsWrapper extends AbstractCustomNode {
  20. /**
  21. * @type {AppendState}
  22. */
  23. protected appendState: AppendState = AppendState.AfterObfuscation;
  24. /**
  25. * @type {UnicodeArray}
  26. */
  27. private unicodeArray: UnicodeArray;
  28. /**
  29. * @type {string}
  30. */
  31. private unicodeArrayName: string;
  32. /**
  33. * @type {string}
  34. */
  35. private unicodeArrayCallsWrapperName: string;
  36. /**
  37. * @param unicodeArrayCallsWrapperName
  38. * @param unicodeArrayName
  39. * @param unicodeArray
  40. * @param options
  41. */
  42. constructor (
  43. unicodeArrayCallsWrapperName: string,
  44. unicodeArrayName: string,
  45. unicodeArray: UnicodeArray,
  46. options: IOptions
  47. ) {
  48. super(options);
  49. this.unicodeArrayCallsWrapperName = unicodeArrayCallsWrapperName;
  50. this.unicodeArrayName = unicodeArrayName;
  51. this.unicodeArray = unicodeArray;
  52. }
  53. /**
  54. * @param blockScopeNode
  55. */
  56. public appendNode (blockScopeNode: TNodeWithBlockStatement): void {
  57. if (!this.unicodeArray.getLength()) {
  58. return;
  59. }
  60. NodeAppender.insertNodeAtIndex(blockScopeNode, this.getNode(), 1);
  61. }
  62. /**
  63. * @returns {string}
  64. */
  65. public getNodeIdentifier (): string {
  66. return this.unicodeArrayCallsWrapperName;
  67. };
  68. /**
  69. * @returns {TStatement[]}
  70. */
  71. public getNode (): TStatement[] {
  72. return super.getNode();
  73. }
  74. /**
  75. * @returns {string}
  76. */
  77. protected getDecodeUnicodeArrayTemplate (): string {
  78. let decodeUnicodeArrayTemplate: string = '',
  79. selfDefendingCode: string = '';
  80. if (this.options.selfDefending) {
  81. selfDefendingCode = SelfDefendingTemplate().formatUnicorn({
  82. unicodeArrayCallsWrapperName: this.unicodeArrayCallsWrapperName,
  83. unicodeArrayName: this.unicodeArrayName
  84. });
  85. }
  86. switch (this.options.unicodeArrayEncoding) {
  87. case UnicodeArrayEncoding.base64:
  88. decodeUnicodeArrayTemplate = UnicodeArrayBase64DecodeNodeTemplate().formatUnicorn({
  89. atobPolyfill: AtobTemplate(),
  90. selfDefendingCode,
  91. unicodeArrayCallsWrapperName: this.unicodeArrayCallsWrapperName
  92. });
  93. break;
  94. case UnicodeArrayEncoding.rc4:
  95. decodeUnicodeArrayTemplate = UnicodeArrayRc4DecodeNodeTemplate().formatUnicorn({
  96. atobPolyfill: AtobTemplate(),
  97. rc4Polyfill: Rc4Template(),
  98. selfDefendingCode,
  99. unicodeArrayCallsWrapperName: this.unicodeArrayCallsWrapperName
  100. });
  101. break;
  102. }
  103. return decodeUnicodeArrayTemplate;
  104. }
  105. /**
  106. * @returns {TStatement[]}
  107. */
  108. protected getNodeStructure (): TStatement[] {
  109. const decodeNodeTemplate: string = this.getDecodeUnicodeArrayTemplate();
  110. return NodeUtils.convertCodeToStructure(
  111. JavaScriptObfuscator.obfuscate(
  112. UnicodeArrayCallsWrapperTemplate().formatUnicorn({
  113. decodeNodeTemplate,
  114. unicodeArrayCallsWrapperName: this.unicodeArrayCallsWrapperName,
  115. unicodeArrayName: this.unicodeArrayName
  116. }),
  117. NO_CUSTOM_NODES_PRESET
  118. ).getObfuscatedCode()
  119. );
  120. }
  121. }