Island, Common: Delete the cached versions of ip retrieval

This commit is contained in:
vakarisz 2022-09-16 16:05:56 +03:00
parent c82ee2ce97
commit 77a23b7920
5 changed files with 12 additions and 35 deletions

View File

@ -16,10 +16,10 @@ from mongoengine import (
StringField, 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.command_control_channel import CommandControlChannel
from monkey_island.cc.models.monkey_ttl import MonkeyTtl, create_monkey_ttl_document 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.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): class ParentNotFoundError(Exception):
@ -123,7 +123,7 @@ class Monkey(Document):
def get_label_by_id(object_id): def get_label_by_id(object_id):
current_monkey = Monkey.get_single_monkey_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] 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: if len(set(current_monkey.ip_addresses).intersection(local_ips)) > 0:
label = "MonkeyIsland - " + label label = "MonkeyIsland - " + label
return label return label

View File

@ -27,6 +27,7 @@ from common.event_serializers import ( # noqa: E402
EventSerializerRegistry, EventSerializerRegistry,
register_common_agent_event_serializers, 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 common.version import get_version # noqa: E402
from monkey_island.cc.app import init_app # noqa: E402 from monkey_island.cc.app import init_app # noqa: E402
from monkey_island.cc.arg_parser import IslandCmdArgs # 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, 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.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.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 island_config_options_validator # noqa: E402
from monkey_island.cc.setup import ( # 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]: def _collect_system_info() -> Tuple[Sequence[str], Deployment, Version]:
deployment = _get_deployment() deployment = _get_deployment()
version = Version(get_version(), 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: def _get_deployment() -> Deployment:

View File

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

View File

@ -4,10 +4,10 @@ from datetime import datetime
from bson import ObjectId from bson import ObjectId
import monkey_island.cc.services.log 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 import models
from monkey_island.cc.database import mongo from monkey_island.cc.database import mongo
from monkey_island.cc.models import Monkey 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.displayed_edge import DisplayedEdgeService
from monkey_island.cc.services.edge.edge import EdgeService from monkey_island.cc.services.edge.edge import EdgeService
from monkey_island.cc.services.utils.node_states import NodeStates from monkey_island.cc.services.utils.node_states import NodeStates
@ -110,7 +110,7 @@ class NodeService:
def get_monkey_label(monkey): def get_monkey_label(monkey):
# todo # todo
label = monkey["hostname"] + " : " + monkey["ip_addresses"][0] 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: if len(set(monkey["ip_addresses"]).intersection(ip_addresses)) > 0:
label = "MonkeyIsland - " + label label = "MonkeyIsland - " + label
return label return label
@ -118,7 +118,7 @@ class NodeService:
@staticmethod @staticmethod
def get_monkey_group(monkey): def get_monkey_group(monkey):
keywords = [] 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"]) keywords.extend(["island", "monkey"])
else: else:
monkey_type = "manual" if NodeService.get_monkey_manual_run(monkey) else "monkey" 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 # It's better to just initialize the island machine on reset I think
@staticmethod @staticmethod
def get_monkey_island_monkey(): def get_monkey_island_monkey():
ip_addresses = get_cached_local_ip_addresses() ip_addresses = get_my_ip_addresses()
for ip_address in ip_addresses: for ip_address in ip_addresses:
monkey = NodeService.get_monkey_by_ip(ip_address) monkey = NodeService.get_monkey_by_ip(ip_address)
if monkey is not None: if monkey is not None:
@ -297,7 +297,7 @@ class NodeService:
@staticmethod @staticmethod
def get_monkey_island_node(): def get_monkey_island_node():
island_node = NodeService.get_monkey_island_pseudo_net_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() island_node["domain_name"] = socket.gethostname()
return island_node return island_node

View File

@ -5,15 +5,12 @@ from itertools import chain, product
from typing import List from typing import List
from common.network.network_range import NetworkRange 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 common.network.segmentation_utils import get_ip_in_src_and_not_in_dst
from monkey_island.cc.database import mongo from monkey_island.cc.database import mongo
from monkey_island.cc.models import Monkey from monkey_island.cc.models import Monkey
from monkey_island.cc.models.report import get_report, save_report from monkey_island.cc.models.report import get_report, save_report
from monkey_island.cc.repository import IAgentConfigurationRepository, ICredentialsRepository 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.node import NodeService
from monkey_island.cc.services.reporting.exploitations.manual_exploitation import get_manual_monkeys from monkey_island.cc.services.reporting.exploitations.manual_exploitation import get_manual_monkeys
from monkey_island.cc.services.reporting.exploitations.monkey_exploitation import ( from monkey_island.cc.services.reporting.exploitations.monkey_exploitation import (
@ -178,7 +175,7 @@ class ReportService:
@staticmethod @staticmethod
def get_island_cross_segment_issues(): def get_island_cross_segment_issues():
issues = [] issues = []
island_ips = get_cached_local_ip_addresses() island_ips = get_my_ip_addresses()
for monkey in mongo.db.monkey.find( for monkey in mongo.db.monkey.find(
{"tunnel": {"$exists": False}}, {"tunnel": 1, "guid": 1, "hostname": 1} {"tunnel": {"$exists": False}}, {"tunnel": 1, "guid": 1, "hostname": 1}
): ):
@ -198,7 +195,7 @@ class ReportService:
"machine": monkey["hostname"], "machine": monkey["hostname"],
"networks": [str(subnet) for subnet in monkey_subnets], "networks": [str(subnet) for subnet in monkey_subnets],
"server_networks": [ "server_networks": [
str(interface.network) for interface in get_cached_local_interfaces() str(interface.network) for interface in get_network_interfaces()
], ],
} }
) )