1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright (C) 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
|
|
|
from typing import List |
20
|
|
|
from datetime import date, timedelta |
21
|
|
|
from argparse import ArgumentParser, RawTextHelpFormatter |
22
|
|
|
|
23
|
|
|
from gvmtools.helper import generate_uuid |
24
|
|
|
|
25
|
|
|
from lxml import etree as e |
26
|
|
|
|
27
|
|
|
HELP_TEXT = "" |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
def get_last_reports_from_tasks(gmp, from_date, to_date): |
31
|
|
|
""" Get the last reports from the tasks in the time period """ |
32
|
|
|
task_filter = "rows=-1 and created>{0} and created<{1}".format( |
33
|
|
|
from_date.isoformat(), to_date.isoformat() |
34
|
|
|
) |
35
|
|
|
|
36
|
|
|
tasks_xml = gmp.get_tasks(filter=task_filter) |
37
|
|
|
reports = [] |
38
|
|
|
for report in tasks_xml.xpath('task/last_report/report/@id'): |
39
|
|
|
reports.append(str(report)) |
40
|
|
|
|
41
|
|
|
print(reports) |
42
|
|
|
return reports |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
def create_filter(gmp, filter_term, date): |
46
|
|
|
filter_name = "Filter for Monthly Report ({})".format(date) |
47
|
|
|
|
48
|
|
|
res = gmp.create_filter(term=filter_term, name=filter_name) |
49
|
|
|
return res.xpath('//@id')[0] |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
def combine_reports(gmp, reports: List, filter_term: str): |
53
|
|
|
print("combine ...") |
54
|
|
|
new_uuid = generate_uuid() |
55
|
|
|
combined_report = e.Element( |
56
|
|
|
'report', |
57
|
|
|
{ |
58
|
|
|
'id': new_uuid, |
59
|
|
|
'format_id': 'd5da9f67-8551-4e51-807b-b6a873d70e34', |
60
|
|
|
'extension': 'xml', |
61
|
|
|
'content_type': 'text/xml', |
62
|
|
|
}, |
63
|
|
|
) |
64
|
|
|
report_elem = e.Element('report', {'id': new_uuid}) |
65
|
|
|
# filter_elem = e.Element('filter', {'id': filter_id}) |
66
|
|
|
|
67
|
|
|
# report_elem.append(filter_elem) |
68
|
|
|
print("Meh") |
69
|
|
|
ports_elem = e.Element('ports', {'start': '1', 'max': '-1'}) |
70
|
|
|
results_elem = e.Element('results', {'start': '1', 'max': '-1'}) |
71
|
|
|
combined_report.append(report_elem) |
72
|
|
|
report_elem.append(results_elem) |
73
|
|
|
|
74
|
|
|
hosts = [] |
75
|
|
|
for report in reports: |
76
|
|
|
current_report = gmp.get_report( |
77
|
|
|
report, filter=filter_term, details=True |
78
|
|
|
)[0] |
79
|
|
|
for port in current_report.xpath('report/ports/port'): |
80
|
|
|
ports_elem.append(port) |
81
|
|
|
for result in current_report.xpath('report/results/result'): |
82
|
|
|
results_elem.append(result) |
83
|
|
|
for host in current_report.xpath('report/host'): |
84
|
|
|
report_elem.append(host) |
85
|
|
|
|
86
|
|
|
return combined_report |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
def send_report(gmp, combined_report, date): |
90
|
|
|
task_name = "Monthly Report ({})".format(date) |
91
|
|
|
|
92
|
|
|
res = gmp.create_container_task( |
93
|
|
|
name=task_name, comment="Created with gvm-tools." |
94
|
|
|
) |
95
|
|
|
|
96
|
|
|
task_id = res.xpath('//@id')[0] |
97
|
|
|
|
98
|
|
|
combined_report = e.tostring(combined_report) |
99
|
|
|
|
100
|
|
|
res = gmp.import_report(combined_report, task_id=task_id) |
101
|
|
|
|
102
|
|
|
return res.xpath('//@id')[0] |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
def parse_args(args): # pylint: disable=unused-argument |
106
|
|
|
parser = ArgumentParser( |
107
|
|
|
prefix_chars="+", |
108
|
|
|
add_help=False, |
109
|
|
|
formatter_class=RawTextHelpFormatter, |
110
|
|
|
description=HELP_TEXT, |
111
|
|
|
) |
112
|
|
|
|
113
|
|
|
parser.add_argument( |
114
|
|
|
"+h", |
115
|
|
|
"++help", |
116
|
|
|
action="help", |
117
|
|
|
help="Show this help message and exit.", |
118
|
|
|
) |
119
|
|
|
|
120
|
|
|
parser.add_argument( |
121
|
|
|
"+d", |
122
|
|
|
"++date", |
123
|
|
|
type=str, |
124
|
|
|
required=True, |
125
|
|
|
dest="date", |
126
|
|
|
help="The month and year to collect reports from: (mm/yyyy)", |
127
|
|
|
) |
128
|
|
|
|
129
|
|
|
parser.add_argument( |
130
|
|
|
"+t", |
131
|
|
|
"++tags", |
132
|
|
|
nargs='+', |
133
|
|
|
type=str, |
134
|
|
|
dest="tags", |
135
|
|
|
help="Filter the reports by given tag(s).", |
136
|
|
|
) |
137
|
|
|
|
138
|
|
|
parser.add_argument( |
139
|
|
|
"+f", |
140
|
|
|
"++filter", |
141
|
|
|
nargs='+', |
142
|
|
|
type=str, |
143
|
|
|
dest="filter", |
144
|
|
|
help="Filter the reports by given filter(s).", |
145
|
|
|
) |
146
|
|
|
|
147
|
|
|
script_args, _ = parser.parse_known_args() |
148
|
|
|
return script_args |
149
|
|
|
|
150
|
|
|
|
151
|
|
|
def main(gmp, args): |
152
|
|
|
# pylint: disable=undefined-variable |
153
|
|
|
|
154
|
|
|
parsed_args = parse_args(args) |
155
|
|
|
|
156
|
|
|
month, year = parsed_args.date.split('/') |
157
|
|
|
from_date = date(int(year), int(month), 1) |
158
|
|
|
to_date = from_date + timedelta(days=31) |
159
|
|
|
# To have the first day in month |
160
|
|
|
to_date = to_date.replace(day=1) |
161
|
|
|
|
162
|
|
|
print(from_date) |
163
|
|
|
print(to_date) |
164
|
|
|
|
165
|
|
|
filter_term = "" |
166
|
|
|
if parsed_args.filter: |
167
|
|
|
filter_term = ' '.join(parsed_args.filter) |
168
|
|
|
# print(filter_term) |
169
|
|
|
# filter_id = create_filter(gmp, filter_term, parsed_args.date) |
170
|
|
|
# print(filter_id) |
171
|
|
|
|
172
|
|
|
reports = get_last_reports_from_tasks(gmp, from_date, to_date) |
173
|
|
|
|
174
|
|
|
combined_report = combine_reports(gmp, reports, filter_term) |
175
|
|
|
|
176
|
|
|
send_report(gmp, combined_report, parsed_args.date) |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
if __name__ == '__gmp__': |
180
|
|
|
main(gmp, args) |
|
|
|
|
181
|
|
|
|