2015-09-29 22:55:54 +08:00
|
|
|
import re
|
2015-11-30 16:56:20 +08:00
|
|
|
from chaos_monkey.network import HostFinger
|
|
|
|
from chaos_monkey.network.tools import check_port_tcp
|
|
|
|
from chaos_monkey.model.host import VictimHost
|
2015-09-29 22:55:54 +08:00
|
|
|
|
|
|
|
SSH_PORT = 22
|
2015-10-12 22:42:54 +08:00
|
|
|
SSH_SERVICE_DEFAULT = 'tcp-22'
|
2015-09-29 22:55:54 +08:00
|
|
|
SSH_REGEX = 'SSH-\d\.\d-OpenSSH'
|
2015-09-30 20:05:30 +08:00
|
|
|
TIMEOUT = 10
|
2015-09-29 22:55:54 +08:00
|
|
|
BANNER_READ = 1024
|
|
|
|
LINUX_DIST_SSH = ['ubuntu', 'debian']
|
|
|
|
|
2015-11-30 16:56:20 +08:00
|
|
|
|
2015-09-29 22:55:54 +08:00
|
|
|
class SSHFinger(HostFinger):
|
|
|
|
def __init__(self):
|
|
|
|
self._config = __import__('config').WormConfiguration
|
|
|
|
self._banner_regex = re.compile(SSH_REGEX, re.IGNORECASE)
|
|
|
|
|
2015-10-12 22:42:54 +08:00
|
|
|
def _banner_match(self, service, host, banner):
|
|
|
|
host.services[service]['name'] = 'ssh'
|
2015-09-29 22:55:54 +08:00
|
|
|
for dist in LINUX_DIST_SSH:
|
|
|
|
if banner.lower().find(dist) != -1:
|
|
|
|
host.os['type'] = 'linux'
|
|
|
|
os_version = banner.split(' ').pop().strip()
|
2015-11-30 16:56:20 +08:00
|
|
|
if 'version' not in host.os:
|
2015-09-29 22:55:54 +08:00
|
|
|
host.os['version'] = os_version
|
|
|
|
else:
|
2015-10-12 22:42:54 +08:00
|
|
|
host.services[service]['os-version'] = os_version
|
2015-09-29 22:55:54 +08:00
|
|
|
break
|
|
|
|
|
|
|
|
def get_host_fingerprint(self, host):
|
|
|
|
assert isinstance(host, VictimHost)
|
|
|
|
|
2015-11-30 16:56:20 +08:00
|
|
|
for name, data in host.services.items():
|
2015-10-12 22:42:54 +08:00
|
|
|
banner = data.get('banner', '')
|
2015-09-29 22:55:54 +08:00
|
|
|
if self._banner_regex.search(banner):
|
2015-10-12 22:42:54 +08:00
|
|
|
self._banner_match(name, host, banner)
|
2015-09-29 22:55:54 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
is_open, banner = check_port_tcp(host.ip_addr, SSH_PORT, TIMEOUT, True)
|
|
|
|
|
|
|
|
if is_open:
|
2015-10-12 22:42:54 +08:00
|
|
|
host.services[SSH_SERVICE_DEFAULT] = {}
|
2015-09-29 22:55:54 +08:00
|
|
|
|
|
|
|
if banner:
|
2015-10-12 22:42:54 +08:00
|
|
|
host.services[SSH_SERVICE_DEFAULT]['banner'] = banner
|
2015-09-29 22:55:54 +08:00
|
|
|
if self._banner_regex.search(banner):
|
2015-10-12 22:42:54 +08:00
|
|
|
self._banner_match(SSH_SERVICE_DEFAULT, host, banner)
|
2015-09-29 22:55:54 +08:00
|
|
|
return True
|
|
|
|
|
2015-11-30 16:56:20 +08:00
|
|
|
return False
|