2022-07-18 23:59:14 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
2023-08-18 08:34:07 +00:00
|
|
|
|
2023-09-14 21:07:15 +00:00
|
|
|
import nxc
|
2023-03-27 04:48:45 +00:00
|
|
|
import importlib
|
2023-08-18 08:34:07 +00:00
|
|
|
import traceback
|
|
|
|
import sys
|
|
|
|
|
2023-05-05 18:44:11 +00:00
|
|
|
from os import listdir
|
|
|
|
from os.path import dirname
|
|
|
|
from os.path import join as path_join
|
2023-04-14 19:12:32 +00:00
|
|
|
|
2023-09-14 21:07:15 +00:00
|
|
|
from nxc.context import Context
|
|
|
|
from nxc.logger import NXCAdapter
|
2023-09-20 04:09:25 +00:00
|
|
|
from nxc.paths import NXC_PATH
|
2016-06-04 05:42:26 +00:00
|
|
|
|
2023-03-24 03:25:26 +00:00
|
|
|
|
2023-03-27 04:48:45 +00:00
|
|
|
class ModuleLoader:
|
2016-06-04 05:42:26 +00:00
|
|
|
def __init__(self, args, db, logger):
|
|
|
|
self.args = args
|
|
|
|
self.db = db
|
|
|
|
self.logger = logger
|
|
|
|
|
|
|
|
def module_is_sane(self, module, module_path):
|
2023-04-04 13:39:54 +00:00
|
|
|
"""
|
|
|
|
Check if a module has the proper attributes
|
|
|
|
"""
|
2016-06-04 05:42:26 +00:00
|
|
|
module_error = False
|
2023-04-12 04:25:38 +00:00
|
|
|
if not hasattr(module, "name"):
|
2023-04-21 10:20:47 +00:00
|
|
|
self.logger.fail(f"{module_path} missing the name variable")
|
2016-06-04 05:42:26 +00:00
|
|
|
module_error = True
|
2023-04-12 04:25:38 +00:00
|
|
|
elif not hasattr(module, "description"):
|
2023-04-21 10:20:47 +00:00
|
|
|
self.logger.fail(f"{module_path} missing the description variable")
|
2016-09-12 06:52:50 +00:00
|
|
|
module_error = True
|
2023-04-12 04:25:38 +00:00
|
|
|
elif not hasattr(module, "supported_protocols"):
|
2023-04-21 10:20:47 +00:00
|
|
|
self.logger.fail(f"{module_path} missing the supported_protocols variable")
|
2016-06-04 07:13:38 +00:00
|
|
|
module_error = True
|
2023-04-12 04:25:38 +00:00
|
|
|
elif not hasattr(module, "opsec_safe"):
|
2023-04-21 10:20:47 +00:00
|
|
|
self.logger.fail(f"{module_path} missing the opsec_safe variable")
|
2016-12-20 07:23:40 +00:00
|
|
|
module_error = True
|
2023-04-12 04:25:38 +00:00
|
|
|
elif not hasattr(module, "multiple_hosts"):
|
2023-04-21 10:20:47 +00:00
|
|
|
self.logger.fail(f"{module_path} missing the multiple_hosts variable")
|
2016-12-20 07:23:40 +00:00
|
|
|
module_error = True
|
2023-04-12 04:25:38 +00:00
|
|
|
elif not hasattr(module, "options"):
|
2023-04-21 10:20:47 +00:00
|
|
|
self.logger.fail(f"{module_path} missing the options function")
|
2016-09-12 06:52:50 +00:00
|
|
|
module_error = True
|
2023-04-12 04:25:38 +00:00
|
|
|
elif not hasattr(module, "on_login") and not (module, "on_admin_login"):
|
2023-05-08 18:39:36 +00:00
|
|
|
self.logger.fail(f"{module_path} missing the on_login/on_admin_login function(s)")
|
2016-06-04 05:42:26 +00:00
|
|
|
module_error = True
|
2023-03-24 03:25:26 +00:00
|
|
|
# elif not hasattr(module, 'chain_support'):
|
2023-04-21 10:20:47 +00:00
|
|
|
# self.logger.fail('{} missing the chain_support variable'.format(module_path))
|
2023-03-24 03:25:26 +00:00
|
|
|
# module_error = True
|
2016-06-04 05:42:26 +00:00
|
|
|
|
2023-03-24 03:25:26 +00:00
|
|
|
if module_error:
|
|
|
|
return False
|
2016-06-04 05:42:26 +00:00
|
|
|
return True
|
2016-09-12 06:52:50 +00:00
|
|
|
|
2016-06-04 05:42:26 +00:00
|
|
|
def load_module(self, module_path):
|
2023-04-04 13:39:54 +00:00
|
|
|
"""
|
|
|
|
Load a module, initializing it and checking that it has the proper attributes
|
|
|
|
"""
|
2017-03-27 21:09:36 +00:00
|
|
|
try:
|
2023-09-17 20:20:40 +00:00
|
|
|
spec = importlib.util.spec_from_file_location("NXCModule", module_path)
|
|
|
|
module = spec.loader.load_module().NXCModule()
|
2023-03-27 04:48:45 +00:00
|
|
|
|
2017-03-27 21:09:36 +00:00
|
|
|
if self.module_is_sane(module, module_path):
|
|
|
|
return module
|
|
|
|
except Exception as e:
|
2023-04-21 10:20:47 +00:00
|
|
|
self.logger.fail(f"Failed loading module at {module_path}: {e}")
|
2023-08-18 08:34:07 +00:00
|
|
|
self.logger.debug(traceback.format_exc())
|
2016-06-04 05:42:26 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
def init_module(self, module_path):
|
2023-04-04 13:39:54 +00:00
|
|
|
"""
|
|
|
|
Initialize a module for execution
|
|
|
|
"""
|
2023-03-24 03:25:26 +00:00
|
|
|
module = None
|
2016-06-04 05:42:26 +00:00
|
|
|
module = self.load_module(module_path)
|
|
|
|
|
2023-04-14 19:12:32 +00:00
|
|
|
if module:
|
|
|
|
self.logger.debug(f"Supported protocols: {module.supported_protocols}")
|
|
|
|
self.logger.debug(f"Protocol: {self.args.protocol}")
|
|
|
|
if self.args.protocol in module.supported_protocols:
|
|
|
|
try:
|
2023-09-14 21:07:15 +00:00
|
|
|
module_logger = NXCAdapter(extra={"module_name": module.name.upper()})
|
2023-04-14 19:12:32 +00:00
|
|
|
except Exception as e:
|
2023-09-14 21:19:11 +00:00
|
|
|
self.logger.fail(f"Error loading NXCAdaptor for module {module.name.upper()}: {e}")
|
2023-04-14 19:12:32 +00:00
|
|
|
context = Context(self.db, module_logger, self.args)
|
|
|
|
module_options = {}
|
2016-06-04 05:42:26 +00:00
|
|
|
|
2023-04-14 19:12:32 +00:00
|
|
|
for option in self.args.module_options:
|
2023-05-02 15:17:59 +00:00
|
|
|
key, value = option.split("=", 1)
|
2023-04-14 19:12:32 +00:00
|
|
|
module_options[str(key).upper()] = value
|
2016-06-04 05:42:26 +00:00
|
|
|
|
2023-04-14 19:12:32 +00:00
|
|
|
module.options(context, module_options)
|
|
|
|
return module
|
|
|
|
else:
|
2023-05-08 18:39:36 +00:00
|
|
|
self.logger.fail(f"Module {module.name.upper()} is not supported for protocol {self.args.protocol}")
|
2023-04-14 19:12:32 +00:00
|
|
|
sys.exit(1)
|
2023-04-04 13:39:54 +00:00
|
|
|
|
|
|
|
def get_module_info(self, module_path):
|
|
|
|
"""
|
|
|
|
Get the path, description, and options from a module
|
|
|
|
"""
|
|
|
|
try:
|
2023-09-17 20:20:40 +00:00
|
|
|
spec = importlib.util.spec_from_file_location("NXCModule", module_path)
|
|
|
|
module_spec = spec.loader.load_module().NXCModule
|
2023-04-04 13:39:54 +00:00
|
|
|
|
|
|
|
module = {
|
2023-05-02 15:17:59 +00:00
|
|
|
f"{module_spec.name.lower()}": {
|
|
|
|
"path": module_path,
|
|
|
|
"description": module_spec.description,
|
|
|
|
"options": module_spec.options.__doc__,
|
|
|
|
"supported_protocols": module_spec.supported_protocols,
|
|
|
|
"opsec_safe": module_spec.opsec_safe,
|
|
|
|
"multiple_hosts": module_spec.multiple_hosts,
|
|
|
|
}
|
2023-04-04 13:39:54 +00:00
|
|
|
}
|
|
|
|
if self.module_is_sane(module_spec, module_path):
|
|
|
|
return module
|
|
|
|
except Exception as e:
|
2023-04-21 10:20:47 +00:00
|
|
|
self.logger.fail(f"Failed loading module at {module_path}: {e}")
|
2023-08-18 08:34:07 +00:00
|
|
|
self.logger.debug(traceback.format_exc())
|
2023-04-04 13:39:54 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
def list_modules(self):
|
|
|
|
"""
|
|
|
|
List modules without initializing them
|
|
|
|
"""
|
|
|
|
modules = {}
|
2023-05-02 15:17:59 +00:00
|
|
|
modules_paths = [
|
2023-09-14 21:07:15 +00:00
|
|
|
path_join(dirname(nxc.__file__), "modules"),
|
2023-09-20 04:09:25 +00:00
|
|
|
path_join(NXC_PATH, "modules"),
|
2023-05-02 15:17:59 +00:00
|
|
|
]
|
2023-04-04 13:39:54 +00:00
|
|
|
|
|
|
|
for path in modules_paths:
|
2023-05-05 18:44:11 +00:00
|
|
|
for module in listdir(path):
|
2023-05-02 15:17:59 +00:00
|
|
|
if module[-3:] == ".py" and module != "example_module.py":
|
2023-08-18 08:11:47 +00:00
|
|
|
try:
|
|
|
|
module_path = path_join(path, module)
|
|
|
|
module_data = self.get_module_info(module_path)
|
|
|
|
modules.update(module_data)
|
2023-09-20 16:24:20 +00:00
|
|
|
except Exception as e:
|
|
|
|
self.logger.debug(f"Error loading module {module}: {e}")
|
2023-08-18 08:11:47 +00:00
|
|
|
pass
|
2023-04-04 13:39:54 +00:00
|
|
|
return modules
|