Introduce fingerprint manager to avoid having to explictly import classes in configuration.

Similar to PBA manager.
This commit is contained in:
Daniel Goldberg 2019-10-30 14:29:27 +02:00
parent ab591fcf4c
commit 8edb55723c
3 changed files with 46 additions and 5 deletions

View File

@ -33,9 +33,6 @@ class Configuration(object):
if self._depth_from_commandline and key == "depth":
continue
# handle in cases
if key == 'finger_classes':
class_objects = [getattr(network_import, val) for val in value]
setattr(self, key, class_objects)
elif key == 'exploiter_classes':
class_objects = [getattr(exploit_import, val) for val in value]
setattr(self, key, class_objects)

View File

@ -12,6 +12,7 @@ from infection_monkey.utils.environment import is_windows_os
from infection_monkey.config import WormConfiguration
from infection_monkey.control import ControlClient
from infection_monkey.model import DELAY_DELETE_CMD
from infection_monkey.network.fingerprinter_manager import get_fingerprint_instances
from infection_monkey.network.firewall import app as firewall
from infection_monkey.network.network_scanner import NetworkScanner
from infection_monkey.system_info import SystemInfoCollector
@ -145,7 +146,7 @@ class InfectionMonkey(object):
self._exploiters = WormConfiguration.exploiter_classes
self._fingerprint = [fingerprint() for fingerprint in WormConfiguration.finger_classes]
self._fingerprint = get_fingerprint_instances()
if not self._keep_running or not WormConfiguration.alive:
break
@ -182,7 +183,8 @@ class InfectionMonkey(object):
if self._default_server:
if self._network.on_island(self._default_server):
machine.set_default_server(get_interface_to_target(machine.ip_addr) +
(':'+self._default_server_port if self._default_server_port else ''))
(
':' + self._default_server_port if self._default_server_port else ''))
else:
machine.set_default_server(self._default_server)
LOG.debug("Default server for machine: %r set to %s" % (machine, machine.default_server))

View File

@ -0,0 +1,42 @@
import importlib
import inspect
import logging
from os.path import dirname, basename, isfile, join
import glob
from infection_monkey.network.HostFinger import HostFinger
LOG = logging.getLogger(__name__)
def get_fingerprint_files():
"""
Gets all files under current directory(/actions)
:return: list of all files without .py ending
"""
files = glob.glob(join(dirname(__file__), "*.py"))
return [basename(f)[:-3] for f in files if isfile(f) and not f.endswith('__init__.py')]
def get_fingerprint_instances():
"""
Returns the fingerprint objects according to configuration as a list
:return: A list of HostFinger objects.
"""
fingerprinter_objects = []
fingerprint_files = get_fingerprint_files()
# Go through all of files
for file in fingerprint_files:
# Import module from that file
module = importlib.import_module(__package__ + '.' + file)
# Get all classes in a module
classes = [m[1] for m in inspect.getmembers(module, inspect.isclass)
if ((m[1].__module__ == module.__name__) and issubclass(m[1], HostFinger))]
# Get object from class
for class_object in classes:
LOG.debug("Checking if should run Fingerprinter {}".format(class_object.__name__))
if class_object.should_run(class_object.__name__):
fingerprinter = class_object()
fingerprinter_objects.append(fingerprinter)
LOG.debug("Added fingerprinter {} to fingerprint list".format(class_object.__name__))
return fingerprinter_objects