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

Fix target list in get_scan_response. #39

Merged
merged 2 commits into from
Sep 3, 2018
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
14 changes: 11 additions & 3 deletions ospd/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ def ids_iterator(self):

return iter(self.scans_table.keys())

def create_scan(self, scan_id='', targets='', options=dict(), vts=''):
def create_scan(self, scan_id='', targets='', target_str=None,
options=dict(), vts=''):
""" Creates a new scan with provided scan information. """

if self.data_manager is None:
Expand All @@ -125,6 +126,7 @@ def create_scan(self, scan_id='', targets='', options=dict(), vts=''):
scan_info['results'] = list()
scan_info['progress'] = 0
scan_info['targets'] = targets
scan_info['legacy_target'] = target_str
scan_info['vts'] = vts
scan_info['options'] = options
scan_info['start_time'] = int(time.time())
Expand Down Expand Up @@ -162,8 +164,14 @@ def get_end_time(self, scan_id):

def get_target(self, scan_id):
""" Get a scan's target list. """

return self.scans_table[scan_id]['targets']
if self.scans_table[scan_id]['legacy_target']:
return self.scans_table[scan_id]['legacy_target']

target_list=[]
for item in self.scans_table[scan_id]['targets']:
target_list.append(item[0])
separ = ','
return separ.join(target_list)

def get_ports(self, scan_id, target):
""" Get a scan's ports list. If a target is specified
Expand Down
6 changes: 3 additions & 3 deletions ospd/ospd.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ def handle_start_scan_command(self, scan_et):
scan_func = self.start_scan
scan_params = self.process_scan_params(params)

scan_id = self.create_scan(scan_id, scan_targets, scan_params, vts)
scan_id = self.create_scan(scan_id, scan_targets, target_str, scan_params, vts)
scan_process = multiprocessing.Process(target=scan_func,
args=(scan_id, scan_targets))
self.scan_processes[scan_id] = scan_process
Expand Down Expand Up @@ -1164,15 +1164,15 @@ def run(self, address, port, unix_path):
sock.shutdown(socket.SHUT_RDWR)
sock.close()

def create_scan(self, scan_id, targets, options, vts):
def create_scan(self, scan_id, targets, target_str, options, vts):
""" Creates a new scan.

@target: Target to scan.
@options: Miscellaneous scan options.

@return: New scan's ID.
"""
return self.scan_collection.create_scan(scan_id, targets, options, vts)
return self.scan_collection.create_scan(scan_id, targets, target_str, options, vts)

def get_scan_options(self, scan_id):
""" Gives a scan's list of options. """
Expand Down
8 changes: 4 additions & 4 deletions tests/testSSHDaemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def testNoParamiko(self):
def testRunCommand(self):
ospd_ssh.paramiko = fakeparamiko
daemon = OSPDaemonSimpleSSH('cert', 'key', 'ca')
scanid = daemon.create_scan(None, [['host.example.com', '80, 443', ''],],
scanid = daemon.create_scan(None, [['host.example.com', '80, 443', ''],], '',
dict(port=5, ssh_timeout=15,
username_password='dummy:pw'), '')
res = daemon.run_command(scanid, 'host.example.com', 'cat /etc/passwd')
Expand All @@ -58,7 +58,7 @@ def testRunCommand(self):
def testRunCommandLegacyCredential(self):
ospd_ssh.paramiko = fakeparamiko
daemon = OSPDaemonSimpleSSH('cert', 'key', 'ca')
scanid = daemon.create_scan(None, [['host.example.com', '80, 443', ''],],
scanid = daemon.create_scan(None, [['host.example.com', '80, 443', ''],], '',
dict(port=5, ssh_timeout=15,
username='dummy', password='pw'), '')
res = daemon.run_command(scanid, 'host.example.com', 'cat /etc/passwd')
Expand All @@ -78,7 +78,7 @@ def testRunCommandNewCredential(self):
'username': 'smbuser'}}

scanid = daemon.create_scan(None,
[['host.example.com', '80, 443', cred_dict],],
[['host.example.com', '80, 443', cred_dict],], '',
dict(port=5, ssh_timeout=15), '')
res = daemon.run_command(scanid, 'host.example.com', 'cat /etc/passwd')
self.assertTrue(isinstance(res, list))
Expand All @@ -87,7 +87,7 @@ def testRunCommandNewCredential(self):
def testRunCommandNoCredential(self):
ospd_ssh.paramiko = fakeparamiko
daemon = OSPDaemonSimpleSSH('cert', 'key', 'ca')
scanid = daemon.create_scan(None, [['host.example.com', '80, 443', ''],],
scanid = daemon.create_scan(None, [['host.example.com', '80, 443', ''],], '',
dict(port=5, ssh_timeout=15), '')
self.assertRaises(ValueError, daemon.run_command,
scanid, 'host.example.com', 'cat /etc/passwd' )
37 changes: 37 additions & 0 deletions tests/testScanAndResult.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ def testScanMultiTarget(self):
print(ET.tostring(response))
self.assertEqual(response.get('status'), '200')


def testMultiTargetWithCredentials(self):
daemon = DummyWrapper([])
response = secET.fromstring(
Expand Down Expand Up @@ -295,3 +296,39 @@ def testMultiTargetWithCredentials(self):
scan_id = response.findtext('id')
response = daemon.get_scan_credentials(scan_id, "192.168.0.0/24")
self.assertEqual(response, cred_dict)

def testScanGetTarget(self):
daemon = DummyWrapper([])
response = secET.fromstring(
daemon.handle_command('<start_scan>' +
'<scanner_params /><vts><vt id="1.2.3.4" />' +
'</vts>' +
'<targets><target>' +
'<hosts>localhosts</hosts>' +
'<ports>80,443</ports>' +
'</target>' +
'<target><hosts>192.168.0.0/24</hosts>' +
'<ports>22</ports></target></targets>' +
'</start_scan>'))
scan_id = response.findtext('id')
response = secET.fromstring(
daemon.handle_command('<get_scans scan_id="%s"/>' % scan_id))
print(ET.tostring(response))
scan_res = response.find('scan')
self.assertEqual(scan_res.get('target'), 'localhosts,192.168.0.0/24')

def testScanGetLegacyTarget(self):
daemon = DummyWrapper([])

response = secET.fromstring(
daemon.handle_command('<start_scan target="localhosts,192.168.0.0/24" ports="22">' +
'<scanner_params /><vts><vt id="1.2.3.4" />' +
'</vts>' +
'</start_scan>'))
scan_id = response.findtext('id')
response = secET.fromstring(
daemon.handle_command('<get_scans scan_id="%s"/>' % scan_id))
print(ET.tostring(response))
scan_res = response.find('scan')
self.assertEqual(scan_res.get('target'), 'localhosts,192.168.0.0/24')