From 3a8a1f446b4056ff07cf6cf055b04a6a12282adf Mon Sep 17 00:00:00 2001 From: Shay Nehmad Date: Sun, 6 Oct 2019 19:55:59 +0300 Subject: [PATCH] Added logs to monkey island startup with hashes of executeables, for ease of deployment + debugging Should ease the "wait which monkey am I running" issue a bit --- monkey/monkey_island/cc/main.py | 14 +++++++--- .../cc/resources/monkey_download.py | 26 ++++++++++++++++++- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/monkey/monkey_island/cc/main.py b/monkey/monkey_island/cc/main.py index 8c817e935..5545e79f5 100644 --- a/monkey/monkey_island/cc/main.py +++ b/monkey/monkey_island/cc/main.py @@ -25,6 +25,7 @@ from monkey_island.cc.services.reporting.exporter_init import populate_exporter_ from monkey_island.cc.utils import local_ip_addresses from monkey_island.cc.environment.environment import env from monkey_island.cc.database import is_db_server_up, get_db_version +from monkey_island.cc.resources.monkey_download import MonkeyDownload def main(): @@ -49,12 +50,19 @@ def main(): ssl_options={'certfile': os.environ.get('SERVER_CRT', crt_path), 'keyfile': os.environ.get('SERVER_KEY', key_path)}) http_server.listen(env.get_island_port()) - logger.info( - 'Monkey Island Server is running on https://{}:{}'.format(local_ip_addresses()[0], env.get_island_port())) - + log_init_info() IOLoop.instance().start() +def log_init_info(): + logger.info( + 'Monkey Island Server is running. Listening on the following URLs: {}'.format( + ", ".join(["https://{}:{}".format(x, env.get_island_port()) for x in local_ip_addresses()]) + ) + ) + MonkeyDownload.log_executable_hashes() + + def wait_for_mongo_db_server(mongo_url): while not is_db_server_up(mongo_url): logger.info('Waiting for MongoDB server on {0}'.format(mongo_url)) diff --git a/monkey/monkey_island/cc/resources/monkey_download.py b/monkey/monkey_island/cc/resources/monkey_download.py index 78a092a26..3456997aa 100644 --- a/monkey/monkey_island/cc/resources/monkey_download.py +++ b/monkey/monkey_island/cc/resources/monkey_download.py @@ -1,3 +1,4 @@ +import hashlib import json import logging import os @@ -83,9 +84,32 @@ class MonkeyDownload(flask_restful.Resource): if result: # change resulting from new base path - real_path = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", 'binaries', result['filename']) + executable_filename = result['filename'] + real_path = MonkeyDownload.get_executable_full_path(executable_filename) if os.path.isfile(real_path): result['size'] = os.path.getsize(real_path) return result return {} + + @staticmethod + def get_executable_full_path(executable_filename): + real_path = os.path.join(MONKEY_ISLAND_ABS_PATH, "cc", 'binaries', executable_filename) + return real_path + + @staticmethod + def log_executable_hashes(): + """ + Logs all the hashes of the monkey executables for debugging ease (can check what Monkey version you have etc.). + """ + filenames = set([x['filename'] for x in MONKEY_DOWNLOADS]) + for filename in filenames: + filepath = MonkeyDownload.get_executable_full_path(filename) + if os.path.isfile(filepath): + with open(filepath, 'rb') as monkey_exec_file: + logger.debug("{} (SHA-512): {}".format( + filepath, + hashlib.sha512(monkey_exec_file.read()).hexdigest()) + ) + else: + logger.debug("No monkey executable for {}.".format(filepath))