UnicodeArrayRotateFunctionNode.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. "use strict";
  2. const esprima = require('esprima');
  3. const AppendState_1 = require("../../enums/AppendState");
  4. const Node_1 = require('../Node');
  5. const NodeUtils_1 = require("../../NodeUtils");
  6. const Utils_1 = require("../../Utils");
  7. class UnicodeArrayRotateFunctionNode extends Node_1.Node {
  8. constructor(unicodeArrayName, unicodeArray, unicodeArrayRotateValue) {
  9. super();
  10. this.appendState = AppendState_1.AppendState.AfterObfuscation;
  11. this.unicodeArrayName = unicodeArrayName;
  12. this.unicodeArray = unicodeArray;
  13. this.unicodeArrayRotateValue = unicodeArrayRotateValue;
  14. this.node = this.getNodeStructure();
  15. }
  16. appendNode(blockScopeNode) {
  17. let programBodyLength = blockScopeNode.body.length, randomIndex = Utils_1.Utils.getRandomInteger(1, programBodyLength);
  18. NodeUtils_1.NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), randomIndex);
  19. }
  20. getNode() {
  21. if (!this.unicodeArray.length) {
  22. return;
  23. }
  24. return super.getNode();
  25. }
  26. getNodeStructure() {
  27. let arrayName = Utils_1.Utils.getRandomVariableName(), timesName = Utils_1.Utils.getRandomVariableName(), tempArrayName = Utils_1.Utils.getRandomVariableName(), node = esprima.parse(`
  28. (function (${arrayName}, ${timesName}) {
  29. if (${timesName} < 0x${Utils_1.Utils.decToHex(0)}) {
  30. return;
  31. }
  32. var ${tempArrayName};
  33. while (${timesName}--) {
  34. ${tempArrayName} = ${arrayName}[${Utils_1.Utils.stringToUnicode('shift')}]();
  35. ${arrayName}[${Utils_1.Utils.stringToUnicode('push')}](${tempArrayName});
  36. }
  37. })(${this.unicodeArrayName}, 0x${Utils_1.Utils.decToHex(this.unicodeArrayRotateValue)});
  38. `);
  39. NodeUtils_1.NodeUtils.addXVerbatimPropertyToLiterals(node);
  40. return NodeUtils_1.NodeUtils.getBlockScopeNodeByIndex(node);
  41. }
  42. }
  43. exports.UnicodeArrayRotateFunctionNode = UnicodeArrayRotateFunctionNode;