UnicodeArrayDecodeNode.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import * as esprima from 'esprima';
  2. import { JavaScriptObfuscator } from '../../JavaScriptObfuscator';
  3. import { INode } from "../../interfaces/nodes/INode";
  4. import { IOptions } from "../../interfaces/IOptions";
  5. import { TBlockScopeNode } from "../../types/TBlockScopeNode";
  6. import { AppendState } from "../../enums/AppendState";
  7. import { JSFuck } from "../../enums/JSFuck";
  8. import { NO_CUSTOM_NODES_PRESET } from "../../preset-options/NoCustomNodesPreset";
  9. import { Node } from '../Node';
  10. import { NodeUtils } from "../../NodeUtils";
  11. import { Utils } from "../../Utils";
  12. export class UnicodeArrayDecodeNode 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 unicodeArrayName
  27. * @param unicodeArray
  28. * @param options
  29. */
  30. constructor (
  31. unicodeArrayName: string,
  32. unicodeArray: string[],
  33. options: IOptions = {}
  34. ) {
  35. super(options);
  36. this.unicodeArrayName = unicodeArrayName;
  37. this.unicodeArray = unicodeArray;
  38. this.node = this.getNodeStructure();
  39. }
  40. /**
  41. * @param blockScopeNode
  42. */
  43. public appendNode (blockScopeNode: TBlockScopeNode): void {
  44. NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), 1);
  45. }
  46. /**
  47. * @returns {INode}
  48. */
  49. public getNode (): INode {
  50. if (!this.unicodeArray.length) {
  51. return;
  52. }
  53. this.updateNode();
  54. return super.getNode();
  55. }
  56. /**
  57. * @returns {INode}
  58. */
  59. protected getNodeStructure (): INode {
  60. const environmentName: string = Utils.getRandomVariableName(),
  61. forLoopFunctionName: string = Utils.getRandomVariableName(),
  62. indexVariableName: string = Utils.getRandomVariableName(),
  63. tempArrayName: string = Utils.getRandomVariableName();
  64. let code: string = '',
  65. node: INode;
  66. if (this.options['selfDefending']) {
  67. code = `
  68. var ${environmentName} = function(){return ${Utils.stringToUnicode('dev')};};
  69. Function(${Utils.stringToUnicode(`return/\\w+ *\\(\\) *{\\w+ *['|"].+['|"];? *}/`)})()[${Utils.stringToUnicode('test')}](${environmentName}[${Utils.stringToUnicode('toString')}]()) !== ${JSFuck.True} && !${this.unicodeArrayName}++ ? []['filter']['constructor'](${Utils.stringToJSFuck('while')} + '(${JSFuck.True}){}')() : Function(${Utils.stringToUnicode('a')}, atob(${Utils.stringToUnicode(Utils.btoa('a()'))}))(${forLoopFunctionName}) ? []['filter']['constructor'](${Utils.stringToJSFuck('while')} + '(${JSFuck.False}){}')() : []['filter']['constructor'](${Utils.stringToJSFuck('while')} + '(${JSFuck.False}){}')();
  70. `;
  71. } else {
  72. code = `${forLoopFunctionName}();`;
  73. }
  74. node = esprima.parse(`
  75. (function () {
  76. ${JavaScriptObfuscator.obfuscate(`
  77. (function () {
  78. var object = []['filter']['constructor']('return this')();
  79. var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  80. object.atob || (
  81. object.atob = function(input) {
  82. var str = String(input).replace(/=+$/, '');
  83. for (
  84. var bc = 0, bs, buffer, idx = 0, output = '';
  85. buffer = str.charAt(idx++);
  86. ~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer,
  87. bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0
  88. ) {
  89. buffer = chars.indexOf(buffer);
  90. }
  91. return output;
  92. });
  93. })();
  94. `, NO_CUSTOM_NODES_PRESET)}
  95. var ${forLoopFunctionName} = function () {
  96. var ${tempArrayName} = [];
  97. for (var ${indexVariableName} in ${this.unicodeArrayName}) {
  98. ${tempArrayName}[${Utils.stringToUnicode('push')}](decodeURI(atob(${this.unicodeArrayName}[${indexVariableName}])));
  99. }
  100. ${this.unicodeArrayName} = ${tempArrayName};
  101. };
  102. ${code}
  103. })();
  104. `);
  105. NodeUtils.addXVerbatimPropertyToLiterals(node);
  106. return NodeUtils.getBlockScopeNodeByIndex(node);
  107. }
  108. }