Skip to content

Commit 2c7220a

Browse files
committed
BB: Test unique agent hashes (polymorphism)
Issue #3244
1 parent 1d29e18 commit 2c7220a

File tree

5 files changed

+62
-7
lines changed

5 files changed

+62
-7
lines changed

envs/monkey_zoo/blackbox/test_blackbox.py

+48-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from http import HTTPStatus
55
from threading import Thread
66
from time import sleep
7-
from typing import List, Optional
7+
from typing import List, Optional, Sequence
88
from uuid import uuid4
99

1010
import pytest
@@ -48,6 +48,7 @@
4848
start_machines,
4949
stop_machines,
5050
)
51+
from monkey_island.cc.models import Agent
5152
from monkey_island.cc.services.authentication_service.flask_resources.agent_otp import (
5253
MAX_OTP_REQUESTS_PER_SECOND,
5354
)
@@ -483,6 +484,12 @@ def test_agent_logout(island):
483484
# to boot up and finish starting services.
484485
# noinspection PyUnresolvedReferences
485486
class TestMonkeyBlackbox:
487+
@staticmethod
488+
def assert_unique_agent_hashes(agents: Sequence[Agent]):
489+
agent_hashes = [a.sha256 for a in agents]
490+
491+
assert len(agent_hashes) == len(set(agent_hashes))
492+
486493
@staticmethod
487494
def run_exploitation_test(
488495
island_client: MonkeyIslandClient,
@@ -518,9 +525,25 @@ def test_credentials_reuse_ssh_key(self, island_client):
518525
)
519526

