Example #1
0
    def hostnameToIP(self, hostname, port=None):
        endpoints = []
        #Check if hostname is IP or Hostname :
        try:
            ipobj = ipaddress.ip_address(hostname)
        except ValueError:
            chan = self.connection.transport.open_channel("session", timeout=3)
            ips = ""
            chan.exec_command("getent hosts " + hostname +
                              " | awk '{ print $1 }'")
            try:
                x = u(chan.recv(1024))
                while len(x) != 0:
                    ips = ips + x
                    x = u(chan.recv(1024))
            except socket.timeout:
                pass
            chan.close()

            ips = ips.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.id is None:
                    endpoint.found = self.connection.endpoint
                if not self.connection.scope:
                    endpoint.scope = False
                try:
                    path = Path(self.connection.endpoint.host, 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.id is None:
                endpoint.found = self.connection.endpoint
            if not self.connection.scope:
                endpoint.scope = False
            if endpoint.id is None:
                endpoint.save()
                self.newEndpoints.append(endpoint)
            try:
                path = Path(self.connection.endpoint.host, endpoint)
            except ValueError:
                pass
            else:
                path.save()
                endpoints.append(endpoint)
        return endpoints
Example #2
0
    def addEndpoint(self,ip,port):
        if not self.checkIsIP(ip):
            print("The address given isn't a valid IP")
            raise ValueError
        if not port.isdigit():
            print("The port given isn't a positive integer")
            raise ValueError

        newEndpoint = Endpoint(ip,port)
        newEndpoint.save()
Example #3
0
 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
Example #4
0
    def run(cls, stmt, workspace):
        nmapfile = getattr(stmt, 'nmapfile')
        from_host = getattr(stmt, 'from', "Local")

        if from_host is None:
            print("No source host specified, ignoring paths")
            distance = None
        elif from_host == "Local":
            src = None
            distance = 0
        else:
            host = Host.find_one(name=from_host)
            if host is None:
                print("No host corresponding.")
                return False
            src = host
            distance = src.distance + 1
        try:
            report = NmapParser.parse_fromfile(nmapfile)
        except Exception as e:
            print("Failed to read source file: " + str(e))
            return False
        count = 0
        count_new = 0
        for host in report.hosts:
            for s in host.services:
                if s.service == "ssh" and s.open():
                    count = count + 1
                    new_endpoint = Endpoint(host.address, s.port)
                    if new_endpoint.id is None:
                        count_new = count_new + 1
                    new_endpoint.save()
                    if distance is not None:
                        if new_endpoint.distance is None or new_endpoint.distance > distance:
                            new_endpoint.distance = distance
                            new_endpoint.save()
                        new_path = Path(src, new_endpoint)
                        new_path.save()
        print(
            str(count) + " endpoints found, " + str(count_new) +
            " new endpoints saved")
        return True
Example #5
0
    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