From 23a0f74b2b4c0b1a58c4a585314480664047e771 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Wed, 8 Dec 2021 09:30:44 -0500 Subject: [PATCH] Agent: Add initial compile_scan_target_list() For the moment, this only handles the ranges_to_scan parameter. Other parameters need to be handled. --- .../network/scan_target_generator.py | 25 +++++++ .../network/test_scan_target_generator.py | 65 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 monkey/infection_monkey/network/scan_target_generator.py create mode 100644 monkey/tests/unit_tests/infection_monkey/network/test_scan_target_generator.py diff --git a/monkey/infection_monkey/network/scan_target_generator.py b/monkey/infection_monkey/network/scan_target_generator.py new file mode 100644 index 000000000..0aac33cc9 --- /dev/null +++ b/monkey/infection_monkey/network/scan_target_generator.py @@ -0,0 +1,25 @@ +from typing import List + +from common.network.network_range import NetworkRange + + +def compile_scan_target_list( + local_ips: List[str], + ranges_to_scan: List[str], + inaccessible_subnets: List[str], + blocklisted_ips: List[str], + enable_local_network_scan: bool, +) -> List[str]: + scan_target_list = _get_ips_from_ranges_to_scan(ranges_to_scan) + + return scan_target_list + + +def _get_ips_from_ranges_to_scan(ranges_to_scan): + scan_target_list = [] + + network_ranges = [NetworkRange.get_range_obj(_range) for _range in ranges_to_scan] + for _range in network_ranges: + scan_target_list.extend([ip for ip in _range]) + + return scan_target_list diff --git a/monkey/tests/unit_tests/infection_monkey/network/test_scan_target_generator.py b/monkey/tests/unit_tests/infection_monkey/network/test_scan_target_generator.py new file mode 100644 index 000000000..89a0775d8 --- /dev/null +++ b/monkey/tests/unit_tests/infection_monkey/network/test_scan_target_generator.py @@ -0,0 +1,65 @@ +import pytest + +from infection_monkey.network.scan_target_generator import compile_scan_target_list + + +def compile_ranges_only(ranges): + return compile_scan_target_list( + local_ips=[], + ranges_to_scan=ranges, + inaccessible_subnets=[], + blocklisted_ips=[], + enable_local_network_scan=False, + ) + + +def test_single_subnet(): + scan_targets = compile_ranges_only(["10.0.0.0/24"]) + + assert len(scan_targets) == 255 + + for i in range(0, 255): + assert f"10.0.0.{i}" in scan_targets + + +@pytest.mark.parametrize("single_ip", ["10.0.0.2", "10.0.0.2/32", "10.0.0.2-10.0.0.2"]) +def test_single_ip(single_ip): + print(single_ip) + scan_targets = compile_ranges_only([single_ip]) + + assert len(scan_targets) == 1 + assert "10.0.0.2" in scan_targets + assert "10.0.0.2" == scan_targets[0] + + +def test_multiple_subnet(): + scan_targets = compile_ranges_only(["10.0.0.0/24", "192.168.56.8/29"]) + + assert len(scan_targets) == 262 + + for i in range(0, 255): + assert f"10.0.0.{i}" in scan_targets + + for i in range(8, 15): + assert f"192.168.56.{i}" in scan_targets + + +def test_middle_of_range_subnet(): + scan_targets = compile_ranges_only(["192.168.56.4/29"]) + + assert len(scan_targets) == 7 + + for i in range(0, 7): + assert f"192.168.56.{i}" in scan_targets + + +@pytest.mark.parametrize( + "ip_range", ["192.168.56.25-192.168.56.33", "192.168.56.25 - 192.168.56.33"] +) +def test_ip_range(ip_range): + scan_targets = compile_ranges_only([ip_range]) + + assert len(scan_targets) == 9 + + for i in range(25, 34): + assert f"192.168.56.{i}" in scan_targets