DebugProtectionFunctionNode.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import * as esprima from 'esprima';
  2. import * as estraverse from 'estraverse';
  3. import { ITreeNode } from '../interfaces/nodes/ITreeNode';
  4. import { Node } from './Node';
  5. import { NodeUtils } from '../NodeUtils';
  6. export class DebugProtectionFunctionNode extends Node {
  7. /**
  8. * @type {ITreeNode}
  9. */
  10. protected node: ITreeNode;
  11. /**
  12. * @type {ITreeNode}
  13. */
  14. private astTree: ITreeNode;
  15. /**
  16. * @type {string}
  17. */
  18. private debugProtectionFunctionName: string;
  19. /**
  20. * @type {number}
  21. */
  22. private debugProtectionFunctionIndex: number;
  23. /**
  24. * @param astTree
  25. * @param debugProtectionFunctionName
  26. * @param debugProtectionFunctionIndex
  27. */
  28. constructor (
  29. astTree: ITreeNode,
  30. debugProtectionFunctionName: string,
  31. debugProtectionFunctionIndex: number
  32. ) {
  33. super();
  34. this.astTree = astTree;
  35. this.debugProtectionFunctionName = debugProtectionFunctionName;
  36. this.debugProtectionFunctionIndex = debugProtectionFunctionIndex;
  37. this.node = this.getNodeStructure();
  38. }
  39. public appendNode (): void {
  40. estraverse.replace(this.astTree, {
  41. leave: (node: ITreeNode, parent: ITreeNode): any => {
  42. if (NodeUtils.isProgramNode(node)) {
  43. node.body.splice(this.debugProtectionFunctionIndex, 0, this.getNode());
  44. return estraverse.VisitorOption.Break;
  45. }
  46. return estraverse.VisitorOption.Skip;
  47. }
  48. });
  49. }
  50. /**
  51. * @returns {string}
  52. */
  53. public getNodeIdentifier (): string {
  54. return this.debugProtectionFunctionName;
  55. }
  56. /**
  57. * Found this trick in JScrambler
  58. *
  59. * @returns any
  60. */
  61. protected getNodeStructure (): any {
  62. return NodeUtils.getBlockScopeNodeByIndex(
  63. esprima.parse(`
  64. var ${this.debugProtectionFunctionName} = function () {
  65. function debuggerProtection (counter) {
  66. if (('' + counter / counter)['length'] !== 1 || counter % 20 === 0) {
  67. (function () {}.constructor('debugger')());
  68. } else {
  69. [].filter.constructor((undefined + '')[2] + (!![] + '')[3] + (Function('return{}')() + '')[2] + (undefined + '')[0] + (![] + [0] + String)[20] + (![] + [0] + String)[20] + (!![] + '')[3] + (!![] + '')[1])();
  70. }
  71. debuggerProtection(++counter);
  72. }
  73. try {
  74. debuggerProtection(0);
  75. } catch (y) {}
  76. };
  77. `)
  78. );
  79. }
  80. }