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