Ejemplo n.º 1
0
    def get_ips_from_hostname(self, hostname):
        """Get IPs from the hostname

        :param hostname: Target hostname
        :type hostname: `str`
        :return: IP addresses of the target hostname as a list
        :rtype: `list`
        """
        ip = ''
        # IP validation based on @marcwickenden's pull request, thanks!
        for sck in [socket.AF_INET, socket.AF_INET6]:
            try:
                socket.inet_pton(sck, hostname)
                ip = hostname
                break
            except socket.error:
                continue
        if not ip:
            try:
                ip = socket.gethostbyname(hostname)
            except socket.gaierror:
                raise UnresolvableTargetException("Unable to resolve: '%s'" %
                                                  hostname)

        ipchunks = ip.strip().split("\n")
        return ipchunks
Ejemplo n.º 2
0
    def get_ip_from_hostname(self, hostname):
        """Get IP from the hostname

        :param hostname: Target hostname
        :type hostname: `str`
        :return: IP address of the target hostname
        :rtype: `str`
        """
        ip = ''
        # IP validation based on @marcwickenden's pull request, thanks!
        for sck in [socket.AF_INET, socket.AF_INET6]:
            try:
                socket.inet_pton(sck, hostname)
                ip = hostname
                break
            except socket.error:
                continue
        if not ip:
            try:
                ip = socket.gethostbyname(hostname)
            except socket.gaierror:
                raise UnresolvableTargetException("Unable to resolve: '%s'" %
                                                  hostname)

        ipchunks = ip.strip().split("\n")
        alternative_ips = []
        if len(ipchunks) > 1:
            ip = ipchunks[0]
            cprint("%s has several IP addresses: (%s).Choosing first: %s" %
                   (hostname, "".join(ipchunks)[0:-3], ip))
            alternative_ips = ipchunks[1:]
        self.set_val('alternative_ips', alternative_ips)
        ip = ip.strip()
        self.set_val('INTERNAL_IP', is_internal_ip(ip))
        logging.info("The IP address for %s is: '%s'" % (hostname, ip))
        return ip
Ejemplo n.º 3
0
    def derive_config_from_url(self, target_URL):
        """Automatically find target information based on target name.

        .note::
            If target does not start with 'http' or 'https', then it is considered as a network target.

            + target host
            + target port
            + target url
            + target path
            + etc.

        :param target_URL: Target url supplied
        :type target_URL: `str`
        :return: Target config dictionary
        :rtype: `dict`
        """
        target_config = dict(target_manager.TARGET_CONFIG)
        target_config['target_url'] = target_URL
        try:
            parsed_url = urlparse(target_URL)
            if not parsed_url.hostname and not parsed_url.path:  # No hostname and no path, urlparse failed.
                raise ValueError
        except ValueError:  # Occurs sometimes when parsing invalid IPv6 host for instance
            raise UnresolvableTargetException("Invalid hostname '%s'" %
                                              str(target_URL))

        host = parsed_url.hostname
        if not host:  # Happens when target is an IP (e.g. 127.0.0.1)
            host = parsed_url.path  # Use the path as host (e.g. 127.0.0.1 => host = '' and path = '127.0.0.1')
            host_path = host
        else:
            host_path = parsed_url.hostname + parsed_url.path

        url_scheme = parsed_url.scheme
        protocol = parsed_url.scheme
        if parsed_url.port is None:  # Port is blank: Derive from scheme (default port set to 80).
            try:
                host, port = host.rsplit(':')
            except ValueError:  # Raised when target doesn't contain the port (e.g. google.fr)
                port = '80'
                if 'https' == url_scheme:
                    port = '443'
        else:  # Port found by urlparse.
            port = str(parsed_url.port)

        # Needed for google resource search.
        target_config['host_path'] = host_path
        # Some tools need this!
        target_config['url_scheme'] = url_scheme
        # Some tools need this!
        target_config['port_number'] = port
        # Set the top URL.
        target_config['host_name'] = host

        host_ip = self.get_ip_from_hostname(host)
        host_ips = self.get_ips_from_hostname(host)
        target_config['host_ip'] = host_ip
        target_config['alternative_ips'] = host_ips

        ip_url = target_config['target_url'].replace(host, host_ip)
        target_config['ip_url'] = ip_url
        target_config['top_domain'] = target_config['host_name']

        hostname_chunks = target_config['host_name'].split('.')
        if target_config['target_url'].startswith(
            ('http', 'https')):  # target considered as hostname (web plugins)
            if not target_config['host_name'] in target_config[
                    'alternative_ips']:
                target_config['top_domain'] = '.'.join(hostname_chunks[1:])
            # Set the top URL (get "example.com" from "www.example.com").
            target_config['top_url'] = "%s://%s:%s" % (protocol, host, port)
        else:  # target considered as IP (net plugins)
            target_config['top_domain'] = ''
            target_config['top_url'] = ''
        return target_config