UnicodeArrayRotateFunctionNode.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. timesArgumentName: string = Utils.getRandomVariableName(),
  70. tempArrayName: string = Utils.getRandomVariableName(),
  71. whileFunctionName: string = Utils.getRandomVariableName(),
  72. node: INode;
  73. if (this.options['selfDefending']) {
  74. code = JavaScriptObfuscator.obfuscate(`
  75. (function () {
  76. var func = function(){return ${Utils.stringToUnicode('dev')};};
  77. !Function(${Utils.stringToUnicode(`return/\\w+ *\\(\\) *{\\w+ *['|"].+['|"];? *}/`)})().test(func.toString()) ? []['filter']['constructor'](${Utils.stringToJSFuck('while')} + '(${JSFuck.True}){}')() : ${JSFuck.Window}.eval(${whileFunctionName}(++${timesName})) ? []['filter']['constructor'](${Utils.stringToJSFuck('while')} + '(${JSFuck.False}){}')() : []['filter']['constructor'](${Utils.stringToJSFuck('while')} + '(${JSFuck.False}){}')();
  78. })();
  79. `, NO_CUSTOM_NODES_PRESET);
  80. } else {
  81. code = `${whileFunctionName}(++${timesName})`;
  82. }
  83. node = esprima.parse(`
  84. (function (${arrayName}, ${timesName}) {
  85. if (${timesName} < 0x${Utils.decToHex(0)}) {
  86. return;
  87. }
  88. var ${tempArrayName};
  89. var ${whileFunctionName} = function (${timesArgumentName}) {
  90. while (--${timesArgumentName}) {
  91. ${tempArrayName} = ${arrayName}[${Utils.stringToUnicode('shift')}]();
  92. ${arrayName}[${Utils.stringToUnicode('push')}](${tempArrayName});
  93. }
  94. };
  95. ${code}
  96. })(${this.unicodeArrayName}, 0x${Utils.decToHex(this.unicodeArrayRotateValue)});
  97. `);
  98. NodeUtils.addXVerbatimPropertyToLiterals(node);
  99. return NodeUtils.getBlockScopeNodeByIndex(node);
  100. }
  101. }