Skip to content

Commit

Permalink
Merge branch '3244-ete-test' into develop
Browse files Browse the repository at this point in the history
Issue #3244
PR #3350
  • Loading branch information
mssalvatore committed May 17, 2023
2 parents cfd949a + a207026 commit 95ff320
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
57 changes: 50 additions & 7 deletions envs/monkey_zoo/blackbox/test_blackbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from http import HTTPStatus
from threading import Thread
from time import sleep
from typing import List, Optional
from typing import List, Optional, Sequence
from uuid import uuid4

import pytest
Expand Down Expand Up @@ -48,6 +48,7 @@
start_machines,
stop_machines,
)
from monkey_island.cc.models import Agent
from monkey_island.cc.services.authentication_service.flask_resources.agent_otp import (
MAX_OTP_REQUESTS_PER_SECOND,
)
Expand Down Expand Up @@ -483,6 +484,12 @@ def test_agent_logout(island):
# to boot up and finish starting services.
# noinspection PyUnresolvedReferences
class TestMonkeyBlackbox:
@staticmethod
def assert_unique_agent_hashes(agents: Sequence[Agent]):
agent_hashes = [a.sha256 for a in agents]

assert len(agent_hashes) == len(set(agent_hashes))

@staticmethod
def run_exploitation_test(
island_client: MonkeyIslandClient,
Expand Down Expand Up @@ -518,9 +525,27 @@ def test_credentials_reuse_ssh_key(self, island_client):
)

def test_depth_2_a(self, island_client):
TestMonkeyBlackbox.run_exploitation_test(
island_client, depth_2_a_test_configuration, "Depth2A test suite"
test_name = "Depth2A test suite"
communication_analyzer = CommunicationAnalyzer(
island_client,
get_target_ips(depth_2_a_test_configuration),
)
log_handler = TestLogsHandler(
test_name, island_client, TestMonkeyBlackbox.get_log_dir_path()
)
exploitation_test = ExploitationTest(
name=test_name,
island_client=island_client,
test_configuration=depth_2_a_test_configuration,
masque=None,
analyzers=[communication_analyzer],
timeout=DEFAULT_TIMEOUT_SECONDS + 30,
log_handler=log_handler,
)
exploitation_test.run()

# asserting that Agent hashes are not unique
assert len({a.sha256 for a in exploitation_test.agents}) == 2

def test_depth_1_a(self, island_client):
test_name = "Depth1A test suite"
Expand Down Expand Up @@ -559,20 +584,38 @@ def test_depth_1_a(self, island_client):
log_handler = TestLogsHandler(
test_name, island_client, TestMonkeyBlackbox.get_log_dir_path()
)
ExploitationTest(
exploitation_test = ExploitationTest(
name=test_name,
island_client=island_client,
test_configuration=depth_1_a_test_configuration,
masque=masque,
analyzers=[stolen_credentials_analyzer, communication_analyzer],
timeout=DEFAULT_TIMEOUT_SECONDS + 30,
log_handler=log_handler,
).run()
)
exploitation_test.run()
TestMonkeyBlackbox.assert_unique_agent_hashes(exploitation_test.agents)

def test_depth_3_a(self, island_client):
TestMonkeyBlackbox.run_exploitation_test(
island_client, depth_3_a_test_configuration, "Depth3A test suite"
test_name = "Depth3A test suite"
communication_analyzer = CommunicationAnalyzer(
island_client,
get_target_ips(depth_3_a_test_configuration),
)
log_handler = TestLogsHandler(
test_name, island_client, TestMonkeyBlackbox.get_log_dir_path()
)
exploitation_test = ExploitationTest(
name=test_name,
island_client=island_client,
test_configuration=depth_3_a_test_configuration,
masque=None,
analyzers=[communication_analyzer],
timeout=DEFAULT_TIMEOUT_SECONDS,
log_handler=log_handler,
)
exploitation_test.run()
TestMonkeyBlackbox.assert_unique_agent_hashes(exploitation_test.agents)

def test_depth_4_a(self, island_client):
TestMonkeyBlackbox.run_exploitation_test(
Expand Down
2 changes: 2 additions & 0 deletions envs/monkey_zoo/blackbox/test_configurations/depth_1_a.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
replace_agent_configuration,
replace_propagation_credentials,
set_maximum_depth,
set_randomize_agent_hash,
)

# Tests:
Expand Down Expand Up @@ -95,6 +96,7 @@ def _add_http_ports(agent_configuration: AgentConfiguration) -> AgentConfigurati
test_agent_configuration = _add_tcp_ports(test_agent_configuration)
test_agent_configuration = _add_credentials_collectors(test_agent_configuration)
test_agent_configuration = _add_http_ports(test_agent_configuration)
test_agent_configuration = set_randomize_agent_hash(test_agent_configuration, True)

CREDENTIALS = (
Credentials(identity=Username(username="m0nk3y"), secret=None),
Expand Down
2 changes: 2 additions & 0 deletions envs/monkey_zoo/blackbox/test_configurations/depth_3_a.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
replace_propagation_credentials,
set_keep_tunnel_open_time,
set_maximum_depth,
set_randomize_agent_hash,
)

# Tests:
Expand Down Expand Up @@ -54,6 +55,7 @@ def _add_tcp_ports(agent_configuration: AgentConfiguration) -> AgentConfiguratio
test_agent_configuration = _add_exploiters(test_agent_configuration)
test_agent_configuration = _add_subnets(test_agent_configuration)
test_agent_configuration = _add_tcp_ports(test_agent_configuration)
test_agent_configuration = set_randomize_agent_hash(test_agent_configuration, True)

CREDENTIALS = (
Credentials(identity=Username(username="m0nk3y"), secret=None),
Expand Down
6 changes: 6 additions & 0 deletions envs/monkey_zoo/blackbox/test_configurations/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ def set_maximum_depth(
return agent_configuration_copy


def set_randomize_agent_hash(agent_configuration: AgentConfiguration, value: bool):
agent_configuration.polymorphism.randomize_agent_hash = value

return agent_configuration


def replace_agent_configuration(
test_configuration: TestConfiguration, agent_configuration: AgentConfiguration
):
Expand Down
4 changes: 4 additions & 0 deletions envs/monkey_zoo/blackbox/tests/exploitation.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import logging
from datetime import datetime
from time import sleep
from typing import Sequence

from envs.monkey_zoo.blackbox.island_client.test_configuration_parser import get_target_ips
from envs.monkey_zoo.blackbox.tests.basic_test import BasicTest
from envs.monkey_zoo.blackbox.utils import bb_singleton
from envs.monkey_zoo.blackbox.utils.test_timer import TestTimer
from monkey_island.cc.models import Agent

MAX_TIME_FOR_MONKEYS_TO_DIE = 2 * 60
WAIT_TIME_BETWEEN_REQUESTS = 1
Expand All @@ -25,6 +27,7 @@ def __init__(
self.analyzers = analyzers
self.timeout = timeout
self.log_handler = log_handler
self.agents: Sequence[Agent] = []

def run(self):
bb_singleton.start_time = datetime.now()
Expand All @@ -39,6 +42,7 @@ def run(self):
self.wait_until_monkeys_die()
self.wait_for_monkey_process_to_finish()
self.parse_logs()
self.agents = self.island_client.get_agents()
self.island_client.reset_island()

def print_test_starting_info(self):
Expand Down

0 comments on commit 95ff320

Please sign in to comment.