DebugProtectionFunctionNode.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 { Node } from '../Node';
  6. import { NodeUtils } from '../../NodeUtils';
  7. import { Utils } from "../../Utils";
  8. export class DebugProtectionFunctionNode extends Node {
  9. /**
  10. * @type {string}
  11. */
  12. private debugProtectionFunctionName: string;
  13. /**
  14. * @param debugProtectionFunctionName
  15. * @param options
  16. */
  17. constructor (debugProtectionFunctionName: string, options: IOptions) {
  18. super(options);
  19. this.debugProtectionFunctionName = debugProtectionFunctionName;
  20. this.node = this.getNodeStructure();
  21. }
  22. /**
  23. * @param blockScopeNode
  24. */
  25. public appendNode (blockScopeNode: TNodeWithBlockStatement): void {
  26. let programBodyLength: number = blockScopeNode.body.length,
  27. randomIndex: number = Utils.getRandomGenerator().integer({
  28. min: 0,
  29. max: programBodyLength
  30. });
  31. NodeUtils.insertNodeAtIndex(blockScopeNode.body, this.getNode(), randomIndex);
  32. }
  33. /**
  34. * @returns {string}
  35. */
  36. public getNodeIdentifier (): string {
  37. return this.debugProtectionFunctionName;
  38. }
  39. /**
  40. * Found this trick in JScrambler
  41. *
  42. * @returns {INode}
  43. */
  44. protected getNodeStructure (): INode {
  45. return NodeUtils.getBlockStatementNodeByIndex(
  46. esprima.parse(`
  47. var ${this.debugProtectionFunctionName} = function () {
  48. function debuggerProtection (counter) {
  49. if (('' + counter / counter)['length'] !== 1 || counter % 20 === 0) {
  50. (function () {}.constructor('debugger')());
  51. } else {
  52. [].filter.constructor(${Utils.stringToJSFuck('debugger')})();
  53. }
  54. debuggerProtection(++counter);
  55. }
  56. try {
  57. debuggerProtection(0);
  58. } catch (y) {}
  59. };
  60. `)
  61. );
  62. }
  63. }