1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
# SPDX-FileCopyrightText: 2025 Martin Boller
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Loosely based on other greenbone scripts
#
# Run with: gvm-script --gmp-username admin-user --gmp-password password socket list-hosts.gmp.py <days>
# example: gvm-script --gmp-username admin --gmp-password top$ecret socket list-hosts.gmp.py 2
import sys
from argparse import ArgumentParser, Namespace, RawTextHelpFormatter
from datetime import date, datetime, time, timedelta
from gvm.protocols.gmp import Gmp
from gvmtools.helper import Table
HELP_TEXT = (
"This script generates a table of hosts (assets) "
"from Greenbone Vulnerability Manager.\n\n"
"table will contain:\n"
"Hostname, IP Address, MAC Address, Operating System, last seen, and severity\n"
)
def check_args(args: Namespace) -> None:
len_args = len(args.script) - 1
if len_args < 1:
message = """
This script requests information about all hosts <days> days prior to today (included) and
displays it as a table. It requires one parameter after the script name:
1. days -- number of days prior to today to pull hosts information from
Examples:
$ gvm-script --gmp-username username --gmp-password password socket list-hosts.gmp.py <days>
$ gvm-script --gmp-username admin --gmp-password 0f6fa69b-32bb-453a-9aa4-b8c9e56b3d00 socket list-hosts.gmp.py 4
"""
print(message)
sys.exit()
def parse_args(args: Namespace) -> Namespace: # pylint: disable=unused-argument
"""Parsing args ..."""
parser = ArgumentParser(
prefix_chars="+",
add_help=False,
formatter_class=RawTextHelpFormatter,
)
parser.add_argument(
"+h",
"++help",
action="help",
help="Show this help message and exit.",
)
parser.add_argument(
"delta_days",
type=int,
help=("Number of days in the past to pull hosts information"),
)
script_args, _ = parser.parse_known_args(args)
return script_args
def list_hosts(gmp: Gmp, from_date: date, to_date: date) -> None:
host_filter = (
f"rows=-1 "
f"and modified>{from_date.isoformat()} "
f"and modified<{to_date.isoformat()}"
)
# Get the XML of hosts
hosts_xml = gmp.get_hosts(filter_string=host_filter)
heading = [
"#",
"IP Address",
"Hostname",
"MAC Address",
"Operating System",
"Last Seen",
"CVSS",
]
rows = []
numberRows = 0
print("Listing hosts.\n" f"From: {from_date}\n" f"To: {to_date}\n")
for host in hosts_xml.xpath("asset"):
# ip will always be there
ip = host.xpath("name/text()")[0]
host_seendates = host.xpath("modification_time/text()")
host_lastseen = host_seendates[0]
# hostnames and other attributes may not be there
hostnames = host.xpath(
'identifiers/identifier/name[text()="hostname"]/../value/text()'
)
if len(hostnames) == 0:
hostname = ""
pass
else:
hostname = hostnames[0]
host_macs = host.xpath(
'identifiers/identifier/name[text()="MAC"]/../value/text()'
)
if len(host_macs) == 0:
host_mac = ""
pass
else:
host_mac = host_macs[0]
host_severity = host.xpath("host/severity/value/text()")
if len(host_severity) == 0:
host_severity = "0.0"
pass
else:
host_severity = host_severity[0]
host_os = host.xpath(
'host/detail/name[text()="best_os_txt"]/../value/text()'
)
if len(host_os) == 0:
host_os = ""
pass
else:
host_os = host_os[0]
# Count number of hosts
numberRows = numberRows + 1
# Cast/convert to text to show in list
rowNumber = str(numberRows)
rows.append(
[
rowNumber,
ip,
hostname,
host_mac,
host_os,
host_lastseen,
host_severity,
]
)
print(Table(heading=heading, rows=rows))
def main(gmp: Gmp, args: Namespace) -> None:
# pylint: disable=undefined-variable
check_args(args)
if args.script:
args = args.script[1:]
parsed_args = parse_args(args=args)
delta_days = parsed_args.delta_days
# simply getting yesterday from midnight to today (now)
from_date = datetime.combine(datetime.today(), time.min) - timedelta(
days=delta_days
)
to_date = datetime.now()
# print(from_date, to_date)
list_hosts(gmp, from_date, to_date)
if __name__ == "__gmp__":
main(gmp, args)
|