Merge branch '2234-extract-network-utils-from-services' into 2234-refactor-reset-agent-config

This commit is contained in:
Mike Salvatore 2022-09-12 11:53:19 -04:00
commit c02f87add0
6 changed files with 43 additions and 89 deletions

View File

@ -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):

View File

@ -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

View File

@ -0,0 +1,39 @@
import ipaddress
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
# 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_ip_addresses() -> Sequence[str]:
ip_list = []
for interface in interfaces():
addresses = ifaddresses(interface).get(AF_INET, [])
ip_list.extend([link["addr"] for link in addresses if link["addr"] != "127.0.0.1"])
return ip_list
# 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_subnets():
subnets = []
for interface in interfaces():
addresses = ifaddresses(interface).get(AF_INET, [])
subnets.extend(
[
ipaddress.ip_interface(link["addr"] + "/" + link["netmask"]).network
for link in addresses
if link["addr"] != "127.0.0.1"
]
)
return subnets

View File

@ -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

View File

@ -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

View File

@ -1,85 +0,0 @@
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
# 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_ip_addresses() -> Sequence[str]:
ip_list = []
for interface in interfaces():
addresses = ifaddresses(interface).get(AF_INET, [])
ip_list.extend([link["addr"] for link in addresses if link["addr"] != "127.0.0.1"])
return ip_list
# 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_subnets():
subnets = []
for interface in interfaces():
addresses = ifaddresses(interface).get(AF_INET, [])
subnets.extend(
[
ipaddress.ip_interface(link["addr"] + "/" + link["netmask"]).network
for link in addresses
if link["addr"] != "127.0.0.1"
]
)
return subnets