Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for alive test settings. #182

Merged
merged 8 commits into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Added
- Add solution method to solution of vt object. [#131](https://github.com/greenbone/ospd-openvas/pull/131)
- Add typing to daemon.py, nvticache.py and db.py. [#161](https://github.com/greenbone/ospd-openvas/pull/161)[#162](https://github.com/greenbone/ospd-openvas/pull/162)[#163](https://github.com/greenbone/ospd-openvas/pull/163)
- Add support for alive test settings. [#182](https://github.com/greenbone/ospd-openvas/pull/182)

### Changed
- Less strict checks for the nvti cache version
Expand Down
118 changes: 64 additions & 54 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

143 changes: 134 additions & 9 deletions ospd_openvas/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import binascii
import copy

from enum import IntEnum
from typing import Optional, Dict, List, Tuple
from datetime import datetime
from base64 import b64decode
Expand Down Expand Up @@ -229,6 +230,17 @@
OID_SMB_AUTH = "1.3.6.1.4.1.25623.1.0.90023"
OID_ESXI_AUTH = "1.3.6.1.4.1.25623.1.0.105058"
OID_SNMP_AUTH = "1.3.6.1.4.1.25623.1.0.105076"
OID_PING_HOST = "1.3.6.1.4.1.25623.1.0.100315"


class AliveTest(IntEnum):
""" Alive Tests. """

ALIVE_TEST_TCP_ACK_SERVICE = 1
ALIVE_TEST_ICMP = 2
ALIVE_TEST_ARP = 4
ALIVE_TEST_CONSIDER_ALIVE = 8
ALIVE_TEST_TCP_SYN_SERVICE = 16
bjoernricks marked this conversation as resolved.
Show resolved Hide resolved


def _from_bool_to_str(value: int) -> str:
Expand Down Expand Up @@ -1195,10 +1207,10 @@ def check_param_type(vt_param_value: str, param_type: str) -> Optional[int]:

return 1

def process_vts(self, vts: List) -> Tuple[list, list]:
def process_vts(self, vts: List) -> Tuple[list, dict]:
""" Add single VTs and their parameters. """
vts_list = []
vts_params = []
vts_params = {}
vtgroups = vts.pop('vt_groups')

if vtgroups:
Expand Down Expand Up @@ -1238,13 +1250,12 @@ def process_vts(self, vts: List) -> Tuple[list, list]:
continue
if type_aux == 'checkbox':
vt_param_value = _from_bool_to_str(int(vt_param_value))
param = [
vts_params[
"{0}:{1}:{2}:{3}".format(
vtid, vt_param_id, param_type, param_name
),
str(vt_param_value),
]
vts_params.append(param)
)
] = str(vt_param_value)

return vts_list, vts_params

@staticmethod
Expand Down Expand Up @@ -1362,6 +1373,110 @@ def build_credentials_as_prefs(credentials: Dict) -> List[str]:

return cred_prefs_list

@staticmethod
def build_alive_test_opt_as_prefs(target_options: Dict) -> List[str]:
""" Parse the target options dictionary.
@param credentials: Dictionary with the target options.

@return A list with the target options in string format to be
added to the redis KB.
"""
target_opt_prefs_list = []
if target_options and target_options.get('alive_test'):
try:
alive_test = int(target_options.get('alive_test'))
except ValueError:
logger.debug(
'Alive test settings not applied. '
'Invalid alive test value %s',
target_options.get('alive_test'),
)
return target_opt_prefs_list

if alive_test < 1 or alive_test > 31:
return target_opt_prefs_list

if (
alive_test & AliveTest.ALIVE_TEST_TCP_ACK_SERVICE
or alive_test & AliveTest.ALIVE_TEST_TCP_SYN_SERVICE
):
value = "yes"
else:
value = "no"
target_opt_prefs_list.append(
OID_PING_HOST
+ ':1:checkbox:'
+ 'Do a TCP ping|||'
+ '{0}'.format(value)
)

if (
alive_test & AliveTest.ALIVE_TEST_TCP_SYN_SERVICE
and alive_test & AliveTest.ALIVE_TEST_TCP_ACK_SERVICE
):
value = "yes"
else:
value = "no"
target_opt_prefs_list.append(
OID_PING_HOST
+ ':2:checkbox:'
+ 'TCP ping tries also TCP-SYN ping|||'
+ '{0}'.format(value)
)

if (alive_test & AliveTest.ALIVE_TEST_TCP_SYN_SERVICE) and not (
alive_test & AliveTest.ALIVE_TEST_TCP_ACK_SERVICE
):
value = "yes"
else:
value = "no"
target_opt_prefs_list.append(
OID_PING_HOST
+ ':7:checkbox:'
+ 'TCP ping tries only TCP-SYN ping|||'
+ '{0}'.format(value)
)

if alive_test & AliveTest.ALIVE_TEST_ICMP:
value = "yes"
else:
value = "no"
target_opt_prefs_list.append(
OID_PING_HOST
+ ':3:checkbox:'
+ 'Do an ICMP ping|||'
+ '{0}'.format(value)
)

if alive_test & AliveTest.ALIVE_TEST_ARP:
value = "yes"
else:
value = "no"
target_opt_prefs_list.append(
OID_PING_HOST
+ ':4:checkbox:'
+ 'Use ARP|||'
+ '{0}'.format(value)
)

if alive_test & AliveTest.ALIVE_TEST_CONSIDER_ALIVE:
value = "no"
else:
value = "yes"
target_opt_prefs_list.append(
OID_PING_HOST
+ ':5:checkbox:'
+ 'Mark unrechable Hosts as dead (not scanning)|||'
+ '{0}'.format(value)
)

# Also select a method, otherwise Ping Host logs a warning.
if alive_test == AliveTest.ALIVE_TEST_CONSIDER_ALIVE:
target_opt_prefs_list.append(
OID_PING_HOST + ':1:checkbox:' + 'Do a TCP ping|||yes'
)
return target_opt_prefs_list

def exec_scan(self, scan_id: str, target: str):
""" Starts the OpenVAS scanner for scan_id scan. """
if self.pending_feed:
Expand Down Expand Up @@ -1484,9 +1599,19 @@ def exec_scan(self, scan_id: str, target: str):
self.openvas_db.add_single_item(
'internal/%s/scanprefs' % openvas_scan_id, [plugin_list]
)
# Set alive test option. Overwrite the scan config settings.
target_options = self.get_scan_target_options(scan_id, target)
if target_options:
alive_test_opt = self.build_alive_test_opt_as_prefs(
target_options
)
for elem in alive_test_opt:
key, val = elem.split("|||", 2)
nvts_params[key] = val

# Add nvts parameters
for elem in nvts_params:
item = '%s|||%s' % (elem[0], elem[1])
for key, val in nvts_params.items():
item = '%s|||%s' % (key, val)
self.openvas_db.add_single_item(
'internal/%s/scanprefs' % openvas_scan_id, [item]
)
Expand Down
Loading