|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# Copyright (C) 2018-2020 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 gvm.protocols.gmpv9.types import get_alive_test_from_string |
|
21
|
|
|
from gvmtools.helper import create_xml_tree, yes_or_no |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
def check_args(args): |
|
25
|
|
|
len_args = len(args.script) - 1 |
|
26
|
|
|
if len_args != 1: |
|
27
|
|
|
message = """ |
|
28
|
|
|
This script pulls target data from an xml document and feeds it to \ |
|
29
|
|
|
a desired GSM |
|
30
|
|
|
One parameter after the script name is required. |
|
31
|
|
|
|
|
32
|
|
|
1. <xml_doc> -- .xml file containing targets |
|
33
|
|
|
|
|
34
|
|
|
Example: |
|
35
|
|
|
$ gvm-script --gmp-username name --gmp-password pass \ |
|
36
|
|
|
ssh --hostname <gsm> scripts/send-targets.gmp.py example_file.xml |
|
37
|
|
|
""" |
|
38
|
|
|
print(message) |
|
39
|
|
|
sys.exit() |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def parse_send_xml_tree(gmp, xml_tree): |
|
43
|
|
|
credential_options = [ |
|
44
|
|
|
'ssh_credential', |
|
45
|
|
|
'smb_credential', |
|
46
|
|
|
'esxi_credential', |
|
47
|
|
|
'snmp_credential', |
|
48
|
|
|
] |
|
49
|
|
|
counter = 1 |
|
50
|
|
|
|
|
51
|
|
|
for target in xml_tree.xpath('target'): |
|
52
|
|
|
keywords = {} # {'make_unique': True} |
|
53
|
|
|
|
|
54
|
|
|
keywords['name'] = target.find('name').text |
|
55
|
|
|
|
|
56
|
|
|
keywords['hosts'] = target.find('hosts').text.split(',') |
|
57
|
|
|
|
|
58
|
|
|
exclude_hosts = target.find('exclude_hosts').text |
|
59
|
|
|
if exclude_hosts is not None: |
|
60
|
|
|
keywords['exclude_hosts'] = exclude_hosts.split(',') |
|
61
|
|
|
|
|
62
|
|
|
comment = target.find('comment').text |
|
63
|
|
|
if comment is not None: |
|
64
|
|
|
keywords['comment'] = comment |
|
65
|
|
|
|
|
66
|
|
|
credentials = gmp.get_credentials()[0].xpath("//credential/@id") |
|
67
|
|
|
|
|
68
|
|
|
for credential in credential_options: |
|
69
|
|
|
cred_id = target.find(credential).xpath('@id')[0] |
|
70
|
|
|
if cred_id == '': |
|
71
|
|
|
continue |
|
72
|
|
|
if cred_id not in credentials: |
|
73
|
|
|
response = yes_or_no( |
|
74
|
|
|
"\nThe credential '{}' for 'target {}' could not be " |
|
75
|
|
|
"located...\nWould you like to continue?".format( |
|
76
|
|
|
credential, counter |
|
77
|
|
|
) |
|
78
|
|
|
) |
|
79
|
|
|
|
|
80
|
|
|
if response is False: |
|
81
|
|
|
print("Terminating...\n") |
|
82
|
|
|
quit() |
|
83
|
|
|
else: |
|
84
|
|
|
continue |
|
85
|
|
|
|
|
86
|
|
|
key = '{}_id'.format(credential) |
|
87
|
|
|
keywords[key] = cred_id |
|
88
|
|
|
elem_path = target.find(credential) |
|
89
|
|
|
port = elem_path.find('port') |
|
90
|
|
|
if port is not None and port.text is not None: |
|
91
|
|
|
port_key = '{}_port'.format(credential) |
|
92
|
|
|
keywords[port_key] = elem_path.find('port').text |
|
93
|
|
|
|
|
94
|
|
|
alive_test = get_alive_test_from_string(target.find('alive_tests').text) |
|
95
|
|
|
|
|
96
|
|
|
if alive_test is not None: |
|
97
|
|
|
keywords['alive_test'] = alive_test |
|
98
|
|
|
|
|
99
|
|
|
reverse_lookup_only = target.find('reverse_lookup_only').text |
|
100
|
|
|
if reverse_lookup_only == '1': |
|
101
|
|
|
keywords['reverse_lookup_only'] = 1 |
|
102
|
|
|
|
|
103
|
|
|
reverse_lookup_unify = target.find('reverse_lookup_unify').text |
|
104
|
|
|
if reverse_lookup_unify == '1': |
|
105
|
|
|
keywords['reverse_lookup_unify'] = 1 |
|
106
|
|
|
|
|
107
|
|
|
port_range = target.find('port_range') |
|
108
|
|
|
if port_range is not None: |
|
109
|
|
|
keywords['port_range'] = port_range.text |
|
110
|
|
|
|
|
111
|
|
|
if target.xpath('port_list/@id') is not None: |
|
112
|
|
|
port_list = {} |
|
113
|
|
|
port_list = target.xpath('port_list/@id')[0] |
|
114
|
|
|
keywords['port_list_id'] = port_list |
|
115
|
|
|
|
|
116
|
|
|
print(keywords) |
|
117
|
|
|
|
|
118
|
|
|
gmp.create_target(**keywords) |
|
119
|
|
|
|
|
120
|
|
|
counter += 1 |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
def main(gmp, args): |
|
124
|
|
|
# pylint: disable=undefined-variable |
|
125
|
|
|
|
|
126
|
|
|
check_args(args) |
|
127
|
|
|
|
|
128
|
|
|
xml_doc = args.script[1] |
|
129
|
|
|
|
|
130
|
|
|
print('\nSending targets...') |
|
131
|
|
|
|
|
132
|
|
|
xml_tree = create_xml_tree(xml_doc) |
|
133
|
|
|
parse_send_xml_tree(gmp, xml_tree) |
|
134
|
|
|
|
|
135
|
|
|
print('\n Target(s) created!\n') |
|
136
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
if __name__ == '__gmp__': |
|
139
|
|
|
main(gmp, args) # pylint: disable=undefined-variable |
|
|
|
|
|
|
140
|
|
|
|