1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2014-2023 Greenbone AG
#
# SPDX-License-Identifier: AGPL-3.0-or-later
from hashlib import sha256
from unittest import TestCase
from unittest.mock import MagicMock, patch
from tests.dummydaemon import DummyDaemon
from tests.helper import assert_called_once
from ospd_openvas.vthelper import VtHelper
class VtHelperTestCase(TestCase):
def test_get_single_vt(self):
dummy = DummyDaemon()
vthelper = VtHelper(dummy.nvti)
res = vthelper.get_single_vt("1.3.6.1.4.1.25623.1.0.100061")
assert_called_once(dummy.nvti.get_nvt_metadata)
self.assertEqual("Mantis Detection", res.get('name'))
@patch('ospd_openvas.daemon.NVTICache')
def test_handle_null_severities(self, nvticlass: MagicMock):
nvti = nvticlass.return_value
nvti.notus = None
nvti.get_nvt_metadata.return_value = {
'category': '3',
'creation_date': '1237458156',
'excluded_keys': 'Settings/disable_cgi_scanning',
'family': 'Product detection',
'filename': 'mantis_detect.nasl',
'last_modification': '1533906565',
'name': 'Mantis Detection',
'qod_type': 'remote_banner',
'required_ports': 'Services/www, 80',
'solution': 'some solution',
'solution_type': 'WillNotFix',
'solution_method': 'DebianAPTUpgrade',
'impact': 'some impact',
'insight': 'some insight',
'summary': 'some summary',
'affected': 'some affection',
'timeout': '0',
'vt_params': {
'1': {
'id': '1',
'default': '',
'description': 'Description',
'name': 'Data length :',
'type': 'entry',
},
'2': {
'id': '2',
'default': 'no',
'description': 'Description',
'name': (
'Do not randomize the order in which ports are'
' scanned'
),
'type': 'checkbox',
},
},
'refs': {
'bid': [''],
'cve': [''],
'xref': ['URL:http://www.mantisbt.org/'],
},
}
vthelper = VtHelper(nvti)
res = vthelper.get_single_vt("1.3.6.1.4.1.25623.1.0.100061")
assert not res
def test_calculate_vts_collection_hash_no_params(self):
dummy = DummyDaemon()
vthelper = VtHelper(dummy.nvti)
hash_out = vthelper.calculate_vts_collection_hash()
vt_hash_str = (
'1.3.6.1.4.1.25623.1.0.10006115339065651Data '
+ 'length :2Do not randomize the order in which '
+ 'ports are scannedno'
)
vt_hash = sha256()
vt_hash.update(vt_hash_str.encode('utf-8'))
hash_test = vt_hash.hexdigest()
self.assertEqual(hash_test, hash_out)
def test_get_vt_iterator(self):
dummy = DummyDaemon()
vthelper = VtHelper(dummy.nvti)
vt = ["1.3.6.1.4.1.25623.1.0.100061"]
for key, _ in vthelper.get_vt_iterator():
self.assertIn(key, vt)
def test_get_vt_iterator_with_filter(self):
dummy = DummyDaemon()
vthelper = VtHelper(dummy.nvti)
vt = ["1.3.6.1.4.1.25623.1.0.100061"]
vtout = dummy.VTS["1.3.6.1.4.1.25623.1.0.100061"]
for key, vt_dict in vthelper.get_vt_iterator(vt_selection=vt):
self.assertIn(key, vt)
for key2 in vtout:
self.assertIn(key2, vt_dict)
def test_get_vt_iterator_with_filter_no_vt(self):
dummy = DummyDaemon()
vthelper = VtHelper(dummy.nvti)
dummy.nvti.get_nvt_metadata.return_value = None
vt = ["1.3.6.1.4.1.25623.1.0.100065"]
for _, values in vthelper.get_vt_iterator(vt_selection=vt):
self.assertIs(values, None)
def test_get_single_vt_severity_cvssv3(self):
dummy = DummyDaemon()
dummy.nvti.get_nvt_metadata.return_value = {
'category': '3',
'creation_date': '1237458156',
'cvss_base_vector': 'AV:N/AC:L/Au:N/C:N/I:N/A:N',
'severity_vector': 'CVSS:3.0/AV:L/AC:H/PR:H/UI:R/S:U/C:N/I:L/A:L',
'severity_date': '1237458156',
'severity_origin': 'Greenbone',
'excluded_keys': 'Settings/disable_cgi_scanning',
'family': 'Product detection',
'filename': 'mantis_detect.nasl',
'last_modification': '1533906565',
'name': 'Mantis Detection',
'qod_type': 'remote_banner',
'required_ports': 'Services/www, 80',
'solution': 'some solution',
'solution_type': 'WillNotFix',
'solution_method': 'DebianAPTUpgrade',
'impact': 'some impact',
'insight': 'some insight',
'summary': 'some summary',
'affected': 'some affection',
'timeout': '0',
'vt_params': {
'1': {
'id': '1',
'default': '',
'description': 'Description',
'name': 'Data length :',
'type': 'entry',
},
'2': {
'id': '2',
'default': 'no',
'description': 'Description',
'name': (
'Do not randomize the order in which ports are'
' scanned'
),
'type': 'checkbox',
},
},
'refs': {
'bid': [''],
'cve': [''],
'xref': ['URL:http://www.mantisbt.org/'],
},
}
vthelper = VtHelper(dummy.nvti)
res = vthelper.get_single_vt("1.3.6.1.4.1.25623.1.0.100061")
assert_called_once(dummy.nvti.get_nvt_metadata)
severities = res.get('severities')
self.assertEqual(
"CVSS:3.0/AV:L/AC:H/PR:H/UI:R/S:U/C:N/I:L/A:L",
severities.get('severity_base_vector'),
)
self.assertEqual("cvss_base_v3", severities.get('severity_type'))
self.assertEqual("Greenbone", severities.get('severity_origin'))
self.assertEqual("1237458156", severities.get('severity_date'))
def test_get_single_vt_severity_cvssv2(self):
dummy = DummyDaemon()
dummy.nvti.get_nvt_metadata.return_value = {
'category': '3',
'creation_date': '1237458156',
'cvss_base_vector': 'AV:N/AC:L/Au:N/C:N/I:N/A:N',
'excluded_keys': 'Settings/disable_cgi_scanning',
'family': 'Product detection',
'filename': 'mantis_detect.nasl',
'last_modification': '1533906565',
'name': 'Mantis Detection',
'qod_type': 'remote_banner',
'required_ports': 'Services/www, 80',
'solution': 'some solution',
'solution_type': 'WillNotFix',
'solution_method': 'DebianAPTUpgrade',
'impact': 'some impact',
'insight': 'some insight',
'summary': 'some summary',
'affected': 'some affection',
'timeout': '0',
'vt_params': {
'1': {
'id': '1',
'default': '',
'description': 'Description',
'name': 'Data length :',
'type': 'entry',
},
'2': {
'id': '2',
'default': 'no',
'description': 'Description',
'name': (
'Do not randomize the order in which ports are'
' scanned'
),
'type': 'checkbox',
},
},
'refs': {
'bid': [''],
'cve': [''],
'xref': ['URL:http://www.mantisbt.org/'],
},
}
vthelper = VtHelper(dummy.nvti)
res = vthelper.get_single_vt("1.3.6.1.4.1.25623.1.0.100061")
assert_called_once(dummy.nvti.get_nvt_metadata)
severities = res.get('severities')
self.assertEqual(
"AV:N/AC:L/Au:N/C:N/I:N/A:N",
severities.get('severity_base_vector'),
)
self.assertEqual("cvss_base_v2", severities.get('severity_type'))
self.assertEqual(None, severities.get('severity_origin'))
self.assertEqual("1237458156", severities.get('severity_date'))
def test_get_severity_score_v2(self):
dummy = DummyDaemon()
vthelper = VtHelper(dummy.nvti)
vtaux = {
'severities': {
'severity_type': 'cvss_base_v2',
'severity_base_vector': 'AV:N/AC:L/Au:N/C:P/I:N/A:N',
}
}
self.assertEqual(vthelper.get_severity_score(vtaux), 5.0)
def test_get_severity_score_v3(self):
dummy = DummyDaemon()
vthelper = VtHelper(dummy.nvti)
vtaux = {
'severities': {
'severity_type': 'cvss_base_v3',
'severity_base_vector': (
'CVSS:3.0/AV:L/AC:H/PR:H/UI:R/S:U/C:N/I:L/A:L'
),
}
}
self.assertEqual(vthelper.get_severity_score(vtaux), 2.9)
|