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 time |
20
|
|
|
import sys |
21
|
|
|
|
22
|
|
|
from lxml import etree as e |
23
|
|
|
|
24
|
|
|
from gvmtools.helper import generate_uuid |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
def check_args(args): |
28
|
|
|
len_args = len(args.script) - 1 |
29
|
|
|
if len_args < 2: |
30
|
|
|
message = """ |
31
|
|
|
This script will combine desired reports into a single report. \ |
32
|
|
|
The combined report will then be sent to a desired container task. \ |
33
|
|
|
This script will create a container task for the combined report to\ |
34
|
|
|
be sent to, however, if you would like the report to be sent to an \ |
35
|
|
|
existing task, place the report of the desired task first and add \ |
36
|
|
|
the argument 'first_task'. |
37
|
|
|
|
38
|
|
|
1. <report_1_uuid> --uuid of report to be combined |
39
|
|
|
2. <report_2_uuid> --uuid of report to be combined |
40
|
|
|
... |
41
|
|
|
n. <report_n_uuid> --uuid of report to be combined |
42
|
|
|
|
43
|
|
|
Example for starting up the routine: |
44
|
|
|
$ gvm-script --gmp-username=namessh --gmp-password=pass ssh --hostname=hostname \ |
45
|
|
|
scripts/combine-reports.gmp.py \ |
46
|
|
|
"d15a337c-56f3-4208-a462-afeb79eb03b7" \ |
47
|
|
|
"303fa0a6-aa9b-43c4-bac0-66ae0b2d1698" 'first_task' |
48
|
|
|
|
49
|
|
|
""" |
50
|
|
|
print(message) |
51
|
|
|
sys.exit() |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
def combine_reports(gmp, args): |
55
|
|
|
new_uuid = generate_uuid() |
56
|
|
|
combined_report = e.Element( |
57
|
|
|
'report', |
58
|
|
|
{ |
59
|
|
|
'id': new_uuid, |
60
|
|
|
'format_id': 'd5da9f67-8551-4e51-807b-b6a873d70e34', |
61
|
|
|
'extension': 'xml', |
62
|
|
|
'content_type': 'text/xml', |
63
|
|
|
}, |
64
|
|
|
) |
65
|
|
|
report_elem = e.Element('report', {'id': new_uuid}) |
66
|
|
|
ports_elem = e.Element('ports', {'start': '1', 'max': '-1'}) |
67
|
|
|
results_elem = e.Element('results', {'start': '1', 'max': '-1'}) |
68
|
|
|
combined_report.append(report_elem) |
69
|
|
|
report_elem.append(results_elem) |
70
|
|
|
|
71
|
|
|
if 'first_task' in args.script: |
72
|
|
|
arg_len = args.script[1:-1] |
73
|
|
|
else: |
74
|
|
|
arg_len = args.script[1:] |
75
|
|
|
|
76
|
|
|
for argument in arg_len: |
77
|
|
|
current_report = gmp.get_report(argument, details=True)[0] |
78
|
|
|
for port in current_report.xpath('report/ports/port'): |
79
|
|
|
ports_elem.append(port) |
80
|
|
|
for result in current_report.xpath('report/results/result'): |
81
|
|
|
results_elem.append(result) |
82
|
|
|
for host in current_report.xpath('report/host'): |
83
|
|
|
report_elem.append(host) |
84
|
|
|
|
85
|
|
|
return combined_report |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
def send_report(gmp, args, combined_report): |
89
|
|
|
if 'first_task' in args.script: |
90
|
|
|
main_report = gmp.get_report(args.script[1])[0] |
91
|
|
|
task_id = main_report.xpath('//task/@id')[0] |
92
|
|
|
else: |
93
|
|
|
the_time = time.strftime("%Y/%m/%d-%H:%M:%S") |
94
|
|
|
task_id = '' |
95
|
|
|
task_name = "Combined_Report_{}".format(the_time) |
96
|
|
|
|
97
|
|
|
res = gmp.create_container_task( |
98
|
|
|
name=task_name, comment="Created with gvm-tools." |
99
|
|
|
) |
100
|
|
|
|
101
|
|
|
task_id = res.xpath('//@id')[0] |
102
|
|
|
|
103
|
|
|
combined_report = e.tostring(combined_report) |
104
|
|
|
|
105
|
|
|
res = gmp.import_report(combined_report, task_id=task_id) |
106
|
|
|
|
107
|
|
|
return res.xpath('//@id')[0] |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
def main(gmp, args): |
111
|
|
|
# pylint: disable=undefined-variable |
112
|
|
|
|
113
|
|
|
check_args(args) |
114
|
|
|
|
115
|
|
|
combined_report = combine_reports(gmp, args) |
116
|
|
|
send_report(gmp, args, combined_report) |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
if __name__ == '__gmp__': |
120
|
|
|
main(gmp, args) |
|
|
|
|
121
|
|
|
|