UnicodeArrayRotateFunctionNode.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import * as esprima from 'esprima';
  2. import { INode } from "../../interfaces/nodes/INode";
  3. import { IOptions } from "../../interfaces/IOptions";
  4. import { TNodeWithBlockStatement } from "../../types/TNodeWithBlockStatement";
  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: TNodeWithBlockStatement): 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. whileFunctionName: string = Utils.getRandomVariableName(),
  71. node: INode;
  72. if (this.options.get('selfDefending')) {
  73. code = JavaScriptObfuscator.obfuscate(`
  74. (function () {
  75. var func = function(){return ${Utils.stringToUnicode('dev')};};
  76. !Function(${Utils.stringToUnicode(`return/\\w+ *\\(\\) *{\\w+ *['|"].+['|"];? *}/`)})().test(func.toString()) ? []['filter']['constructor'](${Utils.stringToJSFuck('while')} + '(${JSFuck.True}){}')() : Function(${Utils.stringToUnicode('a')}, ${Utils.stringToUnicode('b')}, ${Utils.stringToUnicode('a(++b)')})(${whileFunctionName}, ${timesName}) ? []['filter']['constructor'](${Utils.stringToJSFuck('while')} + '(${JSFuck.False}){}')() : []['filter']['constructor'](${Utils.stringToJSFuck('while')} + '(${JSFuck.False}){}')();
  77. })();
  78. `, NO_CUSTOM_NODES_PRESET);
  79. } else {
  80. code = `${whileFunctionName}(++${timesName})`;
  81. }
  82. node = esprima.parse(`
  83. (function (${arrayName}, ${timesName}) {
  84. var ${whileFunctionName} = function (${timesArgumentName}) {
  85. while (--${timesArgumentName}) {
  86. ${arrayName}[${Utils.stringToUnicode('push')}](${arrayName}[${Utils.stringToUnicode('shift')}]());
  87. }
  88. };
  89. ${code}
  90. })(${this.unicodeArrayName}, 0x${Utils.decToHex(this.unicodeArrayRotateValue)});
  91. `);
  92. NodeUtils.addXVerbatimPropertyToLiterals(node);
  93. return NodeUtils.getBlockStatementNodeByIndex(node);
  94. }
  95. }