monkey/chaos_monkey/network/sshfinger.py

57 lines
1.7 KiB
Python
Raw Normal View History

import re
import sys
import socket
import struct
import string
import logging
from network import HostFinger
import socket
import select
from network.tools import check_port_tcp
from model.host import VictimHost
SSH_PORT = 22
2015-10-12 22:42:54 +08:00
SSH_SERVICE_DEFAULT = 'tcp-22'
SSH_REGEX = 'SSH-\d\.\d-OpenSSH'
2015-09-30 20:05:30 +08:00
TIMEOUT = 10
BANNER_READ = 1024
LINUX_DIST_SSH = ['ubuntu', 'debian']
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'
for dist in LINUX_DIST_SSH:
if banner.lower().find(dist) != -1:
host.os['type'] = 'linux'
os_version = banner.split(' ').pop().strip()
if not host.os.has_key('version'):
host.os['version'] = os_version
else:
2015-10-12 22:42:54 +08:00
host.services[service]['os-version'] = os_version
break
def get_host_fingerprint(self, host):
assert isinstance(host, VictimHost)
2015-10-12 22:42:54 +08:00
for name,data in host.services.items():
banner = data.get('banner', '')
if self._banner_regex.search(banner):
2015-10-12 22:42:54 +08:00
self._banner_match(name, host, banner)
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] = {}
if banner:
2015-10-12 22:42:54 +08:00
host.services[SSH_SERVICE_DEFAULT]['banner'] = banner
if self._banner_regex.search(banner):
2015-10-12 22:42:54 +08:00
self._banner_match(SSH_SERVICE_DEFAULT, host, banner)
return True
return False