From 2be0e088e0162d340055e76a78f3fe72f65bb9db Mon Sep 17 00:00:00 2001 From: Shreya Malviya Date: Fri, 5 Aug 2022 13:35:39 +0530 Subject: [PATCH] Island: Fix all references to 'local' IPs which may not always be local --- monkey/monkey_island/cc/models/monkey.py | 4 ++-- monkey/monkey_island/cc/resources/ip_addresses.py | 12 ++++++------ monkey/monkey_island/cc/server_setup.py | 6 ++---- monkey/monkey_island/cc/services/initialize.py | 4 ++-- monkey/monkey_island/cc/services/node.py | 10 +++++----- monkey/monkey_island/cc/services/reporting/report.py | 4 ++-- monkey/monkey_island/cc/services/run_local_monkey.py | 6 +++--- .../monkey_island/cc/services/utils/network_utils.py | 2 +- 8 files changed, 23 insertions(+), 25 deletions(-) diff --git a/monkey/monkey_island/cc/models/monkey.py b/monkey/monkey_island/cc/models/monkey.py index 3e518444d..003e14509 100644 --- a/monkey/monkey_island/cc/models/monkey.py +++ b/monkey/monkey_island/cc/models/monkey.py @@ -19,7 +19,7 @@ from mongoengine import ( from monkey_island.cc.models.command_control_channel import CommandControlChannel from monkey_island.cc.models.monkey_ttl import MonkeyTtl, create_monkey_ttl_document from monkey_island.cc.server_utils.consts import DEFAULT_MONKEY_TTL_EXPIRY_DURATION_IN_SECONDS -from monkey_island.cc.services.utils.network_utils import get_local_ip_addresses +from monkey_island.cc.services.utils.network_utils import get_ip_addresses class ParentNotFoundError(Exception): @@ -123,7 +123,7 @@ class Monkey(Document): def get_label_by_id(object_id): current_monkey = Monkey.get_single_monkey_by_id(object_id) label = Monkey.get_hostname_by_id(object_id) + " : " + current_monkey.ip_addresses[0] - if len(set(current_monkey.ip_addresses).intersection(get_local_ip_addresses())) > 0: + if len(set(current_monkey.ip_addresses).intersection(get_ip_addresses())) > 0: label = "MonkeyIsland - " + label return label diff --git a/monkey/monkey_island/cc/resources/ip_addresses.py b/monkey/monkey_island/cc/resources/ip_addresses.py index 981adfb8d..3ba7d6123 100644 --- a/monkey/monkey_island/cc/resources/ip_addresses.py +++ b/monkey/monkey_island/cc/resources/ip_addresses.py @@ -6,20 +6,20 @@ from monkey_island.cc.resources.request_authentication import jwt_required class IPAddresses(AbstractResource): """ - Endpoint for the Monkey Island's local IP addresses + Endpoint for the Monkey Island's IP addresses """ urls = ["/api/island/ip-addresses"] - def __init__(self, local_ip_addresses: Sequence[str]): - self._local_ips = local_ip_addresses + def __init__(self, ip_addresses: Sequence[str]): + self._ips = ip_addresses @jwt_required def get(self) -> Sequence[str]: """ - Sends the local IP addresses of the Island + Sends the IP addresses of the Island - :return: Local IPs + :return: IP addresses """ - return self._local_ips + return self._ips diff --git a/monkey/monkey_island/cc/server_setup.py b/monkey/monkey_island/cc/server_setup.py index 9dce509ef..eed949a02 100644 --- a/monkey/monkey_island/cc/server_setup.py +++ b/monkey/monkey_island/cc/server_setup.py @@ -27,7 +27,7 @@ from monkey_island.cc.server_utils.consts import ( # noqa: E402 ) from monkey_island.cc.server_utils.island_logger import reset_logger, setup_logging # noqa: E402 from monkey_island.cc.services.initialize import initialize_services # noqa: E402 -from monkey_island.cc.services.utils.network_utils import get_local_ip_addresses # noqa: E402 +from monkey_island.cc.services.utils.network_utils import get_ip_addresses # noqa: E402 from monkey_island.cc.setup import PyWSGILoggingFilter # noqa: E402 from monkey_island.cc.setup import island_config_options_validator # noqa: E402 from monkey_island.cc.setup.data_dir import IncompatibleDataDirectory, setup_data_dir # noqa: E402 @@ -168,9 +168,7 @@ def _log_init_info(): def _log_web_interface_access_urls(): - web_interface_urls = ", ".join( - [f"https://{ip}:{ISLAND_PORT}" for ip in get_local_ip_addresses()] - ) + web_interface_urls = ", ".join([f"https://{ip}:{ISLAND_PORT}" for ip in get_ip_addresses()]) logger.info( "To access the web interface, navigate to one of the the following URLs using your " f"browser: {web_interface_urls}" diff --git a/monkey/monkey_island/cc/services/initialize.py b/monkey/monkey_island/cc/services/initialize.py index a03f2f6c0..d89d931a0 100644 --- a/monkey/monkey_island/cc/services/initialize.py +++ b/monkey/monkey_island/cc/services/initialize.py @@ -46,7 +46,7 @@ from monkey_island.cc.services.telemetry.processing.credentials.credentials_pars from monkey_island.cc.services.telemetry.processing.processing import ( TELEMETRY_CATEGORY_TO_PROCESSING_FUNC, ) -from monkey_island.cc.services.utils.network_utils import get_local_ip_addresses +from monkey_island.cc.services.utils.network_utils import get_ip_addresses from monkey_island.cc.setup.mongo.mongo_setup import MONGO_URL from monkey_island.cc.version import Version @@ -98,7 +98,7 @@ def _register_conventions(container: DIContainer, data_dir: Path): ) container.register_convention(Path, "island_log_file_path", get_log_file_path(data_dir)) container.register_convention(str, "version_number", get_version()) - container.register_convention(Sequence[str], "local_ip_addresses", get_local_ip_addresses()) + container.register_convention(Sequence[str], "ip_addresses", get_ip_addresses()) def _register_repositories(container: DIContainer, data_dir: Path): diff --git a/monkey/monkey_island/cc/services/node.py b/monkey/monkey_island/cc/services/node.py index e65a410fd..faac42539 100644 --- a/monkey/monkey_island/cc/services/node.py +++ b/monkey/monkey_island/cc/services/node.py @@ -9,7 +9,7 @@ from monkey_island.cc.database import mongo from monkey_island.cc.models import Monkey from monkey_island.cc.services.edge.displayed_edge import DisplayedEdgeService from monkey_island.cc.services.edge.edge import EdgeService -from monkey_island.cc.services.utils.network_utils import get_local_ip_addresses +from monkey_island.cc.services.utils.network_utils import get_ip_addresses from monkey_island.cc.services.utils.node_states import NodeStates @@ -110,7 +110,7 @@ class NodeService: def get_monkey_label(monkey): # todo label = monkey["hostname"] + " : " + monkey["ip_addresses"][0] - ip_addresses = get_local_ip_addresses() + ip_addresses = get_ip_addresses() if len(set(monkey["ip_addresses"]).intersection(ip_addresses)) > 0: label = "MonkeyIsland - " + label return label @@ -118,7 +118,7 @@ class NodeService: @staticmethod def get_monkey_group(monkey): keywords = [] - if len(set(monkey["ip_addresses"]).intersection(get_local_ip_addresses())) != 0: + if len(set(monkey["ip_addresses"]).intersection(get_ip_addresses())) != 0: keywords.extend(["island", "monkey"]) else: monkey_type = "manual" if NodeService.get_monkey_manual_run(monkey) else "monkey" @@ -275,7 +275,7 @@ class NodeService: # It's better to just initialize the island machine on reset I think @staticmethod def get_monkey_island_monkey(): - ip_addresses = get_local_ip_addresses() + ip_addresses = get_ip_addresses() for ip_address in ip_addresses: monkey = NodeService.get_monkey_by_ip(ip_address) if monkey is not None: @@ -297,7 +297,7 @@ class NodeService: @staticmethod def get_monkey_island_node(): island_node = NodeService.get_monkey_island_pseudo_net_node() - island_node["ip_addresses"] = get_local_ip_addresses() + island_node["ip_addresses"] = get_ip_addresses() island_node["domain_name"] = socket.gethostname() return island_node diff --git a/monkey/monkey_island/cc/services/reporting/report.py b/monkey/monkey_island/cc/services/reporting/report.py index c21be0efb..32761c7a6 100644 --- a/monkey/monkey_island/cc/services/reporting/report.py +++ b/monkey/monkey_island/cc/services/reporting/report.py @@ -19,7 +19,7 @@ from monkey_island.cc.services.reporting.pth_report import PTHReportService from monkey_island.cc.services.reporting.report_generation_synchronisation import ( safe_generate_regular_report, ) -from monkey_island.cc.services.utils.network_utils import get_local_ip_addresses, get_subnets +from monkey_island.cc.services.utils.network_utils import get_ip_addresses, get_subnets from .. import AWSService from . import aws_exporter @@ -175,7 +175,7 @@ class ReportService: @staticmethod def get_island_cross_segment_issues(): issues = [] - island_ips = get_local_ip_addresses() + island_ips = get_ip_addresses() for monkey in mongo.db.monkey.find( {"tunnel": {"$exists": False}}, {"tunnel": 1, "guid": 1, "hostname": 1} ): diff --git a/monkey/monkey_island/cc/services/run_local_monkey.py b/monkey/monkey_island/cc/services/run_local_monkey.py index f47a73a74..36c348b85 100644 --- a/monkey/monkey_island/cc/services/run_local_monkey.py +++ b/monkey/monkey_island/cc/services/run_local_monkey.py @@ -19,11 +19,11 @@ class LocalMonkeyRunService: self, data_dir: Path, agent_binary_repository: IAgentBinaryRepository, - local_ip_addresses: Sequence[str], + ip_addresses: Sequence[str], ): self._data_dir = data_dir self._agent_binary_repository = agent_binary_repository - self._local_ips = local_ip_addresses + self._ips = ip_addresses def run_local_monkey(self): # get the monkey executable suitable to run on the server @@ -65,7 +65,7 @@ class LocalMonkeyRunService: # run the monkey try: - ip = self._local_ips[0] + ip = self._ips[0] port = ISLAND_PORT args = [str(dest_path), "m0nk3y", "-s", f"{ip}:{port}"] diff --git a/monkey/monkey_island/cc/services/utils/network_utils.py b/monkey/monkey_island/cc/services/utils/network_utils.py index e25d50708..8af255ee0 100644 --- a/monkey/monkey_island/cc/services/utils/network_utils.py +++ b/monkey/monkey_island/cc/services/utils/network_utils.py @@ -58,7 +58,7 @@ else: # lot of times during the report generation. This means that if the interfaces of the Island machine # change, the Island process needs to be restarted. @lru(maxsize=1) -def get_local_ip_addresses() -> Sequence[str]: +def get_ip_addresses() -> Sequence[str]: ip_list = [] for interface in interfaces(): addresses = ifaddresses(interface).get(AF_INET, [])