def remove_scanner_info(self, scanner_id): """ 删除指定的scanner信息 Parameters: scanner_id - int """ host = self._scanner_list[scanner_id]["host"] port = self._scanner_list[scanner_id]["port"] host_port = common.concat_host(host, port) del self._scanner_map[host_port] self._scanner_list[scanner_id] = None
def mod_config(self, module_params): """ 修改扫描目标的配置 Parameters: module_params - dict, 结构为{ "host":str, 目标主机, "port":int, 目标端口 "config": dict, 配置信息 } """ host_port = common.concat_host(module_params["host"], module_params["port"]) self._config.mod_config(host_port, module_params["config"])
def get_config(self, module_params): """ 获取扫描目标的配置 Parameters: module_params - dict, 结构为{ "host":str, 目标主机, "port":int, 目标端口 } Returns: dict - 配置信息 """ host_port = common.concat_host(module_params["host"], module_params["port"]) return self._config.get_config(host_port)
def new_scanner(self, module_params): """ 创建一个新的扫描任务 Parameters: module_params - dict, 结构为{ "host":str, 目标主机, "port":int, 目标端口 "config": dict, 配置信息 } Raises: exceptions.MaximumScannerExceede - 扫描任务数量到达上限,引发此异常 exceptions.TargetIsScanning - 指定目标正在被其他任务扫描,引发此异常 """ host = module_params["host"] port = module_params["port"] host_port = common.concat_host(host, port) if self._scanner_info.is_scanning(host_port): raise exceptions.TargetIsScanning idle_scanner = self._scanner_info.get_idle_scanner() if idle_scanner is None: raise exceptions.MaximumScannerExceede # 确保数据库中存在扫描配置 self._config.mod_config(host_port, {}) # 启动扫描进程 scanner_process_kwargs = { "module_cls": modules.Scanner, "instance_id": idle_scanner, "module_params": { "host": host, "port": port } } Communicator().reset_all_value("Scanner_" + str(idle_scanner)) pid = ForkProxy().fork(scanner_process_kwargs) # 在共享内存中记录pid Communicator().set_value("pid", pid, "Scanner_" + str(idle_scanner)) # 记录扫描器id相关信息 self._scanner_info.set_scanner_info(idle_scanner, pid, host, port)
def set_scanner_info(self, scanner_id, pid, host, port): """ 添加一条scanner信息 Parameters: scanner_id - int, 指定的scanner的id, 该id必须未使用 pid - int, 进程pid host - str, 目标host port - int, 目标端口 """ scanner_info = { "scanner_id": scanner_id, "pid": pid, "host": host, "port": port } self._scanner_list[scanner_id] = scanner_info self._scanner_map[common.concat_host(host, port)] = scanner_info
def clean_target(self, host, port, url_only=False): """ 清空目标对应的数据库,同时重置预处理lru Parameters: host - str, 目标host port - int, 目标port url_only - bool, 是否仅清空url Raises: exceptions.DatabaseError - 数据库出错时引发此异常 """ host_port = common.concat_host(host, port) if self._scanner_info.get_scanner_id(host_port) is not None: raise exceptions.TargetIsScanning if url_only: NewRequestModel(host_port, multiplexing_conn=True).truncate_table() else: NewRequestModel(host_port, multiplexing_conn=True).drop_table() ReportModel(host_port, multiplexing_conn=True).drop_table() self._config.del_config(host_port) Communicator().set_clean_lru([host_port])