1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright (C) 2017-2021 Greenbone Networks GmbH |
3
|
|
|
# |
4
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later |
5
|
|
|
# |
6
|
|
|
# This program is free software: you can redistribute it and/or modify |
7
|
|
|
# it under the terms of the GNU General Public License as published by |
8
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
# (at your option) any later version. |
10
|
|
|
# |
11
|
|
|
# This program is distributed in the hope that it will be useful, |
12
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
# GNU General Public License for more details. |
15
|
|
|
# |
16
|
|
|
# You should have received a copy of the GNU General Public License |
17
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
|
19
|
|
|
import sys |
20
|
|
|
from datetime import date, timedelta |
21
|
|
|
|
22
|
|
|
from terminaltables import AsciiTable |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
def check_args(args): |
26
|
|
|
len_args = len(args.script) - 1 |
27
|
|
|
if len_args < 2: |
28
|
|
|
message = """ |
29
|
|
|
This script will display all vulnerabilities from the hosts of the |
30
|
|
|
reports in a given month! |
31
|
|
|
|
32
|
|
|
1. <month> -- month of the monthly report |
33
|
|
|
2. <year> -- year of the monthly report |
34
|
|
|
|
35
|
|
|
The third is 'with-tables' parameter to activate a verbose output of |
36
|
|
|
hosts. Explicitly made for GOS 3.1. |
37
|
|
|
|
38
|
|
|
Example: |
39
|
|
|
$ gvm-script --gmp-username name --gmp-password pass \ |
40
|
|
|
ssh --hostname <gsm> scripts/monthly-report.gmp.py 05 2017 with-tables |
41
|
|
|
""" |
42
|
|
|
print(message) |
43
|
|
|
sys.exit() |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
def get_reports_xml(gmp, from_date, to_date): |
47
|
|
|
""" Getting the Reports in the defined time period """ |
48
|
|
|
|
49
|
|
|
report_filter = "rows=-1 created>{0} and created<{1}".format( |
50
|
|
|
from_date.isoformat(), to_date.isoformat() |
51
|
|
|
) |
52
|
|
|
|
53
|
|
|
return gmp.get_reports(filter=report_filter) |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
def print_result_sums(reports_xml, from_date, to_date): |
57
|
|
|
print('Found {0} reports'.format(len(reports_xml.xpath('report')))) |
58
|
|
|
|
59
|
|
|
sum_high = reports_xml.xpath( |
60
|
|
|
'sum(report/report/result_count/hole/full/text())' |
61
|
|
|
) |
62
|
|
|
sum_medium = reports_xml.xpath( |
63
|
|
|
'sum(report/report/result_count/warning/full/text())' |
64
|
|
|
) |
65
|
|
|
sum_low = reports_xml.xpath( |
66
|
|
|
'sum(report/report/result_count/info/full/text())' |
67
|
|
|
) |
68
|
|
|
|
69
|
|
|
print( |
70
|
|
|
'Summary of results from {3} to {4}\nHigh: {0}\nMedium: {1}\nLow: ' |
71
|
|
|
'{2}\n\n'.format( |
72
|
|
|
int(sum_high), |
73
|
|
|
int(sum_medium), |
74
|
|
|
int(sum_low), |
75
|
|
|
from_date.isoformat(), |
76
|
|
|
to_date.isoformat(), |
77
|
|
|
) |
78
|
|
|
) |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
def print_result_tables(gmp, reports_xml): |
82
|
|
|
report_list = reports_xml.xpath('report') |
83
|
|
|
|
84
|
|
|
for report in report_list: |
85
|
|
|
report_id = report.xpath('report/@id')[0] |
86
|
|
|
name = report.xpath('name/text()')[0] |
87
|
|
|
|
88
|
|
|
res = gmp.get_report(report_id) |
89
|
|
|
|
90
|
|
|
print('\nReport: {0}'.format(report_id)) |
91
|
|
|
|
92
|
|
|
table_data = [['Hostname', 'IP', 'Bericht', 'high', 'medium', 'low']] |
93
|
|
|
|
94
|
|
|
for host in res.xpath('report/report/host'): |
95
|
|
|
hostname = host.xpath( |
96
|
|
|
'detail/name[text()="hostname"]/../' 'value/text()' |
97
|
|
|
) |
98
|
|
|
if len(hostname) > 0: |
99
|
|
|
hostname = str(hostname[0]) |
100
|
|
|
else: |
101
|
|
|
hostname = "" |
102
|
|
|
|
103
|
|
|
ip = host.xpath('ip/text()')[0] |
104
|
|
|
high = host.xpath('result_count/hole/page/text()')[0] |
105
|
|
|
medium = host.xpath('result_count/warning/page/text()')[0] |
106
|
|
|
low = host.xpath('result_count/info/page/text()')[0] |
107
|
|
|
|
108
|
|
|
table_data.append([hostname, ip, name, high, medium, low]) |
109
|
|
|
host.clear() |
110
|
|
|
del host |
111
|
|
|
|
112
|
|
|
table = AsciiTable(table_data) |
113
|
|
|
print(table.table + '\n') |
114
|
|
|
res.clear() |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
def main(gmp, args): |
118
|
|
|
# pylint: disable=undefined-variable |
119
|
|
|
|
120
|
|
|
check_args(args) |
121
|
|
|
|
122
|
|
|
month = int(args.script[1]) |
123
|
|
|
year = int(args.script[2]) |
124
|
|
|
from_date = date(year, month, 1) |
125
|
|
|
to_date = from_date + timedelta(days=31) |
126
|
|
|
# To have the first day in month |
127
|
|
|
to_date = to_date.replace(day=1) |
128
|
|
|
|
129
|
|
|
reports_xml = get_reports_xml(gmp, from_date, to_date) |
130
|
|
|
|
131
|
|
|
print_result_sums(reports_xml, from_date, to_date) |
132
|
|
|
if "with-tables" in args.script: |
133
|
|
|
print_result_tables(gmp, reports_xml) |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
if __name__ == '__gmp__': |
137
|
|
|
main(gmp, args) |
|
|
|
|
138
|
|
|
|