|
1
|
|
|
import unittest |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
from ospd import ospd_ssh |
|
4
|
|
|
from ospd.ospd_ssh import OSPDaemonSimpleSSH |
|
5
|
|
|
|
|
6
|
|
|
class FakeFile(object): |
|
|
|
|
|
|
7
|
|
|
def __init__(self, content): |
|
8
|
|
|
self.content = content |
|
9
|
|
|
def readlines(self): |
|
|
|
|
|
|
10
|
|
|
return self.content.split('\n') |
|
11
|
|
|
|
|
12
|
|
|
commands = None |
|
|
|
|
|
|
13
|
|
|
class FakeSSHClient(object): |
|
|
|
|
|
|
14
|
|
|
def __init__(self): |
|
15
|
|
|
global commands |
|
|
|
|
|
|
16
|
|
|
commands = [] |
|
17
|
|
|
def set_missing_host_key_policy(self, policy): |
|
|
|
|
|
|
18
|
|
|
pass |
|
19
|
|
|
def connect(self, **kwargs): |
|
|
|
|
|
|
20
|
|
|
pass |
|
21
|
|
|
def exec_command(self, cmd): |
|
|
|
|
|
|
22
|
|
|
commands.append(cmd) |
|
23
|
|
|
return None, FakeFile(''), None |
|
24
|
|
|
def close(self): |
|
|
|
|
|
|
25
|
|
|
pass |
|
26
|
|
|
|
|
27
|
|
|
class FakeExceptions(object): |
|
|
|
|
|
|
28
|
|
|
AuthenticationException = None |
|
29
|
|
|
|
|
30
|
|
|
class fakeparamiko(object): |
|
|
|
|
|
|
31
|
|
|
@staticmethod |
|
32
|
|
|
def SSHClient(*args): |
|
|
|
|
|
|
33
|
|
|
return FakeSSHClient(*args) |
|
34
|
|
|
|
|
35
|
|
|
@staticmethod |
|
36
|
|
|
def AutoAddPolicy(): |
|
|
|
|
|
|
37
|
|
|
pass |
|
38
|
|
|
|
|
39
|
|
|
ssh_exception = FakeExceptions |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
class TestSSH(unittest.TestCase): |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
def testNoParamiko(self): |
|
|
|
|
|
|
45
|
|
|
ospd_ssh.paramiko = None |
|
46
|
|
|
self.assertRaises(ImportError, OSPDaemonSimpleSSH, 'cert', 'key', 'ca') |
|
47
|
|
|
|
|
48
|
|
|
def testRunCommand(self): |
|
|
|
|
|
|
49
|
|
|
ospd_ssh.paramiko = fakeparamiko |
|
50
|
|
|
daemon = OSPDaemonSimpleSSH('cert', 'key', 'ca') |
|
51
|
|
|
scanid = daemon.create_scan(None, [['host.example.com', '80, 443', ''],], |
|
52
|
|
|
dict(port=5, ssh_timeout=15, |
|
53
|
|
|
username_password='dummy:pw'), '') |
|
54
|
|
|
res = daemon.run_command(scanid, 'host.example.com', 'cat /etc/passwd') |
|
55
|
|
|
self.assertTrue(isinstance(res, list)) |
|
56
|
|
|
self.assertEqual(commands, ['cat /etc/passwd']) |
|
57
|
|
|
|
|
58
|
|
|
def testRunCommandLegacyCredential(self): |
|
|
|
|
|
|
59
|
|
|
ospd_ssh.paramiko = fakeparamiko |
|
60
|
|
|
daemon = OSPDaemonSimpleSSH('cert', 'key', 'ca') |
|
61
|
|
|
scanid = daemon.create_scan(None, [['host.example.com', '80, 443', ''],], |
|
62
|
|
|
dict(port=5, ssh_timeout=15, |
|
63
|
|
|
username='dummy', password='pw'), '') |
|
64
|
|
|
res = daemon.run_command(scanid, 'host.example.com', 'cat /etc/passwd') |
|
65
|
|
|
self.assertTrue(isinstance(res, list)) |
|
66
|
|
|
self.assertEqual(commands, ['cat /etc/passwd']) |
|
67
|
|
|
|
|
68
|
|
|
def testRunCommandNewCredential(self): |
|
|
|
|
|
|
69
|
|
|
ospd_ssh.paramiko = fakeparamiko |
|
70
|
|
|
daemon = OSPDaemonSimpleSSH('cert', 'key', 'ca') |
|
71
|
|
|
|
|
72
|
|
|
cred_dict = {'ssh': {'type': 'up', |
|
73
|
|
|
'password': 'mypass', |
|
74
|
|
|
'port': '22', |
|
75
|
|
|
'username': 'scanuser'}, |
|
76
|
|
|
'smb': {'type': 'up', |
|
77
|
|
|
'password': 'mypass', |
|
78
|
|
|
'username': 'smbuser'}} |
|
79
|
|
|
|
|
80
|
|
|
scanid = daemon.create_scan(None, |
|
81
|
|
|
[['host.example.com', '80, 443', cred_dict],], |
|
82
|
|
|
dict(port=5, ssh_timeout=15), '') |
|
83
|
|
|
res = daemon.run_command(scanid, 'host.example.com', 'cat /etc/passwd') |
|
84
|
|
|
self.assertTrue(isinstance(res, list)) |
|
85
|
|
|
self.assertEqual(commands, ['cat /etc/passwd']) |
|
86
|
|
|
|
|
87
|
|
|
def testRunCommandNoCredential(self): |
|
|
|
|
|
|
88
|
|
|
ospd_ssh.paramiko = fakeparamiko |
|
89
|
|
|
daemon = OSPDaemonSimpleSSH('cert', 'key', 'ca') |
|
90
|
|
|
scanid = daemon.create_scan(None, [['host.example.com', '80, 443', ''],], |
|
91
|
|
|
dict(port=5, ssh_timeout=15), '') |
|
92
|
|
|
self.assertRaises(ValueError, daemon.run_command, |
|
93
|
|
|
scanid, 'host.example.com', 'cat /etc/passwd' ) |
|
|
|
|
|
|
94
|
|
|
|
This check looks for invalid names for a range of different identifiers.
You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.
If your project includes a Pylint configuration file, the settings contained in that file take precedence.
To find out more about Pylint, please refer to their site.