diff --git a/monkey/monkey_island/cc/island_event_handlers/handle_agent_registration.py b/monkey/monkey_island/cc/island_event_handlers/handle_agent_registration.py index 4daefc5c4fe..aae1d9b7758 100644 --- a/monkey/monkey_island/cc/island_event_handlers/handle_agent_registration.py +++ b/monkey/monkey_island/cc/island_event_handlers/handle_agent_registration.py @@ -108,6 +108,7 @@ def _add_agent(self, agent_registration_data: AgentRegistrationData, machine: Ma start_time=agent_registration_data.start_time, parent_id=agent_registration_data.parent_id, cc_server=agent_registration_data.cc_server, + sha256=agent_registration_data.sha256, ) self._agent_repository.upsert_agent(new_agent) diff --git a/monkey/monkey_island/cc/models/agent.py b/monkey/monkey_island/cc/models/agent.py index b3283d68995..45030f5aeb5 100644 --- a/monkey/monkey_island/cc/models/agent.py +++ b/monkey/monkey_island/cc/models/agent.py @@ -32,3 +32,6 @@ class Agent(MutableInfectionMonkeyBaseModel): cc_server: Optional[SocketAddress] """The address that the agent used to communicate with the island""" + + sha256: str = Field(allow_mutation=False, regex="^[0-9a-fA-F]{64}$") + """The SHA256 hash of the agent binary""" diff --git a/monkey/tests/unit_tests/monkey_island/cc/agent_event_handlers/test_scan_event_handler.py b/monkey/tests/unit_tests/monkey_island/cc/agent_event_handlers/test_scan_event_handler.py index 8876a7e464a..9113430378f 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/agent_event_handlers/test_scan_event_handler.py +++ b/monkey/tests/unit_tests/monkey_island/cc/agent_event_handlers/test_scan_event_handler.py @@ -27,8 +27,14 @@ AGENT_ID = UUID("1d8ce743-a0f4-45c5-96af-91106529d3e2") SOURCE_MACHINE_ID = 11 CC_SERVER = SocketAddress(ip="10.10.10.100", port="5000") +AGENT_SHA256 = "c21dafe326222ba3ba65f5aebb6ea09c50696bf40eebca184caffe54f102746c" AGENT = Agent( - id=AGENT_ID, machine_id=SOURCE_MACHINE_ID, start_time=0, parent_id=None, cc_server=CC_SERVER + id=AGENT_ID, + machine_id=SOURCE_MACHINE_ID, + start_time=0, + parent_id=None, + cc_server=CC_SERVER, + sha256=AGENT_SHA256, ) SOURCE_MACHINE = Machine( id=SOURCE_MACHINE_ID, diff --git a/monkey/tests/unit_tests/monkey_island/cc/agent_event_handlers/test_update_agent_shutdown_status.py b/monkey/tests/unit_tests/monkey_island/cc/agent_event_handlers/test_update_agent_shutdown_status.py index 2ab6c910f4d..f49baac3150 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/agent_event_handlers/test_update_agent_shutdown_status.py +++ b/monkey/tests/unit_tests/monkey_island/cc/agent_event_handlers/test_update_agent_shutdown_status.py @@ -12,13 +12,19 @@ from monkey_island.cc.repositories import IAgentRepository, UnknownRecordError AGENT_ID = UUID("1d8ce743-a0f4-45c5-96af-91106529d3e2") +AGENT_SHA256 = "35f129207bbe966ef786d0db4aab5113f3d6ea673a0c6890c2e9116617c9816f" MACHINE_ID = 11 CC_SERVER = SocketAddress(ip="10.10.10.100", port="5000") def get_agent_object() -> Agent: return Agent( - id=AGENT_ID, machine_id=MACHINE_ID, start_time=0, parent_id=None, cc_server=CC_SERVER + id=AGENT_ID, + machine_id=MACHINE_ID, + start_time=0, + parent_id=None, + cc_server=CC_SERVER, + sha256=AGENT_SHA256, ) diff --git a/monkey/tests/unit_tests/monkey_island/cc/agent_event_handlers/test_update_machine_hostname.py b/monkey/tests/unit_tests/monkey_island/cc/agent_event_handlers/test_update_machine_hostname.py index d1a612748a3..3ab9728aaeb 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/agent_event_handlers/test_update_machine_hostname.py +++ b/monkey/tests/unit_tests/monkey_island/cc/agent_event_handlers/test_update_machine_hostname.py @@ -27,8 +27,11 @@ # The agent AGENT_ID = UUID("72a64013-b3ab-4be9-9f05-0ffaccf01950") +AGENT_SHA256 = "142e6b8c77382ebaa41d3eb5cc6520dc5922d1030ecf2fa6fbb9b2462af11bbe" CC_SERVER = SocketAddress(ip="10.10.10.100", port="5000") -AGENT = Agent(id=AGENT_ID, machine_id=MACHINE_ID, start_time=0, cc_server=CC_SERVER) +AGENT = Agent( + id=AGENT_ID, machine_id=MACHINE_ID, start_time=0, cc_server=CC_SERVER, sha256=AGENT_SHA256 +) # The event EVENT = HostnameDiscoveryEvent(source=AGENT_ID, hostname="hostname") diff --git a/monkey/tests/unit_tests/monkey_island/cc/agent_event_handlers/test_update_machine_os.py b/monkey/tests/unit_tests/monkey_island/cc/agent_event_handlers/test_update_machine_os.py index 0ee12df17e1..e06f6e718f5 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/agent_event_handlers/test_update_machine_os.py +++ b/monkey/tests/unit_tests/monkey_island/cc/agent_event_handlers/test_update_machine_os.py @@ -30,8 +30,11 @@ # The agent AGENT_ID = UUID("72a64013-b3ab-4be9-9f05-0ffaccf01950") +AGENT_SHA256 = "142e6b8c77382ebaa41d3eb5cc6520dc5922d1030ecf2fa6fbb9b2462af11bbe" CC_SERVER = SocketAddress(ip="10.10.10.100", port="5000") -AGENT = Agent(id=AGENT_ID, machine_id=MACHINE_ID, start_time=0, cc_server=CC_SERVER) +AGENT = Agent( + id=AGENT_ID, machine_id=MACHINE_ID, start_time=0, cc_server=CC_SERVER, sha256=AGENT_SHA256 +) # The event EVENT = OSDiscoveryEvent(source=AGENT_ID, os=OperatingSystem.LINUX, version="blah") diff --git a/monkey/tests/unit_tests/monkey_island/cc/island_event_handlers/test_agent_heartbeat_monitor.py b/monkey/tests/unit_tests/monkey_island/cc/island_event_handlers/test_agent_heartbeat_monitor.py index 6f13f5b5c66..1a62ce65aeb 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/island_event_handlers/test_agent_heartbeat_monitor.py +++ b/monkey/tests/unit_tests/monkey_island/cc/island_event_handlers/test_agent_heartbeat_monitor.py @@ -13,11 +13,13 @@ from monkey_island.cc.models import Agent AGENT_ID_1 = UUID("2d56f972-78a8-4026-9f47-2dfd550ee207") +AGENT_SHA256 = "142e6b8c77382ebaa41d3eb5cc6520dc5922d1030ecf2fa6fbb9b2462af11bbe" AGENT_1 = Agent( id=AGENT_ID_1, machine_id=1, start_time=100, stop_time=None, + sha256=AGENT_SHA256, ) AGENT_ID_2 = UUID("65c641f2-af47-4a42-929b-109b30f0d8d6") @@ -26,6 +28,7 @@ machine_id=2, start_time=100, stop_time=None, + sha256=AGENT_SHA256, ) AGENT_ID_3 = UUID("290da3c3-f410-4f5e-a472-b04416860a2c") @@ -34,6 +37,7 @@ machine_id=3, start_time=300, stop_time=None, + sha256=AGENT_SHA256, ) AGENT_ID_ALREADY_STOPPED = UUID("e5cd334a-5ca5-4f19-a2ab-a68d515fea46") @@ -42,6 +46,7 @@ machine_id=4, start_time=600, stop_time=700, + sha256=AGENT_SHA256, ) diff --git a/monkey/tests/unit_tests/monkey_island/cc/island_event_handlers/test_handle_agent_registration.py b/monkey/tests/unit_tests/monkey_island/cc/island_event_handlers/test_handle_agent_registration.py index e6a10f60658..c56c6a904ff 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/island_event_handlers/test_handle_agent_registration.py +++ b/monkey/tests/unit_tests/monkey_island/cc/island_event_handlers/test_handle_agent_registration.py @@ -172,6 +172,7 @@ def test_add_agent(handler, agent_repository): start_time=AGENT_REGISTRATION_DATA.start_time, parent_id=AGENT_REGISTRATION_DATA.parent_id, cc_server=AGENT_REGISTRATION_DATA.cc_server, + sha256=AGENT_REGISTRATION_DATA.sha256, ) handler(AGENT_REGISTRATION_DATA) diff --git a/monkey/tests/unit_tests/monkey_island/cc/models/test_agent.py b/monkey/tests/unit_tests/monkey_island/cc/models/test_agent.py index 3d1adf370bb..56368b0a29c 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/models/test_agent.py +++ b/monkey/tests/unit_tests/monkey_island/cc/models/test_agent.py @@ -7,6 +7,7 @@ AGENT_ID = UUID("012e7238-7b81-4108-8c7f-0787bc3f3c10") PARENT_ID = UUID("0fc9afcb-1902-436b-bd5c-1ad194252484") +SHA256 = "6b524293febf78ac659ce4ca368b8fc74df6e14462e12a43e4044eafe2a5f947" AGENT_OBJECT_DICT = { "id": AGENT_ID, @@ -14,6 +15,7 @@ "parent_id": PARENT_ID, "registration_time": datetime.fromtimestamp(1660848410, tz=timezone.utc), "start_time": datetime.fromtimestamp(1660848408, tz=timezone.utc), + "sha256": SHA256, } AGENT_SIMPLE_DICT = { @@ -22,6 +24,7 @@ "parent_id": str(PARENT_ID), "registration_time": "2022-08-18T18:46:50+00:00", "start_time": "2022-08-18T18:46:48+00:00", + "sha256": SHA256, } @@ -61,6 +64,7 @@ def test_to_dict(): ("stop_time", []), ("parent_id", 2.1), ("cc_server", [1]), + ("sha256", []), ], ) def test_construct_invalid_field__type_error(key, value): @@ -79,6 +83,10 @@ def test_construct_invalid_field__type_error(key, value): ("start_time", "not-a-datetime"), ("stop_time", "not-a-datetime"), ("cc_server", []), + ("sha256", "abcdef"), # too short + ("sha256", "this_string_has_the_right_length_but_includes_non_hex_characters"), + ("sha256", "1234567812345678123456781234567812345678123456781234567812345678abcdef"), + ("sha256", 1), ], ) def test_construct_invalid_field__value_error(key, value): @@ -136,3 +144,10 @@ def test_cc_server_set_validated(): with pytest.raises(ValueError): a.cc_server = [] + + +def test_sha256_immutable(): + a = Agent(**AGENT_SIMPLE_DICT) + + with pytest.raises(TypeError): + a.sha256 = "testing!" diff --git a/monkey/tests/unit_tests/monkey_island/cc/repositories/test_agent_machine_facade.py b/monkey/tests/unit_tests/monkey_island/cc/repositories/test_agent_machine_facade.py index 69b237cacd3..78f8c7de0a2 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/repositories/test_agent_machine_facade.py +++ b/monkey/tests/unit_tests/monkey_island/cc/repositories/test_agent_machine_facade.py @@ -20,12 +20,14 @@ ) AGENT_ID = UUID("655fd01c-5eec-4e42-b6e3-1fb738c2978d") +AGENT_SHA256 = "5d1bb53850d782d42b0b9d86497ca95986d4945d3284a0e5fc0f7beaccde19c6" AGENT = Agent( id=AGENT_ID, machine_id=SOURCE_MACHINE_ID, start_time=0, parent_id=None, cc_server=(SocketAddress(ip="10.10.10.10", port=5000)), + sha256=AGENT_SHA256, ) @@ -71,6 +73,7 @@ def test_cache_reset__get_machine_id_from_agent_id( start_time=0, parent_id=None, cc_server=(SocketAddress(ip="10.10.10.10", port=5000)), + sha256=AGENT_SHA256, ) agent_repository.reset() diff --git a/monkey/tests/unit_tests/monkey_island/cc/repositories/test_mongo_agent_repository.py b/monkey/tests/unit_tests/monkey_island/cc/repositories/test_mongo_agent_repository.py index 3d94aa8333b..dfd6d37a70b 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/repositories/test_mongo_agent_repository.py +++ b/monkey/tests/unit_tests/monkey_island/cc/repositories/test_mongo_agent_repository.py @@ -21,8 +21,12 @@ VICTIM_TWO_ID = uuid4() VICTIM_THREE_ID = uuid4() +AGENT_SHA256 = "087ef38f6c65013519853f192decca09ca45a1ed289fe12a7829e1d29d198362" PROGENITOR_AGENT = Agent( - id=VICTIM_ZERO_ID, machine_id=1, start_time=datetime.fromtimestamp(1661856718) + id=VICTIM_ZERO_ID, + machine_id=1, + start_time=datetime.fromtimestamp(1661856718), + sha256=AGENT_SHA256, ) DESCENDANT_AGENT = Agent( @@ -30,6 +34,7 @@ machine_id=4, start_time=datetime.fromtimestamp(1661856868), parent_id=VICTIM_TWO_ID, + sha256=AGENT_SHA256, ) RUNNING_AGENTS = ( @@ -39,6 +44,7 @@ machine_id=2, start_time=datetime.fromtimestamp(1661856818), parent_id=VICTIM_ZERO_ID, + sha256=AGENT_SHA256, ), DESCENDANT_AGENT, ) @@ -49,6 +55,7 @@ start_time=datetime.fromtimestamp(1661856758), parent_id=VICTIM_ZERO_ID, stop_time=datetime.fromtimestamp(1661856773), + sha256=AGENT_SHA256, ), ) AGENTS = ( @@ -99,6 +106,7 @@ def test_upsert_agent__insert(agent_repository): machine_id=2, start_time=datetime.fromtimestamp(1661858139), parent_id=VICTIM_ZERO_ID, + sha256=AGENT_SHA256, ) agent_repository.upsert_agent(new_agent) @@ -208,7 +216,9 @@ def test_get_progenitor(agent_repository, agent): def test_get_progenitor__id_not_found(agent_repository): - dummy_agent = Agent(id=uuid4(), machine_id=10, start_time=datetime.now(), parent_id=uuid4()) + dummy_agent = Agent( + id=uuid4(), machine_id=10, start_time=datetime.now(), parent_id=uuid4(), sha256=AGENT_SHA256 + ) with pytest.raises(UnknownRecordError): agent_repository.get_progenitor(dummy_agent) diff --git a/monkey/tests/unit_tests/monkey_island/cc/repositories/test_network_model_update_facade.py b/monkey/tests/unit_tests/monkey_island/cc/repositories/test_network_model_update_facade.py index 8ab6220f1b5..a35ac116dd9 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/repositories/test_network_model_update_facade.py +++ b/monkey/tests/unit_tests/monkey_island/cc/repositories/test_network_model_update_facade.py @@ -32,6 +32,7 @@ class FakeEvent(AbstractAgentEvent): network_interfaces=[IPv4Interface(SOURCE_IP_ADDRESS)], ) +AGENT_SHA256 = "0204d7e486443c17c30a822ac191feca4fcfd038b3a33d8227499a69828dca1f" SOURCE_AGENT_ID = UUID("655fd01c-5eec-4e42-b6e3-1fb738c2978d") SOURCE_AGENT = Agent( id=SOURCE_AGENT_ID, @@ -39,6 +40,7 @@ class FakeEvent(AbstractAgentEvent): start_time=0, parent_id=None, cc_server=(SocketAddress(ip="10.10.10.10", port=5000)), + sha256=AGENT_SHA256, ) TARGET_IP_ADDRESS = IPv4Address("10.10.10.100") diff --git a/monkey/tests/unit_tests/monkey_island/cc/resources/test_agents.py b/monkey/tests/unit_tests/monkey_island/cc/resources/test_agents.py index a7096be592e..97e60648974 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/resources/test_agents.py +++ b/monkey/tests/unit_tests/monkey_island/cc/resources/test_agents.py @@ -22,6 +22,7 @@ "sha256": "cf5c10a8073aa923877ee66df8c1912cac2dbb4b85a97d09cb95d57bde4d2876", } +AGENT_SHA256 = "7ac0f5c62a9bcb81af3e9d67a764d7bbd3cce9af7cd26c211f136400ebe703c4" AGENTS = ( Agent( id=UUID("12345678-1234-1234-1234-123456789abc"), @@ -29,6 +30,7 @@ start_time=0, stop_time=10, cc_server=SocketAddress(ip="10.0.0.1", port=5000), + sha256=AGENT_SHA256, ), Agent( id=UUID("abcdef78-abcd-abcd-abcd-abcdef123456"), @@ -36,6 +38,7 @@ start_time=5, stop_time=15, cc_server=SocketAddress(ip="10.0.0.1", port=5000), + sha256=AGENT_SHA256, ), ) diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/reporting/test_report.py b/monkey/tests/unit_tests/monkey_island/cc/services/reporting/test_report.py index 64afa407749..22b8bc3ac19 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/reporting/test_report.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/reporting/test_report.py @@ -101,6 +101,7 @@ network_interfaces=[IPv4Interface("10.10.10.3/24")], ) +AGENT_SHA256 = "59e822fe452926447efb74fb980e885a84e5c26a0c6bb4ce0634f6982390d50b" AGENTS = [ Agent( id=UUID("2d56f972-78a8-4026-9f47-2dfd550ee207"), @@ -108,6 +109,7 @@ start_time=100, stop_time=500, cc_server=SocketAddress(ip="127.0.0.1", port=5000), + sha256=AGENT_SHA256, ), Agent( id=UUID("65c641f2-af47-4a42-929b-109b30f0d8d6"), @@ -115,6 +117,7 @@ start_time=200, stop_time=600, cc_server=SocketAddress(ip="127.0.0.1", port=5000), + sha256=AGENT_SHA256, ), Agent( id=UUID("290da3c3-f410-4f5e-a472-b04416860a2c"), @@ -122,6 +125,7 @@ start_time=300, stop_time=700, cc_server=SocketAddress(ip="127.0.0.1", port=5000), + sha256=AGENT_SHA256, ), Agent( id=UUID("e5cd334a-5ca5-4f19-a2ab-a68d515fea46"), @@ -129,6 +133,7 @@ start_time=600, stop_time=40309, cc_server=SocketAddress(ip="127.0.0.1", port=5000), + sha256=AGENT_SHA256, ), ] @@ -138,6 +143,7 @@ start_time=601, stop_time=None, cc_server=SocketAddress(ip="127.0.0.1", port=5000), + sha256=AGENT_SHA256, ) NODES = [ diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/test_agent_signals_service.py b/monkey/tests/unit_tests/monkey_island/cc/services/test_agent_signals_service.py index 203c21bb5b1..90762ed0782 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/test_agent_signals_service.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/test_agent_signals_service.py @@ -13,11 +13,13 @@ ) from monkey_island.cc.services import AgentSignalsService +AGENT_SHA256 = "2d374cfed2946b0a69d9f5831b00335b303b0d47e5a89649807d0f87b6748ea0" AGENT_1 = Agent( id=UUID("f811ad00-5a68-4437-bd51-7b5cc1768ad5"), machine_id=1, start_time=100, parent_id=None, + sha256=AGENT_SHA256, ) AGENT_2 = Agent( @@ -25,6 +27,7 @@ machine_id=2, start_time=200, parent_id=AGENT_1.id, + sha256=AGENT_SHA256, ) AGENT_3 = Agent( @@ -33,6 +36,7 @@ registration_time=301, start_time=300, parent_id=AGENT_2.id, + sha256=AGENT_SHA256, ) DUPLICATE_MACHINE_AGENT = Agent( @@ -41,6 +45,7 @@ registration_time=302, start_time=299, parent_id=AGENT_2.id, + sha256=AGENT_SHA256, ) AGENTS = [AGENT_1, AGENT_2, AGENT_3] @@ -51,6 +56,7 @@ start_time=400, stop_time=500, parent_id=AGENT_3.id, + sha256=AGENT_SHA256, ) ALL_AGENTS = [*AGENTS, DUPLICATE_MACHINE_AGENT, STOPPED_AGENT]