Skip to content

Commit 6a5f9a1

Browse files
committed
Agent: Shorten reservation ID
Per Issue #4187, there are cases when download strings must be shorter. In order to achieve this, random strings from a set of 62 characters are generated. Using 5 characters gives something like 916M possible values, which is more than enough for any single agent. Issue #4187 PR #4189
1 parent 7ff053b commit 6a5f9a1

File tree

5 files changed

+44
-53
lines changed

5 files changed

+44
-53
lines changed

monkey/infection_monkey/Pipfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ email-validator = "*"
2626
monkey-types = "*"
2727
monkeyevents = "*"
2828
monkeytoolbox = "*"
29-
monkey-agentpluginapi = ">=0.7.0"
29+
monkey-agentpluginapi = "*"
3030

3131
[dev-packages]
3232
mypy = "*"

monkey/infection_monkey/Pipfile.lock

+13-25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

monkey/infection_monkey/exploit/http_agent_binary_server.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from http.server import HTTPServer
44
from ipaddress import IPv4Address
55
from typing import Callable, Optional, Type
6-
from uuid import uuid4
76

87
from agentpluginapi import (
98
AgentBinaryDownloadReservation,
@@ -12,7 +11,11 @@
1211
LocalMachineInfo,
1312
ReservationID,
1413
)
15-
from monkeytoolbox import create_daemon_thread, insecure_generate_random_string
14+
from monkeytoolbox import (
15+
create_daemon_thread,
16+
insecure_generate_random_string,
17+
secure_generate_random_string,
18+
)
1619
from monkeytypes import Event, Lock, NetworkPort, OperatingSystem
1720

