diff --git a/monkey/monkey_island/cc/models/monkey.py b/monkey/monkey_island/cc/models/monkey.py index 988454b1a..a3e96baca 100644 --- a/monkey/monkey_island/cc/models/monkey.py +++ b/monkey/monkey_island/cc/models/monkey.py @@ -16,10 +16,10 @@ from mongoengine import ( StringField, ) +from common.network.network_utils import get_my_ip_addresses 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.server_utils.network_utils import get_cached_local_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] - local_ips = map(str, get_cached_local_ip_addresses()) + local_ips = map(str, get_my_ip_addresses()) if len(set(current_monkey.ip_addresses).intersection(local_ips)) > 0: label = "MonkeyIsland - " + label return label diff --git a/monkey/monkey_island/cc/server_setup.py b/monkey/monkey_island/cc/server_setup.py index 9d092da53..084a18d9e 100644 --- a/monkey/monkey_island/cc/server_setup.py +++ b/monkey/monkey_island/cc/server_setup.py @@ -27,6 +27,7 @@ from common.event_serializers import ( # noqa: E402 EventSerializerRegistry, register_common_agent_event_serializers, ) +from common.network.network_utils import get_my_ip_addresses # noqa: E402 from common.version import get_version # noqa: E402 from monkey_island.cc.app import init_app # noqa: E402 from monkey_island.cc.arg_parser import IslandCmdArgs # noqa: E402 @@ -37,7 +38,6 @@ from monkey_island.cc.server_utils.consts import ( # noqa: E402 MONKEY_ISLAND_ABS_PATH, ) from monkey_island.cc.server_utils.island_logger import reset_logger, setup_logging # noqa: E402 -from monkey_island.cc.server_utils.network_utils import get_cached_local_ip_addresses # noqa: E402 from monkey_island.cc.services.initialize import initialize_services # noqa: E402 from monkey_island.cc.setup import island_config_options_validator # noqa: E402 from monkey_island.cc.setup import ( # noqa: E402 @@ -106,7 +106,7 @@ def _configure_logging(config_options): def _collect_system_info() -> Tuple[Sequence[str], Deployment, Version]: deployment = _get_deployment() version = Version(get_version(), deployment) - return (get_cached_local_ip_addresses(), deployment, version) + return (get_my_ip_addresses(), deployment, version) def _get_deployment() -> Deployment: diff --git a/monkey/monkey_island/cc/server_utils/network_utils.py b/monkey/monkey_island/cc/server_utils/network_utils.py deleted file mode 100644 index d3611f0e5..000000000 --- a/monkey/monkey_island/cc/server_utils/network_utils.py +++ /dev/null @@ -1,20 +0,0 @@ -from ipaddress import IPv4Address, IPv4Interface -from typing import Sequence - -from ring import lru - -from common.network.network_utils import get_my_ip_addresses, get_network_interfaces - - -@lru(maxsize=1) -def get_cached_local_ip_addresses() -> Sequence[IPv4Address]: - return get_my_ip_addresses() - - -# The subnets list should not change often. Therefore, we can cache the result and never call this -# function more than once. This stopgap measure is here since this function is called a lot of times -# during the report generation. This means that if the interfaces or subnets of the Island machine -# change, the Island process needs to be restarted. -@lru(maxsize=1) -def get_cached_local_interfaces() -> Sequence[IPv4Interface]: - return get_network_interfaces() diff --git a/monkey/monkey_island/cc/services/node.py b/monkey/monkey_island/cc/services/node.py index 6797cc7a9..0bacb53b8 100644 --- a/monkey/monkey_island/cc/services/node.py +++ b/monkey/monkey_island/cc/services/node.py @@ -4,10 +4,10 @@ from datetime import datetime from bson import ObjectId import monkey_island.cc.services.log +from common.network.network_utils import get_my_ip_addresses from monkey_island.cc import models from monkey_island.cc.database import mongo from monkey_island.cc.models import Monkey -from monkey_island.cc.server_utils.network_utils import get_cached_local_ip_addresses 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.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_cached_local_ip_addresses() + ip_addresses = get_my_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_cached_local_ip_addresses())) != 0: + if len(set(monkey["ip_addresses"]).intersection(get_my_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_cached_local_ip_addresses() + ip_addresses = get_my_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_cached_local_ip_addresses() + island_node["ip_addresses"] = get_my_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 59c40499a..b80f3c3b6 100644 --- a/monkey/monkey_island/cc/services/reporting/report.py +++ b/monkey/monkey_island/cc/services/reporting/report.py @@ -5,15 +5,12 @@ from itertools import chain, product from typing import List from common.network.network_range import NetworkRange +from common.network.network_utils import get_my_ip_addresses, get_network_interfaces from common.network.segmentation_utils import get_ip_in_src_and_not_in_dst from monkey_island.cc.database import mongo from monkey_island.cc.models import Monkey from monkey_island.cc.models.report import get_report, save_report from monkey_island.cc.repository import IAgentConfigurationRepository, ICredentialsRepository -from monkey_island.cc.server_utils.network_utils import ( - get_cached_local_interfaces, - get_cached_local_ip_addresses, -) from monkey_island.cc.services.node import NodeService from monkey_island.cc.services.reporting.exploitations.manual_exploitation import get_manual_monkeys from monkey_island.cc.services.reporting.exploitations.monkey_exploitation import ( @@ -178,7 +175,7 @@ class ReportService: @staticmethod def get_island_cross_segment_issues(): issues = [] - island_ips = get_cached_local_ip_addresses() + island_ips = get_my_ip_addresses() for monkey in mongo.db.monkey.find( {"tunnel": {"$exists": False}}, {"tunnel": 1, "guid": 1, "hostname": 1} ): @@ -198,7 +195,7 @@ class ReportService: "machine": monkey["hostname"], "networks": [str(subnet) for subnet in monkey_subnets], "server_networks": [ - str(interface.network) for interface in get_cached_local_interfaces() + str(interface.network) for interface in get_network_interfaces() ], } )