From bb8b4742af02f2ebba6f477013dad18f6a3bd637 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Wed, 14 Sep 2022 16:00:56 +0530 Subject: [PATCH] Agent: Implement event serializer logic for SendAllAgentEventsToIsland --- monkey/infection_monkey/monkey.py | 12 +++++++++--- monkey/infection_monkey/send_all_events_to_island.py | 10 +++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/monkey/infection_monkey/monkey.py b/monkey/infection_monkey/monkey.py index f621dc3ec..bfdbc5004 100644 --- a/monkey/infection_monkey/monkey.py +++ b/monkey/infection_monkey/monkey.py @@ -177,7 +177,7 @@ class InfectionMonkey: if firewall.is_enabled(): firewall.add_firewall_rule() - _ = self._setup_agent_event_serializers() + self._agent_event_serializer_registry = self._setup_agent_event_serializers() control_channel = ControlChannel( self._control_client.server_address, GUID, self._control_client.proxies @@ -221,7 +221,10 @@ class InfectionMonkey: event_queue = PyPubSubAgentEventQueue(Publisher()) InfectionMonkey._subscribe_events( - event_queue, propagation_credentials_repository, self._control_client.server_address + event_queue, + propagation_credentials_repository, + self._control_client.server_address, + self._agent_event_serializer_registry, ) puppet = self._build_puppet(propagation_credentials_repository, event_queue) @@ -247,6 +250,7 @@ class InfectionMonkey: event_queue: IAgentEventQueue, propagation_credentials_repository: IPropagationCredentialsRepository, server_address: str, + agent_event_serializer_registry: EventSerializerRegistry, ): event_queue.subscribe_type( CredentialsStolenEvent, @@ -254,7 +258,9 @@ class InfectionMonkey: propagation_credentials_repository ), ) - event_queue.subscribe_all_events(SendAllAgentEventsToIsland(server_address).send_event) + event_queue.subscribe_all_events( + SendAllAgentEventsToIsland(server_address, agent_event_serializer_registry).send_event + ) @staticmethod def _get_local_network_interfaces() -> List[IPv4Interface]: diff --git a/monkey/infection_monkey/send_all_events_to_island.py b/monkey/infection_monkey/send_all_events_to_island.py index 4ae418d3f..3bdb08db9 100644 --- a/monkey/infection_monkey/send_all_events_to_island.py +++ b/monkey/infection_monkey/send_all_events_to_island.py @@ -6,6 +6,7 @@ from time import sleep import requests from common.common_consts.timeouts import MEDIUM_REQUEST_TIMEOUT +from common.event_serializers import EventSerializerRegistry from common.event_serializers.i_event_serializer import JSONSerializable from common.events import AbstractAgentEvent @@ -21,8 +22,11 @@ class SendAllAgentEventsToIsland: Sends information about the events carried out by the Agent to the Island in batches """ - def __init__(self, server_address: str): + def __init__( + self, server_address: str, agent_event_serializer_registry: EventSerializerRegistry + ): self._server_address = server_address + self._agent_event_serializer_registry = agent_event_serializer_registry self._agent_events_to_island_sender = AgentEventsToIslandSender(self._server_address) self._agent_events_to_island_sender.start() @@ -38,8 +42,8 @@ class SendAllAgentEventsToIsland: ) def _serialize_event(self, event: AbstractAgentEvent): - # get serializer from registry and serialize - pass + serializer = self._agent_event_serializer_registry[event.__class__] + return serializer.serialize(event) class AgentEventsToIslandSender: