| 28 | |
| 29 | |
| 30 | class WebUtils: |
| 31 | def __init__(self, shared_data, logger): |
| 32 | self.shared_data = shared_data |
| 33 | self.logger = logger |
| 34 | self.actions: List[Any] = [] # List that contains all actions |
| 35 | self.standalone_actions: List[Any] = [] # List that contains all standalone actions |
| 36 | self.actions_dir = getattr(shared_data, 'actions_dir', '') |
| 37 | self.actions_file = getattr(shared_data, 'actions_file', '') |
| 38 | self._actions_loaded = False |
| 39 | |
| 40 | def load_actions(self): |
| 41 | """Load all actions from the actions file""" |
| 42 | if self._actions_loaded: |
| 43 | return |
| 44 | |
| 45 | self.actions.clear() |
| 46 | self.standalone_actions.clear() |
| 47 | self.actions_dir = self.shared_data.actions_dir |
| 48 | with open(self.shared_data.actions_file, 'r') as file: |
| 49 | actions_config = json.load(file) |
| 50 | for action in actions_config: |
| 51 | module_name = action["b_module"] |
| 52 | if module_name == 'scanning': |
| 53 | self.load_scanner(module_name) |
| 54 | elif module_name == 'nmap_vuln_scanner': |
| 55 | self.load_nmap_vuln_scanner(module_name) |
| 56 | else: |
| 57 | self.load_action(module_name, action) |
| 58 | |
| 59 | self._actions_loaded = True |
| 60 | |
| 61 | def load_scanner(self, module_name): |
| 62 | """Load the network scanner""" |
| 63 | module = importlib.import_module(f'actions.{module_name}') |
| 64 | b_class = getattr(module, 'b_class') |
| 65 | self.network_scanner = getattr(module, b_class)(self.shared_data) |
| 66 | |
| 67 | def load_nmap_vuln_scanner(self, module_name): |
| 68 | """Load the nmap vulnerability scanner""" |
| 69 | self.nmap_vuln_scanner = NmapVulnScanner(self.shared_data) |
| 70 | |
| 71 | def load_action(self, module_name, action): |
| 72 | """Load an action from the actions file""" |
| 73 | module = importlib.import_module(f'actions.{module_name}') |
| 74 | try: |
| 75 | b_class = action["b_class"] |
| 76 | action_instance = getattr(module, b_class)(self.shared_data) |
| 77 | action_instance.action_name = b_class |
| 78 | action_instance.port = action.get("b_port") |
| 79 | action_instance.b_parent_action = action.get("b_parent") |
| 80 | if action_instance.port == 0: |
| 81 | self.standalone_actions.append(action_instance) |
| 82 | else: |
| 83 | self.actions.append(action_instance) |
| 84 | except AttributeError as e: |
| 85 | self.logger.error(f"Module {module_name} is missing required attributes: {e}") |
| 86 | |
| 87 | def serve_netkb_data_json(self, handler): |