def list_hostandsession(): hosts = Host.list_hosts() sessions = HeartBeat.list_sessions() # 初始化session列表 for host in hosts: host['session'] = None hosts_with_session = [] # 聚合Session和host host_exist = False for session in sessions: for host in hosts: if session.get("session_host") == host.get('ipaddress'): temp_host = copy.deepcopy(host) temp_host['session'] = session hosts_with_session.append(temp_host) host_exist = True break if host_exist is True: host_exist = False else: if session.get("session_host") is None or session.get( "session_host") == "": host_exist = False else: # 减少新建无效的host if session.get("available"): host_create = Host.create_host( session.get("session_host")) else: host_create = Host.create_host("255.255.255.255") host_create['session'] = session hosts_with_session.append(host_create) host_exist = False for host in hosts: add = True for temp_host in hosts_with_session: if temp_host.get("id") == host.get("id"): add = False break if add: hosts_with_session.append(host) # 设置host的proxy信息 # 收集所有hostip ipaddress_list = [] for host in hosts_with_session: ipaddress_list.append(host.get('ipaddress')) i = 0 for one in hosts_with_session: one["order_id"] = i i += 1 return hosts_with_session
def destroy(self, request, pk=None, **kwargs): try: hid_str = request.query_params.get('hid', -1) # 多个 if "," in hid_str: hids = [] for i in hid_str.split(","): hids.append(int(i)) context = Host.destory_mulit(hids) else: hid = int(hid_str) context = Host.destory_single(hid) except Exception as E: logger.error(E) context = data_return(500, CODE_MSG.get(500), {}) return Response(context)
def destroy(self, request, pk=None, **kwargs): try: ipaddress_str = request.data.get('ipaddress', None) # 多个 if "," in ipaddress_str: ipaddress_list = [] for i in ipaddress_str.split(","): ipaddress_list.append(i) context = Host.destory_mulit(ipaddress_list) else: ipaddress = ipaddress_str context = Host.destory_single(ipaddress) except Exception as E: logger.error(E) context = data_return(500, CODE_MSG.get(500), {}) return Response(context)
def add_host(self, ipaddress, source, linktype, data): """新增主机 ipaddress:新增主机ip地址 source:数据来源,ip地址格式 linktype: 如何连接到网络拓扑中, "scan"标识通过网络扫描新增主机,source必须填写正确ip地址 data:补充信息 {"method": "arp"} """ result = Host.create_host(ipaddress, source, linktype, data) return result
def update(self, request, pk=None, **kwargs): try: hid = int(request.data.get('hid', None)) tag = str(request.data.get('tag', None)) comment = request.data.get('comment', None) context = Host.update(hid, tag, comment) except Exception as E: logger.error(E) context = data_return(500, CODE_MSG.get(500), {}) return Response(context)
def list(hid=None, loadpath=None): host = Host.get_by_hid(hid) result = Xcache.get_module_result(ipaddress=host.get("ipaddress"), loadpath=loadpath) result_dict = { "hid": hid, "loadpath": loadpath, "update_time": result.get("update_time"), "result": result.get("result") } context = data_return(200, CODE_MSG.get(200), result_dict) return context
def add_vulnerability(self, hid_or_ipaddress=None, extra_data=None, desc=''): if extra_data is None: extra_data = {} if isinstance(extra_data, dict) is not True: logger.warning('数据类型检查错误,数据 {}'.format(extra_data)) extra_data = {} if isinstance(hid_or_ipaddress, int): result = Vulnerability.add_or_update(hid_or_ipaddress, self.loadpath, extra_data, desc) return result elif isinstance(hid_or_ipaddress, str): result = Vulnerability.add_or_update( Host.get_by_ipaddress(hid_or_ipaddress).get('id'), self.loadpath, extra_data, desc) return result
def list(): route_list = Route.list_route() socks_list = Socks.list_msf_socks() portfwds = PortFwd.list_portfwd() # 检查host对应的路由信息 ipaddresses = [] hosts = Host.list_hosts() for onehost in hosts: ipaddresses.append(onehost.get("ipaddress")) route_session_list = Route.get_match_route_for_ipaddress_list( ipaddresses) if route_session_list is None: for host in hosts: host['route'] = {'type': 'DIRECT', 'data': None} else: try: for host, route_session in zip(hosts, route_session_list): sessionid = route_session.get('session') if sessionid is None: # TODO 处理socks代理类型 host['route'] = {'type': 'DIRECT', 'data': None} else: host['route'] = {'type': 'ROUTE', 'data': sessionid} except Exception as E: logger.error(E) result = { 'socks': socks_list, 'routes': route_list, 'portfwds': portfwds, 'hostsRoute': hosts } context = data_return(200, CODE_MSG.get(200), result) return context
def list(self, request, **kwargs): context = Host.list() return Response(context)
def __init__(self, sessionid, hid, custom_param): super().__init__(custom_param) # 父类无需入参 # 设置内部参数 self._hid = hid # 前端传入的hid信息 self._sessionid = sessionid # 前端传入的sessionid self._ipaddress = Host.get_ipaddress_by_hid(self._hid)
def add_host(self, ipaddress): result = Host.create_host(ipaddress) hid = result.get('id') return hid