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
|
|
|
# pylint: disable=redefined-builtin |
20
|
|
|
|
21
|
|
|
from enum import Enum |
22
|
|
|
from typing import Any, Optional |
23
|
|
|
|
24
|
|
|
from gvm.errors import RequiredArgument, InvalidArgument, InvalidArgumentType |
25
|
|
|
from gvm.utils import add_filter, to_bool |
26
|
|
|
from gvm.xml import XmlCommand |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class FilterType(Enum): |
30
|
|
|
"""Enum for filter types""" |
31
|
|
|
|
32
|
|
|
ALERT = "alert" |
33
|
|
|
ASSET = "asset" |
34
|
|
|
SCAN_CONFIG = "config" |
35
|
|
|
CREDENTIAL = "credential" |
36
|
|
|
FILTER = "filter" |
37
|
|
|
GROUP = "group" |
38
|
|
|
HOST = "host" |
39
|
|
|
NOTE = "note" |
40
|
|
|
OPERATING_SYSTEM = "os" |
41
|
|
|
OVERRIDE = "override" |
42
|
|
|
PERMISSION = "permission" |
43
|
|
|
PORT_LIST = "port_list" |
44
|
|
|
REPORT = "report" |
45
|
|
|
REPORT_FORMAT = "report_format" |
46
|
|
|
RESULT = "result" |
47
|
|
|
ROLE = "role" |
48
|
|
|
SCHEDULE = "schedule" |
49
|
|
|
ALL_SECINFO = "secinfo" |
50
|
|
|
TAG = "tag" |
51
|
|
|
TARGET = "target" |
52
|
|
|
TASK = "task" |
53
|
|
|
TICKET = "ticket" |
54
|
|
|
TLS_CERTIFICATE = "tls_certificate" |
55
|
|
|
USER = "user" |
56
|
|
|
VULNERABILITY = "vuln" |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
def get_filter_type_from_string( |
60
|
|
|
filter_type: Optional[str], |
61
|
|
|
) -> Optional[FilterType]: |
62
|
|
|
"""Convert a filter type string to an actual FilterType instance |
63
|
|
|
|
64
|
|
|
Arguments: |
65
|
|
|
filter_type (str): Filter type string to convert to a FilterType |
66
|
|
|
""" |
67
|
|
|
if not filter_type: |
68
|
|
|
return None |
69
|
|
|
|
70
|
|
|
if filter_type == 'vuln': |
71
|
|
|
return FilterType.VULNERABILITY |
72
|
|
|
|
73
|
|
|
if filter_type == 'os': |
74
|
|
|
return FilterType.OPERATING_SYSTEM |
75
|
|
|
|
76
|
|
|
if filter_type == 'config': |
77
|
|
|
return FilterType.SCAN_CONFIG |
78
|
|
|
|
79
|
|
|
if filter_type == 'secinfo': |
80
|
|
|
return FilterType.ALL_SECINFO |
81
|
|
|
|
82
|
|
|
try: |
83
|
|
|
return FilterType[filter_type.upper()] |
84
|
|
|
except KeyError: |
85
|
|
|
raise InvalidArgument( |
86
|
|
|
argument='filter_type', |
87
|
|
|
function=get_filter_type_from_string.__name__, |
88
|
|
|
) from None |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
class FiltersMixin: |
92
|
|
|
def clone_filter(self, filter_id: str) -> Any: |
93
|
|
|
"""Clone an existing filter |
94
|
|
|
|
95
|
|
|
Arguments: |
96
|
|
|
filter_id: UUID of an existing filter to clone from |
97
|
|
|
|
98
|
|
|
Returns: |
99
|
|
|
The response. See :py:meth:`send_command` for details. |
100
|
|
|
""" |
101
|
|
|
if not filter_id: |
102
|
|
|
raise RequiredArgument( |
103
|
|
|
function=self.clone_filter.__name__, argument='filter_id' |
104
|
|
|
) |
105
|
|
|
|
106
|
|
|
cmd = XmlCommand("create_filter") |
107
|
|
|
cmd.add_element("copy", filter_id) |
108
|
|
|
return self._send_xml_command(cmd) |
109
|
|
|
|
110
|
|
View Code Duplication |
def create_filter( |
|
|
|
|
111
|
|
|
self, |
112
|
|
|
name: str, |
113
|
|
|
*, |
114
|
|
|
filter_type: Optional[FilterType] = None, |
115
|
|
|
comment: Optional[str] = None, |
116
|
|
|
term: Optional[str] = None, |
117
|
|
|
) -> Any: |
118
|
|
|
"""Create a new filter |
119
|
|
|
|
120
|
|
|
Arguments: |
121
|
|
|
name: Name of the new filter |
122
|
|
|
filter_type: Filter for entity type |
123
|
|
|
comment: Comment for the filter |
124
|
|
|
term: Filter term e.g. 'name=foo' |
125
|
|
|
|
126
|
|
|
Returns: |
127
|
|
|
The response. See :py:meth:`send_command` for details. |
128
|
|
|
""" |
129
|
|
|
if not name: |
130
|
|
|
raise RequiredArgument( |
131
|
|
|
function=self.create_filter.__name__, argument="name" |
132
|
|
|
) |
133
|
|
|
|
134
|
|
|
cmd = XmlCommand("create_filter") |
135
|
|
|
_xmlname = cmd.add_element("name", name) |
136
|
|
|
|
137
|
|
|
if comment: |
138
|
|
|
cmd.add_element("comment", comment) |
139
|
|
|
|
140
|
|
|
if term: |
141
|
|
|
cmd.add_element("term", term) |
142
|
|
|
|
143
|
|
|
if filter_type: |
144
|
|
|
if not isinstance(filter_type, FilterType): |
145
|
|
|
raise InvalidArgumentType( |
146
|
|
|
function=self.create_filter.__name__, |
147
|
|
|
argument="filter_type", |
148
|
|
|
arg_type=FilterType.__name__, |
149
|
|
|
) |
150
|
|
|
|
151
|
|
|
cmd.add_element("type", filter_type.value) |
152
|
|
|
|
153
|
|
|
return self._send_xml_command(cmd) |
154
|
|
|
|
155
|
|
|
def delete_filter( |
156
|
|
|
self, filter_id: str, *, ultimate: Optional[bool] = False |
157
|
|
|
) -> Any: |
158
|
|
|
"""Deletes an existing filter |
159
|
|
|
|
160
|
|
|
Arguments: |
161
|
|
|
filter_id: UUID of the filter to be deleted. |
162
|
|
|
ultimate: Whether to remove entirely, or to the trashcan. |
163
|
|
|
""" |
164
|
|
|
if not filter_id: |
165
|
|
|
raise RequiredArgument( |
166
|
|
|
function=self.delete_filter.__name__, argument='filter_id' |
167
|
|
|
) |
168
|
|
|
|
169
|
|
|
cmd = XmlCommand("delete_filter") |
170
|
|
|
cmd.set_attribute("filter_id", filter_id) |
171
|
|
|
cmd.set_attribute("ultimate", to_bool(ultimate)) |
172
|
|
|
|
173
|
|
|
return self._send_xml_command(cmd) |
174
|
|
|
|
175
|
|
View Code Duplication |
def get_filters( |
|
|
|
|
176
|
|
|
self, |
177
|
|
|
*, |
178
|
|
|
filter: Optional[str] = None, |
179
|
|
|
filter_id: Optional[str] = None, |
180
|
|
|
trash: Optional[bool] = None, |
181
|
|
|
alerts: Optional[bool] = None, |
182
|
|
|
) -> Any: |
183
|
|
|
"""Request a list of filters |
184
|
|
|
|
185
|
|
|
Arguments: |
186
|
|
|
filter: Filter term to use for the query |
187
|
|
|
filter_id: UUID of an existing filter to use for the query |
188
|
|
|
trash: Whether to get the trashcan filters instead |
189
|
|
|
alerts: Whether to include list of alerts that use the filter. |
190
|
|
|
|
191
|
|
|
Returns: |
192
|
|
|
The response. See :py:meth:`send_command` for details. |
193
|
|
|
""" |
194
|
|
|
cmd = XmlCommand("get_filters") |
195
|
|
|
|
196
|
|
|
add_filter(cmd, filter, filter_id) |
197
|
|
|
|
198
|
|
|
if trash is not None: |
199
|
|
|
cmd.set_attribute("trash", to_bool(trash)) |
200
|
|
|
|
201
|
|
|
if alerts is not None: |
202
|
|
|
cmd.set_attribute("alerts", to_bool(alerts)) |
203
|
|
|
|
204
|
|
|
return self._send_xml_command(cmd) |
205
|
|
|
|
206
|
|
|
def get_filter( |
207
|
|
|
self, filter_id: str, *, alerts: Optional[bool] = None |
208
|
|
|
) -> Any: |
209
|
|
|
"""Request a single filter |
210
|
|
|
|
211
|
|
|
Arguments: |
212
|
|
|
filter_id: UUID of an existing filter |
213
|
|
|
alerts: Whether to include list of alerts that use the filter. |
214
|
|
|
|
215
|
|
|
Returns: |
216
|
|
|
The response. See :py:meth:`send_command` for details. |
217
|
|
|
""" |
218
|
|
|
cmd = XmlCommand("get_filters") |
219
|
|
|
|
220
|
|
|
if not filter_id: |
221
|
|
|
raise RequiredArgument( |
222
|
|
|
function=self.get_filter.__name__, argument='filter_id' |
223
|
|
|
) |
224
|
|
|
|
225
|
|
|
cmd.set_attribute("filter_id", filter_id) |
226
|
|
|
|
227
|
|
|
if alerts is not None: |
228
|
|
|
cmd.set_attribute("alerts", to_bool(alerts)) |
229
|
|
|
|
230
|
|
|
return self._send_xml_command(cmd) |
231
|
|
|
|
232
|
|
View Code Duplication |
def modify_filter( |
|
|
|
|
233
|
|
|
self, |
234
|
|
|
filter_id: str, |
235
|
|
|
*, |
236
|
|
|
comment: Optional[str] = None, |
237
|
|
|
name: Optional[str] = None, |
238
|
|
|
term: Optional[str] = None, |
239
|
|
|
filter_type: Optional[FilterType] = None, |
240
|
|
|
) -> Any: |
241
|
|
|
"""Modifies an existing filter. |
242
|
|
|
|
243
|
|
|
Arguments: |
244
|
|
|
filter_id: UUID of the filter to be modified |
245
|
|
|
comment: Comment on filter. |
246
|
|
|
name: Name of filter. |
247
|
|
|
term: Filter term. |
248
|
|
|
filter_type: Resource type filter applies to. |
249
|
|
|
|
250
|
|
|
Returns: |
251
|
|
|
The response. See :py:meth:`send_command` for details. |
252
|
|
|
""" |
253
|
|
|
if not filter_id: |
254
|
|
|
raise RequiredArgument( |
255
|
|
|
function=self.modify_filter.__name__, argument='filter_id' |
256
|
|
|
) |
257
|
|
|
|
258
|
|
|
cmd = XmlCommand("modify_filter") |
259
|
|
|
cmd.set_attribute("filter_id", filter_id) |
260
|
|
|
|
261
|
|
|
if comment: |
262
|
|
|
cmd.add_element("comment", comment) |
263
|
|
|
|
264
|
|
|
if name: |
265
|
|
|
cmd.add_element("name", name) |
266
|
|
|
|
267
|
|
|
if term: |
268
|
|
|
cmd.add_element("term", term) |
269
|
|
|
|
270
|
|
|
if filter_type: |
271
|
|
|
if not isinstance(filter_type, FilterType): |
272
|
|
|
raise InvalidArgumentType( |
273
|
|
|
function=self.modify_filter.__name__, |
274
|
|
|
argument='filter_type', |
275
|
|
|
arg_type=FilterType.__name__, |
276
|
|
|
) |
277
|
|
|
cmd.add_element("type", filter_type.value) |
278
|
|
|
|
279
|
|
|
return self._send_xml_command(cmd) |
280
|
|
|
|