SelfDefendingUnicodeNode.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 SelfDefendingUnicodeNode extends Node {
  13. /**
  14. * @type {AppendState}
  15. */
  16. protected appendState: AppendState = AppendState.AfterObfuscation;
  17. /**
  18. * @param options
  19. */
  20. constructor (options: IOptions = {}) {
  21. super(options);
  22. this.node = this.getNodeStructure();
  23. }
  24. /**
  25. * @param blockScopeNode
  26. */
  27. public appendNode (blockScopeNode: TBlockScopeNode): void {
  28. let programBodyLength: number = blockScopeNode.body.length,
  29. randomIndex: number = 0;
  30. if (programBodyLength > 2) {
  31. randomIndex = Utils.getRandomInteger(programBodyLength / 2, programBodyLength - 1);
  32. }
  33. NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), randomIndex);
  34. }
  35. /**
  36. * @returns {INode}
  37. */
  38. protected getNodeStructure (): INode {
  39. let node: INode = esprima.parse(
  40. JavaScriptObfuscator.obfuscate(`
  41. (function () {
  42. var func = function(){return ${Utils.stringToUnicode('dev')};},
  43. func2 = function () {
  44. return 'window';
  45. };
  46. !Function(${Utils.stringToUnicode(`return/\\w+ *\\(\\) *{\\w+ *['|"].+['|"];? *}/`)})().test(func.toString()) ? Function(${Utils.stringToUnicode(`return/(\\\\[x|u](\\w){2,4})+/`)})().test(func2.toString()) ? []['filter']['constructor'](${Utils.stringToJSFuck('while')} + '(${JSFuck.False}){}')() : []['filter']['constructor'](${Utils.stringToJSFuck('while')} + '(${JSFuck.True}){}')() : []['filter']['constructor'](${Utils.stringToJSFuck('while')} + '(${JSFuck.False}){}')();
  47. })();
  48. `, NO_CUSTOM_NODES_PRESET)
  49. );
  50. NodeUtils.addXVerbatimPropertyToLiterals(node);
  51. return NodeUtils.getBlockScopeNodeByIndex(node);
  52. }
  53. }