FunctionObfuscator.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import * as estraverse from 'estraverse';
  2. import { IBlockStatementNode } from "../interfaces/nodes/IBlockStatementNode";
  3. import { IFunctionNode } from "../interfaces/nodes/IFunctionNode";
  4. import { INode } from "../interfaces/nodes/INode";
  5. import { NodeObfuscator } from './NodeObfuscator';
  6. import { Nodes } from "../Nodes";
  7. import { Utils } from '../Utils';
  8. /**
  9. * replaces:
  10. * function foo (argument1) { return argument1; };
  11. *
  12. * on:
  13. * function foo (_0x12d45f) { return _0x12d45f; };
  14. *
  15. */
  16. export class FunctionObfuscator extends NodeObfuscator {
  17. /**
  18. * @type {Map<string, string>}
  19. */
  20. private functionParams: Map <string, string> = new Map <string, string> ();
  21. /**
  22. * @param functionNode
  23. */
  24. public obfuscateNode (functionNode: IFunctionNode): void {
  25. this.replaceFunctionParams(functionNode);
  26. this.replaceFunctionParamsInBlockStatement(functionNode);
  27. }
  28. /**
  29. * @param functionNode
  30. */
  31. private replaceFunctionParams (functionNode: IFunctionNode): void {
  32. functionNode.params.forEach((paramsNode: INode) => {
  33. estraverse.replace(paramsNode, {
  34. leave: (node: INode): any => this.replaceAndStoreIdentifiersNames(node, this.functionParams)
  35. });
  36. });
  37. }
  38. /**
  39. * @param functionNode
  40. */
  41. private replaceFunctionParamsInBlockStatement (functionNode: IFunctionNode): void {
  42. estraverse.replace(functionNode.body, {
  43. leave: (node: INode, parentNode: INode): any => {
  44. this.replaceNodeIdentifierByNewValue(node, parentNode, this.functionParams);
  45. }
  46. });
  47. }
  48. }