Skip to content

Commit 1178f8a

Browse files
committed
Add monthly-report ++reports option, fix report id
The monthly-report script now looks for reports in identifiers modified in the given month. A new ++reports option has been added to choose whether to skip getting the reports (only returning the vulnerability counts), get the last report in the selected month or get a list of all reports in the selected month. For this the argument parsing and help/usage text are changed to use argparse. This addresses the problem that the shown report ids could previously be outside the selected date range.
1 parent 5a1e625 commit 1178f8a

1 file changed

Lines changed: 92 additions & 41 deletions

File tree

scripts/monthly-report-gos24.10.gmp.py

Lines changed: 92 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,16 @@
33
# SPDX-License-Identifier: GPL-3.0-or-later
44

55
import sys
6-
from argparse import Namespace
7-
from datetime import date, timedelta
6+
from argparse import ArgumentParser, Namespace, RawDescriptionHelpFormatter
7+
from datetime import date, datetime, timedelta
88

99
from gvm.protocols.gmp import Gmp
1010
from terminaltables import AsciiTable
1111

1212

13-
def check_args(args: Namespace) -> None:
14-
len_args = len(args.script) - 1
15-
if len_args < 2:
16-
message = """
17-
This script will display all vulnerabilities from the hosts of the
18-
reports in a given month!
19-
It needs two parameters after the script name.
20-
First one is the month and second one is the year.
21-
Both parameters are plain numbers, so no text.
22-
23-
Explicitly made for GOS 24.10.
24-
25-
1. <month> -- month of the monthly report
26-
2. <year> -- year of the monthly report
27-
28-
Example:
29-
$ gvm-script --gmp-username name --gmp-password pass \
30-
ssh --hostname <gsm> scripts/monthly-report2.gmp.py 05 2019
31-
"""
32-
print(message)
33-
sys.exit()
34-
35-
36-
def print_reports(gmp: Gmp, from_date: date, to_date: date) -> None:
13+
def print_reports(
14+
gmp: Gmp, from_date: date, to_date: date, reports_choice: str
15+
) -> None:
3716
host_filter = (
3817
f"rows=-1 and modified>{from_date.isoformat()} "
3918
f"and created<{to_date.isoformat()}"
@@ -45,9 +24,30 @@ def print_reports(gmp: Gmp, from_date: date, to_date: date) -> None:
4524
sum_high = 0
4625
sum_medium = 0
4726
sum_low = 0
48-
table_data = [
49-
["Hostname", "IP", "Bericht", "critical", "high", "medium", "low"]
50-
]
27+
28+
table_header = ["Hostname", "IP", "Critical", "High", "Medium", "Low"]
29+
if reports_choice == "last":
30+
table_header = [
31+
"Hostname",
32+
"IP",
33+
"Report",
34+
"Critical",
35+
"High",
36+
"Medium",
37+
"Low",
38+
]
39+
elif reports_choice == "list":
40+
table_header = [
41+
"Hostname",
42+
"IP",
43+
"Reports",
44+
"Critical",
45+
"High",
46+
"Medium",
47+
"Low",
48+
]
49+
50+
table_data = [table_header]
5151

5252
for host in hosts_xml.xpath("asset"):
5353
ip = host.xpath("name/text()")[0]
@@ -95,13 +95,35 @@ def print_reports(gmp: Gmp, from_date: date, to_date: date) -> None:
9595
sum_high += high
9696
sum_critical += critical
9797

98-
best_os_cpe_report_id = host.xpath(
99-
'host/detail/name[text()="best_os_cpe"]/../source/@id'
100-
)[0]
101-
102-
table_data.append(
103-
[hostname, ip, best_os_cpe_report_id, critical, high, medium, low]
104-
)
98+
if reports_choice == "none":
99+
table_data.append([hostname, ip, critical, high, medium, low])
100+
else:
101+
report_host_identifiers = host.xpath(
102+
"identifiers/identifier[source/deleted = 0 and"
103+
' (source/type = "Report Host"'
104+
' or source/type = "Report Host Detail")]'
105+
)
106+
report_ids = []
107+
for identifier in report_host_identifiers:
108+
mod_date = datetime.fromisoformat(
109+
identifier.findtext("modification_time")
110+
).date()
111+
112+
if mod_date >= to_date or mod_date < from_date:
113+
continue
114+
115+
report_ids.append(identifier.find("source").get("id"))
116+
if reports_choice == 'last':
117+
break
118+
119+
if reports_choice == 'last':
120+
table_data.append(
121+
[hostname, ip, report_ids[0], critical, high, medium, low]
122+
)
123+
else:
124+
table_data.append(
125+
[hostname, ip, ',\n'.join(report_ids)+'\n', critical, high, medium, low]
126+
)
105127

106128
table = AsciiTable(table_data)
107129
print(f"{table.table}\n")
@@ -118,16 +140,45 @@ def print_reports(gmp: Gmp, from_date: date, to_date: date) -> None:
118140
def main(gmp: Gmp, args: Namespace) -> None:
119141
# pylint: disable=undefined-variable
120142

121-
check_args(args)
143+
description_message = """
144+
This script will display all vulnerabilities from the hosts of the \
145+
reports in a given month and year.
146+
These must be given after the script name as plain numbers.
147+
148+
This version is explicitly made for GOS 24.10.
149+
150+
Example:
151+
$ gvm-script --gmp-username name --gmp-password pass \
152+
ssh --hostname <gsm> scripts/monthly-report2.gmp.py 05 2019
153+
"""
154+
155+
parser = ArgumentParser(
156+
prog=("gvm-script [...] " + args.script[0]),
157+
formatter_class=RawDescriptionHelpFormatter,
158+
prefix_chars="+",
159+
description=description_message,
160+
)
161+
parser.add_argument("month", type=int, help="month of the monthly report")
162+
parser.add_argument("year", type=int, help="year of the monthly report")
163+
parser.add_argument(
164+
"++reports",
165+
choices=["none", "last", "list"],
166+
default="last",
167+
help=(
168+
"what to show in the reports column:"
169+
' "none": do not show a reports column;'
170+
' "last": show the last report in the selected month;'
171+
' "list": show a list of reports in the selected month.'
172+
),
173+
)
174+
script_args, _ = parser.parse_known_args(args.script[1:])
122175

123-
month = int(args.script[1])
124-
year = int(args.script[2])
125-
from_date = date(year, month, 1)
176+
from_date = date(script_args.year, script_args.month, 1)
126177
to_date = from_date + timedelta(days=31)
127178
# To have the first day in month
128179
to_date = to_date.replace(day=1)
129180

130-
print_reports(gmp, from_date, to_date)
181+
print_reports(gmp, from_date, to_date, script_args.reports)
131182

132183

133184
if __name__ == "__gmp__":

0 commit comments

Comments
 (0)