Extracted segmentation utils function
This commit is contained in:
parent
fb01bface6
commit
6ec4e613cf
|
@ -0,0 +1,19 @@
|
||||||
|
from common.network.network_range import NetworkRange
|
||||||
|
|
||||||
|
|
||||||
|
def get_ip_in_src_and_not_in_dst(ip_addresses, source_subnet, target_subnet):
|
||||||
|
# type: (List[str], NetworkRange, NetworkRange) -> Union[str, None]
|
||||||
|
"""
|
||||||
|
Finds an IP address in ip_addresses which is in source_subnet but not in target_subnet.
|
||||||
|
:param ip_addresses: List of IP addresses to test.
|
||||||
|
:param source_subnet: Subnet to want an IP to not be in.
|
||||||
|
:param target_subnet: Subnet we want an IP to be in.
|
||||||
|
:return: The cross segment IP if in source but not in target, else None.
|
||||||
|
"""
|
||||||
|
for ip_address in ip_addresses:
|
||||||
|
if target_subnet.is_in_range(ip_address):
|
||||||
|
return None
|
||||||
|
for ip_address in ip_addresses:
|
||||||
|
if source_subnet.is_in_range(ip_address):
|
||||||
|
return ip_address
|
||||||
|
return None
|
|
@ -0,0 +1,19 @@
|
||||||
|
from common.network.network_range import *
|
||||||
|
from common.network.segmentation_utils import get_ip_in_src_and_not_in_dst
|
||||||
|
from monkey_island.cc.testing.IslandTestCase import IslandTestCase
|
||||||
|
|
||||||
|
|
||||||
|
class TestSegmentationUtils(IslandTestCase):
|
||||||
|
def test_get_ip_in_src_and_not_in_dst(self):
|
||||||
|
self.fail_if_not_testing_env()
|
||||||
|
source = CidrRange("1.1.1.0/24")
|
||||||
|
target = CidrRange("2.2.2.0/24")
|
||||||
|
self.assertIsNone(get_ip_in_src_and_not_in_dst(
|
||||||
|
[text_type("2.2.2.2")], source, target
|
||||||
|
))
|
||||||
|
self.assertIsNone(get_ip_in_src_and_not_in_dst(
|
||||||
|
[text_type("3.3.3.3"), text_type("4.4.4.4")], source, target
|
||||||
|
))
|
||||||
|
self.assertIsNotNone(get_ip_in_src_and_not_in_dst(
|
||||||
|
[text_type("8.8.8.8"), text_type("1.1.1.1")], source, target
|
||||||
|
))
|
|
@ -9,6 +9,7 @@ from enum import Enum
|
||||||
|
|
||||||
from six import text_type
|
from six import text_type
|
||||||
|
|
||||||
|
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.report_exporter_manager import ReportExporterManager
|
from monkey_island.cc.report_exporter_manager import ReportExporterManager
|
||||||
|
@ -424,23 +425,6 @@ class ReportService:
|
||||||
|
|
||||||
return issues
|
return issues
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def get_ip_in_src_and_not_in_dst(ip_addresses, source_subnet, target_subnet):
|
|
||||||
"""
|
|
||||||
Finds an IP address in ip_addresses which is in source_subnet but not in target_subnet.
|
|
||||||
:param ip_addresses: List of IP addresses to test.
|
|
||||||
:param source_subnet: Subnet to want an IP to not be in.
|
|
||||||
:param target_subnet: Subnet we want an IP to be in.
|
|
||||||
:return:
|
|
||||||
"""
|
|
||||||
for ip_address in ip_addresses:
|
|
||||||
if target_subnet.is_in_range(ip_address):
|
|
||||||
return None
|
|
||||||
for ip_address in ip_addresses:
|
|
||||||
if source_subnet.is_in_range(ip_address):
|
|
||||||
return ip_address
|
|
||||||
return None
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_cross_segment_issues_of_single_machine(source_subnet_range, target_subnet_range):
|
def get_cross_segment_issues_of_single_machine(source_subnet_range, target_subnet_range):
|
||||||
"""
|
"""
|
||||||
|
@ -503,9 +487,9 @@ class ReportService:
|
||||||
target_ip = scan['data']['machine']['ip_addr']
|
target_ip = scan['data']['machine']['ip_addr']
|
||||||
if target_subnet_range.is_in_range(text_type(target_ip)):
|
if target_subnet_range.is_in_range(text_type(target_ip)):
|
||||||
monkey = NodeService.get_monkey_by_guid(scan['monkey_guid'])
|
monkey = NodeService.get_monkey_by_guid(scan['monkey_guid'])
|
||||||
cross_segment_ip = ReportService.get_ip_in_src_and_not_in_dst(monkey['ip_addresses'],
|
cross_segment_ip = get_ip_in_src_and_not_in_dst(monkey['ip_addresses'],
|
||||||
source_subnet_range,
|
source_subnet_range,
|
||||||
target_subnet_range)
|
target_subnet_range)
|
||||||
|
|
||||||
if cross_segment_ip is not None:
|
if cross_segment_ip is not None:
|
||||||
cross_segment_issues.append(
|
cross_segment_issues.append(
|
||||||
|
|
Loading…
Reference in New Issue