diff --git a/monkey/monkey_island/cc/models/monkey.py b/monkey/monkey_island/cc/models/monkey.py index a106f9965..3e518444d 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 local_ip_addresses +from monkey_island.cc.services.utils.network_utils import get_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] - if len(set(current_monkey.ip_addresses).intersection(local_ip_addresses())) > 0: + if len(set(current_monkey.ip_addresses).intersection(get_local_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 ef0e0fa4b..9b61d3e45 100644 --- a/monkey/monkey_island/cc/resources/ip_addresses.py +++ b/monkey/monkey_island/cc/resources/ip_addresses.py @@ -2,7 +2,7 @@ from typing import Mapping, Sequence from monkey_island.cc.resources.AbstractResource import AbstractResource from monkey_island.cc.resources.request_authentication import jwt_required -from monkey_island.cc.services.utils.network_utils import local_ip_addresses +from monkey_island.cc.services.utils.network_utils import get_local_ip_addresses class IpAddresses(AbstractResource): @@ -15,6 +15,6 @@ class IpAddresses(AbstractResource): :return: a dictionary with "ip_addresses" key that points to a list of IP's """ - local_ips = local_ip_addresses() + local_ips = get_local_ip_addresses() return {"ip_addresses": local_ips} diff --git a/monkey/monkey_island/cc/resources/root.py b/monkey/monkey_island/cc/resources/root.py index ef53ff788..0bfc94a8c 100644 --- a/monkey/monkey_island/cc/resources/root.py +++ b/monkey/monkey_island/cc/resources/root.py @@ -6,7 +6,7 @@ from monkey_island.cc.database import mongo from monkey_island.cc.resources.AbstractResource import AbstractResource from monkey_island.cc.resources.request_authentication import jwt_required from monkey_island.cc.services.infection_lifecycle import get_completed_steps -from monkey_island.cc.services.utils.network_utils import local_ip_addresses +from monkey_island.cc.services.utils.network_utils import get_local_ip_addresses logger = logging.getLogger(__name__) @@ -29,7 +29,7 @@ class Root(AbstractResource): @jwt_required def get_server_info(self): return jsonify( - ip_addresses=local_ip_addresses(), + ip_addresses=get_local_ip_addresses(), mongo=str(mongo.db), completed_steps=get_completed_steps(), ) diff --git a/monkey/monkey_island/cc/server_setup.py b/monkey/monkey_island/cc/server_setup.py index 3b08cd2f2..9dce509ef 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 local_ip_addresses # noqa: E402 +from monkey_island.cc.services.utils.network_utils import get_local_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,7 +168,9 @@ def _log_init_info(): def _log_web_interface_access_urls(): - web_interface_urls = ", ".join([f"https://{ip}:{ISLAND_PORT}" for ip in local_ip_addresses()]) + web_interface_urls = ", ".join( + [f"https://{ip}:{ISLAND_PORT}" for ip in get_local_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/node.py b/monkey/monkey_island/cc/services/node.py index 4ffb3935d..e65a410fd 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 local_ip_addresses +from monkey_island.cc.services.utils.network_utils import get_local_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 = local_ip_addresses() + ip_addresses = get_local_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(local_ip_addresses())) != 0: + if len(set(monkey["ip_addresses"]).intersection(get_local_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 = local_ip_addresses() + ip_addresses = get_local_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"] = local_ip_addresses() + island_node["ip_addresses"] = get_local_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 89c6d2a78..c21be0efb 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_subnets, local_ip_addresses +from monkey_island.cc.services.utils.network_utils import get_local_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 = local_ip_addresses() + island_ips = get_local_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 a56642e2c..529b67926 100644 --- a/monkey/monkey_island/cc/services/run_local_monkey.py +++ b/monkey/monkey_island/cc/services/run_local_monkey.py @@ -7,7 +7,7 @@ from shutil import copyfileobj from monkey_island.cc.repository import IAgentBinaryRepository, RetrievalError from monkey_island.cc.server_utils.consts import ISLAND_PORT -from monkey_island.cc.services.utils.network_utils import local_ip_addresses +from monkey_island.cc.services.utils.network_utils import get_local_ip_addresses logger = logging.getLogger(__name__) @@ -59,7 +59,7 @@ class LocalMonkeyRunService: # run the monkey try: - ip = local_ip_addresses()[0] + ip = get_local_ip_addresses()[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 e68a68748..e25d50708 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 local_ip_addresses() -> Sequence[str]: +def get_local_ip_addresses() -> Sequence[str]: ip_list = [] for interface in interfaces(): addresses = ifaddresses(interface).get(AF_INET, [])