nmap-get.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/python
  2. import sys
  3. import json
  4. from rich.console import Console
  5. from rich.table import Table
  6. import argparse
  7. import re
  8. # Setup Table
  9. console = Console()
  10. table = Table(show_header=True, header_style="bold green")
  11. table.add_column("IP", style="dim")
  12. table.add_column("Host")
  13. table.add_column("Port")
  14. table.add_column("Service")
  15. table.add_column("Version", justify="left")
  16. def pprint_table(hostlist, iponly):
  17. global table
  18. for host in hostlist:
  19. for port in host["ports"]:
  20. if iponly:
  21. print(host["ip"])
  22. else:
  23. table.add_row(
  24. host["ip"],
  25. host["hostname"],
  26. str(port["port_number"]),
  27. port["service"],
  28. port["version"]
  29. )
  30. if not iponly and table.row_count > 0:
  31. console.print(table)
  32. def filter_by_port(port):
  33. global hosts
  34. out = []
  35. ports = []
  36. for host in hosts:
  37. for p in host["ports"]:
  38. if port == str(p["port_number"]) and p["state"] == "open":
  39. ports.append(p)
  40. host["ports"] = ports
  41. ports = []
  42. out.append(host)
  43. return out
  44. def filter_by_service(service):
  45. global hosts
  46. recomp = re.compile(service, re.IGNORECASE)
  47. out = []
  48. ports = []
  49. for host in hosts:
  50. for p in host["ports"]:
  51. if len(recomp.findall(p["service"])) > 0:
  52. ports.append(p)
  53. host["ports"] = ports
  54. ports = []
  55. out.append(host)
  56. return out
  57. def filter_by_version(version):
  58. global hosts
  59. # Support regular expressions for version matching
  60. recomp = re.compile(version, re.IGNORECASE)
  61. out = []
  62. ports = []
  63. for host in hosts:
  64. for p in host["ports"]:
  65. if len(recomp.findall(p["version"])) > 0:
  66. ports.append(p)
  67. host["ports"] = ports
  68. ports = []
  69. out.append(host)
  70. return out
  71. # Setup Argument Parser
  72. parser = argparse.ArgumentParser(description='Filtering nmap')
  73. parser.add_argument('file', action='store', nargs='?',
  74. help='Input File')
  75. parser.add_argument('--port', dest='port', action='store',
  76. help='Filter by port number')
  77. parser.add_argument('--version', dest='version', action='store',
  78. help='Filter by version string')
  79. parser.add_argument('--service', dest='service', action='store',
  80. help='Filter by service')
  81. parser.add_argument('--ip-only', '-i', dest='ip', action='store_true',
  82. help='Only print the ips')
  83. args = parser.parse_args()
  84. if args.file:
  85. with open(args.file, "r") as inp_file:
  86. hosts = json.loads(inp_file.read())
  87. else:
  88. hosts = json.loads(sys.stdin.read())
  89. if args.port:
  90. pprint_table(filter_by_port(args.port), args.ip)
  91. elif args.version:
  92. pprint_table(filter_by_version(args.version), args.ip)
  93. elif args.service:
  94. pprint_table(filter_by_service(args.service), args.ip)
  95. else:
  96. pprint_table(hosts, args.ip)