|
| 1 | +from ipaddress import IPv4Address |
| 2 | +from threading import Event |
| 3 | +from typing import Callable |
| 4 | +from unittest.mock import MagicMock |
| 5 | + |
| 6 | +import pytest |
| 7 | +from agent_plugins.exploiters.snmp.src.snmp_exploiter import ( |
| 8 | + CommunityStringGenerator, |
| 9 | + SNMPExploiter, |
| 10 | + StubSNMPExploitClient, |
| 11 | + StubSNMPOptions, |
| 12 | +) |
| 13 | + |
| 14 | +from common import OperatingSystem |
| 15 | +from infection_monkey.exploit import IAgentOTPProvider |
| 16 | +from infection_monkey.exploit.tools import HTTPBytesServer |
| 17 | +from infection_monkey.i_puppet import ExploiterResultData, TargetHost |
| 18 | +from infection_monkey.utils.ids import get_agent_id |
| 19 | + |
| 20 | +AGENT_ID = get_agent_id() |
| 21 | +TARGET_IP = IPv4Address("1.1.1.1") |
| 22 | +SERVERS = ["10.10.10.10"] |
| 23 | +DOWNLOAD_URL = "http://download.me" |
| 24 | +COMMUNITY_STRINGS = ["public", "private"] |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture() |
| 28 | +def target_host() -> TargetHost: |
| 29 | + return TargetHost(ip=IPv4Address("1.1.1.1"), operating_system=OperatingSystem.LINUX) |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def mock_bytes_server() -> HTTPBytesServer: |
| 34 | + mock_bytes_server = MagicMock(spec=HTTPBytesServer) |
| 35 | + mock_bytes_server.download_url = DOWNLOAD_URL |
| 36 | + mock_bytes_server.bytes_downloaded = Event() |
| 37 | + mock_bytes_server.bytes_downloaded.set() |
| 38 | + return mock_bytes_server |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture |
| 42 | +def mock_snmp_exploit_client() -> StubSNMPExploitClient: |
| 43 | + mock_snmp_exploit_client = MagicMock() |
| 44 | + mock_snmp_exploit_client.exploit_host.return_value = (False, False) |
| 45 | + return mock_snmp_exploit_client |
| 46 | + |
| 47 | + |
| 48 | +@pytest.fixture |
| 49 | +def mock_start_agent_binary_server(mock_bytes_server) -> HTTPBytesServer: |
| 50 | + return MagicMock(return_value=mock_bytes_server) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.fixture |
| 54 | +def mock_otp_provider(): |
| 55 | + mock_otp_provider = MagicMock(spec=IAgentOTPProvider) |
| 56 | + mock_otp_provider.get_otp.return_value = "123456" |
| 57 | + return mock_otp_provider |
| 58 | + |
| 59 | + |
| 60 | +@pytest.fixture |
| 61 | +def mock_community_string_generator(): |
| 62 | + return MagicMock(return_value=COMMUNITY_STRINGS) |
| 63 | + |
| 64 | + |
| 65 | +@pytest.fixture |
| 66 | +def snmp_exploiter( |
| 67 | + mock_snmp_exploit_client: StubSNMPExploitClient, |
| 68 | + mock_start_agent_binary_server: Callable[[TargetHost], HTTPBytesServer], |
| 69 | + mock_community_string_generator: CommunityStringGenerator, |
| 70 | + mock_otp_provider: IAgentOTPProvider, |
| 71 | +) -> SNMPExploiter: |
| 72 | + return SNMPExploiter( |
| 73 | + AGENT_ID, |
| 74 | + mock_snmp_exploit_client, |
| 75 | + mock_start_agent_binary_server, |
| 76 | + mock_community_string_generator, |
| 77 | + mock_otp_provider, |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +@pytest.fixture |
| 82 | +def exploit_host( |
| 83 | + snmp_exploiter: SNMPExploiter, target_host: TargetHost |
| 84 | +) -> Callable[[], ExploiterResultData]: |
| 85 | + def _inner() -> ExploiterResultData: |
| 86 | + return snmp_exploiter.exploit_host( |
| 87 | + host=target_host, |
| 88 | + servers=SERVERS, |
| 89 | + current_depth=1, |
| 90 | + options=StubSNMPOptions(), |
| 91 | + interrupt=Event(), |
| 92 | + ) |
| 93 | + |
| 94 | + return _inner |
| 95 | + |
| 96 | + |
| 97 | +def test_exploit_host__succeeds(exploit_host, mock_snmp_exploit_client, mock_bytes_server): |
| 98 | + mock_snmp_exploit_client.exploit_host.return_value = (True, True) |
| 99 | + result = exploit_host() |
| 100 | + |
| 101 | + assert mock_bytes_server.stop.called |
| 102 | + assert result.exploitation_success |
| 103 | + assert result.propagation_success |
| 104 | + |
| 105 | + |
| 106 | +def test_exploit_host__fails_if_server_fails_to_start( |
| 107 | + exploit_host, mock_start_agent_binary_server, mock_bytes_server |
| 108 | +): |
| 109 | + mock_start_agent_binary_server.side_effect = Exception() |
| 110 | + result = exploit_host() |
| 111 | + |
| 112 | + assert not mock_bytes_server.stop.called |
| 113 | + assert not result.exploitation_success |
| 114 | + assert not result.propagation_success |
| 115 | + |
| 116 | + |
| 117 | +def test_exploit_host__success_returned_on_server_stop_fail( |
| 118 | + exploit_host, mock_snmp_exploit_client, mock_bytes_server |
| 119 | +): |
| 120 | + mock_snmp_exploit_client.exploit_host.return_value = (True, True) |
| 121 | + mock_bytes_server.stop.side_effect = Exception() |
| 122 | + |
| 123 | + result = exploit_host() |
| 124 | + |
| 125 | + assert mock_bytes_server.stop.called |
| 126 | + assert result.exploitation_success |
| 127 | + assert result.propagation_success |
| 128 | + |
| 129 | + |
| 130 | +def test_exploit_host__fails_on_snmp_exception( |
| 131 | + mock_snmp_exploit_client, exploit_host, mock_bytes_server |
| 132 | +): |
| 133 | + mock_snmp_exploit_client.exploit_host.side_effect = Exception() |
| 134 | + result = exploit_host() |
| 135 | + |
| 136 | + assert mock_bytes_server.stop.called |
| 137 | + assert not result.exploitation_success |
| 138 | + assert not result.propagation_success |
| 139 | + |
| 140 | + |
| 141 | +def test_exploit_attempt_on_all_community_strings( |
| 142 | + snmp_exploiter: SNMPExploiter, |
| 143 | + mock_snmp_exploit_client: StubSNMPExploitClient, |
| 144 | + target_host: TargetHost, |
| 145 | +): |
| 146 | + snmp_exploiter.exploit_host( |
| 147 | + host=target_host, |
| 148 | + servers=SERVERS, |
| 149 | + current_depth=1, |
| 150 | + options=StubSNMPOptions(), |
| 151 | + interrupt=Event(), |
| 152 | + ) |
| 153 | + |
| 154 | + community_strings_passed_to_exploit = [ |
| 155 | + args[0][2] for args in mock_snmp_exploit_client.exploit_host.call_args_list |
| 156 | + ] |
| 157 | + |
| 158 | + assert len(community_strings_passed_to_exploit) == len(COMMUNITY_STRINGS) |
| 159 | + for community_string in COMMUNITY_STRINGS: |
| 160 | + assert community_string in community_strings_passed_to_exploit |
| 161 | + |
| 162 | + |
| 163 | +def test_exploit_attempt_skipped_on_interrupt( |
| 164 | + snmp_exploiter: SNMPExploiter, |
| 165 | + mock_snmp_exploit_client: StubSNMPExploitClient, |
| 166 | + target_host: TargetHost, |
| 167 | +): |
| 168 | + interrupt = Event() |
| 169 | + interrupt.set() |
| 170 | + snmp_exploiter.exploit_host( |
| 171 | + host=target_host, |
| 172 | + servers=SERVERS, |
| 173 | + current_depth=1, |
| 174 | + options=StubSNMPOptions(), |
| 175 | + interrupt=interrupt, |
| 176 | + ) |
| 177 | + |
| 178 | + assert mock_snmp_exploit_client.exploit_host.call_count == 0 |
0 commit comments