UnicodeArrayRotateFunctionNode.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict";
  2. const esprima = require('esprima');
  3. const AppendState_1 = require("../../enums/AppendState");
  4. const JSFuck_1 = require("../../enums/JSFuck");
  5. const NoCustomNodesPreset_1 = require("../../preset-options/NoCustomNodesPreset");
  6. const JavaScriptObfuscator_1 = require("../../JavaScriptObfuscator");
  7. const Node_1 = require('../Node');
  8. const NodeUtils_1 = require("../../NodeUtils");
  9. const Utils_1 = require("../../Utils");
  10. class UnicodeArrayRotateFunctionNode extends Node_1.Node {
  11. constructor(unicodeArrayName, unicodeArray, unicodeArrayRotateValue, options = {}) {
  12. super(options);
  13. this.appendState = AppendState_1.AppendState.AfterObfuscation;
  14. this.unicodeArrayName = unicodeArrayName;
  15. this.unicodeArray = unicodeArray;
  16. this.unicodeArrayRotateValue = unicodeArrayRotateValue;
  17. this.node = this.getNodeStructure();
  18. }
  19. appendNode(blockScopeNode) {
  20. NodeUtils_1.NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), 1);
  21. }
  22. getNode() {
  23. if (!this.unicodeArray.length) {
  24. return;
  25. }
  26. return super.getNode();
  27. }
  28. getNodeStructure() {
  29. let arrayName = Utils_1.Utils.getRandomVariableName(), code = '', timesName = Utils_1.Utils.getRandomVariableName(), tempArrayName = Utils_1.Utils.getRandomVariableName(), node;
  30. if (this.options['selfDefending']) {
  31. code = JavaScriptObfuscator_1.JavaScriptObfuscator.obfuscate(`
  32. (function () {
  33. var func = function () {
  34. return 'window';
  35. };
  36. if (
  37. !/(\\\\\[x|u](\\w){2,4})+/.test(func.toString())
  38. ) {
  39. []['filter']['constructor'](${Utils_1.Utils.stringToJSFuck('while')} + '(${JSFuck_1.JSFuck.True}){}')();
  40. }
  41. })();
  42. `, NoCustomNodesPreset_1.NO_CUSTOM_NODES_PRESET);
  43. }
  44. node = esprima.parse(`
  45. (function (${arrayName}, ${timesName}) {
  46. if (${timesName} < 0x${Utils_1.Utils.decToHex(0)}) {
  47. return;
  48. }
  49. var ${tempArrayName};
  50. while (${timesName}--) {
  51. ${code}
  52. ${tempArrayName} = ${arrayName}[${Utils_1.Utils.stringToUnicode('shift')}]();
  53. ${arrayName}[${Utils_1.Utils.stringToUnicode('push')}](${tempArrayName});
  54. }
  55. })(${this.unicodeArrayName}, 0x${Utils_1.Utils.decToHex(this.unicodeArrayRotateValue)});
  56. `);
  57. NodeUtils_1.NodeUtils.addXVerbatimPropertyToLiterals(node);
  58. return NodeUtils_1.NodeUtils.getBlockScopeNodeByIndex(node);
  59. }
  60. }
  61. exports.UnicodeArrayRotateFunctionNode = UnicodeArrayRotateFunctionNode;