|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# Copyright (C) 2020 Greenbone Networks GmbH |
|
3
|
|
|
# |
|
4
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later |
|
5
|
|
|
# |
|
6
|
|
|
# This program is free software; you can redistribute it and/or |
|
7
|
|
|
# modify it under the terms of the GNU General Public License |
|
8
|
|
|
# as published by the Free Software Foundation; either version 2 |
|
9
|
|
|
# of the 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 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, write to the Free Software |
|
18
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
19
|
|
|
|
|
20
|
|
|
import logging |
|
21
|
|
|
|
|
22
|
|
|
from unittest import TestCase |
|
23
|
|
|
from unittest.mock import patch, Mock, MagicMock |
|
24
|
|
|
|
|
25
|
|
|
from ospd.vts import Vts |
|
26
|
|
|
|
|
27
|
|
|
from tests.dummydaemon import DummyDaemon |
|
28
|
|
|
from tests.helper import assert_called_once |
|
29
|
|
|
|
|
30
|
|
|
import ospd_openvas.db |
|
31
|
|
|
|
|
32
|
|
|
from ospd_openvas.openvas import Openvas |
|
33
|
|
|
from ospd_openvas.preferencehandler import PreferenceHandler |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
class PreferenceHandlerTestCase(TestCase): |
|
37
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
38
|
|
|
def test_process_vts_not_found(self, mock_kb): |
|
39
|
|
|
w = DummyDaemon() |
|
40
|
|
|
logging.Logger.warning = Mock() |
|
41
|
|
|
|
|
42
|
|
|
vts = { |
|
43
|
|
|
'1.3.6.1.4.1.25623.1.0.100065': {'3': 'new value'}, |
|
44
|
|
|
'vt_groups': ['family=debian', 'family=general'], |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
w.load_vts() |
|
48
|
|
|
temp_vts = w.vts |
|
49
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, temp_vts) |
|
50
|
|
|
|
|
51
|
|
|
p._process_vts(vts) |
|
52
|
|
|
|
|
53
|
|
|
assert_called_once(logging.Logger.warning) |
|
54
|
|
|
|
|
55
|
|
|
def test_process_vts_bad_param_id(self): |
|
56
|
|
|
w = DummyDaemon() |
|
57
|
|
|
|
|
58
|
|
|
vts = { |
|
59
|
|
|
'1.3.6.1.4.1.25623.1.0.100061': {'3': 'new value'}, |
|
60
|
|
|
'vt_groups': ['family=debian', 'family=general'], |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
w.load_vts() |
|
64
|
|
|
temp_vts = w.vts |
|
65
|
|
|
p = PreferenceHandler('1234-1234', None, w.scan_collection, temp_vts) |
|
66
|
|
|
|
|
67
|
|
|
ret = p._process_vts(vts) |
|
68
|
|
|
|
|
69
|
|
|
self.assertFalse(ret[1]) |
|
70
|
|
|
|
|
71
|
|
|
def test_process_vts(self): |
|
72
|
|
|
w = DummyDaemon() |
|
73
|
|
|
|
|
74
|
|
|
vts = { |
|
75
|
|
|
'1.3.6.1.4.1.25623.1.0.100061': {'1': 'new value'}, |
|
76
|
|
|
'vt_groups': ['family=debian', 'family=general'], |
|
77
|
|
|
} |
|
78
|
|
|
vt_out = ( |
|
79
|
|
|
['1.3.6.1.4.1.25623.1.0.100061'], |
|
80
|
|
|
{'1.3.6.1.4.1.25623.1.0.100061:1:entry:Data length :': 'new value'}, |
|
81
|
|
|
) |
|
82
|
|
|
|
|
83
|
|
|
w.load_vts() |
|
84
|
|
|
temp_vts = w.vts |
|
85
|
|
|
p = PreferenceHandler('1234-1234', None, w.scan_collection, temp_vts) |
|
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
|
|
|
w.load_vts() |
|
97
|
|
|
temp_vts = w.vts |
|
98
|
|
|
|
|
99
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, temp_vts) |
|
100
|
|
|
p.kbdb.add_scan_preferences = Mock() |
|
101
|
|
|
r = p.prepare_plugins_for_openvas() |
|
102
|
|
|
|
|
103
|
|
|
self.assertFalse(r) |
|
104
|
|
|
|
|
105
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
106
|
|
|
def test_set_plugins_true(self, mock_kb): |
|
107
|
|
|
w = DummyDaemon() |
|
108
|
|
|
|
|
109
|
|
|
vts = { |
|
110
|
|
|
'1.3.6.1.4.1.25623.1.0.100061': {'3': 'new value'}, |
|
111
|
|
|
'vt_groups': ['family=debian', 'family=general'], |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
w.scan_collection.get_vts = Mock() |
|
115
|
|
|
w.scan_collection.get_vts.return_value = vts |
|
116
|
|
|
w.load_vts() |
|
117
|
|
|
temp_vts = w.vts |
|
118
|
|
|
|
|
119
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, temp_vts) |
|
120
|
|
|
p.kbdb.add_scan_preferences = Mock() |
|
121
|
|
|
r = p.prepare_plugins_for_openvas() |
|
122
|
|
|
|
|
123
|
|
|
self.assertTrue(r) |
|
124
|
|
|
|
|
125
|
|
|
def test_build_credentials_ssh_up(self): |
|
126
|
|
|
w = DummyDaemon() |
|
127
|
|
|
|
|
128
|
|
|
cred_out = [ |
|
129
|
|
|
'auth_port_ssh|||22', |
|
130
|
|
|
'1.3.6.1.4.1.25623.1.0.103591:1:entry:SSH login name:|||username', |
|
131
|
|
|
'1.3.6.1.4.1.25623.1.0.103591:3:password:SSH password (unsafe!):|||pass', |
|
132
|
|
|
] |
|
133
|
|
|
cred_dict = { |
|
134
|
|
|
'ssh': { |
|
135
|
|
|
'type': 'up', |
|
136
|
|
|
'port': '22', |
|
137
|
|
|
'username': 'username', |
|
138
|
|
|
'password': 'pass', |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
|
142
|
|
|
|
|
143
|
|
|
ret = p.build_credentials_as_prefs(cred_dict) |
|
144
|
|
|
|
|
145
|
|
|
self.assertEqual(ret, cred_out) |
|
146
|
|
|
|
|
147
|
|
|
def test_build_credentials(self): |
|
148
|
|
|
w = DummyDaemon() |
|
149
|
|
|
|
|
150
|
|
|
cred_out = [ |
|
151
|
|
|
'1.3.6.1.4.1.25623.1.0.105058:1:entry:ESXi login name:|||username', |
|
152
|
|
|
'1.3.6.1.4.1.25623.1.0.105058:2:password:ESXi login password:|||pass', |
|
153
|
|
|
'auth_port_ssh|||22', |
|
154
|
|
|
'1.3.6.1.4.1.25623.1.0.103591:1:entry:SSH login name:|||username', |
|
155
|
|
|
'1.3.6.1.4.1.25623.1.0.103591:2:password:SSH key passphrase:|||pass', |
|
156
|
|
|
'1.3.6.1.4.1.25623.1.0.103591:4:file:SSH private key:|||', |
|
157
|
|
|
'1.3.6.1.4.1.25623.1.0.90023:1:entry:SMB login:|||username', |
|
158
|
|
|
'1.3.6.1.4.1.25623.1.0.90023:2:password]:SMB password :|||pass', |
|
159
|
|
|
'1.3.6.1.4.1.25623.1.0.105076:1:password:SNMP Community:some comunity', |
|
160
|
|
|
'1.3.6.1.4.1.25623.1.0.105076:2:entry:SNMPv3 Username:username', |
|
161
|
|
|
'1.3.6.1.4.1.25623.1.0.105076:3:password:SNMPv3 Password:pass', |
|
162
|
|
|
'1.3.6.1.4.1.25623.1.0.105076:4:radio:SNMPv3 Authentication Algorithm:some auth algo', |
|
163
|
|
|
'1.3.6.1.4.1.25623.1.0.105076:5:password:SNMPv3 Privacy Password:privacy pass', |
|
164
|
|
|
'1.3.6.1.4.1.25623.1.0.105076:6:radio:SNMPv3 Privacy Algorithm:privacy algo', |
|
165
|
|
|
] |
|
166
|
|
|
cred_dict = { |
|
167
|
|
|
'ssh': { |
|
168
|
|
|
'type': 'ssh', |
|
169
|
|
|
'port': '22', |
|
170
|
|
|
'username': 'username', |
|
171
|
|
|
'password': 'pass', |
|
172
|
|
|
}, |
|
173
|
|
|
'smb': {'type': 'smb', 'username': 'username', 'password': 'pass'}, |
|
174
|
|
|
'esxi': { |
|
175
|
|
|
'type': 'esxi', |
|
176
|
|
|
'username': 'username', |
|
177
|
|
|
'password': 'pass', |
|
178
|
|
|
}, |
|
179
|
|
|
'snmp': { |
|
180
|
|
|
'type': 'snmp', |
|
181
|
|
|
'username': 'username', |
|
182
|
|
|
'password': 'pass', |
|
183
|
|
|
'community': 'some comunity', |
|
184
|
|
|
'auth_algorithm': 'some auth algo', |
|
185
|
|
|
'privacy_password': 'privacy pass', |
|
186
|
|
|
'privacy_algorithm': 'privacy algo', |
|
187
|
|
|
}, |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
|
191
|
|
|
ret = p.build_credentials_as_prefs(cred_dict) |
|
192
|
|
|
|
|
193
|
|
|
self.assertEqual(len(ret), len(cred_out)) |
|
194
|
|
|
self.assertIn('auth_port_ssh|||22', cred_out) |
|
195
|
|
|
self.assertIn( |
|
196
|
|
|
'1.3.6.1.4.1.25623.1.0.90023:1:entry:SMB login:|||username', |
|
197
|
|
|
cred_out, |
|
198
|
|
|
) |
|
199
|
|
|
|
|
200
|
|
|
def test_build_alive_test_opt_empty(self): |
|
201
|
|
|
w = DummyDaemon() |
|
202
|
|
|
|
|
203
|
|
|
target_options_dict = {'alive_test': '0'} |
|
204
|
|
|
|
|
205
|
|
|
p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
|
206
|
|
|
ret = p.build_alive_test_opt_as_prefs(target_options_dict) |
|
207
|
|
|
|
|
208
|
|
|
self.assertEqual(ret, []) |
|
209
|
|
|
|
|
210
|
|
|
def test_build_alive_test_opt(self): |
|
211
|
|
|
w = DummyDaemon() |
|
212
|
|
|
|
|
213
|
|
|
alive_test_out = [ |
|
214
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping|||no", |
|
215
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:2:checkbox:TCP ping tries also TCP-SYN ping|||no", |
|
216
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:7:checkbox:TCP ping tries only TCP-SYN ping|||no", |
|
217
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:3:checkbox:Do an ICMP ping|||yes", |
|
218
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:4:checkbox:Use ARP|||no", |
|
219
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:5:checkbox:Mark unrechable Hosts as dead (not scanning)|||yes", |
|
220
|
|
|
] |
|
221
|
|
|
target_options_dict = {'alive_test': '2'} |
|
222
|
|
|
p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
|
223
|
|
|
ret = p.build_alive_test_opt_as_prefs(target_options_dict) |
|
224
|
|
|
|
|
225
|
|
|
self.assertEqual(ret, alive_test_out) |
|
226
|
|
|
|
|
227
|
|
|
def test_build_alive_test_opt_fail_1(self): |
|
228
|
|
|
w = DummyDaemon() |
|
229
|
|
|
logging.Logger.debug = Mock() |
|
230
|
|
|
|
|
231
|
|
|
target_options_dict = {'alive_test': 'a'} |
|
232
|
|
|
p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
|
233
|
|
|
target_options = p.build_alive_test_opt_as_prefs(target_options_dict) |
|
234
|
|
|
|
|
235
|
|
|
assert_called_once(logging.Logger.debug) |
|
236
|
|
|
self.assertEqual(len(target_options), 0) |
|
237
|
|
|
|
|
238
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
239
|
|
|
def test_set_target(self, mock_kb): |
|
240
|
|
|
w = DummyDaemon() |
|
241
|
|
|
|
|
242
|
|
|
w.scan_collection.get_host_list = MagicMock(return_value='192.168.0.1') |
|
243
|
|
|
|
|
244
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
245
|
|
|
p._openvas_scan_id = '456-789' |
|
246
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
247
|
|
|
p.prepare_target_for_openvas() |
|
248
|
|
|
|
|
249
|
|
|
p.kbdb.add_scan_preferences.assert_called_with( |
|
250
|
|
|
p._openvas_scan_id, ['TARGET|||192.168.0.1'], |
|
251
|
|
|
) |
|
252
|
|
|
|
|
253
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
254
|
|
|
def test_set_ports(self, mock_kb): |
|
255
|
|
|
w = DummyDaemon() |
|
256
|
|
|
|
|
257
|
|
|
w.scan_collection.get_ports = MagicMock(return_value='80,443') |
|
258
|
|
|
|
|
259
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
260
|
|
|
p._openvas_scan_id = '456-789' |
|
261
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
262
|
|
|
p.prepare_ports_for_openvas() |
|
263
|
|
|
|
|
264
|
|
|
p.kbdb.add_scan_preferences.assert_called_with( |
|
265
|
|
|
p._openvas_scan_id, ['port_range|||80,443'], |
|
266
|
|
|
) |
|
267
|
|
|
|
|
268
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
269
|
|
|
def test_set_main_kbindex(self, mock_kb): |
|
270
|
|
|
w = DummyDaemon() |
|
271
|
|
|
|
|
272
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
273
|
|
|
p.kbdb.add_scan_preferences = Mock() |
|
274
|
|
|
p.kbdb.index = 2 |
|
275
|
|
|
p.prepare_main_kbindex_for_openvas() |
|
276
|
|
|
|
|
277
|
|
|
p.kbdb.add_scan_preferences.assert_called_with( |
|
278
|
|
|
p._openvas_scan_id, ['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_false(self, mock_kb): |
|
321
|
|
|
w = DummyDaemon() |
|
322
|
|
|
|
|
323
|
|
|
creds = {} |
|
324
|
|
|
|
|
325
|
|
|
w.scan_collection.get_credentials = MagicMock(return_value=creds) |
|
326
|
|
|
|
|
327
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
328
|
|
|
p._openvas_scan_id = '456-789' |
|
329
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
330
|
|
|
r = p.prepare_credentials_for_openvas() |
|
331
|
|
|
|
|
332
|
|
|
self.assertFalse(r) |
|
333
|
|
|
|
|
334
|
|
View Code Duplication |
@patch('ospd_openvas.db.KbDB') |
|
|
|
|
|
|
335
|
|
|
def test_set_host_options(self, mock_kb): |
|
336
|
|
|
w = DummyDaemon() |
|
337
|
|
|
|
|
338
|
|
|
exc = '192.168.0.1' |
|
339
|
|
|
fin = ['192.168.0.2'] |
|
340
|
|
|
|
|
341
|
|
|
w.scan_collection.get_exclude_hosts = MagicMock(return_value=exc) |
|
342
|
|
|
w.scan_collection.get_hosts_finished = MagicMock(return_value=fin) |
|
343
|
|
|
|
|
344
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
345
|
|
|
p._openvas_scan_id = '456-789' |
|
346
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
347
|
|
|
p.prepare_host_options_for_openvas() |
|
348
|
|
|
|
|
349
|
|
|
p.kbdb.add_scan_preferences.assert_called_with( |
|
350
|
|
|
p._openvas_scan_id, ['exclude_hosts|||192.168.0.1,192.168.0.2'], |
|
351
|
|
|
) |
|
352
|
|
|
|
|
353
|
|
View Code Duplication |
@patch('ospd_openvas.db.KbDB') |
|
|
|
|
|
|
354
|
|
|
def test_set_host_options_no_exclude(self, mock_kb): |
|
355
|
|
|
w = DummyDaemon() |
|
356
|
|
|
|
|
357
|
|
|
exc = '' |
|
358
|
|
|
fin = ['192.168.0.2'] |
|
359
|
|
|
|
|
360
|
|
|
w.scan_collection.get_exclude_hosts = MagicMock(return_value=exc) |
|
361
|
|
|
w.scan_collection.get_hosts_finished = MagicMock(return_value=fin) |
|
362
|
|
|
|
|
363
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
364
|
|
|
p._openvas_scan_id = '456-789' |
|
365
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
366
|
|
|
p.prepare_host_options_for_openvas() |
|
367
|
|
|
|
|
368
|
|
|
p.kbdb.add_scan_preferences.assert_called_with( |
|
369
|
|
|
p._openvas_scan_id, ['exclude_hosts|||192.168.0.2'], |
|
370
|
|
|
) |
|
371
|
|
|
|
|
372
|
|
View Code Duplication |
@patch('ospd_openvas.db.KbDB') |
|
|
|
|
|
|
373
|
|
|
def test_set_host_options_no_finished(self, mock_kb): |
|
374
|
|
|
w = DummyDaemon() |
|
375
|
|
|
|
|
376
|
|
|
exc = '192.168.0.1' |
|
377
|
|
|
fin = [] |
|
378
|
|
|
|
|
379
|
|
|
w.scan_collection.get_exclude_hosts = MagicMock(return_value=exc) |
|
380
|
|
|
w.scan_collection.get_hosts_finished = MagicMock(return_value=fin) |
|
381
|
|
|
|
|
382
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
383
|
|
|
p._openvas_scan_id = '456-789' |
|
384
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
385
|
|
|
p.prepare_host_options_for_openvas() |
|
386
|
|
|
|
|
387
|
|
|
p.kbdb.add_scan_preferences.assert_called_with( |
|
388
|
|
|
p._openvas_scan_id, ['exclude_hosts|||192.168.0.1'], |
|
389
|
|
|
) |
|
390
|
|
|
|
|
391
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
392
|
|
|
def test_set_host_options_none(self, mock_kb): |
|
393
|
|
|
w = DummyDaemon() |
|
394
|
|
|
|
|
395
|
|
|
exc = '' |
|
396
|
|
|
fin = [] |
|
397
|
|
|
|
|
398
|
|
|
w.scan_collection.get_exclude_hosts = MagicMock(return_value=exc) |
|
399
|
|
|
w.scan_collection.get_hosts_finished = MagicMock(return_value=fin) |
|
400
|
|
|
|
|
401
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
402
|
|
|
p._openvas_scan_id = '456-789' |
|
403
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
404
|
|
|
p.prepare_host_options_for_openvas() |
|
405
|
|
|
|
|
406
|
|
|
p.kbdb.add_scan_preferences.assert_not_called() |
|
407
|
|
|
|
|
408
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
409
|
|
|
def test_set_scan_params(self, mock_kb): |
|
410
|
|
|
w = DummyDaemon() |
|
411
|
|
|
|
|
412
|
|
|
OSPD_PARAMS_MOCK = { |
|
413
|
|
|
'drop_privileges': { |
|
414
|
|
|
'type': 'boolean', |
|
415
|
|
|
'name': 'drop_privileges', |
|
416
|
|
|
'default': 0, |
|
417
|
|
|
'mandatory': 1, |
|
418
|
|
|
'description': '', |
|
419
|
|
|
}, |
|
420
|
|
|
} |
|
421
|
|
|
|
|
422
|
|
|
opt = {'drop_privileges': 1} |
|
423
|
|
|
|
|
424
|
|
|
w.scan_collection.get_options = MagicMock(return_value=opt) |
|
425
|
|
|
|
|
426
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
427
|
|
|
p._openvas_scan_id = '456-789' |
|
428
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
429
|
|
|
p.prepare_scan_params_for_openvas(OSPD_PARAMS_MOCK) |
|
430
|
|
|
|
|
431
|
|
|
p.kbdb.add_scan_preferences.assert_called_with( |
|
432
|
|
|
p._openvas_scan_id, ['drop_privileges|||yes'] |
|
433
|
|
|
) |
|
434
|
|
|
|
|
435
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
436
|
|
|
def test_set_reverse_lookup_opt(self, mock_kb): |
|
437
|
|
|
w = DummyDaemon() |
|
438
|
|
|
|
|
439
|
|
|
t_opt = {'reverse_lookup_only': 1} |
|
440
|
|
|
w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
|
441
|
|
|
|
|
442
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
443
|
|
|
p._openvas_scan_id = '456-789' |
|
444
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
445
|
|
|
p.prepare_reverse_lookup_opt_for_openvas() |
|
446
|
|
|
|
|
447
|
|
|
p.kbdb.add_scan_preferences.assert_called_with( |
|
448
|
|
|
p._openvas_scan_id, |
|
449
|
|
|
['reverse_lookup_only|||yes', 'reverse_lookup_unify|||no',], |
|
450
|
|
|
) |
|
451
|
|
|
|
|
452
|
|
View Code Duplication |
@patch('ospd_openvas.db.KbDB') |
|
|
|
|
|
|
453
|
|
|
def test_set_alive_only(self, mock_kb): |
|
454
|
|
|
w = DummyDaemon() |
|
455
|
|
|
|
|
456
|
|
|
t_opt = {'alive_test': 16} |
|
457
|
|
|
w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
|
458
|
|
|
|
|
459
|
|
|
ov_setting = {'test_alive_hosts_only': 'yes'} |
|
460
|
|
|
|
|
461
|
|
|
Openvas.get_settings = MagicMock(return_value=ov_setting) |
|
462
|
|
|
|
|
463
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
464
|
|
|
p._openvas_scan_id = '456-789' |
|
465
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
466
|
|
|
p.prepare_alive_test_option_for_openvas() |
|
467
|
|
|
|
|
468
|
|
|
p.kbdb.add_scan_preferences.assert_called_with( |
|
469
|
|
|
p._openvas_scan_id, ['ALIVE_TEST|||16'], |
|
470
|
|
|
) |
|
471
|
|
|
|
|
472
|
|
|
@patch('ospd_openvas.db.KbDB') |
|
473
|
|
|
def test_set_alive_no_setting(self, mock_kb): |
|
474
|
|
|
w = DummyDaemon() |
|
475
|
|
|
|
|
476
|
|
|
t_opt = {} |
|
477
|
|
|
w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
|
478
|
|
|
|
|
479
|
|
|
ov_setting = {} |
|
480
|
|
|
|
|
481
|
|
|
Openvas.get_settings = MagicMock(return_value=ov_setting) |
|
482
|
|
|
|
|
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_alive_test_option_for_openvas() |
|
487
|
|
|
|
|
488
|
|
|
p.kbdb.add_scan_preferences.assert_not_called() |
|
489
|
|
|
|
|
490
|
|
View Code Duplication |
@patch('ospd_openvas.db.KbDB') |
|
|
|
|
|
|
491
|
|
|
def test_set_alive_pinghost(self, mock_kb): |
|
492
|
|
|
w = DummyDaemon() |
|
493
|
|
|
|
|
494
|
|
|
alive_test_out = [ |
|
495
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping|||no", |
|
496
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:2:checkbox:TCP ping tries also TCP-SYN ping|||no", |
|
497
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:7:checkbox:TCP ping tries only TCP-SYN ping|||no", |
|
498
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:3:checkbox:Do an ICMP ping|||yes", |
|
499
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:4:checkbox:Use ARP|||no", |
|
500
|
|
|
"1.3.6.1.4.1.25623.1.0.100315:5:checkbox:Mark unrechable Hosts as dead (not scanning)|||yes", |
|
501
|
|
|
] |
|
502
|
|
|
|
|
503
|
|
|
t_opt = {'alive_test': 2} |
|
504
|
|
|
w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
|
505
|
|
|
|
|
506
|
|
|
ov_setting = {'some_setiting': 1} |
|
507
|
|
|
Openvas.get_settings = Mock() |
|
508
|
|
|
Openvas.get_settings.reprepare_mock() |
|
509
|
|
|
Openvas.get_settings.return_value = ov_setting |
|
510
|
|
|
|
|
511
|
|
|
p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
|
512
|
|
|
|
|
513
|
|
|
p._openvas_scan_id = '456-789' |
|
514
|
|
|
p.kbdb.add_scan_preferences = MagicMock() |
|
515
|
|
|
p.prepare_alive_test_option_for_openvas() |
|
516
|
|
|
|
|
517
|
|
|
p.kbdb.add_scan_preferences.assert_called_with( |
|
518
|
|
|
p._openvas_scan_id, alive_test_out, |
|
519
|
|
|
) |
|
520
|
|
|
|