CatchClauseObfuscator.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import * as estraverse from 'estraverse';
  2. import { ICatchClauseNode } from "../interfaces/nodes/ICatchClauseNode";
  3. import { ITreeNode } from '../interfaces/nodes/ITreeNode';
  4. import { NodeObfuscator } from './NodeObfuscator';
  5. import { NodeUtils } from "../NodeUtils";
  6. import { Utils } from '../Utils';
  7. /**
  8. * replaces:
  9. * try {} catch (e) { console.log(e); };
  10. *
  11. * by:
  12. * try {} catch (_0x12d45f) { console.log(_0x12d45f); };
  13. *
  14. */
  15. export class CatchClauseObfuscator extends NodeObfuscator {
  16. /**
  17. * @type {Map<string, string>}
  18. */
  19. private catchClauseParam: Map <string, string> = new Map <string, string> ();
  20. /**
  21. * @param catchClauseNode
  22. */
  23. public obfuscateNode (catchClauseNode: ICatchClauseNode): void {
  24. this.replaceCatchClauseParam(catchClauseNode);
  25. this.replaceCatchClauseParamInBlock(catchClauseNode);
  26. }
  27. /**
  28. * @param catchClauseNode
  29. */
  30. private replaceCatchClauseParam (catchClauseNode: ICatchClauseNode): void {
  31. estraverse.replace(catchClauseNode.param, {
  32. leave: (node: ITreeNode, parentNode: ITreeNode) => {
  33. if (NodeUtils.isIdentifierNode(node)) {
  34. this.catchClauseParam.set(node.name, Utils.getRandomVariableName());
  35. node.name = this.catchClauseParam.get(node.name);
  36. return;
  37. }
  38. return estraverse.VisitorOption.Skip;
  39. }
  40. });
  41. }
  42. /**
  43. * @param catchClauseNode
  44. */
  45. private replaceCatchClauseParamInBlock (catchClauseNode: ICatchClauseNode): void {
  46. estraverse.replace(catchClauseNode.body, {
  47. leave: (node: ITreeNode, parentNode: ITreeNode) => {
  48. this.replaceNodeIdentifierByNewValue(node, parentNode, this.catchClauseParam);
  49. }
  50. });
  51. }
  52. }