async def hostnameToIP(self, hostname, port=None): endpoints = [] #Check if hostname is IP or Hostname : try: ipobj = ipaddress.ip_address(hostname) except ValueError: res = await self.socket.run("getent hosts " + hostname + " | awk '{ print $1 }'") ips = res.stdout.splitlines() for ip in ips: ipobj = ipaddress.ip_address(ip) if ipobj.is_loopback: continue endpoint = Endpoint(ip, port if port is not None else 22) if endpoint.getId() is None: endpoint.setFound(self.connection.getEndpoint()) if not self.connection.inScope(): endpoint.unscope() try: path = Path(self.connection.getEndpoint().getHost(), endpoint) except ValueError: pass else: endpoint.save() path.save() endpoints.append(endpoint) else: if ipobj.is_loopback: return [] endpoint = Endpoint(hostname, port if port is not None else 22) if endpoint.getId() is None: endpoint.setFound(self.connection.getEndpoint()) if not self.connection.inScope(): endpoint.unscope() if endpoint.getId() is None: endpoint.save() self.newEndpoints.append(endpoint) try: path = Path(self.connection.getEndpoint().getHost(), endpoint) except ValueError: pass else: path.save() endpoints.append(endpoint) return endpoints
def run(cls, stmt, workspace): nmapfile = getattr(stmt, 'nmapfile') fromHost = getattr(stmt, 'from', "Local") if fromHost is None: src = None print("No source host specified, using Local") elif fromHost == "Local": src = None else: hosts = Host.findByName(fromHost) if len(hosts) > 1: print("Several hosts corresponding.") return False elif len(hosts) == 0: print("No host corresponding.") return False src = hosts[0] try: report = NmapParser.parse_fromfile(nmapfile) except Exception as e: print("Failed to read source file: " + str(e)) return False count = 0 countNew = 0 for host in report.hosts: for s in host.services: if s.service == "ssh": count = count + 1 newEndpoint = Endpoint(host.address, s.port) if newEndpoint.getId() is None: countNew = countNew + 1 newEndpoint.save() newPath = Path(src, newEndpoint) newPath.save() print( str(count) + " endpoints found, " + str(countNew) + " new endpoints saved") return True