Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3244 agent sha256 #3347

Merged
merged 2 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions monkey/monkey_island/cc/models/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -42,6 +46,7 @@
machine_id=4,
start_time=600,
stop_time=700,
sha256=AGENT_SHA256,
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 15 additions & 0 deletions monkey/tests/unit_tests/monkey_island/cc/models/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@

AGENT_ID = UUID("012e7238-7b81-4108-8c7f-0787bc3f3c10")
PARENT_ID = UUID("0fc9afcb-1902-436b-bd5c-1ad194252484")
SHA256 = "6b524293febf78ac659ce4ca368b8fc74df6e14462e12a43e4044eafe2a5f947"

AGENT_OBJECT_DICT = {
"id": AGENT_ID,
"machine_id": 2,
"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 = {
Expand All @@ -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,
}


Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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!"
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)


Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@
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(
id=VICTIM_THREE_ID,
machine_id=4,
start_time=datetime.fromtimestamp(1661856868),
parent_id=VICTIM_TWO_ID,
sha256=AGENT_SHA256,
)

RUNNING_AGENTS = (
Expand All @@ -39,6 +44,7 @@
machine_id=2,
start_time=datetime.fromtimestamp(1661856818),
parent_id=VICTIM_ZERO_ID,
sha256=AGENT_SHA256,
),
DESCENDANT_AGENT,
)
Expand All @@ -49,6 +55,7 @@
start_time=datetime.fromtimestamp(1661856758),
parent_id=VICTIM_ZERO_ID,
stop_time=datetime.fromtimestamp(1661856773),
sha256=AGENT_SHA256,
),
)
AGENTS = (
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ 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,
machine_id=SOURCE_MACHINE_ID,
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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,23 @@
"sha256": "cf5c10a8073aa923877ee66df8c1912cac2dbb4b85a97d09cb95d57bde4d2876",
}

AGENT_SHA256 = "7ac0f5c62a9bcb81af3e9d67a764d7bbd3cce9af7cd26c211f136400ebe703c4"
AGENTS = (
Agent(
id=UUID("12345678-1234-1234-1234-123456789abc"),
machine_id=2,
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"),
machine_id=3,
start_time=5,
stop_time=15,
cc_server=SocketAddress(ip="10.0.0.1", port=5000),
sha256=AGENT_SHA256,
),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,34 +101,39 @@
network_interfaces=[IPv4Interface("10.10.10.3/24")],
)

AGENT_SHA256 = "59e822fe452926447efb74fb980e885a84e5c26a0c6bb4ce0634f6982390d50b"
AGENTS = [
Agent(
id=UUID("2d56f972-78a8-4026-9f47-2dfd550ee207"),
machine_id=1,
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"),
machine_id=2,
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"),
machine_id=3,
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"),
machine_id=1,
start_time=600,
stop_time=40309,
cc_server=SocketAddress(ip="127.0.0.1", port=5000),
sha256=AGENT_SHA256,
),
]

Expand All @@ -138,6 +143,7 @@
start_time=601,
stop_time=None,
cc_server=SocketAddress(ip="127.0.0.1", port=5000),
sha256=AGENT_SHA256,
)

NODES = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@
)
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(
id=UUID("012e7238-7b81-4108-8c7f-0787bc3f3c10"),
machine_id=2,
start_time=200,
parent_id=AGENT_1.id,
sha256=AGENT_SHA256,
)

AGENT_3 = Agent(
Expand All @@ -33,6 +36,7 @@
registration_time=301,
start_time=300,
parent_id=AGENT_2.id,
sha256=AGENT_SHA256,
)

DUPLICATE_MACHINE_AGENT = Agent(
Expand All @@ -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]
Expand All @@ -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]
Expand Down