From 26fc6fd5b94fb3f33e3731d43a0d4c5ea2569af9 Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Wed, 14 Sep 2022 17:17:32 +0200 Subject: [PATCH 1/5] Island: Add save_event_to_event_repository subscriber --- .../cc/agent_event_subscribers/__init__.py | 1 + .../save_event_to_event_repository.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 monkey/monkey_island/cc/agent_event_subscribers/__init__.py create mode 100644 monkey/monkey_island/cc/agent_event_subscribers/save_event_to_event_repository.py diff --git a/monkey/monkey_island/cc/agent_event_subscribers/__init__.py b/monkey/monkey_island/cc/agent_event_subscribers/__init__.py new file mode 100644 index 000000000..f206087d7 --- /dev/null +++ b/monkey/monkey_island/cc/agent_event_subscribers/__init__.py @@ -0,0 +1 @@ +from .save_event_to_event_repository import save_event_to_event_repository diff --git a/monkey/monkey_island/cc/agent_event_subscribers/save_event_to_event_repository.py b/monkey/monkey_island/cc/agent_event_subscribers/save_event_to_event_repository.py new file mode 100644 index 000000000..0249369b3 --- /dev/null +++ b/monkey/monkey_island/cc/agent_event_subscribers/save_event_to_event_repository.py @@ -0,0 +1,17 @@ +import logging + +from common.events import AbstractAgentEvent +from monkey_island.cc.repository import IEventRepository, StorageError + +logger = logging.getLogger(__name__) + + +class save_event_to_event_repository: + def __init__(self, event_repository: IEventRepository): + self._event_repository = event_repository + + def __call__(self, event: AbstractAgentEvent): + try: + self._event_repository.save_event(event) + except StorageError as err: + logger.error(f"Error occured storing event {event}: {err}") From 5a836daaa7434ce914a2dbff07309f3f505d6471 Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Wed, 14 Sep 2022 17:18:55 +0200 Subject: [PATCH 2/5] Island: Add agent_event_handlers --- monkey/monkey_island/cc/setup/__init__.py | 1 + .../cc/setup/agent_event_handlers.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 monkey/monkey_island/cc/setup/agent_event_handlers.py diff --git a/monkey/monkey_island/cc/setup/__init__.py b/monkey/monkey_island/cc/setup/__init__.py index 3642bf9b2..97a7ccfc2 100644 --- a/monkey/monkey_island/cc/setup/__init__.py +++ b/monkey/monkey_island/cc/setup/__init__.py @@ -1,2 +1,3 @@ from .pywsgi_logging_filter import PyWSGILoggingFilter from .island_event_handlers import setup_island_event_handlers +from .agent_event_handlers import setup_agent_event_handlers diff --git a/monkey/monkey_island/cc/setup/agent_event_handlers.py b/monkey/monkey_island/cc/setup/agent_event_handlers.py new file mode 100644 index 000000000..9fd0a2ea7 --- /dev/null +++ b/monkey/monkey_island/cc/setup/agent_event_handlers.py @@ -0,0 +1,16 @@ +from common import DIContainer +from common.event_queue import IAgentEventQueue +from monkey_island.cc.agent_event_subscribers import save_event_to_event_repository +from monkey_island.cc.repository import IEventRepository + + +def setup_agent_event_handlers(container: DIContainer): + _subscribe_and_store_to_event_repository(container) + + +def _subscribe_and_store_to_event_repository(container: DIContainer): + agent_event_queue = container.resolve(IAgentEventQueue) + + event_repository = container.resolve(IEventRepository) + save_event_subscriber = save_event_to_event_repository(event_repository) + agent_event_queue.subscribe_all_events(save_event_subscriber) From 83f1e4cc2ab671924d8a0481b40d0f49089b9d0f Mon Sep 17 00:00:00 2001 From: Ilija Lazoroski Date: Wed, 14 Sep 2022 17:19:28 +0200 Subject: [PATCH 3/5] Island: Use agent_event_handlers on island setup --- monkey/monkey_island/cc/server_setup.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/monkey/monkey_island/cc/server_setup.py b/monkey/monkey_island/cc/server_setup.py index 95084d703..4f6989e12 100644 --- a/monkey/monkey_island/cc/server_setup.py +++ b/monkey/monkey_island/cc/server_setup.py @@ -40,7 +40,11 @@ from monkey_island.cc.server_utils.island_logger import reset_logger, setup_logg from monkey_island.cc.server_utils.network_utils import get_ip_addresses # noqa: E402 from monkey_island.cc.services.initialize import initialize_services # noqa: E402 from monkey_island.cc.setup import island_config_options_validator # noqa: E402 -from monkey_island.cc.setup import PyWSGILoggingFilter, setup_island_event_handlers # noqa: E402 +from monkey_island.cc.setup import ( # noqa: E402 + PyWSGILoggingFilter, + setup_agent_event_handlers, + setup_island_event_handlers, +) from monkey_island.cc.setup.data_dir import IncompatibleDataDirectory, setup_data_dir # noqa: E402 from monkey_island.cc.setup.gevent_hub_error_handler import GeventHubErrorHandler # noqa: E402 from monkey_island.cc.setup.island_config_options import IslandConfigOptions # noqa: E402 @@ -63,6 +67,7 @@ def run_monkey_island(): _send_analytics(deployment, version) container = _initialize_di_container(ip_addresses, version, config_options.data_dir) setup_island_event_handlers(container) + setup_agent_event_handlers(container) _setup_agent_event_serializers(container) _initialize_mongodb_connection(config_options.start_mongodb, config_options.data_dir) From f5711e2fab35e8cf9be74ce5068d0a7f2ab6b09d Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 14 Sep 2022 13:35:49 -0400 Subject: [PATCH 4/5] Island: Add StubbedEventRepository A placeholder until MongoEventRepository is implemented --- .../monkey_island/cc/repository/__init__.py | 1 + .../cc/repository/stubbed_event_repository.py | 29 +++++++++++++++++++ vulture_allowlist.py | 7 ++++- 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 monkey/monkey_island/cc/repository/stubbed_event_repository.py diff --git a/monkey/monkey_island/cc/repository/__init__.py b/monkey/monkey_island/cc/repository/__init__.py index e2ce11dd4..a414b5eb0 100644 --- a/monkey/monkey_island/cc/repository/__init__.py +++ b/monkey/monkey_island/cc/repository/__init__.py @@ -26,3 +26,4 @@ from .mongo_credentials_repository import MongoCredentialsRepository from .mongo_machine_repository import MongoMachineRepository from .mongo_agent_repository import MongoAgentRepository from .mongo_node_repository import MongoNodeRepository +from .stubbed_event_repository import StubbedEventRepository diff --git a/monkey/monkey_island/cc/repository/stubbed_event_repository.py b/monkey/monkey_island/cc/repository/stubbed_event_repository.py new file mode 100644 index 000000000..d5260b6af --- /dev/null +++ b/monkey/monkey_island/cc/repository/stubbed_event_repository.py @@ -0,0 +1,29 @@ +from typing import Sequence, Type, TypeVar + +from common.events import AbstractAgentEvent +from common.types import AgentID + +from . import IEventRepository + +T = TypeVar("T", bound=AbstractAgentEvent) + + +# TODO: Remove this class after #2180 is complete +class StubbedEventRepository(IEventRepository): + def save_event(self, event: AbstractAgentEvent): + return + + def get_events(self) -> Sequence[AbstractAgentEvent]: + return [] + + def get_events_by_type(self, event_type: Type[T]) -> Sequence[T]: + return [] + + def get_events_by_tag(self, tag: str) -> Sequence[AbstractAgentEvent]: + return [] + + def get_events_by_source(self, source: AgentID) -> Sequence[AbstractAgentEvent]: + return [] + + def reset(self): + return diff --git a/vulture_allowlist.py b/vulture_allowlist.py index d942d34b2..8c57586b8 100644 --- a/vulture_allowlist.py +++ b/vulture_allowlist.py @@ -12,7 +12,11 @@ from infection_monkey.exploit.log4shell_utils.ldap_server import LDAPServerFacto from monkey_island.cc.event_queue import IslandEventTopic, PyPubSubIslandEventQueue from monkey_island.cc.models import Report from monkey_island.cc.models.networkmap import Arc, NetworkMap -from monkey_island.cc.repository import MongoAgentRepository, MongoMachineRepository +from monkey_island.cc.repository import ( + MongoAgentRepository, + MongoMachineRepository, + StubbedEventRepository, +) from monkey_island.cc.repository.attack.IMitigationsRepository import IMitigationsRepository from monkey_island.cc.repository.i_agent_repository import IAgentRepository from monkey_island.cc.repository.i_attack_repository import IAttackRepository @@ -279,6 +283,7 @@ IEventRepository.get_events IFindingRepository.get_findings MongoAgentRepository MongoMachineRepository +StubbedEventRepository key_list simulation netmap From 6f4c3d4669b94f23803e3fdbe932c8436a094699 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 14 Sep 2022 13:36:55 -0400 Subject: [PATCH 5/5] Island: Register StubbedEventRepository in DIContainer --- monkey/monkey_island/cc/services/initialize.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/monkey/monkey_island/cc/services/initialize.py b/monkey/monkey_island/cc/services/initialize.py index 1b6a3e4d5..6f363f3ed 100644 --- a/monkey/monkey_island/cc/services/initialize.py +++ b/monkey/monkey_island/cc/services/initialize.py @@ -25,6 +25,7 @@ from monkey_island.cc.repository import ( IAgentBinaryRepository, IAgentConfigurationRepository, ICredentialsRepository, + IEventRepository, IFileRepository, ISimulationRepository, IUserRepository, @@ -32,6 +33,7 @@ from monkey_island.cc.repository import ( LocalStorageFileRepository, MongoCredentialsRepository, RetrievalError, + StubbedEventRepository, ) from monkey_island.cc.server_utils.consts import MONKEY_ISLAND_ABS_PATH from monkey_island.cc.server_utils.encryption import ILockableEncryptor, RepositoryEncryptor @@ -106,6 +108,9 @@ def _register_repositories(container: DIContainer, data_dir: Path): ) container.register_instance(IUserRepository, container.resolve(JSONFileUserRepository)) + # TODO: Replace with MongoEventRepository + container.register_instance(IEventRepository, StubbedEventRepository()) + def _decorate_file_repository(file_repository: IFileRepository) -> IFileRepository: return FileRepositoryLockingDecorator(