UnicodeArrayRotateFunctionNode.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import * as esprima from 'esprima';
  2. import { INode } from "../../interfaces/nodes/INode";
  3. import { IOptions } from "../../interfaces/IOptions";
  4. import { TBlockScopeNode } from "../../types/TBlockScopeNode";
  5. import { AppendState } from "../../enums/AppendState";
  6. import { JSFuck } from "../../enums/JSFuck";
  7. import { NO_CUSTOM_NODES_PRESET } from "../../preset-options/NoCustomNodesPreset";
  8. import { JavaScriptObfuscator } from "../../JavaScriptObfuscator";
  9. import { Node } from '../Node';
  10. import { NodeUtils } from "../../NodeUtils";
  11. import { Utils } from "../../Utils";
  12. export class UnicodeArrayRotateFunctionNode extends Node {
  13. /**
  14. * @type {AppendState}
  15. */
  16. protected appendState: AppendState = AppendState.AfterObfuscation;
  17. /**
  18. * @type {string[]}
  19. */
  20. private unicodeArray: string[];
  21. /**
  22. * @type {string}
  23. */
  24. private unicodeArrayName: string;
  25. /**
  26. * @param {number}
  27. */
  28. private unicodeArrayRotateValue: number;
  29. /**
  30. * @param unicodeArrayName
  31. * @param unicodeArray
  32. * @param unicodeArrayRotateValue
  33. * @param options
  34. */
  35. constructor (
  36. unicodeArrayName: string,
  37. unicodeArray: string[],
  38. unicodeArrayRotateValue: number,
  39. options: IOptions = {}
  40. ) {
  41. super(options);
  42. this.unicodeArrayName = unicodeArrayName;
  43. this.unicodeArray = unicodeArray;
  44. this.unicodeArrayRotateValue = unicodeArrayRotateValue;
  45. this.node = this.getNodeStructure();
  46. }
  47. /**
  48. * @param blockScopeNode
  49. */
  50. public appendNode (blockScopeNode: TBlockScopeNode): void {
  51. NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), 1);
  52. }
  53. /**
  54. * @returns {INode}
  55. */
  56. public getNode (): INode {
  57. if (!this.unicodeArray.length) {
  58. return;
  59. }
  60. return super.getNode();
  61. }
  62. /**
  63. * @returns {INode}
  64. */
  65. protected getNodeStructure (): INode {
  66. let arrayName: string = Utils.getRandomVariableName(),
  67. code: string = '',
  68. timesName: string = Utils.getRandomVariableName(),
  69. tempArrayName: string = Utils.getRandomVariableName(),
  70. node: INode;
  71. if (this.options['selfDefending']) {
  72. code = JavaScriptObfuscator.obfuscate(`
  73. (function () {
  74. var func = function () {
  75. return 'window';
  76. };
  77. if (
  78. !/(\\\\\[x|u](\\w){2,4})+/.test(func.toString())
  79. ) {
  80. []['filter']['constructor'](${Utils.stringToJSFuck('while')} + '(${JSFuck.True}){}')();
  81. }
  82. })();
  83. `, NO_CUSTOM_NODES_PRESET);
  84. }
  85. node = esprima.parse(`
  86. (function (${arrayName}, ${timesName}) {
  87. if (${timesName} < 0x${Utils.decToHex(0)}) {
  88. return;
  89. }
  90. var ${tempArrayName};
  91. while (${timesName}--) {
  92. ${code}
  93. ${tempArrayName} = ${arrayName}[${Utils.stringToUnicode('shift')}]();
  94. ${arrayName}[${Utils.stringToUnicode('push')}](${tempArrayName});
  95. }
  96. })(${this.unicodeArrayName}, 0x${Utils.decToHex(this.unicodeArrayRotateValue)});
  97. `);
  98. NodeUtils.addXVerbatimPropertyToLiterals(node);
  99. return NodeUtils.getBlockScopeNodeByIndex(node);
  100. }
  101. }