monkey/chaos_monkey/network/httpfinger.py

45 lines
1.6 KiB
Python
Raw Normal View History

from network import HostFinger
2016-08-25 20:46:29 +08:00
from model.host import VictimHost
class HTTPFinger(HostFinger):
2016-08-25 20:45:47 +08:00
"""
Goal is to recognise HTTP servers, where what we currently care about is apache.
2016-08-25 20:45:47 +08:00
"""
def __init__(self):
self._config = __import__('config').WormConfiguration
2016-08-25 20:45:47 +08:00
self.HTTP = [(port, str(port)) for port in self._config.HTTP_PORTS]
@staticmethod
def _banner_match(service, host, banner):
pass
def get_host_fingerprint(self, host):
assert isinstance(host, VictimHost)
2016-08-25 21:32:16 +08:00
from requests import head
2016-08-25 20:45:47 +08:00
from requests.exceptions import Timeout, ConnectionError
from contextlib import closing
2016-08-25 20:43:59 +08:00
for port in self.HTTP:
# check both http and https
2016-08-25 20:45:47 +08:00
http = "http://" + host.ip_addr + ":" + port[1]
https = "https://" + host.ip_addr + ":" + port[1]
# try http, we don't optimise for 443
for url in (https, http): # start with https and downgrade
2016-08-25 20:43:59 +08:00
try:
2016-08-25 21:32:16 +08:00
with closing(head(url, verify=False, timeout=1)) as req:
2016-08-25 20:43:59 +08:00
server = req.headers.get('Server')
ssl = True if 'https://' in url else False
host.services['tcp-' + port[1]] = {}
host.services['tcp-' + port[1]]['name'] = 'http'
host.services['tcp-' + port[1]]['data'] = (server,ssl)
2016-08-25 20:45:47 +08:00
break # https will be the same on the same port
2016-08-25 20:43:59 +08:00
except Timeout:
pass
2016-08-25 20:45:47 +08:00
except ConnectionError: # Someone doesn't like us
2016-08-25 20:43:59 +08:00
pass
2016-08-25 20:45:47 +08:00
return True