| Total Complexity | 67 |
| Total Lines | 1206 |
| Duplicated Lines | 9.62 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like tests.test_preferencehandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 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 | BOREAS_ALIVE_TEST_PORTS, |
||
| 39 | PreferenceHandler, |
||
| 40 | alive_test_methods_to_bit_field, |
||
| 41 | ) |
||
| 42 | |||
| 43 | |||
| 44 | class PreferenceHandlerTestCase(TestCase): |
||
| 45 | @patch('ospd_openvas.db.KbDB') |
||
| 46 | def test_process_vts_not_found(self, mock_kb): |
||
| 47 | w = DummyDaemon() |
||
| 48 | logging.Logger.warning = Mock() |
||
| 49 | |||
| 50 | vts = { |
||
| 51 | '1.3.6.1.4.1.25623.1.0.100065': {'3': 'new value'}, |
||
| 52 | 'vt_groups': ['family=debian', 'family=general'], |
||
| 53 | } |
||
| 54 | |||
| 55 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, w.nvti) |
||
| 56 | w.nvti.get_nvt_metadata.return_value = None |
||
| 57 | p._process_vts(vts) |
||
| 58 | |||
| 59 | assert_called_once(logging.Logger.warning) |
||
| 60 | |||
| 61 | def test_process_vts_bad_param_id(self): |
||
| 62 | w = DummyDaemon() |
||
| 63 | |||
| 64 | vts = { |
||
| 65 | '1.3.6.1.4.1.25623.1.0.100061': {'3': 'new value'}, |
||
| 66 | 'vt_groups': ['family=debian', 'family=general'], |
||
| 67 | } |
||
| 68 | |||
| 69 | p = PreferenceHandler('1234-1234', None, w.scan_collection, w.nvti) |
||
| 70 | |||
| 71 | ret = p._process_vts(vts) |
||
| 72 | |||
| 73 | self.assertFalse(ret[1]) |
||
| 74 | |||
| 75 | def test_process_vts(self): |
||
| 76 | w = DummyDaemon() |
||
| 77 | |||
| 78 | vts = { |
||
| 79 | '1.3.6.1.4.1.25623.1.0.100061': {'1': 'new value'}, |
||
| 80 | 'vt_groups': ['family=debian', 'family=general'], |
||
| 81 | } |
||
| 82 | vt_out = ( |
||
| 83 | ['1.3.6.1.4.1.25623.1.0.100061'], |
||
| 84 | {'1.3.6.1.4.1.25623.1.0.100061:1:entry:Data length :': 'new value'}, |
||
| 85 | ) |
||
| 86 | |||
| 87 | p = PreferenceHandler('1234-1234', None, w.scan_collection, w.nvti) |
||
| 88 | ret = p._process_vts(vts) |
||
| 89 | |||
| 90 | self.assertEqual(ret, vt_out) |
||
| 91 | |||
| 92 | @patch('ospd_openvas.db.KbDB') |
||
| 93 | def test_set_plugins_false(self, mock_kb): |
||
| 94 | w = DummyDaemon() |
||
| 95 | |||
| 96 | w.scan_collection.get_vts = Mock() |
||
| 97 | w.scan_collection.get_vts.return_value = {} |
||
| 98 | |||
| 99 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, w.nvti) |
||
| 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 | |||
| 117 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, w.nvti) |
||
| 118 | p.kbdb.add_scan_preferences = Mock() |
||
| 119 | r = p.prepare_plugins_for_openvas() |
||
| 120 | |||
| 121 | self.assertTrue(r) |
||
| 122 | |||
| 123 | def test_build_credentials_ssh_up(self): |
||
| 124 | w = DummyDaemon() |
||
| 125 | |||
| 126 | cred_out = [ |
||
| 127 | 'auth_port_ssh|||22', |
||
| 128 | '1.3.6.1.4.1.25623.1.0.103591:1:entry:SSH login name:|||username', |
||
| 129 | '1.3.6.1.4.1.25623.1.0.103591:3:password:SSH password (unsafe!):|||pass', |
||
| 130 | ] |
||
| 131 | cred_dict = { |
||
| 132 | 'ssh': { |
||
| 133 | 'type': 'up', |
||
| 134 | 'port': '22', |
||
| 135 | 'username': 'username', |
||
| 136 | 'password': 'pass', |
||
| 137 | } |
||
| 138 | } |
||
| 139 | p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
||
| 140 | |||
| 141 | ret = p.build_credentials_as_prefs(cred_dict) |
||
| 142 | |||
| 143 | self.assertCountEqual(ret, cred_out) |
||
| 144 | |||
| 145 | def test_build_credentials(self): |
||
| 146 | w = DummyDaemon() |
||
| 147 | |||
| 148 | cred_out = [ |
||
| 149 | '1.3.6.1.4.1.25623.1.0.105058:1:entry:ESXi login name:|||username', |
||
| 150 | '1.3.6.1.4.1.25623.1.0.105058:2:password:ESXi login password:|||pass', |
||
| 151 | 'auth_port_ssh|||22', |
||
| 152 | '1.3.6.1.4.1.25623.1.0.103591:1:entry:SSH login name:|||username', |
||
| 153 | '1.3.6.1.4.1.25623.1.0.103591:2:password:SSH key passphrase:|||pass', |
||
| 154 | '1.3.6.1.4.1.25623.1.0.103591:4:file:SSH private key:|||', |
||
| 155 | '1.3.6.1.4.1.25623.1.0.90023:1:entry:SMB login:|||username', |
||
| 156 | '1.3.6.1.4.1.25623.1.0.90023:2:password]:SMB password :|||pass', |
||
| 157 | '1.3.6.1.4.1.25623.1.0.105076:1:password:SNMP Community:some comunity', |
||
| 158 | '1.3.6.1.4.1.25623.1.0.105076:2:entry:SNMPv3 Username:username', |
||
| 159 | '1.3.6.1.4.1.25623.1.0.105076:3:password:SNMPv3 Password:pass', |
||
| 160 | '1.3.6.1.4.1.25623.1.0.105076:4:radio:SNMPv3 Authentication Algorithm:some auth algo', |
||
| 161 | '1.3.6.1.4.1.25623.1.0.105076:5:password:SNMPv3 Privacy Password:privacy pass', |
||
| 162 | '1.3.6.1.4.1.25623.1.0.105076:6:radio:SNMPv3 Privacy Algorithm:privacy algo', |
||
| 163 | ] |
||
| 164 | cred_dict = { |
||
| 165 | 'ssh': { |
||
| 166 | 'type': 'usk', |
||
| 167 | 'port': '22', |
||
| 168 | 'username': 'username', |
||
| 169 | 'password': 'pass', |
||
| 170 | 'private': 'some key', |
||
| 171 | }, |
||
| 172 | 'smb': {'type': 'up', 'username': 'username', 'password': 'pass'}, |
||
| 173 | 'esxi': { |
||
| 174 | 'type': 'up', |
||
| 175 | 'username': 'username', |
||
| 176 | 'password': 'pass', |
||
| 177 | }, |
||
| 178 | 'snmp': { |
||
| 179 | 'type': 'snmp', |
||
| 180 | 'username': 'username', |
||
| 181 | 'password': 'pass', |
||
| 182 | 'community': 'some comunity', |
||
| 183 | 'auth_algorithm': 'md5', |
||
| 184 | 'privacy_password': 'privacy pass', |
||
| 185 | 'privacy_algorithm': 'aes', |
||
| 186 | }, |
||
| 187 | } |
||
| 188 | |||
| 189 | p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
||
| 190 | ret = p.build_credentials_as_prefs(cred_dict) |
||
| 191 | |||
| 192 | self.assertEqual(len(ret), len(cred_out)) |
||
| 193 | self.assertIn('auth_port_ssh|||22', cred_out) |
||
| 194 | self.assertIn( |
||
| 195 | '1.3.6.1.4.1.25623.1.0.90023:1:entry:SMB login:|||username', |
||
| 196 | cred_out, |
||
| 197 | ) |
||
| 198 | |||
| 199 | def test_build_alive_test_opt_empty(self): |
||
| 200 | w = DummyDaemon() |
||
| 201 | |||
| 202 | target_options_dict = {'alive_test': '0'} |
||
| 203 | |||
| 204 | p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
||
| 205 | ret = p.build_alive_test_opt_as_prefs(target_options_dict) |
||
| 206 | |||
| 207 | self.assertEqual(ret, {}) |
||
| 208 | |||
| 209 | # alive test was supplied via separate xml element |
||
| 210 | w = DummyDaemon() |
||
| 211 | target_options_dict = {'alive_test_methods': '1', 'icmp': '0'} |
||
| 212 | p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
||
| 213 | ret = p.build_alive_test_opt_as_prefs(target_options_dict) |
||
| 214 | self.assertEqual(ret, {}) |
||
| 215 | |||
| 216 | def test_build_alive_test_opt(self): |
||
| 217 | w = DummyDaemon() |
||
| 218 | |||
| 219 | alive_test_out = { |
||
| 220 | "1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping": "no", |
||
| 221 | "1.3.6.1.4.1.25623.1.0.100315:2:checkbox:TCP ping tries also TCP-SYN ping": "no", |
||
| 222 | "1.3.6.1.4.1.25623.1.0.100315:7:checkbox:TCP ping tries only TCP-SYN ping": "no", |
||
| 223 | "1.3.6.1.4.1.25623.1.0.100315:3:checkbox:Do an ICMP ping": "yes", |
||
| 224 | "1.3.6.1.4.1.25623.1.0.100315:4:checkbox:Use ARP": "no", |
||
| 225 | "1.3.6.1.4.1.25623.1.0.100315:5:checkbox:Mark unrechable Hosts as dead (not scanning)": "yes", |
||
| 226 | } |
||
| 227 | |||
| 228 | target_options_dict = {'alive_test': '2'} |
||
| 229 | p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
||
| 230 | ret = p.build_alive_test_opt_as_prefs(target_options_dict) |
||
| 231 | |||
| 232 | self.assertEqual(ret, alive_test_out) |
||
| 233 | |||
| 234 | # alive test was supplied via sepertae xml element |
||
| 235 | w = DummyDaemon() |
||
| 236 | target_options_dict = {'alive_test_methods': '1', 'icmp': '1'} |
||
| 237 | p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
||
| 238 | ret = p.build_alive_test_opt_as_prefs(target_options_dict) |
||
| 239 | self.assertEqual(ret, alive_test_out) |
||
| 240 | |||
| 241 | def test_build_alive_test_opt_fail_1(self): |
||
| 242 | w = DummyDaemon() |
||
| 243 | logging.Logger.debug = Mock() |
||
| 244 | |||
| 245 | target_options_dict = {'alive_test': 'a'} |
||
| 246 | p = PreferenceHandler('1234-1234', None, w.scan_collection, None) |
||
| 247 | target_options = p.build_alive_test_opt_as_prefs(target_options_dict) |
||
| 248 | |||
| 249 | assert_called_once(logging.Logger.debug) |
||
| 250 | self.assertEqual(len(target_options), 0) |
||
| 251 | |||
| 252 | @patch('ospd_openvas.db.KbDB') |
||
| 253 | def test_set_target(self, mock_kb): |
||
| 254 | w = DummyDaemon() |
||
| 255 | |||
| 256 | w.scan_collection.get_host_list = MagicMock(return_value='192.168.0.1') |
||
| 257 | |||
| 258 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 259 | p.scan_id = '456-789' |
||
| 260 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 261 | p.prepare_target_for_openvas() |
||
| 262 | |||
| 263 | p.kbdb.add_scan_preferences.assert_called_with( |
||
| 264 | p.scan_id, |
||
| 265 | ['TARGET|||192.168.0.1'], |
||
| 266 | ) |
||
| 267 | |||
| 268 | @patch('ospd_openvas.db.KbDB') |
||
| 269 | def test_set_ports(self, mock_kb): |
||
| 270 | w = DummyDaemon() |
||
| 271 | |||
| 272 | w.scan_collection.get_ports = MagicMock(return_value='80,443') |
||
| 273 | |||
| 274 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 275 | p.scan_id = '456-789' |
||
| 276 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 277 | p.prepare_ports_for_openvas() |
||
| 278 | |||
| 279 | p.kbdb.add_scan_preferences.assert_called_with( |
||
| 280 | p.scan_id, |
||
| 281 | ['port_range|||80,443'], |
||
| 282 | ) |
||
| 283 | |||
| 284 | @patch('ospd_openvas.db.KbDB') |
||
| 285 | def test_set_ports_invalid(self, mock_kb): |
||
| 286 | w = DummyDaemon() |
||
| 287 | |||
| 288 | w.scan_collection.get_ports = MagicMock(return_value='2,-9,4') |
||
| 289 | |||
| 290 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 291 | p.scan_id = '456-789' |
||
| 292 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 293 | self.assertFalse(p.prepare_ports_for_openvas()) |
||
| 294 | |||
| 295 | @patch('ospd_openvas.db.KbDB') |
||
| 296 | def test_set_main_kbindex(self, mock_kb): |
||
| 297 | w = DummyDaemon() |
||
| 298 | |||
| 299 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 300 | p.kbdb.add_scan_preferences = Mock() |
||
| 301 | p.kbdb.index = 2 |
||
| 302 | p.prepare_main_kbindex_for_openvas() |
||
| 303 | |||
| 304 | p.kbdb.add_scan_preferences.assert_called_with( |
||
| 305 | p.scan_id, |
||
| 306 | ['ov_maindbid|||2'], |
||
| 307 | ) |
||
| 308 | |||
| 309 | @patch('ospd_openvas.db.KbDB') |
||
| 310 | def test_set_credentials(self, mock_kb): |
||
| 311 | w = DummyDaemon() |
||
| 312 | |||
| 313 | creds = { |
||
| 314 | 'ssh': { |
||
| 315 | 'type': 'up', |
||
| 316 | 'port': '22', |
||
| 317 | 'username': 'username', |
||
| 318 | 'password': 'pass', |
||
| 319 | }, |
||
| 320 | 'smb': {'type': 'up', 'username': 'username', 'password': 'pass'}, |
||
| 321 | 'esxi': { |
||
| 322 | 'type': 'up', |
||
| 323 | 'username': 'username', |
||
| 324 | 'password': 'pass', |
||
| 325 | }, |
||
| 326 | 'snmp': { |
||
| 327 | 'type': 'snmp', |
||
| 328 | 'username': 'username', |
||
| 329 | 'password': 'pass', |
||
| 330 | 'community': 'some comunity', |
||
| 331 | 'auth_algorithm': 'md5', |
||
| 332 | 'privacy_password': 'privacy pass', |
||
| 333 | 'privacy_algorithm': 'aes', |
||
| 334 | }, |
||
| 335 | } |
||
| 336 | |||
| 337 | w.scan_collection.get_credentials = MagicMock(return_value=creds) |
||
| 338 | |||
| 339 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 340 | p.scan_id = '456-789' |
||
| 341 | p.kbdb.add_credentials_to_scan_preferences = MagicMock() |
||
| 342 | r = p.prepare_credentials_for_openvas() |
||
| 343 | |||
| 344 | self.assertTrue(r) |
||
| 345 | assert_called_once(p.kbdb.add_credentials_to_scan_preferences) |
||
| 346 | |||
| 347 | @patch('ospd_openvas.db.KbDB') |
||
| 348 | def test_set_bad_service_credentials(self, mock_kb): |
||
| 349 | w = DummyDaemon() |
||
| 350 | |||
| 351 | # bad cred type shh instead of ssh |
||
| 352 | creds = { |
||
| 353 | 'shh': { |
||
| 354 | 'type': 'up', |
||
| 355 | 'port': '22', |
||
| 356 | 'username': 'username', |
||
| 357 | 'password': 'pass', |
||
| 358 | }, |
||
| 359 | } |
||
| 360 | |||
| 361 | w.scan_collection.get_credentials = MagicMock(return_value=creds) |
||
| 362 | |||
| 363 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 364 | p.scan_id = '456-789' |
||
| 365 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 366 | r = p.prepare_credentials_for_openvas() |
||
| 367 | |||
| 368 | self.assertFalse(r) |
||
| 369 | self.assertIn("Unknown service type for credential: shh", p.errors) |
||
| 370 | |||
| 371 | @patch('ospd_openvas.db.KbDB') |
||
| 372 | def test_set_bad_ssh_port_credentials(self, mock_kb): |
||
| 373 | w = DummyDaemon() |
||
| 374 | |||
| 375 | creds = { |
||
| 376 | 'ssh': { |
||
| 377 | 'type': 'up', |
||
| 378 | 'port': 'ab', |
||
| 379 | 'username': 'username', |
||
| 380 | 'password': 'pass', |
||
| 381 | }, |
||
| 382 | } |
||
| 383 | |||
| 384 | w.scan_collection.get_credentials = MagicMock(return_value=creds) |
||
| 385 | |||
| 386 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 387 | p.scan_id = '456-789' |
||
| 388 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 389 | r = p.prepare_credentials_for_openvas() |
||
| 390 | |||
| 391 | self.assertFalse(r) |
||
| 392 | self.assertIn("Port for SSH 'ab' is not a valid number.", p.errors) |
||
| 393 | |||
| 394 | @patch('ospd_openvas.db.KbDB') |
||
| 395 | def test_missing_ssh_port_credentials(self, mock_kb): |
||
| 396 | w = DummyDaemon() |
||
| 397 | |||
| 398 | creds = { |
||
| 399 | 'ssh': { |
||
| 400 | 'type': 'up', |
||
| 401 | 'username': 'username', |
||
| 402 | 'password': 'pass', |
||
| 403 | }, |
||
| 404 | } |
||
| 405 | |||
| 406 | w.scan_collection.get_credentials = MagicMock(return_value=creds) |
||
| 407 | |||
| 408 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 409 | p.scan_id = '456-789' |
||
| 410 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 411 | r = p.prepare_credentials_for_openvas() |
||
| 412 | |||
| 413 | self.assertFalse(r) |
||
| 414 | self.assertIn("Port for SSH is missing.", p.errors) |
||
| 415 | |||
| 416 | @patch('ospd_openvas.db.KbDB') |
||
| 417 | def test_ssh_port_out_of_range_credentials(self, mock_kb): |
||
| 418 | w = DummyDaemon() |
||
| 419 | |||
| 420 | creds = { |
||
| 421 | 'ssh': { |
||
| 422 | 'type': 'up', |
||
| 423 | 'port': '65536', |
||
| 424 | 'username': 'username', |
||
| 425 | 'password': 'pass', |
||
| 426 | }, |
||
| 427 | } |
||
| 428 | |||
| 429 | w.scan_collection.get_credentials = MagicMock(return_value=creds) |
||
| 430 | |||
| 431 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 432 | p.scan_id = '456-789' |
||
| 433 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 434 | r = p.prepare_credentials_for_openvas() |
||
| 435 | |||
| 436 | self.assertFalse(r) |
||
| 437 | self.assertIn("Port for SSH is out of range (0-65535): 65536", p.errors) |
||
| 438 | |||
| 439 | @patch('ospd_openvas.db.KbDB') |
||
| 440 | def test_bad_type_for_ssh_credentials(self, mock_kb): |
||
| 441 | w = DummyDaemon() |
||
| 442 | |||
| 443 | creds = { |
||
| 444 | 'ssh': { |
||
| 445 | 'type': 'ups', |
||
| 446 | 'port': '22', |
||
| 447 | 'username': 'username', |
||
| 448 | 'password': 'pass', |
||
| 449 | }, |
||
| 450 | } |
||
| 451 | |||
| 452 | w.scan_collection.get_credentials = MagicMock(return_value=creds) |
||
| 453 | |||
| 454 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 455 | p.scan_id = '456-789' |
||
| 456 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 457 | r = p.prepare_credentials_for_openvas() |
||
| 458 | |||
| 459 | self.assertFalse(r) |
||
| 460 | self.assertIn( |
||
| 461 | "Unknown Credential Type for SSH: " |
||
| 462 | + "ups" |
||
| 463 | + ". Use 'up' for Username + Password" |
||
| 464 | + " or 'usk' for Username + SSH Key.", |
||
| 465 | p.errors, |
||
| 466 | ) |
||
| 467 | |||
| 468 | @patch('ospd_openvas.db.KbDB') |
||
| 469 | def test_missing_type_for_ssh_credentials(self, mock_kb): |
||
| 470 | w = DummyDaemon() |
||
| 471 | |||
| 472 | creds = { |
||
| 473 | 'ssh': { |
||
| 474 | 'port': '22', |
||
| 475 | 'username': 'username', |
||
| 476 | 'password': 'pass', |
||
| 477 | }, |
||
| 478 | } |
||
| 479 | |||
| 480 | w.scan_collection.get_credentials = MagicMock(return_value=creds) |
||
| 481 | |||
| 482 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 483 | p.scan_id = '456-789' |
||
| 484 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 485 | r = p.prepare_credentials_for_openvas() |
||
| 486 | |||
| 487 | self.assertFalse(r) |
||
| 488 | self.assertIn( |
||
| 489 | "Missing Credential Type for SSH." |
||
| 490 | + " Use 'up' for Username + Password" |
||
| 491 | + " or 'usk' for Username + SSH Key.", |
||
| 492 | p.errors, |
||
| 493 | ) |
||
| 494 | |||
| 495 | View Code Duplication | @patch('ospd_openvas.db.KbDB') |
|
|
|
|||
| 496 | def test_snmp_no_priv_alg_but_pw_credentials(self, mock_kb): |
||
| 497 | w = DummyDaemon() |
||
| 498 | |||
| 499 | creds = { |
||
| 500 | 'snmp': { |
||
| 501 | 'type': 'snmp', |
||
| 502 | 'username': 'username', |
||
| 503 | 'password': 'pass', |
||
| 504 | 'community': 'some comunity', |
||
| 505 | 'auth_algorithm': 'sha1', |
||
| 506 | 'privacy_password': 'privacy pass', |
||
| 507 | }, |
||
| 508 | } |
||
| 509 | |||
| 510 | w.scan_collection.get_credentials = MagicMock(return_value=creds) |
||
| 511 | |||
| 512 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 513 | p.scan_id = '456-789' |
||
| 514 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 515 | r = p.prepare_credentials_for_openvas() |
||
| 516 | |||
| 517 | self.assertFalse(r) |
||
| 518 | self.assertIn( |
||
| 519 | "When no privacy algorithm is used, the privacy" |
||
| 520 | + " password also has to be empty.", |
||
| 521 | p.errors, |
||
| 522 | ) |
||
| 523 | |||
| 524 | View Code Duplication | @patch('ospd_openvas.db.KbDB') |
|
| 525 | def test_snmp_unknown_priv_alg_credentials(self, mock_kb): |
||
| 526 | w = DummyDaemon() |
||
| 527 | |||
| 528 | creds = { |
||
| 529 | 'snmp': { |
||
| 530 | 'type': 'snmp', |
||
| 531 | 'username': 'username', |
||
| 532 | 'password': 'pass', |
||
| 533 | 'community': 'some comunity', |
||
| 534 | 'auth_algorithm': 'sha1', |
||
| 535 | 'privacy_password': 'privacy pass', |
||
| 536 | 'privacy_algorithm': 'das', |
||
| 537 | }, |
||
| 538 | } |
||
| 539 | |||
| 540 | w.scan_collection.get_credentials = MagicMock(return_value=creds) |
||
| 541 | |||
| 542 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 543 | p.scan_id = '456-789' |
||
| 544 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 545 | r = p.prepare_credentials_for_openvas() |
||
| 546 | |||
| 547 | self.assertFalse(r) |
||
| 548 | self.assertIn( |
||
| 549 | "Unknows privacy algorithm used: " |
||
| 550 | + "das" |
||
| 551 | + ". Use 'aes', 'des' or '' (none).", |
||
| 552 | p.errors, |
||
| 553 | ) |
||
| 554 | |||
| 555 | @patch('ospd_openvas.db.KbDB') |
||
| 556 | def test_snmp_missing_auth_alg_credentials(self, mock_kb): |
||
| 557 | w = DummyDaemon() |
||
| 558 | |||
| 559 | creds = { |
||
| 560 | 'snmp': { |
||
| 561 | 'type': 'snmp', |
||
| 562 | 'username': 'username', |
||
| 563 | 'password': 'pass', |
||
| 564 | 'community': 'some comunity', |
||
| 565 | }, |
||
| 566 | } |
||
| 567 | |||
| 568 | w.scan_collection.get_credentials = MagicMock(return_value=creds) |
||
| 569 | |||
| 570 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 571 | p.scan_id = '456-789' |
||
| 572 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 573 | r = p.prepare_credentials_for_openvas() |
||
| 574 | |||
| 575 | self.assertFalse(r) |
||
| 576 | self.assertIn( |
||
| 577 | "Missing authentification algorithm for SNMP." |
||
| 578 | + " Use 'md5' or 'sha1'.", |
||
| 579 | p.errors, |
||
| 580 | ) |
||
| 581 | |||
| 582 | View Code Duplication | @patch('ospd_openvas.db.KbDB') |
|
| 583 | def test_snmp_unknown_auth_alg_credentials(self, mock_kb): |
||
| 584 | w = DummyDaemon() |
||
| 585 | |||
| 586 | creds = { |
||
| 587 | 'snmp': { |
||
| 588 | 'type': 'snmp', |
||
| 589 | 'username': 'username', |
||
| 590 | 'password': 'pass', |
||
| 591 | 'community': 'some comunity', |
||
| 592 | 'auth_algorithm': 'sha2', |
||
| 593 | }, |
||
| 594 | } |
||
| 595 | |||
| 596 | w.scan_collection.get_credentials = MagicMock(return_value=creds) |
||
| 597 | |||
| 598 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 599 | p.scan_id = '456-789' |
||
| 600 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 601 | r = p.prepare_credentials_for_openvas() |
||
| 602 | |||
| 603 | self.assertFalse(r) |
||
| 604 | self.assertIn( |
||
| 605 | "Unknown authentification algorithm: " |
||
| 606 | + "sha2" |
||
| 607 | + ". Use 'md5' or 'sha1'.", |
||
| 608 | p.errors, |
||
| 609 | ) |
||
| 610 | |||
| 611 | @patch('ospd_openvas.db.KbDB') |
||
| 612 | def test_set_credentials_empty(self, mock_kb): |
||
| 613 | w = DummyDaemon() |
||
| 614 | |||
| 615 | creds = {} |
||
| 616 | |||
| 617 | w.scan_collection.get_credentials = MagicMock(return_value=creds) |
||
| 618 | |||
| 619 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 620 | p.scan_id = '456-789' |
||
| 621 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 622 | r = p.prepare_credentials_for_openvas() |
||
| 623 | |||
| 624 | self.assertTrue(r) |
||
| 625 | |||
| 626 | @patch('ospd_openvas.db.KbDB') |
||
| 627 | def test_set_host_options(self, mock_kb): |
||
| 628 | w = DummyDaemon() |
||
| 629 | |||
| 630 | exc = '192.168.0.1' |
||
| 631 | |||
| 632 | w.scan_collection.get_exclude_hosts = MagicMock(return_value=exc) |
||
| 633 | |||
| 634 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 635 | p.scan_id = '456-789' |
||
| 636 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 637 | p.prepare_host_options_for_openvas() |
||
| 638 | |||
| 639 | p.kbdb.add_scan_preferences.assert_called_with( |
||
| 640 | p.scan_id, |
||
| 641 | ['exclude_hosts|||192.168.0.1'], |
||
| 642 | ) |
||
| 643 | |||
| 644 | @patch('ospd_openvas.db.KbDB') |
||
| 645 | def test_set_host_options_none(self, mock_kb): |
||
| 646 | w = DummyDaemon() |
||
| 647 | |||
| 648 | exc = '' |
||
| 649 | |||
| 650 | w.scan_collection.get_exclude_hosts = MagicMock(return_value=exc) |
||
| 651 | |||
| 652 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 653 | p.scan_id = '456-789' |
||
| 654 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 655 | p.prepare_host_options_for_openvas() |
||
| 656 | |||
| 657 | p.kbdb.add_scan_preferences.assert_not_called() |
||
| 658 | |||
| 659 | @patch('ospd_openvas.db.KbDB') |
||
| 660 | def test_set_scan_params(self, mock_kb): |
||
| 661 | w = DummyDaemon() |
||
| 662 | |||
| 663 | OSPD_PARAMS_MOCK = { |
||
| 664 | 'drop_privileges': { |
||
| 665 | 'type': 'boolean', |
||
| 666 | 'name': 'drop_privileges', |
||
| 667 | 'default': 0, |
||
| 668 | 'mandatory': 1, |
||
| 669 | 'description': '', |
||
| 670 | }, |
||
| 671 | } |
||
| 672 | |||
| 673 | opt = {'drop_privileges': 1} |
||
| 674 | |||
| 675 | w.scan_collection.get_options = MagicMock(return_value=opt) |
||
| 676 | |||
| 677 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 678 | p.scan_id = '456-789' |
||
| 679 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 680 | p.prepare_scan_params_for_openvas(OSPD_PARAMS_MOCK) |
||
| 681 | |||
| 682 | p.kbdb.add_scan_preferences.assert_called_with( |
||
| 683 | p.scan_id, ['drop_privileges|||yes'] |
||
| 684 | ) |
||
| 685 | |||
| 686 | @patch('ospd_openvas.db.KbDB') |
||
| 687 | def test_set_reverse_lookup_opt(self, mock_kb): |
||
| 688 | w = DummyDaemon() |
||
| 689 | |||
| 690 | t_opt = {'reverse_lookup_only': 1} |
||
| 691 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 692 | |||
| 693 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 694 | p.scan_id = '456-789' |
||
| 695 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 696 | p.prepare_reverse_lookup_opt_for_openvas() |
||
| 697 | |||
| 698 | p.kbdb.add_scan_preferences.assert_called_with( |
||
| 699 | p.scan_id, |
||
| 700 | [ |
||
| 701 | 'reverse_lookup_only|||yes', |
||
| 702 | 'reverse_lookup_unify|||no', |
||
| 703 | ], |
||
| 704 | ) |
||
| 705 | |||
| 706 | @patch('ospd_openvas.db.KbDB') |
||
| 707 | def test_set_boreas_alive_test_with_settings(self, mock_kb): |
||
| 708 | # No Boreas config setting (BOREAS_SETTING_NAME) set |
||
| 709 | w = DummyDaemon() |
||
| 710 | ov_setting = {'not_the_correct_setting': 1} |
||
| 711 | t_opt = {} |
||
| 712 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 713 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 714 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 715 | p.scan_id = '456-789' |
||
| 716 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 717 | p.prepare_boreas_alive_test() |
||
| 718 | |||
| 719 | p.kbdb.add_scan_preferences.assert_not_called() |
||
| 720 | |||
| 721 | # Boreas config setting set but invalid alive_test. |
||
| 722 | w = DummyDaemon() |
||
| 723 | t_opt = {'alive_test': "error"} |
||
| 724 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 725 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 726 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 727 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 728 | p.scan_id = '456-789' |
||
| 729 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 730 | p.prepare_boreas_alive_test() |
||
| 731 | |||
| 732 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||2'])] |
||
| 733 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 734 | |||
| 735 | # ALIVE_TEST_TCP_SYN_SERVICE as alive test. |
||
| 736 | w = DummyDaemon() |
||
| 737 | t_opt = {'alive_test': AliveTest.ALIVE_TEST_TCP_SYN_SERVICE} |
||
| 738 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 739 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 740 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 741 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 742 | p.scan_id = '456-789' |
||
| 743 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 744 | p.prepare_boreas_alive_test() |
||
| 745 | |||
| 746 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||16'])] |
||
| 747 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 748 | |||
| 749 | # ICMP was chosen as alive test. |
||
| 750 | w = DummyDaemon() |
||
| 751 | t_opt = {'alive_test': AliveTest.ALIVE_TEST_ICMP} |
||
| 752 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 753 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 754 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 755 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 756 | p.scan_id = '456-789' |
||
| 757 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 758 | p.prepare_boreas_alive_test() |
||
| 759 | |||
| 760 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||2'])] |
||
| 761 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 762 | |||
| 763 | # "Scan Config Default" as alive_test. |
||
| 764 | w = DummyDaemon() |
||
| 765 | t_opt = {'alive_test': AliveTest.ALIVE_TEST_SCAN_CONFIG_DEFAULT} |
||
| 766 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 767 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 768 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 769 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 770 | p.scan_id = '456-789' |
||
| 771 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 772 | p.prepare_boreas_alive_test() |
||
| 773 | |||
| 774 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||2'])] |
||
| 775 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 776 | |||
| 777 | # TCP-SYN alive test and dedicated port list for alive scan provided. |
||
| 778 | w = DummyDaemon() |
||
| 779 | t_opt = { |
||
| 780 | 'alive_test_ports': "80,137", |
||
| 781 | 'alive_test': AliveTest.ALIVE_TEST_TCP_SYN_SERVICE, |
||
| 782 | } |
||
| 783 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 784 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 785 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 786 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 787 | p.scan_id = '456-789' |
||
| 788 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 789 | p.prepare_boreas_alive_test() |
||
| 790 | |||
| 791 | calls = [ |
||
| 792 | call(p.scan_id, [BOREAS_ALIVE_TEST + '|||16']), |
||
| 793 | call(p.scan_id, [BOREAS_ALIVE_TEST_PORTS + '|||80,137']), |
||
| 794 | ] |
||
| 795 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 796 | |||
| 797 | @patch('ospd_openvas.db.KbDB') |
||
| 798 | def test_set_boreas_alive_test_not_as_enum(self, mock_kb): |
||
| 799 | # No Boreas config setting (BOREAS_SETTING_NAME) set |
||
| 800 | w = DummyDaemon() |
||
| 801 | ov_setting = {'not_the_correct_setting': 1} |
||
| 802 | t_opt = {} |
||
| 803 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 804 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 805 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 806 | p.scan_id = '456-789' |
||
| 807 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 808 | p.prepare_boreas_alive_test() |
||
| 809 | |||
| 810 | p.kbdb.add_scan_preferences.assert_not_called() |
||
| 811 | |||
| 812 | # Boreas config setting set but invalid alive_test. |
||
| 813 | w = DummyDaemon() |
||
| 814 | t_opt = {'alive_test_methods': "1", 'arp': '-1'} |
||
| 815 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 816 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 817 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 818 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 819 | p.scan_id = '456-789' |
||
| 820 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 821 | p.prepare_boreas_alive_test() |
||
| 822 | |||
| 823 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||2'])] |
||
| 824 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 825 | |||
| 826 | # ICMP was chosen as alive test. |
||
| 827 | w = DummyDaemon() |
||
| 828 | t_opt = {'alive_test_methods': "1", 'icmp': '1'} |
||
| 829 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 830 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 831 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 832 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 833 | p.scan_id = '456-789' |
||
| 834 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 835 | p.prepare_boreas_alive_test() |
||
| 836 | |||
| 837 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||2'])] |
||
| 838 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 839 | |||
| 840 | # tcp_syn as alive test. |
||
| 841 | w = DummyDaemon() |
||
| 842 | t_opt = {'alive_test_methods': "1", 'tcp_syn': '1'} |
||
| 843 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 844 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 845 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 846 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 847 | p.scan_id = '456-789' |
||
| 848 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 849 | p.prepare_boreas_alive_test() |
||
| 850 | |||
| 851 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||16'])] |
||
| 852 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 853 | |||
| 854 | # tcp_ack as alive test. |
||
| 855 | w = DummyDaemon() |
||
| 856 | t_opt = {'alive_test_methods': "1", 'tcp_ack': '1'} |
||
| 857 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 858 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 859 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 860 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 861 | p.scan_id = '456-789' |
||
| 862 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 863 | p.prepare_boreas_alive_test() |
||
| 864 | |||
| 865 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||1'])] |
||
| 866 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 867 | |||
| 868 | # arp as alive test. |
||
| 869 | w = DummyDaemon() |
||
| 870 | t_opt = {'alive_test_methods': "1", 'arp': '1'} |
||
| 871 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 872 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 873 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 874 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 875 | p.scan_id = '456-789' |
||
| 876 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 877 | p.prepare_boreas_alive_test() |
||
| 878 | |||
| 879 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||4'])] |
||
| 880 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 881 | |||
| 882 | # arp as alive test. |
||
| 883 | w = DummyDaemon() |
||
| 884 | t_opt = {'alive_test_methods': "1", 'consider_alive': '1'} |
||
| 885 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 886 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 887 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 888 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 889 | p.scan_id = '456-789' |
||
| 890 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 891 | p.prepare_boreas_alive_test() |
||
| 892 | |||
| 893 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||8'])] |
||
| 894 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 895 | |||
| 896 | # all alive test methods |
||
| 897 | w = DummyDaemon() |
||
| 898 | t_opt = { |
||
| 899 | 'alive_test_methods': "1", |
||
| 900 | 'icmp': '1', |
||
| 901 | 'tcp_ack': '1', |
||
| 902 | 'tcp_syn': '1', |
||
| 903 | 'arp': '1', |
||
| 904 | 'consider_alive': '1', |
||
| 905 | } |
||
| 906 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 907 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 908 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 909 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 910 | p.scan_id = '456-789' |
||
| 911 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 912 | p.prepare_boreas_alive_test() |
||
| 913 | |||
| 914 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||31'])] |
||
| 915 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 916 | |||
| 917 | # TCP-SYN alive test and dedicated port list for alive scan provided. |
||
| 918 | w = DummyDaemon() |
||
| 919 | t_opt = { |
||
| 920 | 'alive_test_ports': "80,137", |
||
| 921 | 'alive_test_methods': "1", |
||
| 922 | 'tcp_syn': '1', |
||
| 923 | } |
||
| 924 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 925 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 926 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 927 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 928 | p.scan_id = '456-789' |
||
| 929 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 930 | p.prepare_boreas_alive_test() |
||
| 931 | |||
| 932 | calls = [ |
||
| 933 | call(p.scan_id, [BOREAS_ALIVE_TEST + '|||16']), |
||
| 934 | call(p.scan_id, [BOREAS_ALIVE_TEST_PORTS + '|||80,137']), |
||
| 935 | ] |
||
| 936 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 937 | |||
| 938 | @patch('ospd_openvas.db.KbDB') |
||
| 939 | def test_set_boreas_alive_test_enum_has_precedence(self, mock_kb): |
||
| 940 | w = DummyDaemon() |
||
| 941 | t_opt = { |
||
| 942 | 'alive_test_methods': "1", |
||
| 943 | 'consider_alive': '1', |
||
| 944 | 'alive_test': AliveTest.ALIVE_TEST_ICMP, |
||
| 945 | } |
||
| 946 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 947 | ov_setting = {BOREAS_SETTING_NAME: 1} |
||
| 948 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 949 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 950 | p.scan_id = '456-789' |
||
| 951 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 952 | p.prepare_boreas_alive_test() |
||
| 953 | |||
| 954 | # has icmp and not consider_alive |
||
| 955 | calls = [call(p.scan_id, [BOREAS_ALIVE_TEST + '|||2'])] |
||
| 956 | p.kbdb.add_scan_preferences.assert_has_calls(calls) |
||
| 957 | |||
| 958 | @patch('ospd_openvas.db.KbDB') |
||
| 959 | def test_set_boreas_alive_test_without_settings(self, mock_kb): |
||
| 960 | w = DummyDaemon() |
||
| 961 | t_opt = {'alive_test': 16} |
||
| 962 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 963 | ov_setting = {} |
||
| 964 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 965 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 966 | p.scan_id = '456-789' |
||
| 967 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 968 | p.prepare_boreas_alive_test() |
||
| 969 | |||
| 970 | p.kbdb.add_scan_preferences.assert_not_called() |
||
| 971 | |||
| 972 | View Code Duplication | @patch('ospd_openvas.db.KbDB') |
|
| 973 | def test_set_alive_no_setting(self, mock_kb): |
||
| 974 | w = DummyDaemon() |
||
| 975 | |||
| 976 | t_opt = {} |
||
| 977 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 978 | |||
| 979 | ov_setting = {} |
||
| 980 | |||
| 981 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 982 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 983 | p.scan_id = '456-789' |
||
| 984 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 985 | p.prepare_alive_test_option_for_openvas() |
||
| 986 | |||
| 987 | p.kbdb.add_scan_preferences.assert_not_called() |
||
| 988 | |||
| 989 | View Code Duplication | @patch('ospd_openvas.db.KbDB') |
|
| 990 | def test_set_alive_no_invalid_alive_test(self, mock_kb): |
||
| 991 | w = DummyDaemon() |
||
| 992 | |||
| 993 | t_opt = {'alive_test': -1} |
||
| 994 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 995 | |||
| 996 | ov_setting = {'some_setting': 1} |
||
| 997 | |||
| 998 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 999 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 1000 | p._nvts_params = {} |
||
| 1001 | p.scan_id = '456-789' |
||
| 1002 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 1003 | p.prepare_alive_test_option_for_openvas() |
||
| 1004 | |||
| 1005 | p.kbdb.add_scan_preferences.assert_not_called() |
||
| 1006 | |||
| 1007 | @patch('ospd_openvas.db.KbDB') |
||
| 1008 | def test_set_alive_no_invalid_alive_test_no_enum(self, mock_kb): |
||
| 1009 | w = DummyDaemon() |
||
| 1010 | |||
| 1011 | t_opt = {'alive_test_methods': '1', 'icmp': '-1'} |
||
| 1012 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 1013 | |||
| 1014 | ov_setting = {'some_setting': 1} |
||
| 1015 | |||
| 1016 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 1017 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 1018 | p._nvts_params = {} |
||
| 1019 | p.scan_id = '456-789' |
||
| 1020 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 1021 | p.prepare_alive_test_option_for_openvas() |
||
| 1022 | |||
| 1023 | p.kbdb.add_scan_preferences.assert_not_called() |
||
| 1024 | |||
| 1025 | @patch('ospd_openvas.db.KbDB') |
||
| 1026 | def test_set_alive_pinghost(self, mock_kb): |
||
| 1027 | w = DummyDaemon() |
||
| 1028 | |||
| 1029 | alive_test_out = [ |
||
| 1030 | "1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping|||no", |
||
| 1031 | "1.3.6.1.4.1.25623.1.0.100315:2:checkbox:TCP ping tries also TCP-SYN ping|||no", |
||
| 1032 | "1.3.6.1.4.1.25623.1.0.100315:7:checkbox:TCP ping tries only TCP-SYN ping|||no", |
||
| 1033 | "1.3.6.1.4.1.25623.1.0.100315:3:checkbox:Do an ICMP ping|||yes", |
||
| 1034 | "1.3.6.1.4.1.25623.1.0.100315:4:checkbox:Use ARP|||no", |
||
| 1035 | "1.3.6.1.4.1.25623.1.0.100315:5:checkbox:Mark unrechable Hosts as dead (not scanning)|||yes", |
||
| 1036 | ] |
||
| 1037 | |||
| 1038 | t_opt = {'alive_test': 2} |
||
| 1039 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 1040 | |||
| 1041 | ov_setting = {'some_setting': 1} |
||
| 1042 | |||
| 1043 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 1044 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 1045 | p._nvts_params = {} |
||
| 1046 | p.scan_id = '456-789' |
||
| 1047 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 1048 | p.prepare_alive_test_option_for_openvas() |
||
| 1049 | |||
| 1050 | for key, value in p._nvts_params.items(): |
||
| 1051 | self.assertTrue( |
||
| 1052 | "{0}|||{1}".format(key, value) in alive_test_out |
||
| 1053 | ) |
||
| 1054 | |||
| 1055 | @patch('ospd_openvas.db.KbDB') |
||
| 1056 | def test_prepare_alive_test_not_supplied_as_enum(self, mock_kb): |
||
| 1057 | w = DummyDaemon() |
||
| 1058 | |||
| 1059 | alive_test_out = { |
||
| 1060 | "1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping": "no", |
||
| 1061 | "1.3.6.1.4.1.25623.1.0.100315:2:checkbox:TCP ping tries also TCP-SYN ping": "no", |
||
| 1062 | "1.3.6.1.4.1.25623.1.0.100315:7:checkbox:TCP ping tries only TCP-SYN ping": "no", |
||
| 1063 | "1.3.6.1.4.1.25623.1.0.100315:3:checkbox:Do an ICMP ping": "yes", |
||
| 1064 | "1.3.6.1.4.1.25623.1.0.100315:4:checkbox:Use ARP": "no", |
||
| 1065 | "1.3.6.1.4.1.25623.1.0.100315:5:checkbox:Mark unrechable Hosts as dead (not scanning)": "yes", |
||
| 1066 | } |
||
| 1067 | |||
| 1068 | t_opt = {'alive_test_methods': '1', 'icmp': '1'} |
||
| 1069 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 1070 | |||
| 1071 | ov_setting = {'some_setting': 1} |
||
| 1072 | |||
| 1073 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 1074 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 1075 | p._nvts_params = {} |
||
| 1076 | p.scan_id = '456-789' |
||
| 1077 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 1078 | p.prepare_alive_test_option_for_openvas() |
||
| 1079 | |||
| 1080 | self.assertEqual(p._nvts_params, alive_test_out) |
||
| 1081 | |||
| 1082 | @patch('ospd_openvas.db.KbDB') |
||
| 1083 | def test_prepare_alive_test_no_enum_no_alive_test(self, mock_kb): |
||
| 1084 | w = DummyDaemon() |
||
| 1085 | |||
| 1086 | t_opt = {'alive_test_methods': '1', 'icmp': '0'} |
||
| 1087 | w.scan_collection.get_target_options = MagicMock(return_value=t_opt) |
||
| 1088 | |||
| 1089 | ov_setting = {'some_setting': 1} |
||
| 1090 | |||
| 1091 | with patch.object(Openvas, 'get_settings', return_value=ov_setting): |
||
| 1092 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 1093 | p._nvts_params = {} |
||
| 1094 | p.scan_id = '456-789' |
||
| 1095 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 1096 | p.prepare_alive_test_option_for_openvas() |
||
| 1097 | |||
| 1098 | p.kbdb.add_scan_preferences.assert_not_called() |
||
| 1099 | |||
| 1100 | def test_alive_test_methods_to_bit_field(self): |
||
| 1101 | |||
| 1102 | self.assertEqual( |
||
| 1103 | AliveTest.ALIVE_TEST_TCP_ACK_SERVICE, |
||
| 1104 | alive_test_methods_to_bit_field( |
||
| 1105 | icmp=False, |
||
| 1106 | tcp_ack=True, |
||
| 1107 | tcp_syn=False, |
||
| 1108 | arp=False, |
||
| 1109 | consider_alive=False, |
||
| 1110 | ), |
||
| 1111 | ) |
||
| 1112 | |||
| 1113 | self.assertEqual( |
||
| 1114 | AliveTest.ALIVE_TEST_ICMP, |
||
| 1115 | alive_test_methods_to_bit_field( |
||
| 1116 | icmp=True, |
||
| 1117 | tcp_ack=False, |
||
| 1118 | tcp_syn=False, |
||
| 1119 | arp=False, |
||
| 1120 | consider_alive=False, |
||
| 1121 | ), |
||
| 1122 | ) |
||
| 1123 | |||
| 1124 | self.assertEqual( |
||
| 1125 | AliveTest.ALIVE_TEST_ARP, |
||
| 1126 | alive_test_methods_to_bit_field( |
||
| 1127 | icmp=False, |
||
| 1128 | tcp_ack=False, |
||
| 1129 | tcp_syn=False, |
||
| 1130 | arp=True, |
||
| 1131 | consider_alive=False, |
||
| 1132 | ), |
||
| 1133 | ) |
||
| 1134 | |||
| 1135 | self.assertEqual( |
||
| 1136 | AliveTest.ALIVE_TEST_CONSIDER_ALIVE, |
||
| 1137 | alive_test_methods_to_bit_field( |
||
| 1138 | icmp=False, |
||
| 1139 | tcp_ack=False, |
||
| 1140 | tcp_syn=False, |
||
| 1141 | arp=False, |
||
| 1142 | consider_alive=True, |
||
| 1143 | ), |
||
| 1144 | ) |
||
| 1145 | |||
| 1146 | self.assertEqual( |
||
| 1147 | AliveTest.ALIVE_TEST_TCP_SYN_SERVICE, |
||
| 1148 | alive_test_methods_to_bit_field( |
||
| 1149 | icmp=False, |
||
| 1150 | tcp_ack=False, |
||
| 1151 | tcp_syn=True, |
||
| 1152 | arp=False, |
||
| 1153 | consider_alive=False, |
||
| 1154 | ), |
||
| 1155 | ) |
||
| 1156 | |||
| 1157 | all_alive_test_methods = ( |
||
| 1158 | AliveTest.ALIVE_TEST_SCAN_CONFIG_DEFAULT |
||
| 1159 | | AliveTest.ALIVE_TEST_TCP_ACK_SERVICE |
||
| 1160 | | AliveTest.ALIVE_TEST_ICMP |
||
| 1161 | | AliveTest.ALIVE_TEST_ARP |
||
| 1162 | | AliveTest.ALIVE_TEST_CONSIDER_ALIVE |
||
| 1163 | | AliveTest.ALIVE_TEST_TCP_SYN_SERVICE |
||
| 1164 | ) |
||
| 1165 | self.assertEqual( |
||
| 1166 | all_alive_test_methods, |
||
| 1167 | alive_test_methods_to_bit_field( |
||
| 1168 | icmp=True, |
||
| 1169 | tcp_ack=True, |
||
| 1170 | tcp_syn=True, |
||
| 1171 | arp=True, |
||
| 1172 | consider_alive=True, |
||
| 1173 | ), |
||
| 1174 | ) |
||
| 1175 | |||
| 1176 | @patch('ospd_openvas.db.KbDB') |
||
| 1177 | def test_prepare_nvt_prefs(self, mock_kb): |
||
| 1178 | w = DummyDaemon() |
||
| 1179 | |||
| 1180 | alive_test_out = [ |
||
| 1181 | "1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping|||no" |
||
| 1182 | ] |
||
| 1183 | |||
| 1184 | p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None) |
||
| 1185 | p._nvts_params = { |
||
| 1186 | "1.3.6.1.4.1.25623.1.0.100315:1:checkbox:Do a TCP ping": "no" |
||
| 1187 | } |
||
| 1188 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 1189 | p.prepare_nvt_preferences() |
||
| 1190 | |||
| 1191 | p.kbdb.add_scan_preferences.assert_called_with( |
||
| 1192 | p.scan_id, |
||
| 1193 | alive_test_out, |
||
| 1194 | ) |
||
| 1195 | |||
| 1196 | @patch('ospd_openvas.db.KbDB') |
||
| 1197 | def test_prepare_nvt_prefs_no_prefs(self, mock_kb): |
||
| 1198 | w = DummyDaemon() |
||
| 1199 | |||
| 1200 | p = PreferenceHandler('456-789', mock_kb, w.scan_collection, None) |
||
| 1201 | p._nvts_params = {} |
||
| 1202 | p.kbdb.add_scan_preferences = MagicMock() |
||
| 1203 | p.prepare_nvt_preferences() |
||
| 1204 | |||
| 1205 | p.kbdb.add_scan_preferences.assert_not_called() |
||
| 1206 |