|
@@ -4,34 +4,35 @@ import json
|
|
|
from rich.console import Console
|
|
|
from rich.table import Table
|
|
|
import argparse
|
|
|
+import re
|
|
|
|
|
|
|
|
|
console = Console()
|
|
|
|
|
|
table = Table(show_header=True, header_style="bold blue")
|
|
|
-table.add_column("Host", style="dim")
|
|
|
table.add_column("IP", style="dim")
|
|
|
+table.add_column("Host")
|
|
|
table.add_column("Port")
|
|
|
table.add_column("Service")
|
|
|
table.add_column("Version", justify="left")
|
|
|
|
|
|
def pprint_table(hostlist, iponly):
|
|
|
-
|
|
|
global table
|
|
|
+
|
|
|
for host in hostlist:
|
|
|
- if iponly:
|
|
|
- print(host["ip"])
|
|
|
- else:
|
|
|
- for port in host["ports"]:
|
|
|
+ for port in host["ports"]:
|
|
|
+ if iponly:
|
|
|
+ print(host["ip"])
|
|
|
+ else:
|
|
|
table.add_row(
|
|
|
- host["hostname"],
|
|
|
host["ip"],
|
|
|
+ host["hostname"],
|
|
|
str(port["port_number"]),
|
|
|
port["service"],
|
|
|
port["version"]
|
|
|
)
|
|
|
|
|
|
- if not iponly:
|
|
|
+ if not iponly and table.row_count > 0:
|
|
|
console.print(table)
|
|
|
|
|
|
def filter_by_port(port):
|
|
@@ -48,19 +49,36 @@ def filter_by_port(port):
|
|
|
return out
|
|
|
|
|
|
|
|
|
+def filter_by_service(service):
|
|
|
+ global hosts
|
|
|
+ out = []
|
|
|
+ ports = []
|
|
|
+ for host in hosts:
|
|
|
+ for p in host["ports"]:
|
|
|
+ if service.lower() in p["service"].lower():
|
|
|
+ ports.append(p)
|
|
|
+ host["ports"] = ports
|
|
|
+ ports = []
|
|
|
+ out.append(host)
|
|
|
+ return out
|
|
|
+
|
|
|
+
|
|
|
def filter_by_version(version):
|
|
|
global hosts
|
|
|
+
|
|
|
+ recomp = re.compile(version)
|
|
|
out = []
|
|
|
ports = []
|
|
|
for host in hosts:
|
|
|
for p in host["ports"]:
|
|
|
- if version.lower() in p["version"].lower():
|
|
|
+ if len(recomp.findall(p["version"])) > 0:
|
|
|
ports.append(p)
|
|
|
host["ports"] = ports
|
|
|
ports = []
|
|
|
out.append(host)
|
|
|
return out
|
|
|
|
|
|
+
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='Filtering nmap')
|
|
|
parser.add_argument('file', action='store', nargs='?',
|
|
@@ -69,7 +87,9 @@ parser.add_argument('--port', dest='port', action='store',
|
|
|
help='Filter by port number')
|
|
|
parser.add_argument('--version', dest='version', action='store',
|
|
|
help='Filter by version string')
|
|
|
-parser.add_argument('--ip', dest='ip', action='store_true',
|
|
|
+parser.add_argument('--service', dest='service', action='store',
|
|
|
+ help='Filter by service')
|
|
|
+parser.add_argument('--ip-only', '-i', dest='ip', action='store_true',
|
|
|
help='Only print the ips')
|
|
|
|
|
|
|
|
@@ -86,5 +106,7 @@ if args.port:
|
|
|
pprint_table(filter_by_port(args.port), args.ip)
|
|
|
elif args.version:
|
|
|
pprint_table(filter_by_version(args.version), args.ip)
|
|
|
+elif args.service:
|
|
|
+ pprint_table(filter_by_service(args.service), args.ip)
|
|
|
else:
|
|
|
pprint_table(hosts, args.ip)
|