Ejemplo n.º 1
0
    def GetIPFromHostname(self, hostname):
        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(hostname + " has several IP addresses: (" +
                   ", ".join(ipchunks)[0:-3] + "). Choosing first: " + ip + "")
            alternative_IPs = ipchunks[1:]
        self.Set('alternative_ips', alternative_IPs)
        ip = ip.strip()
        self.Set('INTERNAL_IP', is_internal_ip(ip))
        logging.info("The IP address for %s is: '%s'" % (hostname, ip))
        return ip
Ejemplo n.º 2
0
    def DeriveConfigFromURL(self, target_URL):
        target_config = dict(target_manager.TARGET_CONFIG)
        # Set the target in the config.
        target_config['target_url'] = target_URL
        # TODO: Use urlparse here.
        try:
            parsed_URL = urlparse(target_URL)
            if not parsed_URL.hostname or parsed_URL.path is None:
                raise ValueError
        except ValueError:  # Occurs when parsing invalid IPv6 host or when host is None
            raise UnresolvableTargetException("Invalid hostname '%s'" %
                                              str(target_URL))
        URL_scheme = parsed_URL.scheme
        protocol = parsed_URL.scheme
        if parsed_URL.port == None:  # Port is blank: Derive from scheme.
            port = '80'
            if 'https' == URL_scheme:
                port = '443'
        else:  # Port found by urlparse.
            port = str(parsed_URL.port)
        host = parsed_URL.hostname
        host_path = parsed_URL.hostname + parsed_URL.path
        # 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.GetIPFromHostname(host)
        host_IPs = self.GetIPsFromHostname(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 not self.hostname_is_ip(host, host_IP) and len(hostname_chunks) > 2:
            # Get "example.com" from "www.example.com"
            target_config['top_domain'] = '.'.join(hostname_chunks[1:])
        # Set the top URL.
        target_config['top_url'] = protocol + "://" + host + ":" + port
        return target_config
Ejemplo n.º 3
0
    def GetIPsFromHostname(self, hostname):
        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.º 4
0
    def DeriveConfigFromURL(self, target_URL):
        """Automatically find target information based on target name.

        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.
        """
        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.GetIPFromHostname(host)
        host_IPs = self.GetIPsFromHostname(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