From 0268fa833f0fa2453a97381ee262e34adafe2f96 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Tue, 19 Mar 2019 18:41:14 +0200 Subject: [PATCH 1/4] New directory for monkey and it's files --- monkey/infection_monkey/config.py | 3 +++ monkey/infection_monkey/monkey.py | 4 ++++ monkey/infection_monkey/utils.py | 23 +++++++++++++++++++ .../cc/services/config_schema.py | 14 ++++++++++- 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/monkey/infection_monkey/config.py b/monkey/infection_monkey/config.py index ff66ff167..5d6fba356 100644 --- a/monkey/infection_monkey/config.py +++ b/monkey/infection_monkey/config.py @@ -107,6 +107,9 @@ class Configuration(object): dropper_target_path_win_32 = r"C:\Windows\monkey32.exe" dropper_target_path_win_64 = r"C:\Windows\monkey64.exe" dropper_target_path_linux = '/tmp/monkey' + # Monkey dir paths + monkey_dir_linux = '/tmp/monkey_dir' + monkey_dir_windows = r'C:\Windows\temp\monkey_dir' ########################### # Kill file diff --git a/monkey/infection_monkey/monkey.py b/monkey/infection_monkey/monkey.py index 92913749e..fe7558714 100644 --- a/monkey/infection_monkey/monkey.py +++ b/monkey/infection_monkey/monkey.py @@ -76,6 +76,9 @@ class InfectionMonkey(object): LOG.info("Monkey couldn't find server. Going down.") return + # Create a dir for monkey files if there isn't one + utils.create_monkey_dir() + if WindowsUpgrader.should_upgrade(): self._upgrading_to_64 = True self._singleton.unlock() @@ -216,6 +219,7 @@ class InfectionMonkey(object): self._singleton.unlock() InfectionMonkey.self_delete() + utils.remove_monkey_dir() LOG.info("Monkey is shutting down") @staticmethod diff --git a/monkey/infection_monkey/utils.py b/monkey/infection_monkey/utils.py index 635f2360d..ccb6010c7 100644 --- a/monkey/infection_monkey/utils.py +++ b/monkey/infection_monkey/utils.py @@ -1,5 +1,6 @@ import os import sys +import shutil import struct from infection_monkey.config import WormConfiguration @@ -35,3 +36,25 @@ def utf_to_ascii(string): # Converts utf string to ascii. Safe to use even if string is already ascii. udata = string.decode("utf-8") return udata.encode("ascii", "ignore") + + +def create_monkey_dir(): + """ + Creates directory for monkey and related files + """ + if is_windows_os(): + if not os.path.exists(WormConfiguration.monkey_dir_windows): + os.mkdir(WormConfiguration.monkey_dir_windows) + else: + if not os.path.exists(WormConfiguration.monkey_log_path_linux): + os.mkdir(WormConfiguration.monkey_dir_linux) + + +def remove_monkey_dir(): + """ + Removes monkey's root directory + """ + if is_windows_os(): + shutil.rmtree(WormConfiguration.monkey_dir_windows, ignore_errors=True) + else: + shutil.rmtree(WormConfiguration.monkey_dir_linux, ignore_errors=True) diff --git a/monkey/monkey_island/cc/services/config_schema.py b/monkey/monkey_island/cc/services/config_schema.py index 8d99540bf..cbcc6ba0a 100644 --- a/monkey/monkey_island/cc/services/config_schema.py +++ b/monkey/monkey_island/cc/services/config_schema.py @@ -423,7 +423,19 @@ SCHEMA = { "type": "integer", "default": 60, "description": "Time to keep tunnel open before going down after last exploit (in seconds)" - } + }, + "monkey_dir_windows": { + "title": "Monkey's windows directory", + "type": "string", + "default": r"C:\Windows\temp\monkey_dir", + "description": "Directory containing all monkey files on windows" + }, + "monkey_dir_linux": { + "title": "Monkey's linux directory", + "type": "string", + "default": "/tmp/monkey_dir", + "description": "Directory containing all monkey files on linux" + }, } }, "classes": { From eb1cb9170487cf60e08dad25bcfb6758e131346b Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Wed, 20 Mar 2019 09:33:10 +0200 Subject: [PATCH 2/4] Removed monkey dir paths from island's configuration --- monkey/infection_monkey/config.py | 7 ++++--- monkey/monkey_island/cc/services/config_schema.py | 14 +------------- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/monkey/infection_monkey/config.py b/monkey/infection_monkey/config.py index 5d6fba356..723806388 100644 --- a/monkey/infection_monkey/config.py +++ b/monkey/infection_monkey/config.py @@ -107,9 +107,6 @@ class Configuration(object): dropper_target_path_win_32 = r"C:\Windows\monkey32.exe" dropper_target_path_win_64 = r"C:\Windows\monkey64.exe" dropper_target_path_linux = '/tmp/monkey' - # Monkey dir paths - monkey_dir_linux = '/tmp/monkey_dir' - monkey_dir_windows = r'C:\Windows\temp\monkey_dir' ########################### # Kill file @@ -164,6 +161,10 @@ class Configuration(object): keep_tunnel_open_time = 60 + # Monkey files directories + monkey_dir_linux = '/tmp/monkey_dir' + monkey_dir_windows = r'C:\Windows\Temp\monkey_dir' + ########################### # scanners config ########################### diff --git a/monkey/monkey_island/cc/services/config_schema.py b/monkey/monkey_island/cc/services/config_schema.py index cbcc6ba0a..8d99540bf 100644 --- a/monkey/monkey_island/cc/services/config_schema.py +++ b/monkey/monkey_island/cc/services/config_schema.py @@ -423,19 +423,7 @@ SCHEMA = { "type": "integer", "default": 60, "description": "Time to keep tunnel open before going down after last exploit (in seconds)" - }, - "monkey_dir_windows": { - "title": "Monkey's windows directory", - "type": "string", - "default": r"C:\Windows\temp\monkey_dir", - "description": "Directory containing all monkey files on windows" - }, - "monkey_dir_linux": { - "title": "Monkey's linux directory", - "type": "string", - "default": "/tmp/monkey_dir", - "description": "Directory containing all monkey files on linux" - }, + } } }, "classes": { From d61c080848d96ff8981041e9cd96e4acc58d7975 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Wed, 20 Mar 2019 14:02:53 +0200 Subject: [PATCH 3/4] Small refactor, values to island's config added. --- monkey/infection_monkey/monkey.py | 2 +- monkey/infection_monkey/utils.py | 16 ++++++++-------- .../monkey_island/cc/services/config_schema.py | 14 +++++++++++++- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/monkey/infection_monkey/monkey.py b/monkey/infection_monkey/monkey.py index fe7558714..f09425f25 100644 --- a/monkey/infection_monkey/monkey.py +++ b/monkey/infection_monkey/monkey.py @@ -219,7 +219,6 @@ class InfectionMonkey(object): self._singleton.unlock() InfectionMonkey.self_delete() - utils.remove_monkey_dir() LOG.info("Monkey is shutting down") @staticmethod @@ -234,6 +233,7 @@ class InfectionMonkey(object): if WormConfiguration.self_delete_in_cleanup \ and -1 == sys.executable.find('python'): try: + utils.remove_monkey_dir() if "win32" == sys.platform: from _subprocess import SW_HIDE, STARTF_USESHOWWINDOW, CREATE_NEW_CONSOLE startupinfo = subprocess.STARTUPINFO() diff --git a/monkey/infection_monkey/utils.py b/monkey/infection_monkey/utils.py index ccb6010c7..741d7c950 100644 --- a/monkey/infection_monkey/utils.py +++ b/monkey/infection_monkey/utils.py @@ -42,19 +42,19 @@ def create_monkey_dir(): """ Creates directory for monkey and related files """ - if is_windows_os(): - if not os.path.exists(WormConfiguration.monkey_dir_windows): - os.mkdir(WormConfiguration.monkey_dir_windows) - else: - if not os.path.exists(WormConfiguration.monkey_log_path_linux): - os.mkdir(WormConfiguration.monkey_dir_linux) + if not os.path.exists(get_monkey_dir_path()): + os.mkdir(get_monkey_dir_path()) def remove_monkey_dir(): """ Removes monkey's root directory """ + shutil.rmtree(get_monkey_dir_path(), ignore_errors=True) + + +def get_monkey_dir_path(): if is_windows_os(): - shutil.rmtree(WormConfiguration.monkey_dir_windows, ignore_errors=True) + return WormConfiguration.monkey_dir_windows else: - shutil.rmtree(WormConfiguration.monkey_dir_linux, ignore_errors=True) + return WormConfiguration.monkey_dir_linux diff --git a/monkey/monkey_island/cc/services/config_schema.py b/monkey/monkey_island/cc/services/config_schema.py index 8d99540bf..cbcc6ba0a 100644 --- a/monkey/monkey_island/cc/services/config_schema.py +++ b/monkey/monkey_island/cc/services/config_schema.py @@ -423,7 +423,19 @@ SCHEMA = { "type": "integer", "default": 60, "description": "Time to keep tunnel open before going down after last exploit (in seconds)" - } + }, + "monkey_dir_windows": { + "title": "Monkey's windows directory", + "type": "string", + "default": r"C:\Windows\temp\monkey_dir", + "description": "Directory containing all monkey files on windows" + }, + "monkey_dir_linux": { + "title": "Monkey's linux directory", + "type": "string", + "default": "/tmp/monkey_dir", + "description": "Directory containing all monkey files on linux" + }, } }, "classes": { From fecfd138a03e9b3aa73578b50578fd77f58eb847 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Mon, 25 Mar 2019 10:48:56 +0200 Subject: [PATCH 4/4] Directory removal moved to happen before self_delete call --- monkey/infection_monkey/monkey.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monkey/infection_monkey/monkey.py b/monkey/infection_monkey/monkey.py index f09425f25..4089a1c07 100644 --- a/monkey/infection_monkey/monkey.py +++ b/monkey/infection_monkey/monkey.py @@ -218,6 +218,7 @@ class InfectionMonkey(object): self.send_log() self._singleton.unlock() + utils.remove_monkey_dir() InfectionMonkey.self_delete() LOG.info("Monkey is shutting down") @@ -233,7 +234,6 @@ class InfectionMonkey(object): if WormConfiguration.self_delete_in_cleanup \ and -1 == sys.executable.find('python'): try: - utils.remove_monkey_dir() if "win32" == sys.platform: from _subprocess import SW_HIDE, STARTF_USESHOWWINDOW, CREATE_NEW_CONSOLE startupinfo = subprocess.STARTUPINFO()