From 20fff43fa986f25846f5e459c93bf76fbd60f50e Mon Sep 17 00:00:00 2001 From: Shreya Date: Thu, 20 Feb 2020 12:24:58 +0530 Subject: [PATCH 1/5] Remove `None` values from list of networks to scan Fixes #540 --- monkey/common/network/network_range.py | 2 +- monkey/infection_monkey/network/network_scanner.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/monkey/common/network/network_range.py b/monkey/common/network/network_range.py index 6a1201e07..15e04f893 100644 --- a/monkey/common/network/network_range.py +++ b/monkey/common/network/network_range.py @@ -44,9 +44,9 @@ class NetworkRange(object, metaclass=ABCMeta): @staticmethod def get_range_obj(address_str): - address_str = address_str.strip() if not address_str: # Empty string return None + address_str = address_str.strip() if NetworkRange.check_if_range(address_str): return IpRange(ip_range=address_str) if -1 != address_str.find('/'): diff --git a/monkey/infection_monkey/network/network_scanner.py b/monkey/infection_monkey/network/network_scanner.py index faa5e9a5f..ec42e1e54 100644 --- a/monkey/infection_monkey/network/network_scanner.py +++ b/monkey/infection_monkey/network/network_scanner.py @@ -34,6 +34,7 @@ class NetworkScanner(object): LOG.info("Found local IP addresses of the machine: %r", self._ip_addresses) # for fixed range, only scan once. self._ranges = [NetworkRange.get_range_obj(address_str=x) for x in WormConfiguration.subnet_scan_list] + self._ranges = list(filter(None, self._ranges)) if WormConfiguration.local_network_scan: self._ranges += get_interfaces_ranges() self._ranges += self._get_inaccessible_subnets_ips() From 16b2b87adcd0d6a34dfd21c9ead44bd40f5f836f Mon Sep 17 00:00:00 2001 From: Shreya Date: Fri, 21 Feb 2020 12:37:42 +0530 Subject: [PATCH 2/5] Remove `None` values from list of TCP ports --- monkey/infection_monkey/network/tcp_scanner.py | 1 + 1 file changed, 1 insertion(+) diff --git a/monkey/infection_monkey/network/tcp_scanner.py b/monkey/infection_monkey/network/tcp_scanner.py index 69a659bf8..5605d5654 100644 --- a/monkey/infection_monkey/network/tcp_scanner.py +++ b/monkey/infection_monkey/network/tcp_scanner.py @@ -31,6 +31,7 @@ class TcpScanner(HostScanner, HostFinger): # maybe hide under really bad detection systems target_ports = self._config.tcp_target_ports[:] + target_ports = list(filter(None, target_ports)) # remove None values shuffle(target_ports) ports, banners = check_tcp_ports(host.ip_addr, target_ports, self._config.tcp_scan_timeout / 1000.0, From 6ff2bbf92e8ddfbd004cdc6d46730a18d6da5b7b Mon Sep 17 00:00:00 2001 From: Shreya Date: Fri, 21 Feb 2020 14:51:58 +0530 Subject: [PATCH 3/5] Discard all 'None' values in Monkey configuration lists Instead of checking individually for problems arising due to 'None' values and fixing them, all 'None' values in all lists in the configuration are discarded. --- monkey/infection_monkey/config.py | 2 ++ monkey/infection_monkey/network/network_scanner.py | 1 - monkey/infection_monkey/network/tcp_scanner.py | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/monkey/infection_monkey/config.py b/monkey/infection_monkey/config.py index 5c5b5a392..0612bf01e 100644 --- a/monkey/infection_monkey/config.py +++ b/monkey/infection_monkey/config.py @@ -27,6 +27,8 @@ class Configuration(object): if self._depth_from_commandline and key == "depth": continue if hasattr(self, key): + if type(value) == list: + value = list(filter(None, value)) setattr(self, key, value) else: unknown_items.append(key) diff --git a/monkey/infection_monkey/network/network_scanner.py b/monkey/infection_monkey/network/network_scanner.py index ec42e1e54..faa5e9a5f 100644 --- a/monkey/infection_monkey/network/network_scanner.py +++ b/monkey/infection_monkey/network/network_scanner.py @@ -34,7 +34,6 @@ class NetworkScanner(object): LOG.info("Found local IP addresses of the machine: %r", self._ip_addresses) # for fixed range, only scan once. self._ranges = [NetworkRange.get_range_obj(address_str=x) for x in WormConfiguration.subnet_scan_list] - self._ranges = list(filter(None, self._ranges)) if WormConfiguration.local_network_scan: self._ranges += get_interfaces_ranges() self._ranges += self._get_inaccessible_subnets_ips() diff --git a/monkey/infection_monkey/network/tcp_scanner.py b/monkey/infection_monkey/network/tcp_scanner.py index 5605d5654..69a659bf8 100644 --- a/monkey/infection_monkey/network/tcp_scanner.py +++ b/monkey/infection_monkey/network/tcp_scanner.py @@ -31,7 +31,6 @@ class TcpScanner(HostScanner, HostFinger): # maybe hide under really bad detection systems target_ports = self._config.tcp_target_ports[:] - target_ports = list(filter(None, target_ports)) # remove None values shuffle(target_ports) ports, banners = check_tcp_ports(host.ip_addr, target_ports, self._config.tcp_scan_timeout / 1000.0, From 5f3458349dc90107bc2414c6400606ea158354d7 Mon Sep 17 00:00:00 2001 From: Shreya Date: Tue, 3 Mar 2020 01:40:05 +0530 Subject: [PATCH 4/5] Filter out `None` values from configuration before saving Configuration is filtered before being saved (can be seen when adding empty fields and pressing the `Submit` button -> empty fields are removed) --- monkey/monkey_island/cc/services/config.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/monkey/monkey_island/cc/services/config.py b/monkey/monkey_island/cc/services/config.py index fd2ed5b8d..96c59cad6 100644 --- a/monkey/monkey_island/cc/services/config.py +++ b/monkey/monkey_island/cc/services/config.py @@ -153,9 +153,18 @@ class ConfigService: def ssh_key_exists(keys, user, ip): return [key for key in keys if key['user'] == user and key['ip'] == ip] + def _filter_none_values(data): + if isinstance(data, dict): + return {k: ConfigService._filter_none_values(v) for k, v in data.items() if k is not None and v is not None} + elif isinstance(data, list): + return [ConfigService._filter_none_values(item) for item in data if item is not None] + else: + return data + @staticmethod def update_config(config_json, should_encrypt): # PBA file upload happens on pba_file_upload endpoint and corresponding config options are set there + config_json = ConfigService._filter_none_values(config_json) monkey_island.cc.services.post_breach_files.set_config_PBA_files(config_json) if should_encrypt: try: From 4592c218293d5bf098580dcb4913dbaeeb23deca Mon Sep 17 00:00:00 2001 From: Shreya Date: Tue, 3 Mar 2020 01:49:35 +0530 Subject: [PATCH 5/5] Undo old logic `None` values in the configuration are now checked before being saved --- monkey/infection_monkey/config.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/monkey/infection_monkey/config.py b/monkey/infection_monkey/config.py index 0612bf01e..5c5b5a392 100644 --- a/monkey/infection_monkey/config.py +++ b/monkey/infection_monkey/config.py @@ -27,8 +27,6 @@ class Configuration(object): if self._depth_from_commandline and key == "depth": continue if hasattr(self, key): - if type(value) == list: - value = list(filter(None, value)) setattr(self, key, value) else: unknown_items.append(key)