1821
from .http_agent_binary_request_handler import AgentBinaryHTTPRequestHandler
@@ -79,7 +82,7 @@ def register(
7982
if not self.server_is_running():
8083
self._start_server()
8184

82-
reservation_id = uuid4()
85+
reservation_id = secure_generate_random_string(n=5)
8386
url = self._build_request_url(reservation_id, operating_system, requestor_ip)
8487
reservation = AgentBinaryDownloadReservation(
8588
reservation_id,

monkey/tests/unit_tests/infection_monkey/exploit/test_http_agent_binary_request_handler.py

+20-20
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from http import HTTPStatus
33
from http.server import HTTPServer
44
from io import BytesIO
5-
from typing import Type
5+
from typing import Final, Type
66
from unittest.mock import MagicMock
77

88
import pytest
@@ -20,16 +20,15 @@
2020
get_http_handler,
2121
)
2222

23-
AGENT_BINARY = b"agent_binary"
24-
DROPPER_BINARY = b"dropper_agent_binary"
25-
IP = "127.0.0.1"
26-
UUID_1 = ReservationID("00000000-0000-0000-0000-000000000001")
27-
UUID_2 = ReservationID("00000000-0000-0000-0000-000000000002")
28-
UUID_3 = ReservationID("00000000-0000-0000-0000-000000000003")
23+
AGENT_BINARY: Final = b"agent_binary"
24+
DROPPER_BINARY: Final = b"dropper_agent_binary"
25+
IP: Final = "127.0.0.1"
26+
RESERVATION_ID_1: Final = ReservationID("abcABC1")
27+
RESERVATION_ID_2: Final = ReservationID("abcABC2")
28+
RESERVATION_ID_3: Final = ReservationID("abcABC2")
2929

30-
31-
DEFAULT_AGENT_TEMPLATE = b"%(agent_binary)s"
32-
DROPPER_AGENT_TEMPLATE = b"dropper_%(agent_binary)s"
30+
DEFAULT_AGENT_TEMPLATE: Final = b"%(agent_binary)s"
31+
DROPPER_AGENT_TEMPLATE: Final = b"dropper_%(agent_binary)s"
3332

3433

3534
@pytest.fixture
@@ -40,54 +39,54 @@ def port(tcp_port_selector) -> int:
4039
@pytest.fixture
4140
def binary_request_1(port) -> AgentBinaryDownloadReservation:
4241
return AgentBinaryDownloadReservation(
43-
UUID_1,
42+
RESERVATION_ID_1,
4443
OperatingSystem.LINUX,
4544
DEFAULT_AGENT_TEMPLATE,
46-
f"http://{IP}:{port}/{UUID_1}",
45+
f"http://{IP}:{port}/{RESERVATION_ID_1}",
4746
threading.Event(),
4847
)
4948

5049

5150
@pytest.fixture
5251
def binary_request_2(port) -> AgentBinaryDownloadReservation:
5352
return AgentBinaryDownloadReservation(
54-
UUID_2,
53+
RESERVATION_ID_2,
5554
OperatingSystem.WINDOWS,
5655
DEFAULT_AGENT_TEMPLATE,
57-
f"http://{IP}:{port}/{UUID_2}",
56+
f"http://{IP}:{port}/{RESERVATION_ID_2}",
5857
threading.Event(),
5958
)
6059

6160

6261
@pytest.fixture
6362
def binary_request_3(port) -> AgentBinaryDownloadReservation:
6463
return AgentBinaryDownloadReservation(
65-
UUID_2,
64+
RESERVATION_ID_2,
6665
OperatingSystem.WINDOWS,
6766
None,
68-
f"http://{IP}:{port}/{UUID_2}",
67+
f"http://{IP}:{port}/{RESERVATION_ID_2}",
6968
threading.Event(),
7069
)
7170

7271

7372
@pytest.fixture
7473
def dropper_request_1(port) -> AgentBinaryDownloadReservation:
7574
return AgentBinaryDownloadReservation(
76-
UUID_1,
75+
RESERVATION_ID_1,
7776
OperatingSystem.LINUX,
7877
DROPPER_AGENT_TEMPLATE,
79-
f"http://{IP}:{port}/{UUID_1}",
78+
f"http://{IP}:{port}/{RESERVATION_ID_1}",
8079
threading.Event(),
8180
)
8281

8382

8483
@pytest.fixture
8584
def dropper_request_2(port) -> AgentBinaryDownloadReservation:
8685
return AgentBinaryDownloadReservation(
87-
UUID_2,
86+
RESERVATION_ID_2,
8887
OperatingSystem.WINDOWS,
8988
DROPPER_AGENT_TEMPLATE,
90-
f"http://{IP}:{port}/{UUID_2}",
89+
f"http://{IP}:{port}/{RESERVATION_ID_2}",
9190
threading.Event(),
9291
)
9392

@@ -302,3 +301,4 @@ def test_agent_binary_request__is_transformed(
302301

303302
assert response.status_code == HTTPStatus.OK
304303
assert response.content == DROPPER_BINARY
304+
assert response.content == DROPPER_BINARY

monkey/tests/unit_tests/infection_monkey/exploit/test_http_agent_binary_server.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from multiprocessing.managers import SyncManager
88
from pathlib import Path
99
from queue import Queue
10-
from typing import List, Tuple, Type
10+
from typing import Final, List, Tuple, Type
1111
from unittest.mock import MagicMock
1212

1313
import pytest
@@ -19,8 +19,8 @@
1919
from infection_monkey.exploit.http_agent_binary_server import HTTPAgentBinaryServer
2020
from infection_monkey.network import TCPPortSelector
2121

22-
REQUESTOR_IP = IPv4Address("1.1.1.1")
23-
UUID_1 = ReservationID("00000000-0000-0000-0000-000000000001")
22+
REQUESTOR_IP: Final = IPv4Address("1.1.1.1")
23+
RESERVATION_ID_1: Final = ReservationID("abcdABCD1")
2424

2525

2626
def use_agent_binary(agent_binary: bytes) -> bytes:
@@ -188,7 +188,7 @@ def test_deregister__raises_error_on_invalid_reservation_id(
188188
mock_http_handler = mock_agent_binary_http_handler
189189
mock_http_handler.clear_reservation_mock.side_effect = KeyError # type: ignore[attr-defined]
190190
with pytest.raises(KeyError):
191-
http_agent_binary_server.deregister(UUID_1)
191+
http_agent_binary_server.deregister(RESERVATION_ID_1)
192192

193193

194194
@pytest.mark.xdist_group(name="tcp_port_selector")

0 commit comments

Comments
 (0)