def import_exploit(path): """ Imports exploit module :param path: absolute path to exploit e.g. routersploit.modules.exploits.asus_auth_bypass :return: exploit module or error """ try: module = importlib.import_module(path) if hasattr(module, "Payload"): return getattr(module, "Payload") elif hasattr(module, "Encoder"): return getattr(module, "Encoder") elif hasattr(module, "Exploit"): return getattr(module, "Exploit") else: raise ImportError("No module named '{}'".format(path)) except (ImportError, AttributeError, KeyError) as err: raise RoutersploitException( "Error during loading '{}'\n\n" "Error: {}\n\n" "It should be valid path to the module. " "Use <tab> key multiple times for completion.".format(humanize_path(path), err) )
def parse_protocal(self, protocal_str: str) -> bool: if not protocal_str.strip().startswith('{}://'.format( self.engine_name)): raise RoutersploitException( "Engine {} protocal must start switch {}://".format( self.engine_name, self.engine_name)) _, _, self.file_path = protocal_str.strip().partition('{}://'.format( self.engine_name)) return True
def get_command_handler(self, command): """ Parsing command and returning appropriate handler. :param command: command :return: command_handler """ try: command_handler = getattr(self, "command_{}".format(command)) except AttributeError: raise RoutersploitException("Unknown command: '{}'".format(command)) return command_handler
def get_engine_module(engine_name: str, engine_directory: str = ENGINE_DIR): """ :param str engine_name: engine name :param str engine_directory: engine directory :return: Search module """ try: engines_dct = index_modules(engine_directory) return import_engine(engines_dct[engine_name]) except RoutersploitException as err: raise err except KeyError: raise RoutersploitException( "Engine {} doesn't exist.".format(engine_name))
def get_save_module(save_name: str, save_directory: str = SAVE_DIR): """ :param str save_name: save name :param str save_directory: save directory :return: Save module """ try: saves_dct = index_modules(save_directory) return import_save(saves_dct[save_name]) except RoutersploitException as err: raise err except KeyError: raise RoutersploitException( "Save module {} doesn't exist.".format(save_name))
def save_to(save_protocal: str, results: dict): """ :param save_protocal: save protocal str :return: """ save_name, _, _ = save_protocal.strip().partition(':') try: save = get_save_module(save_name) save.save(save_protocal, results) except RoutersploitException as err: raise err except Exception as err: raise RoutersploitException( "Error during save result to '{}' Save module\n\n" "Error: {}\n\n".format(save_name, err))
def import_engine(path: str): """ Import Search engine :param str path: engine python path :return: Search module """ try: module = importlib.import_module(path) if hasattr(module, "Search"): return getattr(module, "Search")() else: raise ImportError("No module named '{}'".format(path)) except (ImportError, AttributeError, KeyError) as err: raise RoutersploitException("Error during loading '{}'\n\n" "Error: {}\n\n".format( humanize_path(path), err))
def get_targets_from_engine(engine_protocal: str): """ :param engine_protocal: engine protocal string :return: Generator contains ip and port """ engine_name, _, _ = engine_protocal.strip().partition(':') try: engine = get_engine_module(engine_name) for target in engine.search(engine_protocal): target, _, port = target.partition(":") yield target, port except RoutersploitException as err: raise err except Exception as err: raise RoutersploitException( "Error during fetch target from '{}' engine\n\n" "Error: {}\n\n".format(engine_name, err))