From 285e6aaa35d47f87c6764a2284ee592906ae673b Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 12 Sep 2022 11:48:36 -0400 Subject: [PATCH 1/2] Island: Remove disused local_ips() functions --- .../cc/services/utils/network_utils.py | 48 ------------------- 1 file changed, 48 deletions(-) diff --git a/monkey/monkey_island/cc/services/utils/network_utils.py b/monkey/monkey_island/cc/services/utils/network_utils.py index 8af255ee0..663a8c948 100644 --- a/monkey/monkey_island/cc/services/utils/network_utils.py +++ b/monkey/monkey_island/cc/services/utils/network_utils.py @@ -1,57 +1,9 @@ -import array import ipaddress -import socket -import struct -import sys from typing import Sequence from netifaces import AF_INET, ifaddresses, interfaces from ring import lru -# Local ips function -# TODO: I can't find anywhere these are actually used. Confirm this is the case, remove these -# functions, and test. -if sys.platform == "win32": - - def local_ips(): - local_hostname = socket.gethostname() - return socket.gethostbyname_ex(local_hostname)[2] - -else: - import fcntl - - def local_ips(): - result = [] - try: - is_64bits = sys.maxsize > 2**32 - struct_size = 40 if is_64bits else 32 - s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - max_possible = 8 # initial value - while True: - struct_bytes = max_possible * struct_size - names = array.array("B", "\0" * struct_bytes) - outbytes = struct.unpack( - "iL", - fcntl.ioctl( - s.fileno(), - 0x8912, # SIOCGIFCONF - struct.pack("iL", struct_bytes, names.buffer_info()[0]), - ), - )[0] - if outbytes == struct_bytes: - max_possible *= 2 - else: - break - namestr = names.tostring() - - for i in range(0, outbytes, struct_size): - addr = socket.inet_ntoa(namestr[i + 20 : i + 24]) - if not addr.startswith("127"): - result.append(addr) - # name of interface is (namestr[i:i+16].split('\0', 1)[0] - finally: - return result - # The local IP addresses 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 From ad3dd6ba36bdbf51061ad6ba0ec267169d1b66cf Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Mon, 12 Sep 2022 11:52:53 -0400 Subject: [PATCH 2/2] Island: Move network_utils from services/ -> server_utils/ --- monkey/monkey_island/cc/models/monkey.py | 2 +- monkey/monkey_island/cc/server_setup.py | 2 +- .../cc/{services/utils => server_utils}/network_utils.py | 2 ++ monkey/monkey_island/cc/services/node.py | 2 +- monkey/monkey_island/cc/services/reporting/report.py | 2 +- 5 files changed, 6 insertions(+), 4 deletions(-) rename monkey/monkey_island/cc/{services/utils => server_utils}/network_utils.py (93%) diff --git a/monkey/monkey_island/cc/models/monkey.py b/monkey/monkey_island/cc/models/monkey.py index 003e14509..cf15c3cd4 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_ip_addresses +from monkey_island.cc.server_utils.network_utils import get_ip_addresses class ParentNotFoundError(Exception): diff --git a/monkey/monkey_island/cc/server_setup.py b/monkey/monkey_island/cc/server_setup.py index 5dd55e22e..8682d345a 100644 --- a/monkey/monkey_island/cc/server_setup.py +++ b/monkey/monkey_island/cc/server_setup.py @@ -33,8 +33,8 @@ 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_ip_addresses # noqa: E402 from monkey_island.cc.services.initialize import initialize_services # 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 diff --git a/monkey/monkey_island/cc/services/utils/network_utils.py b/monkey/monkey_island/cc/server_utils/network_utils.py similarity index 93% rename from monkey/monkey_island/cc/services/utils/network_utils.py rename to monkey/monkey_island/cc/server_utils/network_utils.py index 663a8c948..60c397c77 100644 --- a/monkey/monkey_island/cc/services/utils/network_utils.py +++ b/monkey/monkey_island/cc/server_utils/network_utils.py @@ -4,6 +4,8 @@ from typing import Sequence from netifaces import AF_INET, ifaddresses, interfaces from ring import lru +# TODO: This functionality is duplicated in the agent. Unify them after 2216-tcp-relay is merged + # The local IP addresses 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 diff --git a/monkey/monkey_island/cc/services/node.py b/monkey/monkey_island/cc/services/node.py index faac42539..2a7f0f661 100644 --- a/monkey/monkey_island/cc/services/node.py +++ b/monkey/monkey_island/cc/services/node.py @@ -7,9 +7,9 @@ import monkey_island.cc.services.log 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_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.network_utils import get_ip_addresses from monkey_island.cc.services.utils.node_states import NodeStates diff --git a/monkey/monkey_island/cc/services/reporting/report.py b/monkey/monkey_island/cc/services/reporting/report.py index 32761c7a6..01ce711d1 100644 --- a/monkey/monkey_island/cc/services/reporting/report.py +++ b/monkey/monkey_island/cc/services/reporting/report.py @@ -10,6 +10,7 @@ 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_ip_addresses, get_subnets 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 ( @@ -19,7 +20,6 @@ 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_ip_addresses, get_subnets from .. import AWSService from . import aws_exporter