From a9e1b99f2f42475b23a696e137c8ecbf4d75b781 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Wed, 28 Sep 2022 17:04:11 +0530 Subject: [PATCH 01/11] Agent: Add agent_id parameter to IIslandAPIClient.send_log() --- .../infection_monkey/island_api_client/i_island_api_client.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/monkey/infection_monkey/island_api_client/i_island_api_client.py b/monkey/infection_monkey/island_api_client/i_island_api_client.py index 3fd83daaa..d14c8deeb 100644 --- a/monkey/infection_monkey/island_api_client/i_island_api_client.py +++ b/monkey/infection_monkey/island_api_client/i_island_api_client.py @@ -30,10 +30,11 @@ class IIslandAPIClient(ABC): """ @abstractmethod - def send_log(self, log_contents: str): + def send_log(self, agent_id: int, log_contents: str): """ Send the contents of the agent's log to the island + :param agent_id: The ID of the agent whose logs are being sent :param log_contents: The contents of the agent's log :raises IslandAPIConnectionError: If the client cannot successfully connect to the island :raises IslandAPIRequestError: If an error occurs while attempting to connect to the From 74e30a2f8843dd357b7f0e139474659d94267c3c Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Wed, 28 Sep 2022 17:06:56 +0530 Subject: [PATCH 02/11] Agent: Update API endpoint in HTTPIslandAPIClient.send_log() --- .../island_api_client/http_island_api_client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monkey/infection_monkey/island_api_client/http_island_api_client.py b/monkey/infection_monkey/island_api_client/http_island_api_client.py index 624a2c504..7c7215589 100644 --- a/monkey/infection_monkey/island_api_client/http_island_api_client.py +++ b/monkey/infection_monkey/island_api_client/http_island_api_client.py @@ -92,9 +92,9 @@ class HTTPIslandAPIClient(IIslandAPIClient): self._api_url = f"https://{island_server}/api" @handle_island_errors - def send_log(self, log_contents: str): + def send_log(self, agent_id: int, log_contents: str): response = requests.post( # noqa: DUO123 - f"{self._api_url}/log", + f"{self._api_url}/agent-logs/{agent_id}", json=log_contents, verify=False, timeout=MEDIUM_REQUEST_TIMEOUT, From 539f4e1c825447b8ff3fba163038d9a360e9f36f Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Wed, 28 Sep 2022 17:07:32 +0530 Subject: [PATCH 03/11] Agent: Fix self._island_api_client.send_log() call in ControlClient --- monkey/infection_monkey/control.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monkey/infection_monkey/control.py b/monkey/infection_monkey/control.py index 41b3511d9..22d0fac92 100644 --- a/monkey/infection_monkey/control.py +++ b/monkey/infection_monkey/control.py @@ -71,7 +71,7 @@ class ControlClient: def send_log(self, log): try: telemetry = {"monkey_guid": GUID, "log": json.dumps(log)} - self._island_api_client.send_log(json.dumps(telemetry)) + self._island_api_client.send_log(GUID, json.dumps(telemetry)) except Exception as exc: logger.warning(f"Error connecting to control server {self.server_address}: {exc}") From 8e3918cebeb74e126e6def22bd88b64fdc22c176 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Wed, 28 Sep 2022 17:09:51 +0530 Subject: [PATCH 04/11] UT: Fix island_api_client.send_log() calls in test_http_island_api_client.py --- .../island_api_client/test_http_island_api_client.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/monkey/tests/unit_tests/infection_monkey/island_api_client/test_http_island_api_client.py b/monkey/tests/unit_tests/infection_monkey/island_api_client/test_http_island_api_client.py index 56a480082..2eb013d9a 100644 --- a/monkey/tests/unit_tests/infection_monkey/island_api_client/test_http_island_api_client.py +++ b/monkey/tests/unit_tests/infection_monkey/island_api_client/test_http_island_api_client.py @@ -37,7 +37,7 @@ AGENT_REGISTRATION = AgentRegistrationData( TIMESTAMP = 123456789 ISLAND_URI = f"https://{SERVER}/api?action=is-up" -ISLAND_SEND_LOG_URI = f"https://{SERVER}/api/log" +ISLAND_SEND_LOG_URI = f"https://{SERVER}/api/agent-logs/{AGENT_ID}" ISLAND_GET_PBA_FILE_URI = f"https://{SERVER}/api/pba/download/{PBA_FILE}" ISLAND_GET_AGENT_BINARY_URI = f"https://{SERVER}/api/agent-binaries/{WINDOWS}" ISLAND_SEND_EVENTS_URI = f"https://{SERVER}/api/agent-events" @@ -120,7 +120,7 @@ def test_island_api_client__send_log(island_api_client, actual_error, expected_e with pytest.raises(expected_error): m.post(ISLAND_SEND_LOG_URI, exc=actual_error) - island_api_client.send_log(log_contents="some_data") + island_api_client.send_log(agent_id=AGENT_ID, log_contents="some_data") @pytest.mark.parametrize( @@ -137,7 +137,7 @@ def test_island_api_client_send_log__status_code(island_api_client, status_code, with pytest.raises(expected_error): m.post(ISLAND_SEND_LOG_URI, status_code=status_code) - island_api_client.send_log(log_contents="some_data") + island_api_client.send_log(agent_id=AGENT_ID, log_contents="some_data") @pytest.mark.parametrize( From 29c08ff40c4e61a19d2b533be3f9a6dbd46d7b25 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Wed, 28 Sep 2022 17:44:28 +0530 Subject: [PATCH 05/11] Agent: Simplify logic for sending logs in ControlClient --- monkey/infection_monkey/control.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/monkey/infection_monkey/control.py b/monkey/infection_monkey/control.py index 22d0fac92..5f3c5372a 100644 --- a/monkey/infection_monkey/control.py +++ b/monkey/infection_monkey/control.py @@ -13,6 +13,7 @@ from infection_monkey.config import GUID from infection_monkey.island_api_client import IIslandAPIClient from infection_monkey.network.info import get_host_subnets from infection_monkey.utils import agent_process +from infection_monkey.utils.ids import get_agent_id disable_warnings() # noqa DUO131 @@ -70,8 +71,9 @@ class ControlClient: def send_log(self, log): try: - telemetry = {"monkey_guid": GUID, "log": json.dumps(log)} - self._island_api_client.send_log(GUID, json.dumps(telemetry)) + monkey_guid = get_agent_id() + log_contents = json.dumps(log) + self._island_api_client.send_log(monkey_guid, log_contents) except Exception as exc: logger.warning(f"Error connecting to control server {self.server_address}: {exc}") From 8dc8a516d52849371672d68a5f7ca9bac67d099a Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Wed, 28 Sep 2022 17:45:26 +0530 Subject: [PATCH 06/11] Agent: Fix type hint in HTTPIslandAPIClient.send_log() --- .../island_api_client/http_island_api_client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monkey/infection_monkey/island_api_client/http_island_api_client.py b/monkey/infection_monkey/island_api_client/http_island_api_client.py index 7c7215589..2c0e2319a 100644 --- a/monkey/infection_monkey/island_api_client/http_island_api_client.py +++ b/monkey/infection_monkey/island_api_client/http_island_api_client.py @@ -16,7 +16,7 @@ from common.common_consts.timeouts import ( SHORT_REQUEST_TIMEOUT, ) from common.credentials import Credentials -from common.types import SocketAddress +from common.types import AgentID, SocketAddress from . import ( AbstractIslandAPIClientFactory, @@ -92,7 +92,7 @@ class HTTPIslandAPIClient(IIslandAPIClient): self._api_url = f"https://{island_server}/api" @handle_island_errors - def send_log(self, agent_id: int, log_contents: str): + def send_log(self, agent_id: AgentID, log_contents: str): response = requests.post( # noqa: DUO123 f"{self._api_url}/agent-logs/{agent_id}", json=log_contents, From 21f01292f7ec7722e11b51bdf20964189f1add51 Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Wed, 28 Sep 2022 17:47:04 +0530 Subject: [PATCH 07/11] Agent: Fix type hint in IIslandAPIClient.send_log() --- .../infection_monkey/island_api_client/i_island_api_client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monkey/infection_monkey/island_api_client/i_island_api_client.py b/monkey/infection_monkey/island_api_client/i_island_api_client.py index d14c8deeb..86d42ee88 100644 --- a/monkey/infection_monkey/island_api_client/i_island_api_client.py +++ b/monkey/infection_monkey/island_api_client/i_island_api_client.py @@ -5,7 +5,7 @@ from common import AgentRegistrationData, AgentSignals, OperatingSystem from common.agent_configuration import AgentConfiguration from common.agent_events import AbstractAgentEvent from common.credentials import Credentials -from common.types import SocketAddress +from common.types import AgentID, SocketAddress class IIslandAPIClient(ABC): @@ -30,7 +30,7 @@ class IIslandAPIClient(ABC): """ @abstractmethod - def send_log(self, agent_id: int, log_contents: str): + def send_log(self, agent_id: AgentID, log_contents: str): """ Send the contents of the agent's log to the island From a1516535f9240734ddafd84d24328b36dd829747 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 28 Sep 2022 15:08:59 -0400 Subject: [PATCH 08/11] Agent: Add InfectionMonkey._agent_id attribute --- monkey/infection_monkey/monkey.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/monkey/infection_monkey/monkey.py b/monkey/infection_monkey/monkey.py index acd10ce95..e998854e8 100644 --- a/monkey/infection_monkey/monkey.py +++ b/monkey/infection_monkey/monkey.py @@ -111,6 +111,7 @@ class InfectionMonkey: self._singleton = SystemSingleton() self._opts = self._get_arguments(args) + self._agent_id = get_agent_id() self._agent_event_serializer_registry = self._setup_agent_event_serializers() @@ -122,7 +123,7 @@ class InfectionMonkey: server_address=self._island_address, island_api_client=self._island_api_client ) self._control_channel = ControlChannel( - str(self._island_address), get_agent_id(), self._island_api_client + str(self._island_address), self._agent_id, self._island_api_client ) self._register_agent() @@ -176,7 +177,7 @@ class InfectionMonkey: def _register_agent(self): agent_registration_data = AgentRegistrationData( - id=get_agent_id(), + id=self._agent_id, machine_hardware_id=get_machine_id(), start_time=agent_process.get_start_time(), # parent_id=parent, From fab67d893f28d71759b337fd70be787b84f19186 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 28 Sep 2022 15:11:42 -0400 Subject: [PATCH 09/11] Agent: Call IIslandAPIClient.send_log() directly ControlClient is going away. It's been replaced by IIslandAPIClient. Now is a good time to remove ControlClient.send_log() --- monkey/infection_monkey/monkey.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/monkey/infection_monkey/monkey.py b/monkey/infection_monkey/monkey.py index e998854e8..c54569360 100644 --- a/monkey/infection_monkey/monkey.py +++ b/monkey/infection_monkey/monkey.py @@ -497,11 +497,11 @@ class InfectionMonkey: monkey_log_path = get_agent_log_path() if monkey_log_path.is_file(): with open(monkey_log_path, "r") as f: - log = f.read() + log_contents = f.read() else: - log = "" + log_contents = "" - self._control_client.send_log(log) + self._island_api_client.send_log(self._agent_id, log_contents) @staticmethod def _self_delete() -> bool: From 87d25d2ac8301684ab37cf6513a447029aa7a36a Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 28 Sep 2022 15:15:32 -0400 Subject: [PATCH 10/11] Agent: Remove disused send_log() from ControlClient --- monkey/infection_monkey/control.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/monkey/infection_monkey/control.py b/monkey/infection_monkey/control.py index 5f3c5372a..887ae4221 100644 --- a/monkey/infection_monkey/control.py +++ b/monkey/infection_monkey/control.py @@ -13,7 +13,6 @@ from infection_monkey.config import GUID from infection_monkey.island_api_client import IIslandAPIClient from infection_monkey.network.info import get_host_subnets from infection_monkey.utils import agent_process -from infection_monkey.utils.ids import get_agent_id disable_warnings() # noqa DUO131 @@ -69,14 +68,6 @@ class ControlClient: except Exception as exc: logger.warning(f"Error connecting to control server {self.server_address}: {exc}") - def send_log(self, log): - try: - monkey_guid = get_agent_id() - log_contents = json.dumps(log) - self._island_api_client.send_log(monkey_guid, log_contents) - except Exception as exc: - logger.warning(f"Error connecting to control server {self.server_address}: {exc}") - def get_pba_file(self, filename): try: return self._island_api_client.get_pba_file(filename) From ff8c8bd0a0c2ee5224751f42610a6d940c86eef7 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 28 Sep 2022 15:22:44 -0400 Subject: [PATCH 11/11] Agent: Use PUT instead of POST to send agent logs --- .../island_api_client/http_island_api_client.py | 2 +- .../island_api_client/test_http_island_api_client.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/monkey/infection_monkey/island_api_client/http_island_api_client.py b/monkey/infection_monkey/island_api_client/http_island_api_client.py index 2c0e2319a..eb2420c95 100644 --- a/monkey/infection_monkey/island_api_client/http_island_api_client.py +++ b/monkey/infection_monkey/island_api_client/http_island_api_client.py @@ -93,7 +93,7 @@ class HTTPIslandAPIClient(IIslandAPIClient): @handle_island_errors def send_log(self, agent_id: AgentID, log_contents: str): - response = requests.post( # noqa: DUO123 + response = requests.put( # noqa: DUO123 f"{self._api_url}/agent-logs/{agent_id}", json=log_contents, verify=False, diff --git a/monkey/tests/unit_tests/infection_monkey/island_api_client/test_http_island_api_client.py b/monkey/tests/unit_tests/infection_monkey/island_api_client/test_http_island_api_client.py index 2eb013d9a..e65ba4eba 100644 --- a/monkey/tests/unit_tests/infection_monkey/island_api_client/test_http_island_api_client.py +++ b/monkey/tests/unit_tests/infection_monkey/island_api_client/test_http_island_api_client.py @@ -119,7 +119,7 @@ def test_island_api_client__send_log(island_api_client, actual_error, expected_e island_api_client.connect(SERVER) with pytest.raises(expected_error): - m.post(ISLAND_SEND_LOG_URI, exc=actual_error) + m.put(ISLAND_SEND_LOG_URI, exc=actual_error) island_api_client.send_log(agent_id=AGENT_ID, log_contents="some_data") @@ -136,7 +136,7 @@ def test_island_api_client_send_log__status_code(island_api_client, status_code, island_api_client.connect(SERVER) with pytest.raises(expected_error): - m.post(ISLAND_SEND_LOG_URI, status_code=status_code) + m.put(ISLAND_SEND_LOG_URI, status_code=status_code) island_api_client.send_log(agent_id=AGENT_ID, log_contents="some_data")