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

Port list validation #411

Merged
merged 3 commits into from
Apr 27, 2021
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## [21.10] (unreleased)

### Added
- Validate port list to be sent to openvas. [#411](https://github.com/greenbone/ospd-openvas/pull/411)

### Changed
### Removed
### Fixed
Expand Down
2 changes: 1 addition & 1 deletion ospd_openvas/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ def exec_scan(self, scan_id: str):

if not scan_prefs.prepare_ports_for_openvas():
self.add_scan_error(
scan_id, name='', host='', value='No port list defined.'
scan_id, name='', host='', value='Invalid port list.'
)
do_not_launch = True

Expand Down
4 changes: 4 additions & 0 deletions ospd_openvas/preferencehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

from ospd.scan import ScanCollection
from ospd.ospd import BASE_SCANNER_PARAMS
from ospd.network import valid_port_list
from ospd_openvas.openvas import Openvas
from ospd_openvas.db import KbDB
from ospd_openvas.nvticache import NVTICache
Expand Down Expand Up @@ -528,6 +529,9 @@ def prepare_ports_for_openvas(self) -> str:
"""Get the port list from the scan collection and store the list
in the kb."""
ports = self.scan_collection.get_ports(self.scan_id)
if not valid_port_list(ports):
return False

port_range = 'port_range|||%s' % ports
self.kbdb.add_scan_preferences(self.scan_id, [port_range])

Expand Down
8 changes: 4 additions & 4 deletions poetry.lock

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

11 changes: 11 additions & 0 deletions tests/test_preferencehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,17 @@ def test_set_ports(self, mock_kb):
['port_range|||80,443'],
)

@patch('ospd_openvas.db.KbDB')
def test_set_ports_invalid(self, mock_kb):
w = DummyDaemon()

w.scan_collection.get_ports = MagicMock(return_value='2,-9,4')

p = PreferenceHandler('1234-1234', mock_kb, w.scan_collection, None)
p.scan_id = '456-789'
p.kbdb.add_scan_preferences = MagicMock()
self.assertFalse(p.prepare_ports_for_openvas())

@patch('ospd_openvas.db.KbDB')
def test_set_main_kbindex(self, mock_kb):
w = DummyDaemon()
Expand Down