CatchClauseObfuscator.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import * as estraverse from 'estraverse';
  2. import { ICatchClauseNode } from "../interfaces/nodes/ICatchClauseNode";
  3. import { INode } from '../interfaces/nodes/INode';
  4. import { NodeObfuscator } from './NodeObfuscator';
  5. /**
  6. * replaces:
  7. * try {} catch (e) { console.log(e); };
  8. *
  9. * on:
  10. * try {} catch (_0x12d45f) { console.log(_0x12d45f); };
  11. *
  12. */
  13. export class CatchClauseObfuscator extends NodeObfuscator {
  14. /**
  15. * @type {Map<string, string>}
  16. */
  17. private catchClauseParam: Map <string, string> = new Map <string, string> ();
  18. /**
  19. * @param catchClauseNode
  20. */
  21. public obfuscateNode (catchClauseNode: ICatchClauseNode): void {
  22. this.storeCatchClauseParam(catchClauseNode);
  23. this.replaceCatchClauseParam(catchClauseNode);
  24. }
  25. /**
  26. * @param catchClauseNode
  27. */
  28. private storeCatchClauseParam (catchClauseNode: ICatchClauseNode): void {
  29. estraverse.traverse(catchClauseNode.param, {
  30. enter: (node: INode): any => this.storeIdentifiersNames(node, this.catchClauseParam)
  31. });
  32. }
  33. /**
  34. * @param catchClauseNode
  35. */
  36. private replaceCatchClauseParam (catchClauseNode: ICatchClauseNode): void {
  37. estraverse.replace(catchClauseNode, {
  38. enter: (node: INode, parentNode: INode): any => {
  39. this.replaceIdentifiersWithRandomNames(node, parentNode, this.catchClauseParam);
  40. }
  41. });
  42. }
  43. }