520527
def test_depth_2_a(self, island_client):
521-
TestMonkeyBlackbox.run_exploitation_test(
522-
island_client, depth_2_a_test_configuration, "Depth2A test suite"
528+
test_name = "Depth2A test suite"
529+
communication_analyzer = CommunicationAnalyzer(
530+
island_client,
531+
get_target_ips(depth_2_a_test_configuration),
532+
)
533+
log_handler = TestLogsHandler(
534+
test_name, island_client, TestMonkeyBlackbox.get_log_dir_path()
535+
)
536+
exploitation_test = ExploitationTest(
537+
name=test_name,
538+
island_client=island_client,
539+
test_configuration=depth_2_a_test_configuration,
540+
masque=None,
541+
analyzers=[communication_analyzer],
542+
timeout=DEFAULT_TIMEOUT_SECONDS + 30,
543+
log_handler=log_handler,
523544
)
545+
exploitation_test.run()
546+
assert len({a.sha256 for a in exploitation_test.agents}) == 2
524547

525548
def test_depth_1_a(self, island_client):
526549
test_name = "Depth1A test suite"
@@ -559,20 +582,38 @@ def test_depth_1_a(self, island_client):
559582
log_handler = TestLogsHandler(
560583
test_name, island_client, TestMonkeyBlackbox.get_log_dir_path()
561584
)
562-
ExploitationTest(
585+
exploitation_test = ExploitationTest(
563586
name=test_name,
564587
island_client=island_client,
565588
test_configuration=depth_1_a_test_configuration,
566589
masque=masque,
567590
analyzers=[stolen_credentials_analyzer, communication_analyzer],
568591
timeout=DEFAULT_TIMEOUT_SECONDS + 30,
569592
log_handler=log_handler,
570-
).run()
593+
)
594+
exploitation_test.run()
595+
TestMonkeyBlackbox.assert_unique_agent_hashes(exploitation_test.agents)
571596

572597
def test_depth_3_a(self, island_client):
573-
TestMonkeyBlackbox.run_exploitation_test(
574-
island_client, depth_3_a_test_configuration, "Depth3A test suite"
598+
test_name = "Depth3A test suite"
599+
communication_analyzer = CommunicationAnalyzer(
600+
island_client,
601+
get_target_ips(depth_3_a_test_configuration),
602+
)
603+
log_handler = TestLogsHandler(
604+
test_name, island_client, TestMonkeyBlackbox.get_log_dir_path()
605+
)
606+
exploitation_test = ExploitationTest(
607+
name=test_name,
608+
island_client=island_client,
609+
test_configuration=depth_3_a_test_configuration,
610+
masque=None,
611+
analyzers=[communication_analyzer],
612+
timeout=DEFAULT_TIMEOUT_SECONDS,
613+
log_handler=log_handler,
575614
)
615+
exploitation_test.run()
616+
TestMonkeyBlackbox.assert_unique_agent_hashes(exploitation_test.agents)
576617

577618
def test_depth_4_a(self, island_client):
578619
TestMonkeyBlackbox.run_exploitation_test(

envs/monkey_zoo/blackbox/test_configurations/depth_1_a.py

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
replace_agent_configuration,
1616
replace_propagation_credentials,
1717
set_maximum_depth,
18+
set_randomize_agent_hash,
1819
)
1920

2021
# Tests:
@@ -95,6 +96,7 @@ def _add_http_ports(agent_configuration: AgentConfiguration) -> AgentConfigurati
9596
test_agent_configuration = _add_tcp_ports(test_agent_configuration)
9697
test_agent_configuration = _add_credentials_collectors(test_agent_configuration)
9798
test_agent_configuration = _add_http_ports(test_agent_configuration)
99+
test_agent_configuration = set_randomize_agent_hash(test_agent_configuration, True)
98100

99101
CREDENTIALS = (
100102
Credentials(identity=Username(username="m0nk3y"), secret=None),

envs/monkey_zoo/blackbox/test_configurations/depth_3_a.py

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
replace_propagation_credentials,
1414
set_keep_tunnel_open_time,
1515
set_maximum_depth,
16+
set_randomize_agent_hash,
1617
)
1718

1819
# Tests:
@@ -54,6 +55,7 @@ def _add_tcp_ports(agent_configuration: AgentConfiguration) -> AgentConfiguratio
5455
test_agent_configuration = _add_exploiters(test_agent_configuration)
5556
test_agent_configuration = _add_subnets(test_agent_configuration)
5657
test_agent_configuration = _add_tcp_ports(test_agent_configuration)
58+
test_agent_configuration = set_randomize_agent_hash(test_agent_configuration, True)
5759

5860
CREDENTIALS = (
5961
Credentials(identity=Username(username="m0nk3y"), secret=None),

envs/monkey_zoo/blackbox/test_configurations/utils.py

+6
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ def set_maximum_depth(
8080
return agent_configuration_copy
8181

8282

83+
def set_randomize_agent_hash(agent_configuration: AgentConfiguration, value: bool):
84+
agent_configuration.polymorphism.randomize_agent_hash = value
85+
86+
return agent_configuration
87+
88+
8389
def replace_agent_configuration(
8490
test_configuration: TestConfiguration, agent_configuration: AgentConfiguration
8591
):

envs/monkey_zoo/blackbox/tests/exploitation.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import logging
22
from datetime import datetime
33
from time import sleep
4+
from typing import Sequence
45

56
from envs.monkey_zoo.blackbox.island_client.test_configuration_parser import get_target_ips
67
from envs.monkey_zoo.blackbox.tests.basic_test import BasicTest
78
from envs.monkey_zoo.blackbox.utils import bb_singleton
89
from envs.monkey_zoo.blackbox.utils.test_timer import TestTimer
10+
from monkey_island.cc.models import Agent
911

1012
MAX_TIME_FOR_MONKEYS_TO_DIE = 2 * 60
1113
WAIT_TIME_BETWEEN_REQUESTS = 1
@@ -25,6 +27,7 @@ def __init__(
2527
self.analyzers = analyzers
2628
self.timeout = timeout
2729
self.log_handler = log_handler
30+
self.agents: Sequence[Agent] = []
2831

2932
def run(self):
3033
bb_singleton.start_time = datetime.now()
@@ -39,6 +42,7 @@ def run(self):
3942
self.wait_until_monkeys_die()
4043
self.wait_for_monkey_process_to_finish()
4144
self.parse_logs()
45+
self.agents = self.island_client.get_agents()
4246
self.island_client.reset_island()
4347

4448
def print_test_starting_info(self):

0 commit comments

Comments
 (0)