Change grouper to be a VictimHost generator

This commit is contained in:
Daniel Goldberg 2019-09-16 15:14:53 +03:00
parent e11be48b80
commit d8bac57eb5
1 changed files with 20 additions and 19 deletions

View File

@ -21,19 +21,24 @@ SCAN_DELAY = 0
ITERATION_BLOCK_SIZE = 5 ITERATION_BLOCK_SIZE = 5
def _grouper(iterables, chunk_size): def generate_victims(net_ranges, chunk_size):
""" """
Goes over an iterable using chunks Generates VictimHosts in chunks from all the netranges
:param iterables: a sequence of iterable objects :param net_ranges: Iterable of network ranges
:param chunk_size: Chunk size, last chunk may be smaller :param chunk_size: Maximum size of each chunk
:return: :return:
""" """
iterable = itertools.chain(*iterables) chunk = []
while True: for net_range in net_ranges:
group = tuple(itertools.islice(iterable, chunk_size)) for address in net_range:
if not group: if hasattr(net_range, 'domain_name'):
break victim = VictimHost(address, net_range.domain_name)
yield group else:
victim = VictimHost(address)
chunk.append(victim)
if len(chunk) == chunk_size:
yield chunk
yield chunk
class NetworkScanner(object): class NetworkScanner(object):
@ -94,16 +99,9 @@ class NetworkScanner(object):
""" """
pool = Pool() pool = Pool()
victims_count = 0 victims_count = 0
for network_chunk in _grouper(self._ranges, ITERATION_BLOCK_SIZE): for victim_chunk in generate_victims(self._ranges, ITERATION_BLOCK_SIZE):
LOG.debug("Scanning for potential victims in chunk %r", network_chunk) LOG.debug("Scanning for potential victims in chunk %r", victim_chunk)
victim_chunk = []
for address in network_chunk:
# if hasattr(net_range, 'domain_name'):
# victim = VictimHost(address, net_range.domain_name)
# else:
victim = VictimHost(address)
victim_chunk.append(victim)
# skip self IP addresses # skip self IP addresses
victim_chunk = [x for x in victim_chunk if x.ip_addr not in self._ip_addresses] victim_chunk = [x for x in victim_chunk if x.ip_addr not in self._ip_addresses]
# skip IPs marked as blocked # skip IPs marked as blocked
@ -133,6 +131,7 @@ class NetworkScanner(object):
# time.sleep uses seconds, while config is in milliseconds # time.sleep uses seconds, while config is in milliseconds
time.sleep(WormConfiguration.tcp_scan_interval / float(1000)) time.sleep(WormConfiguration.tcp_scan_interval / float(1000))
@staticmethod @staticmethod
def _is_any_ip_in_subnet(ip_addresses, subnet_str): def _is_any_ip_in_subnet(ip_addresses, subnet_str):
for ip_address in ip_addresses: for ip_address in ip_addresses:
@ -140,6 +139,7 @@ class NetworkScanner(object):
return True return True
return False return False
def scan_machine(self, victim): def scan_machine(self, victim):
""" """
Scans specific machine using given scanner Scans specific machine using given scanner
@ -153,5 +153,6 @@ class NetworkScanner(object):
else: else:
return None return None
def on_island(self, server): def on_island(self, server):
return bool([x for x in self._ip_addresses if x in server]) return bool([x for x in self._ip_addresses if x in server])