CatchClauseObfuscator.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import * as estraverse from 'estraverse';
  2. import * as ESTree from 'estree';
  3. import { ICustomNode } from '../interfaces/custom-nodes/ICustomNode';
  4. import { IOptions } from '../interfaces/IOptions';
  5. import { AbstractNodeObfuscator } from './AbstractNodeObfuscator';
  6. import { IdentifierReplacer } from './replacers/IdentifierReplacer';
  7. import { Node } from '../node/Node';
  8. /**
  9. * replaces:
  10. * try {} catch (e) { console.log(e); };
  11. *
  12. * on:
  13. * try {} catch (_0x12d45f) { console.log(_0x12d45f); };
  14. *
  15. */
  16. export class CatchClauseObfuscator extends AbstractNodeObfuscator {
  17. /**
  18. * @type {IdentifierReplacer}
  19. */
  20. private identifierReplacer: IdentifierReplacer;
  21. /**
  22. * @param nodes
  23. * @param options
  24. */
  25. constructor(nodes: Map <string, ICustomNode>, options: IOptions) {
  26. super(nodes, options);
  27. this.identifierReplacer = new IdentifierReplacer(this.nodes, this.options);
  28. }
  29. /**
  30. * @param catchClauseNode
  31. */
  32. public obfuscateNode (catchClauseNode: ESTree.CatchClause): void {
  33. this.storeCatchClauseParam(catchClauseNode);
  34. this.replaceCatchClauseParam(catchClauseNode);
  35. }
  36. /**
  37. * @param catchClauseNode
  38. */
  39. private storeCatchClauseParam (catchClauseNode: ESTree.CatchClause): void {
  40. if (Node.isIdentifierNode(catchClauseNode.param)) {
  41. this.identifierReplacer.storeNames(catchClauseNode.param.name);
  42. }
  43. }
  44. /**
  45. * @param catchClauseNode
  46. */
  47. private replaceCatchClauseParam (catchClauseNode: ESTree.CatchClause): void {
  48. estraverse.replace(catchClauseNode, {
  49. enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
  50. if (Node.isReplaceableIdentifierNode(node, parentNode)) {
  51. const newNodeName: string = this.identifierReplacer.replace(node.name);
  52. if (node.name !== newNodeName) {
  53. node.name = newNodeName;
  54. node.obfuscated = true;
  55. }
  56. }
  57. }
  58. });
  59. }
  60. }