|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# Copyright (C) 2014-2021 Greenbone Networks GmbH |
|
3
|
|
|
# |
|
4
|
|
|
# SPDX-License-Identifier: AGPL-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 Affero General Public License as |
|
8
|
|
|
# published by the Free Software Foundation, either version 3 of the |
|
9
|
|
|
# License, or (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 Affero General Public License for more details. |
|
15
|
|
|
# |
|
16
|
|
|
# You should have received a copy of the GNU Affero General Public License |
|
17
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
import logging |
|
21
|
|
|
|
|
22
|
|
|
from unittest import TestCase |
|
23
|
|
|
from unittest.mock import call, patch, Mock, MagicMock |
|
24
|
|
|
from collections import OrderedDict |
|
25
|
|
|
|
|
26
|
|
|
from ospd.vts import Vts |
|
27
|
|
|
|
|
28
|
|
|
from tests.dummydaemon import DummyDaemon |
|
29
|
|
|
from tests.helper import assert_called_once |
|
30
|
|
|
|
|
31
|
|
|
import ospd_openvas.db |
|
32
|
|
|
|
|
33
|
|
|
from ospd_openvas.openvas import Openvas |
|
34
|
|
|
from ospd_openvas.preferencehandler import ( |
|
35
|
|
|
AliveTest, |
|
36
|
|
|
BOREAS_SETTING_NAME, |
|
37
|
|
|
BOREAS_ALIVE_TEST, |
|
38
|
|
|
PreferenceHandler, |
|
39
|
|
|
) |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
class PreferenceHandlerTestCase(TestCase): |
|
43
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
44
|
|
|
def test_process_vts_not_found(self, mock_kb): |
|
45
|
|
|
w = DummyDaemon() |
|
46
|
|
|
logging.Logger.warning = Mock() |
|
47
|
|
|
|
|
48
|
|
|
vts = { |
|
49
|
|
|
'1.3.6.1.4.1.25623.1.0.100065': {'3': 'new value'}, |
|
50
|
|
|
'vt_groups': ['family=debian', 'family=general'], |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, w.nvti) |
|
54
|
|
|
w.nvti.get_nvt_metadata.return_value = None |
|
55
|
|
|
p._process_vts(vts) |
|
56
|
|
|
|
|
57
|
|
|
assert_called_once(logging.Logger.warning) |
|
58
|
|
|
|
|
59
|
|
|
def test_process_vts_bad_param_id(self): |
|
60
|
|
|
w = DummyDaemon() |
|
61
|
|
|
|
|
62
|
|
|
vts = { |
|
63
|
|
|
'1.3.6.1.4.1.25623.1.0.100061': {'3': 'new value'}, |
|
64
|
|
|
'vt_groups': ['family=debian', 'family=general'], |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
p = PreferenceHandler('1234-1234', None, w.scan_collection, w.nvti) |
|
68
|
|
|
|
|
69
|
|
|
ret = p._process_vts(vts) |
|
70
|
|
|
|
|
71
|
|
|
self.assertFalse(ret[1]) |
|
72
|
|
|
|
|
73
|
|
|
def test_process_vts(self): |
|
74
|
|
|
w = DummyDaemon() |
|
75
|
|
|
|
|
76
|
|
|
vts = { |
|
77
|
|
|
'1.3.6.1.4.1.25623.1.0.100061': {'1': 'new value'}, |
|
78
|
|
|
'vt_groups': ['family=debian', 'family=general'], |
|
79
|
|
|
} |
|
80
|
|
|
vt_out = ( |
|
81
|
|
|
['1.3.6.1.4.1.25623.1.0.100061'], |
|
82
|
|
|
{'1.3.6.1.4.1.25623.1.0.100061:1:entry:Data length :': 'new value'}, |
|
83
|
|
|
) |
|
84
|
|
|
|
|
85
|
|
|
p = PreferenceHandler('1234-1234', None, w.scan_collection, w.nvti) |
|
86
|
|
|
ret = p._process_vts(vts) |
|
87
|
|
|
|
|
88
|
|
|
self.assertEqual(ret, vt_out) |
|
89
|
|
|
|
|
90
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
91
|
|
|
def test_set_plugins_false(self, mock_kb): |
|
92
|
|
|
w = DummyDaemon() |
|
93
|
|
|
|
|
94
|
|
|
w.scan_collection.get_vts = Mock() |
|
95
|
|
|
w.scan_collection.get_vts.return_value = {} |
|
96
|
|
|
|
|
97
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, w.nvti) |
|
98
|
|
|
p.kbdb.add_scan_preferences = Mock() |
|
99
|
|
|
r = p.prepare_plugins_for_openvas() |
|
100
|
|
|
|
|
101
|
|
|
self.assertFalse(r) |
|
102
|
|
|
|
|
103
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
104
|
|
|
def test_set_plugins_true(self, mock_kb): |
|
105
|
|
|
w = DummyDaemon() |
|
106
|
|
|
|
|
107
|
|
|
vts = { |
|
108
|
|
|
'1.3.6.1.4.1.25623.1.0.100061': {'3': 'new value'}, |
|
109
|
|
|
'vt_groups': ['family=debian', 'family=general'], |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
w.scan_collection.get_vts = Mock() |
|
113
|
|
|
w.scan_collection.get_vts.return_value = vts |
|
114
|
|
|
|
|
115
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, w.nvti) |
|
116
|
|
|
p.kbdb.add_scan_preferences = Mock() |
|
117
|
|
|
r = p.prepare_plugins_for_openvas() |
|
118
|
|
|
|
|
119
|
|
|
self.assertTrue(r) |
|
120
|
|
|
|
|
121
|
|
|
def test_build_credentials_ssh_up(self): |
|
122
|
|
|
w = DummyDaemon() |
|
123
|
|
|
|
|
124
|
|
|
cred_out = [ |
|
125
|
|
|
'auth_port_ssh|||22', |
|
126
|
|
|
'1.3.6.1.4.1.25623.1.0.103591:1:entry:SSH login name:|||username', |
|
127
|
|
|
'1.3.6.1.4.1.25623.1.0.103591:3:password:SSH password (unsafe!):|||pass', |
|
128
|
|
|
] |
|
129
|
|
|
cred_dict = { |
|
130
|
|
|
'ssh': { |
|
131
|
|
|
'type': 'up', |
|
132
|
|
|
'port': '22', |
|
133
|
|
|
'username': 'username', |
|
134
|
|
|
'password': 'pass', |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
|
138
|
|
|
|
|
139
|
|
|
ret = p.build_credentials_as_prefs(cred_dict) |
|
140
|
|
|
|
|
141
|
|
|
self.assertEqual(ret, cred_out) |
|
142
|
|
|
|
|
143
|
|
|
def test_build_credentials(self): |
|
144
|
|
|
w = DummyDaemon() |
|
145
|
|
|
|
|
146
|
|
|
cred_out = [ |
|
147
|
|
|
'1.3.6.1.4.1.25623.1.0.105058:1:entry:ESXi login name:|||username', |
|
148
|
|
|
'1.3.6.1.4.1.25623.1.0.105058:2:password:ESXi login password:|||pass', |
|
149
|
|
|
'auth_port_ssh|||22', |
|
150
|
|
|
'1.3.6.1.4.1.25623.1.0.103591:1:entry:SSH login name:|||username', |
|
151
|
|
|
'1.3.6.1.4.1.25623.1.0.103591:2:password:SSH key passphrase:|||pass', |
|
152
|
|
|
'1.3.6.1.4.1.25623.1.0.103591:4:file:SSH private key:|||', |
|
153
|
|
|
'1.3.6.1.4.1.25623.1.0.90023:1:entry:SMB login:|||username', |
|
154
|
|
|
'1.3.6.1.4.1.25623.1.0.90023:2:password]:SMB password :|||pass', |
|
155
|
|
|
'1.3.6.1.4.1.25623.1.0.105076:1:password:SNMP Community:some comunity', |
|
156
|
|
|
'1.3.6.1.4.1.25623.1.0.105076:2:entry:SNMPv3 Username:username', |
|
157
|
|
|
'1.3.6.1.4.1.25623.1.0.105076:3:password:SNMPv3 Password:pass', |
|
158
|
|
|
'1.3.6.1.4.1.25623.1.0.105076:4:radio:SNMPv3 Authentication Algorithm:some auth algo', |
|
159
|
|
|
'1.3.6.1.4.1.25623.1.0.105076:5:password:SNMPv3 Privacy Password:privacy pass', |
|
160
|
|
|
'1.3.6.1.4.1.25623.1.0.105076:6:radio:SNMPv3 Privacy Algorithm:privacy algo', |
|
161
|
|
|
] |
|
162
|
|
|
cred_dict = { |
|
163
|
|
|
'ssh': { |
|
164
|
|
|
'type': 'ssh', |
|
165
|
|
|
'port': '22', |
|
166
|
|
|
'username': 'username', |
|
167
|
|
|
'password': 'pass', |
|
168
|
|
|
}, |
|
169
|
|
|
'smb': {'type': 'smb', 'username': 'username', 'password': 'pass'}, |
|
170
|
|
|
'esxi': { |
|
171
|
|
|
'type': 'esxi', |
|
172
|
|
|
'username': 'username', |
|
173
|
|
|
'password': 'pass', |
|
174
|
|
|
}, |
|
175
|
|
|
'snmp': { |
|
176
|
|
|
'type': 'snmp', |
|
177
|
|
|
'username': 'username', |
|
178
|
|
|
'password': 'pass', |
|
179
|
|
|
'community': 'some comunity', |
|
180
|
|
|
'auth_algorithm': 'some auth algo', |
|
181
|
|
|
'privacy_password': 'privacy pass', |
|
182
|
|
|
'privacy_algorithm': 'privacy algo', |
|
183
|
|
|
}, |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
|
187
|
|
|
ret = p.build_credentials_as_prefs(cred_dict) |
|
188
|
|
|
|
|
189
|
|
|
self.assertEqual(len(ret), len(cred_out)) |
|
190
|
|
|
self.assertIn('auth_port_ssh|||22', cred_out) |
|
191
|
|
|
self.assertIn( |
|
192
|
|
|
'1.3.6.1.4.1.25623.1.0.90023:1:entry:SMB login:|||username', |
|
193
|
|
|
cred_out, |
|
194
|
|
|
) |
|
195
|
|
|
|
|
196
|
|
|
def test_build_alive_test_opt_empty(self): |
|
197
|
|
|
w = DummyDaemon() |
|
198
|
|
|
|
|
199
|
|
|
target_options_dict = {'alive_test': '0'} |
|
200
|
|
|
|
|
201
|
|
|
p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
|
202
|
|
|
ret = p.build_alive_test_opt_as_prefs(target_options_dict) |
|
203
|
|
|
|
|
204
|
|
|
self.assertEqual(ret, {}) |
|
205
|
|
|
|
|
206
|
|
|
def test_build_alive_test_opt(self): |
|
207
|
|
|
w = DummyDaemon() |
|
208
|
|
|
|
|
209
|
|
|
alive_test_out = { |
|
210
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping": "no", |
|
211
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:2:checkbox:TCP ping tries also TCP-SYN ping": "no", |
|
212
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:7:checkbox:TCP ping tries only TCP-SYN ping": "no", |
|
213
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:3:checkbox:Do an ICMP ping": "yes", |
|
214
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:4:checkbox:Use ARP": "no", |
|
215
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:5:checkbox:Mark unrechable Hosts as dead (not scanning)": "yes", |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
target_options_dict = {'alive_test': '2'} |
|
219
|
|
|
p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
|
220
|
|
|
ret = p.build_alive_test_opt_as_prefs(target_options_dict) |
|
221
|
|
|
|
|
222
|
|
|
self.assertEqual(ret, alive_test_out) |
|
223
|
|
|
|
|
224
|
|
|
def test_build_alive_test_opt_fail_1(self): |
|
225
|
|
|
w = DummyDaemon() |
|
226
|
|
|
logging.Logger.debug = Mock() |
|
227
|
|
|
|
|
228
|
|
|
target_options_dict = {'alive_test': 'a'} |
|
229
|
|
|
p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
|
230
|
|
|
target_options = p.build_alive_test_opt_as_prefs(target_options_dict) |
|
231
|
|
|
|
|
232
|
|
|
assert_called_once(logging.Logger.debug) |
|
233
|
|
|
self.assertEqual(len(target_options), 0) |
|
234
|
|
|
|
|
235
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
236
|
|
|
def test_set_target(self, mock_kb): |
|
237
|
|
|
w = DummyDaemon() |
|
238
|
|
|
|
|
239
|
|
|
w.scan_collection.get_host_list = MagicMock(return_value='192.168.0.1') |
|
240
|
|
|
|
|
241
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
242
|
|
|
p._openvas_scan_id = '456-789' |
|
243
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
244
|
|
|
p.prepare_target_for_openvas() |
|
245
|
|
|
|
|
246
|
|
|
p.kbdb.add_scan_preferences.assert_called_with( |
|
247
|
|
|
p._openvas_scan_id, |
|
248
|
|
|
['TARGET|||192.168.0.1'], |
|
249
|
|
|
) |
|
250
|
|
|
|
|
251
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
252
|
|
|
def test_set_ports(self, mock_kb): |
|
253
|
|
|
w = DummyDaemon() |
|
254
|
|
|
|
|
255
|
|
|
w.scan_collection.get_ports = MagicMock(return_value='80,443') |
|
256
|
|
|
|
|
257
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
258
|
|
|
p._openvas_scan_id = '456-789' |
|
259
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
260
|
|
|
p.prepare_ports_for_openvas() |
|
261
|
|
|
|
|
262
|
|
|
p.kbdb.add_scan_preferences.assert_called_with( |
|
263
|
|
|
p._openvas_scan_id, |
|
264
|
|
|
['port_range|||80,443'], |
|
265
|
|
|
) |
|
266
|
|
|
|
|
267
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
268
|
|
|
def test_set_main_kbindex(self, mock_kb): |
|
269
|
|
|
w = DummyDaemon() |
|
270
|
|
|
|
|
271
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
272
|
|
|
p.kbdb.add_scan_preferences = Mock() |
|
273
|
|
|
p.kbdb.index = 2 |
|
274
|
|
|
p.prepare_main_kbindex_for_openvas() |
|
275
|
|
|
|
|
276
|
|
|
p.kbdb.add_scan_preferences.assert_called_with( |
|
277
|
|
|
p._openvas_scan_id, |
|
278
|
|
|
['ov_maindbid|||2'], |
|
279
|
|
|
) |
|
280
|
|
|
|
|
281
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
282
|
|
|
def test_set_credentials(self, mock_kb): |
|
283
|
|
|
w = DummyDaemon() |
|
284
|
|
|
|
|
285
|
|
|
creds = { |
|
286
|
|
|
'ssh': { |
|
287
|
|
|
'type': 'ssh', |
|
288
|
|
|
'port': '22', |
|
289
|
|
|
'username': 'username', |
|
290
|
|
|
'password': 'pass', |
|
291
|
|
|
}, |
|
292
|
|
|
'smb': {'type': 'smb', 'username': 'username', 'password': 'pass'}, |
|
293
|
|
|
'esxi': { |
|
294
|
|
|
'type': 'esxi', |
|
295
|
|
|
'username': 'username', |
|
296
|
|
|
'password': 'pass', |
|
297
|
|
|
}, |
|
298
|
|
|
'snmp': { |
|
299
|
|
|
'type': 'snmp', |
|
300
|
|
|
'username': 'username', |
|
301
|
|
|
'password': 'pass', |
|
302
|
|
|
'community': 'some comunity', |
|
303
|
|
|
'auth_algorithm': 'some auth algo', |
|
304
|
|
|
'privacy_password': 'privacy pass', |
|
305
|
|
|
'privacy_algorithm': 'privacy algo', |
|
306
|
|
|
}, |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
w.scan_collection.get_credentials = MagicMock(return_value=creds) |
|
310
|
|
|
|
|
311
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
312
|
|
|
p._openvas_scan_id = '456-789' |
|
313
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
314
|
|
|
r = p.prepare_credentials_for_openvas() |
|
315
|
|
|
|
|
316
|
|
|
self.assertTrue(r) |
|
317
|
|
|
assert_called_once(p.kbdb.add_scan_preferences) |
|
318
|
|
|
|
|
319
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
320
|
|
|
def test_set_credentials(self, mock_kb): |
|
321
|
|
|
w = DummyDaemon() |
|
322
|
|
|
|
|
323
|
|
|
# bad cred type shh instead of ssh |
|
324
|
|
|
creds = { |
|
325
|
|
|
'shh': { |
|
326
|
|
|
'type': 'ssh', |
|
327
|
|
|
'port': '22', |
|
328
|
|
|
'username': 'username', |
|
329
|
|
|
'password': 'pass', |
|
330
|
|
|
}, |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
w.scan_collection.get_credentials = MagicMock(return_value=creds) |
|
334
|
|
|
|
|
335
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
336
|
|
|
p._openvas_scan_id = '456-789' |
|
337
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
338
|
|
|
r = p.prepare_credentials_for_openvas() |
|
339
|
|
|
|
|
340
|
|
|
self.assertFalse(r) |
|
341
|
|
|
|
|
342
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
343
|
|
|
def test_set_credentials_empty(self, mock_kb): |
|
344
|
|
|
w = DummyDaemon() |
|
345
|
|
|
|
|
346
|
|
|
creds = {} |
|
347
|
|
|
|
|
348
|
|
|
w.scan_collection.get_credentials = MagicMock(return_value=creds) |
|
349
|
|
|
|
|
350
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
351
|
|
|
p._openvas_scan_id = '456-789' |
|
352
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
353
|
|
|
r = p.prepare_credentials_for_openvas() |
|
354
|
|
|
|
|
355
|
|
|
self.assertTrue(r) |
|
356
|
|
|
|
|
357
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
358
|
|
|
def test_set_host_options(self, mock_kb): |
|
359
|
|
|
w = DummyDaemon() |
|
360
|
|
|
|
|
361
|
|
|
exc = '192.168.0.1' |
|
362
|
|
|
|
|
363
|
|
|
w.scan_collection.get_exclude_hosts = MagicMock(return_value=exc) |
|
364
|
|
|
|
|
365
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
366
|
|
|
p._openvas_scan_id = '456-789' |
|
367
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
368
|
|
|
p.prepare_host_options_for_openvas() |
|
369
|
|
|
|
|
370
|
|
|
p.kbdb.add_scan_preferences.assert_called_with( |
|
371
|
|
|
p._openvas_scan_id, |
|
372
|
|
|
['exclude_hosts|||192.168.0.1'], |
|
373
|
|
|
) |
|
374
|
|
|
|
|
375
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
376
|
|
|
def test_set_host_options_none(self, mock_kb): |
|
377
|
|
|
w = DummyDaemon() |
|
378
|
|
|
|
|
379
|
|
|
exc = '' |
|
380
|
|
|
|
|
381
|
|
|
w.scan_collection.get_exclude_hosts = MagicMock(return_value=exc) |
|
382
|
|
|
|
|
383
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
384
|
|
|
p._openvas_scan_id = '456-789' |
|
385
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
386
|
|
|
p.prepare_host_options_for_openvas() |
|
387
|
|
|
|
|
388
|
|
|
p.kbdb.add_scan_preferences.assert_not_called() |
|
389
|
|
|
|
|
390
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
391
|
|
|
def test_set_scan_params(self, mock_kb): |
|
392
|
|
|
w = DummyDaemon() |
|
393
|
|
|
|
|
394
|
|
|
OSPD_PARAMS_MOCK = { |
|
395
|
|
|
'drop_privileges': { |
|
396
|
|
|
'type': 'boolean', |
|
397
|
|
|
'name': 'drop_privileges', |
|
398
|
|
|
'default': 0, |
|
399
|
|
|
'mandatory': 1, |
|
400
|
|
|
'description': '', |
|
401
|
|
|
}, |
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
|
|
opt = {'drop_privileges': 1} |
|
405
|
|
|
|
|
406
|
|
|
w.scan_collection.get_options = MagicMock(return_value=opt) |
|
407
|
|
|
|
|
408
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
409
|
|
|
p._openvas_scan_id = '456-789' |
|
410
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
411
|
|
|
p.prepare_scan_params_for_openvas(OSPD_PARAMS_MOCK) |
|
412
|
|
|
|
|
413
|
|
|
p.kbdb.add_scan_preferences.assert_called_with( |
|
414
|
|
|
p._openvas_scan_id, ['drop_privileges|||yes'] |
|
415
|
|
|
) |
|
416
|
|
|
|
|
417
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
418
|
|
|
def test_set_reverse_lookup_opt(self, mock_kb): |
|
419
|
|
|
w = DummyDaemon() |
|
420
|
|
|
|
|
421
|
|
|
t_opt = {'reverse_lookup_only': 1} |
|
422
|
|
|
w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
|
423
|
|
|
|
|
424
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
425
|
|
|
p._openvas_scan_id = '456-789' |
|
426
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
427
|
|
|
p.prepare_reverse_lookup_opt_for_openvas() |
|
428
|
|
|
|
|
429
|
|
|
p.kbdb.add_scan_preferences.assert_called_with( |
|
430
|
|
|
p._openvas_scan_id, |
|
431
|
|
|
[ |
|
432
|
|
|
'reverse_lookup_only|||yes', |
|
433
|
|
|
'reverse_lookup_unify|||no', |
|
434
|
|
|
], |
|
435
|
|
|
) |
|
436
|
|
|
|
|
437
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
438
|
|
|
def test_set_boreas_alive_test_with_settings(self, mock_kb): |
|
439
|
|
|
# No Boreas config setting (BOREAS_SETTING_NAME) set |
|
440
|
|
|
w = DummyDaemon() |
|
441
|
|
|
ov_setting = {'not_the_correct_setting': 1} |
|
442
|
|
|
with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
|
443
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
444
|
|
|
p._openvas_scan_id = '456-789' |
|
445
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
446
|
|
|
p.prepare_boreas_alive_test() |
|
447
|
|
|
|
|
448
|
|
|
p.kbdb.add_scan_preferences.assert_not_called() |
|
449
|
|
|
|
|
450
|
|
|
# Boreas config setting set but invalid alive_test. |
|
451
|
|
|
w = DummyDaemon() |
|
452
|
|
|
t_opt = {'alive_test': "error"} |
|
453
|
|
|
w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
|
454
|
|
|
ov_setting = {BOREAS_SETTING_NAME: 1} |
|
455
|
|
|
with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
|
456
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
457
|
|
|
p._openvas_scan_id = '456-789' |
|
458
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
459
|
|
|
p.prepare_boreas_alive_test() |
|
460
|
|
|
|
|
461
|
|
|
p.kbdb.add_scan_preferences.assert_not_called() |
|
462
|
|
|
|
|
463
|
|
|
# ALIVE_TEST_TCP_SYN_SERVICE as alive test. |
|
464
|
|
|
w = DummyDaemon() |
|
465
|
|
|
t_opt = {'alive_test': AliveTest.ALIVE_TEST_TCP_SYN_SERVICE} |
|
466
|
|
|
w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
|
467
|
|
|
ov_setting = {BOREAS_SETTING_NAME: 1} |
|
468
|
|
|
with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
|
469
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
470
|
|
|
p._openvas_scan_id = '456-789' |
|
471
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
472
|
|
|
p.prepare_boreas_alive_test() |
|
473
|
|
|
|
|
474
|
|
|
calls = [call(p._openvas_scan_id, [BOREAS_ALIVE_TEST + '|||16'])] |
|
475
|
|
|
p.kbdb.add_scan_preferences.assert_has_calls(calls) |
|
476
|
|
|
|
|
477
|
|
|
# ICMP was chosen as alive test. |
|
478
|
|
|
w = DummyDaemon() |
|
479
|
|
|
t_opt = {'alive_test': AliveTest.ALIVE_TEST_ICMP} |
|
480
|
|
|
w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
|
481
|
|
|
ov_setting = {BOREAS_SETTING_NAME: 1} |
|
482
|
|
|
with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
|
483
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
484
|
|
|
p._openvas_scan_id = '456-789' |
|
485
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
486
|
|
|
p.prepare_boreas_alive_test() |
|
487
|
|
|
|
|
488
|
|
|
calls = [call(p._openvas_scan_id, [BOREAS_ALIVE_TEST + '|||2'])] |
|
489
|
|
|
p.kbdb.add_scan_preferences.assert_has_calls(calls) |
|
490
|
|
|
|
|
491
|
|
|
# "Scan Config Default" as alive_test. |
|
492
|
|
|
w = DummyDaemon() |
|
493
|
|
|
t_opt = {'alive_test': AliveTest.ALIVE_TEST_SCAN_CONFIG_DEFAULT} |
|
494
|
|
|
w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
|
495
|
|
|
ov_setting = {BOREAS_SETTING_NAME: 1} |
|
496
|
|
|
with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
|
497
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
498
|
|
|
p._openvas_scan_id = '456-789' |
|
499
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
500
|
|
|
p.prepare_boreas_alive_test() |
|
501
|
|
|
|
|
502
|
|
|
calls = [call(p._openvas_scan_id, [BOREAS_ALIVE_TEST + '|||2'])] |
|
503
|
|
|
p.kbdb.add_scan_preferences.assert_has_calls(calls) |
|
504
|
|
|
|
|
505
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
506
|
|
|
def test_set_boreas_alive_test_without_settings(self, mock_kb): |
|
507
|
|
|
w = DummyDaemon() |
|
508
|
|
|
t_opt = {'alive_test': 16} |
|
509
|
|
|
w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
|
510
|
|
|
ov_setting = {} |
|
511
|
|
|
with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
|
512
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
513
|
|
|
p._openvas_scan_id = '456-789' |
|
514
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
515
|
|
|
p.prepare_boreas_alive_test() |
|
516
|
|
|
|
|
517
|
|
|
p.kbdb.add_scan_preferences.assert_not_called() |
|
518
|
|
|
|
|
519
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
520
|
|
|
def test_set_alive_no_setting(self, mock_kb): |
|
521
|
|
|
w = DummyDaemon() |
|
522
|
|
|
|
|
523
|
|
|
t_opt = {} |
|
524
|
|
|
w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
|
525
|
|
|
|
|
526
|
|
|
ov_setting = {} |
|
527
|
|
|
|
|
528
|
|
|
with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
|
529
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
530
|
|
|
p._openvas_scan_id = '456-789' |
|
531
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
532
|
|
|
p.prepare_alive_test_option_for_openvas() |
|
533
|
|
|
|
|
534
|
|
|
p.kbdb.add_scan_preferences.assert_not_called() |
|
535
|
|
|
|
|
536
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
537
|
|
|
def test_set_alive_pinghost(self, mock_kb): |
|
538
|
|
|
w = DummyDaemon() |
|
539
|
|
|
|
|
540
|
|
|
alive_test_out = [ |
|
541
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping|||no", |
|
542
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:2:checkbox:TCP ping tries also TCP-SYN ping|||no", |
|
543
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:7:checkbox:TCP ping tries only TCP-SYN ping|||no", |
|
544
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:3:checkbox:Do an ICMP ping|||yes", |
|
545
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:4:checkbox:Use ARP|||no", |
|
546
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:5:checkbox:Mark unrechable Hosts as dead (not scanning)|||yes", |
|
547
|
|
|
] |
|
548
|
|
|
|
|
549
|
|
|
t_opt = {'alive_test': 2} |
|
550
|
|
|
w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
|
551
|
|
|
|
|
552
|
|
|
ov_setting = {'some_setting': 1} |
|
553
|
|
|
|
|
554
|
|
|
with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
|
555
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
556
|
|
|
p._nvts_params = {} |
|
557
|
|
|
p._openvas_scan_id = '456-789' |
|
558
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
559
|
|
|
p.prepare_alive_test_option_for_openvas() |
|
560
|
|
|
|
|
561
|
|
|
for key, value in p._nvts_params.items(): |
|
562
|
|
|
self.assertTrue( |
|
563
|
|
|
"{0}|||{1}".format(key, value) in alive_test_out |
|
564
|
|
|
) |
|
565
|
|
|
|
|
566
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
567
|
|
|
def test_prepare_nvt_prefs(self, mock_kb): |
|
568
|
|
|
w = DummyDaemon() |
|
569
|
|
|
|
|
570
|
|
|
alive_test_out = [ |
|
571
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping|||no" |
|
572
|
|
|
] |
|
573
|
|
|
|
|
574
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
575
|
|
|
p._nvts_params = { |
|
576
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping": "no" |
|
577
|
|
|
} |
|
578
|
|
|
p._openvas_scan_id = '456-789' |
|
579
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
580
|
|
|
p.prepare_nvt_preferences() |
|
581
|
|
|
|
|
582
|
|
|
p.kbdb.add_scan_preferences.assert_called_with( |
|
583
|
|
|
p._openvas_scan_id, |
|
584
|
|
|
alive_test_out, |
|
585
|
|
|
) |
|
586
|
|
|
|
|
587
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
588
|
|
|
def test_prepare_nvt_prefs_no_prefs(self, mock_kb): |
|
589
|
|
|
w = DummyDaemon() |
|
590
|
|
|
|
|
591
|
|
|
p = PreferenceHandler('456-789', mock_kb, w.scan_collection, None) |
|
592
|
|
|
p._nvts_params = {} |
|
593
|
|
|
p._openvas_scan_id = '456-789' |
|
594
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
595
|
|
|
p.prepare_nvt_preferences() |
|
596
|
|
|
|
|
597
|
|
|
p.kbdb.add_scan_preferences.assert_not_called() |
|
598
|
|
|
|