Skip to content
This repository has been archived by the owner on Nov 29, 2021. It is now read-only.

Commit

Permalink
Fix get_count_total(). Accept -1 value set by the server.
Browse files Browse the repository at this point in the history
The value set by the server has priority over the value
calculated from the original target list by ospd.
As ospd is not intelligent enough to check the amount of valid
hosts, check for duplicated or invalid hosts, consider a negative
value set for the server, in case it detects an invalid target string
or a different amount than the orignal amount in the target list.
  • Loading branch information
jjnicola committed Jan 29, 2021
1 parent 98ebda9 commit ce2f8fd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Fix target_to_ipv4_short(). [#338](https://github.com/greenbone/ospd/pull/338)
- Fix malformed target. [#341](https://github.com/greenbone/ospd/pull/341)
- Initialize end_time with create_scan. [#354](https://github.com/greenbone/ospd/pull/354)
- Fix get_count_total(). Accept -1 value set by the server. [#355](https://github.com/greenbone/ospd/pull/355)

[20.8.2]: https://github.com/greenbone/ospd/compare/v20.8.1...ospd-20.08

Expand Down
15 changes: 13 additions & 2 deletions ospd/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,18 @@ def get_count_total(self, scan_id: str) -> int:
""" Get a scan's total host count. """

count_total = self.scans_table[scan_id]['count_total']
if not count_total:

# The value set by the server has priority over the value
# calculated from the original target list by ospd.
# As ospd is not intelligent enough to check the amount of valid
# hosts, check for duplicated or invalid hosts, consider a negative
# value set for the server, in case it detects an invalid target string
# or a different amount than the orignal amount in the target list.
if count_total == -1:
count_total = 0
# If the server does not set the total host count
# ospd set the amount of host from the original host list.
elif not count_total:
count_total = self.get_host_count(scan_id)
self.update_count_total(scan_id, count_total)

Expand Down Expand Up @@ -458,7 +469,7 @@ def calculate_target_progress(self, scan_id: str) -> int:
)
except ZeroDivisionError:
# Consider the case in which all hosts are dead or excluded
LOGGER.debug('%s: All hosts dead or excluded.')
LOGGER.debug('%s: All hosts dead or excluded.', scan_id)
t_prog = ScanProgress.FINISHED.value

return t_prog
Expand Down
30 changes: 30 additions & 0 deletions tests/test_scan_and_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,36 @@ def test_set_scan_total_hosts(self):
count = self.daemon.scan_collection.get_count_total(scan_id)
self.assertEqual(count, 3)

self.daemon.set_scan_total_hosts(scan_id, 3)
count = self.daemon.scan_collection.get_count_total(scan_id)
self.assertEqual(count, 3)

def test_set_scan_total_hosts_invalid_target(self):

fs = FakeStream()
self.daemon.handle_command(
'<start_scan parallel="2">'
'<scanner_params />'
'<targets><target>'
'<hosts>localhost1, localhost2, localhost3, localhost4</hosts>'
'<ports>22</ports>'
'</target></targets>'
'</start_scan>',
fs,
)
self.daemon.start_queued_scans()

response = fs.get_response()
scan_id = response.findtext('id')

count = self.daemon.scan_collection.get_count_total(scan_id)
self.assertEqual(count, 4)

# The total host is set by the server as -1, because invalid target
self.daemon.set_scan_total_hosts(scan_id, -1)
count = self.daemon.scan_collection.get_count_total(scan_id)
self.assertEqual(count, 0)

def test_get_scan_progress_xml(self):

fs = FakeStream()
Expand Down

0 comments on commit ce2f8fd

Please sign in to comment.