Factor out generating VictimHosts from NetworkRange object

This commit is contained in:
Daniel Goldberg 2019-09-20 22:58:20 +03:00
parent 4dcc919b49
commit a32a783257
2 changed files with 18 additions and 6 deletions

View File

@ -46,3 +46,17 @@ class VictimHost(object):
def set_default_server(self, default_server): def set_default_server(self, default_server):
self.default_server = default_server self.default_server = default_server
def generate_victims_from_range(net_range):
"""
Generates VictimHosts from a given netrange
:param net_range: Network range object
:return: Generator of VictimHost objects
"""
for address in net_range:
if hasattr(net_range, 'domain_name'):
victim = VictimHost(address, net_range.domain_name)
else:
victim = VictimHost(address)
yield victim

View File

@ -3,8 +3,8 @@ import logging
from common.network.network_range import NetworkRange from common.network.network_range import NetworkRange
from infection_monkey.config import WormConfiguration from infection_monkey.config import WormConfiguration
from infection_monkey.model.host import generate_victims_from_range
from infection_monkey.network.info import local_ips, get_interfaces_ranges from infection_monkey.network.info import local_ips, get_interfaces_ranges
from infection_monkey.model import VictimHost
from infection_monkey.network import TcpScanner, PingScanner from infection_monkey.network import TcpScanner, PingScanner
from infection_monkey.utils import is_windows_os from infection_monkey.utils import is_windows_os
@ -27,15 +27,13 @@ def generate_victims(net_ranges, chunk_size):
""" """
chunk = [] chunk = []
for net_range in net_ranges: for net_range in net_ranges:
for address in net_range: for victim in generate_victims_from_range(net_range):
if hasattr(net_range, 'domain_name'):
victim = VictimHost(address, net_range.domain_name)
else:
victim = VictimHost(address)
chunk.append(victim) chunk.append(victim)
if len(chunk) == chunk_size: if len(chunk) == chunk_size:
yield chunk yield chunk
chunk = [] chunk = []
if chunk: # finished with number of victims < chunk_size
yield chunk
class NetworkScanner(object): class NetworkScanner(object):