Agent: Add initial compile_scan_target_list()

For the moment, this only handles the ranges_to_scan parameter. Other
parameters need to be handled.
This commit is contained in:
Mike Salvatore 2021-12-08 09:30:44 -05:00 committed by vakarisz
parent f2e95daa56
commit 23a0f74b2b
2 changed files with 90 additions and 0 deletions

View File

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

View File

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