diff --git a/monkey/infection_monkey/config.py b/monkey/infection_monkey/config.py index 60799e938..8feb3f3f7 100644 --- a/monkey/infection_monkey/config.py +++ b/monkey/infection_monkey/config.py @@ -67,15 +67,6 @@ class Configuration(object): return result - ########################### - # logging config - ########################### - - dropper_log_directory_linux = "/tmp/" - dropper_log_directory_windows = "%temp%\\" - monkey_log_directory_linux = "/tmp/" - monkey_log_directory_windows = "%temp%\\" - ########################### # dropper config ########################### diff --git a/monkey/infection_monkey/example.conf b/monkey/infection_monkey/example.conf index 2aaafa728..ebadf1429 100644 --- a/monkey/infection_monkey/example.conf +++ b/monkey/infection_monkey/example.conf @@ -16,8 +16,6 @@ "dropper_date_reference_path_windows": "%windir%\\system32\\kernel32.dll", "dropper_date_reference_path_linux": "/bin/sh", - "dropper_log_directory_linux": "/tmp/", - "dropper_log_directory_windows": "%temp%\\", "dropper_set_date": true, "dropper_target_path_win_64": "C:\\Windows\\temp\\monkey64.exe", "dropper_target_path_linux": "/tmp/monkey", @@ -38,8 +36,6 @@ "MSSQLFingerprint", "ElasticFinger" ], - "monkey_log_directory_windows": "%temp%\\", - "monkey_log_directory_linux": "/tmp/", "ping_scan_timeout": 10000, "smb_download_timeout": 300, "smb_service_name": "InfectionMonkey", diff --git a/monkey/infection_monkey/main.py b/monkey/infection_monkey/main.py index 9388d5431..f3e6b0a01 100644 --- a/monkey/infection_monkey/main.py +++ b/monkey/infection_monkey/main.py @@ -16,7 +16,7 @@ from infection_monkey.config import EXTERNAL_CONFIG_FILE, WormConfiguration from infection_monkey.dropper import MonkeyDrops from infection_monkey.model import DROPPER_ARG, MONKEY_ARG from infection_monkey.monkey import InfectionMonkey -from infection_monkey.utils.monkey_log_path import get_dropper_log_path, get_monkey_log_path +from infection_monkey.utils.monkey_log_path import get_log_path logger = None @@ -80,10 +80,10 @@ def main(): try: if MONKEY_ARG == monkey_mode: - log_path = get_monkey_log_path() + log_path = get_log_path("agent") monkey_cls = InfectionMonkey elif DROPPER_ARG == monkey_mode: - log_path = get_dropper_log_path() + log_path = get_log_path("dropper") monkey_cls = MonkeyDrops else: return True diff --git a/monkey/infection_monkey/monkey.py b/monkey/infection_monkey/monkey.py index 218b0e92a..0035b5cf6 100644 --- a/monkey/infection_monkey/monkey.py +++ b/monkey/infection_monkey/monkey.py @@ -52,7 +52,7 @@ from infection_monkey.utils.monkey_dir import ( get_monkey_dir_path, remove_monkey_dir, ) -from infection_monkey.utils.monkey_log_path import get_monkey_log_path +from infection_monkey.utils.monkey_log_path import get_log_path from infection_monkey.utils.signal_handler import register_signal_handlers, reset_signal_handlers logger = logging.getLogger(__name__) @@ -288,7 +288,7 @@ class InfectionMonkey: @staticmethod def _send_log(): - monkey_log_path = get_monkey_log_path() + monkey_log_path = get_log_path("agent") if os.path.exists(monkey_log_path): with open(monkey_log_path, "r") as f: log = f.read() diff --git a/monkey/infection_monkey/utils/monkey_log_path.py b/monkey/infection_monkey/utils/monkey_log_path.py index 3c5e7e327..bad203542 100644 --- a/monkey/infection_monkey/utils/monkey_log_path.py +++ b/monkey/infection_monkey/utils/monkey_log_path.py @@ -1,41 +1,23 @@ import os -import string import sys +import tempfile import time -from random import SystemRandom - -from infection_monkey.config import WormConfiguration +from functools import lru_cache -def get_monkey_log_path(): +@lru_cache(maxsize=None) +def get_log_path(monkey_arg: str): return ( - os.path.expandvars( - _generate_random_log_filepath(WormConfiguration.monkey_log_directory_windows, "agent") - ) + os.path.expandvars(_generate_random_log_filepath(monkey_arg)) if sys.platform == "win32" - else _generate_random_log_filepath(WormConfiguration.monkey_log_directory_linux, "agent") + else _generate_random_log_filepath(monkey_arg) ) -def get_dropper_log_path(): - return ( - os.path.expandvars( - _generate_random_log_filepath( - WormConfiguration.dropper_log_directory_windows, "dropper" - ) - ) - if sys.platform == "win32" - else _generate_random_log_filepath(WormConfiguration.dropper_log_directory_linux, "dropper") - ) - - -def _generate_random_log_filepath(log_directory: str, monkey_arg: str) -> str: - safe_random = SystemRandom() - random_string = "".join( - [safe_random.choice(string.ascii_lowercase + string.digits) for _ in range(8)] - ) +def _generate_random_log_filepath(monkey_arg: str) -> str: prefix = f"infection-monkey-{monkey_arg}-" suffix = f"-{time.strftime('%Y-%m-%d-%H-%M-%S', time.gmtime())}.log" - log_file_path = os.path.join(log_directory, prefix + random_string + suffix) - return log_file_path + _, monkey_log_path = tempfile.mkstemp(suffix=suffix, prefix=prefix) + + return monkey_log_path diff --git a/monkey/monkey_island/cc/services/config_schema/internal.py b/monkey/monkey_island/cc/services/config_schema/internal.py index c492d7904..98ab8b95e 100644 --- a/monkey/monkey_island/cc/services/config_schema/internal.py +++ b/monkey/monkey_island/cc/services/config_schema/internal.py @@ -184,36 +184,6 @@ INTERNAL = { }, }, }, - "logging": { - "title": "Logging", - "type": "object", - "properties": { - "dropper_log_directory_linux": { - "title": "Dropper log directory path on Linux", - "type": "string", - "default": "/tmp/", - "description": "The directory path of the dropper log file on Linux", - }, - "dropper_log_directory_windows": { - "title": "Dropper log directory path on Windows", - "type": "string", - "default": "%temp%\\", - "description": "The directory path of the dropper log file on Windows", - }, - "monkey_log_directory_linux": { - "title": "Monkey log directory path on Linux", - "type": "string", - "default": "/tmp/", - "description": "The directory path of the monkey log file on Linux", - }, - "monkey_log_directory_windows": { - "title": "Monkey log directory path on Windows", - "type": "string", - "default": "%temp%\\", - "description": "The directory path of the monkey log file on Windows", - }, - }, - }, "exploits": { "title": "Exploits", "type": "object", diff --git a/monkey/monkey_island/cc/ui/src/components/configuration-components/InternalConfig.js b/monkey/monkey_island/cc/ui/src/components/configuration-components/InternalConfig.js index d7d13db54..42a86dbff 100644 --- a/monkey/monkey_island/cc/ui/src/components/configuration-components/InternalConfig.js +++ b/monkey/monkey_island/cc/ui/src/components/configuration-components/InternalConfig.js @@ -5,7 +5,6 @@ import {Nav} from 'react-bootstrap'; const sectionOrder = [ 'network', 'island_server', - 'logging', 'exploits', 'dropper', 'classes', diff --git a/monkey/tests/data_for_tests/monkey_configs/flat_config.json b/monkey/tests/data_for_tests/monkey_configs/flat_config.json index d7cc0734a..1f82c5499 100644 --- a/monkey/tests/data_for_tests/monkey_configs/flat_config.json +++ b/monkey/tests/data_for_tests/monkey_configs/flat_config.json @@ -23,8 +23,6 @@ "depth": 2, "dropper_date_reference_path_linux": "/bin/sh", "dropper_date_reference_path_windows": "%windir%\\system32\\kernel32.dll", - "dropper_log_directory_linux": "/tmp/", - "dropper_log_directory_windows": "%temp%\\", "dropper_set_date": true, "dropper_target_path_linux": "/tmp/monkey", "dropper_target_path_win_64": "C:\\Windows\\temp\\monkey64.exe", @@ -71,8 +69,6 @@ "keep_tunnel_open_time": 60, "local_network_scan": true, "max_depth": null, - "monkey_log_directory_linux": "/tmp/", - "monkey_log_directory_windows": "%temp%\\", "ping_scan_timeout": 1000, "post_breach_actions": [ "CommunicateAsBackdoorUser", diff --git a/monkey/tests/data_for_tests/monkey_configs/monkey_config_standard.json b/monkey/tests/data_for_tests/monkey_configs/monkey_config_standard.json index 447a775b6..f0c95e5b3 100644 --- a/monkey/tests/data_for_tests/monkey_configs/monkey_config_standard.json +++ b/monkey/tests/data_for_tests/monkey_configs/monkey_config_standard.json @@ -106,12 +106,6 @@ "dropper_target_path_linux": "/tmp/monkey", "dropper_target_path_win_64": "C:\\Windows\\temp\\monkey64.exe" }, - "logging": { - "dropper_log_directory_linux": "/tmp/", - "dropper_log_directory_windows": "%temp%\\", - "monkey_log_directory_linux": "/tmp/", - "monkey_log_directory_windows": "%temp%\\" - }, "exploits": { "exploit_lm_hash_list": [], "exploit_ntlm_hash_list": [],