CatchClauseObfuscator.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 { NodeType } from '../../enums/NodeType';
  6. import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
  7. import { IdentifierReplacer } from './replacers/IdentifierReplacer';
  8. import { Node } from '../../node/Node';
  9. import { NodeUtils } from '../../node/NodeUtils';
  10. /**
  11. * replaces:
  12. * try {} catch (e) { console.log(e); };
  13. *
  14. * on:
  15. * try {} catch (_0x12d45f) { console.log(_0x12d45f); };
  16. *
  17. */
  18. export class CatchClauseObfuscator extends AbstractNodeTransformer {
  19. /**
  20. * @type {IdentifierReplacer}
  21. */
  22. private readonly identifierReplacer: IdentifierReplacer;
  23. /**
  24. * @param nodes
  25. * @param options
  26. */
  27. constructor(nodes: Map <string, ICustomNode>, options: IOptions) {
  28. super(nodes, options);
  29. this.identifierReplacer = new IdentifierReplacer(this.nodes, this.options);
  30. }
  31. /**
  32. * @param catchClauseNode
  33. */
  34. public transformNode (catchClauseNode: ESTree.CatchClause): void {
  35. this.storeCatchClauseParam(catchClauseNode);
  36. this.replaceCatchClauseParam(catchClauseNode);
  37. }
  38. /**
  39. * @param catchClauseNode
  40. */
  41. private storeCatchClauseParam (catchClauseNode: ESTree.CatchClause): void {
  42. NodeUtils.typedTraverse(catchClauseNode.param, NodeType.Identifier, {
  43. enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name)
  44. });
  45. }
  46. /**
  47. * @param catchClauseNode
  48. */
  49. private replaceCatchClauseParam (catchClauseNode: ESTree.CatchClause): void {
  50. estraverse.replace(catchClauseNode, {
  51. enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
  52. if (Node.isReplaceableIdentifierNode(node, parentNode)) {
  53. node.name = this.identifierReplacer.replace(node.name);
  54. }
  55. }
  56. });
  57. }
  58. }