Python dns.resolver.Timeout() Examples

The following are 15 code examples of dns.resolver.Timeout(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module dns.resolver , or try the search function .
Example #1
Source File: cloudfront.py    From Sitadel with GNU General Public License v3.0 7 votes vote down vote up
def process(self, headers, content):
        request = Services.get("request_factory")
        hostname = urlparse(request.url).hostname
        _ = False
        try:
            resolver = Resolver(configure=False)
            resolver.nameservers = [settings.dns_resolver]
            resolver.timeout = 2
            resolver.lifetime = 2

            dns_query = resolver.query(hostname, "CNAME")

            if len(dns_query) > 0:
                for answer in dns_query:
                    _ |= re.search(r"cloudfront\.net", str(answer), re.I) is not None
            if _:
                return "CloudFront CDN (Amazon)"
        except NoAnswer:
            pass
        except NXDOMAIN:
            pass
        except Timeout:
            pass 
Example #2
Source File: akamai.py    From Sitadel with GNU General Public License v3.0 6 votes vote down vote up
def process(self, headers, content):
        request = Services.get("request_factory")
        hostname = urlparse(request.url).hostname
        try:
            resolver = Resolver(configure=False)
            resolver.nameservers = [settings.dns_resolver]
            resolver.timeout = 2
            resolver.lifetime = 2

            dns_query = resolver.query(hostname + ".edgekey.net", "A")

            if len(dns_query) > 0:
                return "Akamai CDN"

        except NXDOMAIN:
            pass
        except NoAnswer:
            pass
        except Timeout:
            pass 
Example #3
Source File: azure.py    From Sitadel with GNU General Public License v3.0 6 votes vote down vote up
def process(self, headers, content):
        request = Services.get("request_factory")
        hostname = urlparse(request.url).hostname
        _ = False

        try:
            resolver = Resolver(configure=False)
            resolver.nameservers = [settings.dns_resolver]
            resolver.timeout = 2
            resolver.lifetime = 2

            dns_query = resolver.query(hostname, "CNAME")
            if len(dns_query) > 0:
                for answer in dns_query:
                    _ |= re.search(r"azureedge\.net", str(answer), re.I) is not None
            if _:
                return "Azure CDN"
        except NoAnswer:
            pass
        except NXDOMAIN:
            pass
        except Timeout:
            pass 
Example #4
Source File: cloudflare.py    From Sitadel with GNU General Public License v3.0 6 votes vote down vote up
def process(self, headers, content):
        request = Services.get("request_factory")
        hostname = urlparse(request.url).hostname
        try:
            resolver = Resolver(configure=False)
            resolver.nameservers = [settings.dns_resolver]
            resolver.timeout = 2
            resolver.lifetime = 2
            dns_query = resolver.query(hostname + ".cdn.cloudflare.net", "A")

            if len(dns_query) > 0:
                return "Cloudflare CDN"

        except NXDOMAIN:
            pass
        except NoAnswer:
            pass
        except Timeout:
            pass 
Example #5
Source File: fastly.py    From Sitadel with GNU General Public License v3.0 6 votes vote down vote up
def process(self, headers, content):
        request = Services.get("request_factory")
        hostname = urlparse(request.url).hostname
        _ = False
        try:
            resolver = Resolver(configure=False)
            resolver.nameservers = [settings.dns_resolver]
            resolver.timeout = 2
            resolver.lifetime = 2

            dns_query = resolver.query(hostname, "CNAME")
            if len(dns_query) > 0:
                for answer in dns_query:
                    _ |= re.search(r"fastly\.net", str(answer), re.I) is not None
            if _:
                return "Fastly CDN"
        except NoAnswer:
            pass
        except NXDOMAIN:
            pass
        except Timeout:
            pass 
Example #6
Source File: __init__.py    From bearded-avenger with Mozilla Public License 2.0 6 votes vote down vote up
def resolve_ns(data, t='A', timeout=HUNTER_RESOLVER_TIMEOUT):
    resolver = dns.resolver.Resolver()
    resolver.timeout = timeout
    resolver.lifetime = timeout
    resolver.search = []
    try:
        answers = resolver.query(data, t)
        resp = []
        for rdata in answers:
            resp.append(rdata)
    except (NoAnswer, NXDOMAIN, EmptyLabel, NoNameservers, Timeout) as e:
        if str(e).startswith('The DNS operation timed out after'):
            logger.info('{} - {} -- this may be normal'.format(data, e))
            return []

        if not str(e).startswith('The DNS response does not contain an answer to the question'):
            if not str(e).startswith('None of DNS query names exist'):
                logger.info('{} - {}'.format(data, e))
        return []

    return resp 
Example #7
Source File: url.py    From sync-engine with GNU Affero General Public License v3.0 6 votes vote down vote up
def get_mx_domains(domain, dns_resolver=_dns_resolver):
    """ Retrieve and return the MX records for a domain. """
    mx_records = []
    try:
        mx_records = dns_resolver().query(domain, 'MX')
    except NoNameservers:
        log.error('NoMXservers', domain=domain)
    except NXDOMAIN:
        log.error('No such domain', domain=domain)
    except Timeout:
        log.error('Time out during resolution', domain=domain)
        raise
    except NoAnswer:
        log.error('No answer from provider', domain=domain)
        mx_records = _fallback_get_mx_domains(domain)

    return [str(rdata.exchange).lower() for rdata in mx_records] 
Example #8
Source File: dnspython.py    From vpn-slice with GNU General Public License v3.0 5 votes vote down vote up
def lookup_host(self, hostname, keep_going=True):
        result = set()

        for source in self.bind_addresses or [None]:
            if source is None:
                self.resolver.nameservers = self.nameservers
            else:
                self.resolver.nameservers = [str(dns) for dns in self.dns_servers if dns.version == source.version]
                if not self.resolver.nameservers:
                    continue

            for rectype in self.rectypes:
                try:
                    # print("Issuing query for hostname %r, rectype %r, source %r, search_domains %r, nameservers %r" % (
                    #     hostname, rectype, source, self.resolver.search_domains, self.resolver.nameservers), file=stderr)
                    a = self.resolver.query(hostname, rectype, source=str(source))
                    print("Got results: %r" % list(a), file=stderr)
                except (NXDOMAIN, NoAnswer):
                    pass
                except Timeout:
                    # No point in retrying with a different rectype if these DNS server(s) are not responding
                    break
                else:
                    result.update(ip_address(r.address) for r in a)
                if result and not keep_going:
                    return result

        return result or None 
Example #9
Source File: fqdn_cname.py    From bearded-avenger with Mozilla Public License 2.0 5 votes vote down vote up
def process(self, i, router):
        if i.itype != 'fqdn':
            return

        if 'search' in i.tags:
            return

        try:
            r = resolve_ns(i.indicator, t='CNAME')
        except Timeout:
            self.logger.info('timeout trying to resolve: {}'.format(i.indicator))
            r = []

        for rr in r:
            # http://serverfault.com/questions/44618/is-a-wildcard-cname-dns-record-valid
            rr = str(rr).rstrip('.').lstrip('*.')
            if rr in ['', 'localhost', '0.0.0.0']:
                continue

            fqdn = Indicator(**i.__dict__())
            fqdn.indicator = rr
            fqdn.lasttime = arrow.utcnow()

            try:
                resolve_itype(fqdn.indicator)
            except InvalidIndicator as e:
                self.logger.error(fqdn)
                self.logger.error(e)
                return

            fqdn.itype = 'fqdn'
            fqdn.confidence = (fqdn.confidence - 1)
            router.indicators_create(fqdn) 
Example #10
Source File: fqdn_ns.py    From bearded-avenger with Mozilla Public License 2.0 5 votes vote down vote up
def process(self, i, router):
        if i.itype != 'fqdn':
            return

        if 'search' in i.tags:
            return

        try:
            r = resolve_ns(i.indicator)
        except Timeout:
            self.logger.info('timeout trying to resolve: {}'.format(i.indicator))
            return

        for rr in r:
            if str(rr).rstrip('.') in ["", 'localhost', '0.0.0.0']:
                continue

            ip = Indicator(**i.__dict__())
            ip.indicator = str(rr)
            ip.lasttime = arrow.utcnow()

            try:
                resolve_itype(ip.indicator)
            except InvalidIndicator as e:
                self.logger.error(ip)
                self.logger.error(e)
            else:
                ip.itype = 'ipv4'
                ip.rdata = i.indicator
                ip.confidence = (ip.confidence - 4) if ip.confidence >= 4 else 0
                router.indicators_create(ip) 
Example #11
Source File: fqdn_mx.py    From bearded-avenger with Mozilla Public License 2.0 5 votes vote down vote up
def process(self, i, router):
        if i.itype != 'fqdn':
            return

        if 'search' in i.tags:
            return

        try:
            r = resolve_ns(i.indicator, t='MX')
        except Timeout:
            self.logger.info('timeout trying to resolve MX for: {}'.format(i.indicator))
            return

        for rr in r:
            rr = re.sub(r'^\d+ ', '', str(rr))
            rr = str(rr).rstrip('.')

            if rr in ["", 'localhost', '0.0.0.0']:
                continue

            fqdn = Indicator(**i.__dict__())
            fqdn.indicator = rr.rstrip('.')
            fqdn.lasttime = arrow.utcnow()

            # 10
            if re.match('^\d+$', rr):
                return

            try:
                resolve_itype(fqdn.indicator)
            except InvalidIndicator as e:
                self.logger.info(fqdn)
                self.logger.info(e)
            else:
                fqdn.itype = 'fqdn'
                fqdn.rdata = i.indicator
                fqdn.confidence = (fqdn.confidence - 5) if fqdn.confidence >= 5 else 0
                router.indicators_create(fqdn) 
Example #12
Source File: fqdn.py    From bearded-avenger with Mozilla Public License 2.0 5 votes vote down vote up
def process(self, i, router):
        if i.itype != 'fqdn':
            return

        if 'search' in i.tags:
            return

        try:
            r = resolve_ns(i.indicator)
        except Timeout:
            self.logger.info('timeout trying to resolve: {}'.format(i.indicator))
            return

        for rr in r:
            rr = str(rr)
            if rr in ["", 'localhost', '0.0.0.0']:
                continue

            ip = Indicator(**i.__dict__())
            ip.lasttime = arrow.utcnow()

            ip.indicator = rr
            try:
                resolve_itype(ip.indicator)
            except InvalidIndicator as e:
                self.logger.error(ip)
                self.logger.error(e)
            else:
                ip.itype = 'ipv4'
                ip.rdata = i.indicator
                ip.confidence = (ip.confidence - 2) if ip.confidence >= 2 else 0
                router.indicators_create(ip)

                # also create a passive dns tag
                ip.tags = 'pdns'
                ip.confidence = 10
                router.indicators_create(ip) 
Example #13
Source File: resolve_hostnames.py    From yeti with Apache License 2.0 4 votes vote down vote up
def consumer(self):
        while True:
            try:
                hostname, rtype = self.queue.get(True, 5)
            except Empty:
                logging.debug("Empty! Bailing")
                return
            try:
                logging.debug("Starting work on {}".format(hostname))
                results = self.resolver.query(hostname, rtype)
                if results:
                    if hostname not in self.results:
                        self.results[hostname] = {}
                    text_results = []
                    for r in results:
                        if isinstance(r, NS_class):
                            text_results.append(r.target.to_text())
                        elif isinstance(r, A_class):
                            text_results.append(r.to_text())
                        else:
                            logging.error(
                                "Unknown record type: {}".format(type(r)))
                    hostname = Hostname(value=hostname)
                    ResolveHostnames.each(hostname, rtype, text_results)
            except NoAnswer:
                continue
            except NXDOMAIN:
                continue
            except Timeout:
                logging.debug("Request timed out for {}".format(hostname))
                continue
            except NoNameservers:
                continue
            except Exception as e:
                import traceback
                logging.error(
                    "Unknown error occurred while working on {} ({})".format(
                        hostname, rtype))
                logging.error("\nERROR: {}".format(hostname, rtype, e))
                logging.error(traceback.print_exc())

            continue 
Example #14
Source File: domain.py    From BloodHound.py with MIT License 4 votes vote down vote up
def gc_connect(self, protocol='ldap'):
        """
        Connect to the global catalog
        """
        if self.hostname in self.ad.gcs():
            # This server is a Global Catalog
            initial_server = self.hostname
        else:
            # Pick the first GC server
            try:
                initial_server = self.ad.gcs()[0]
            except IndexError:
                # TODO: implement fallback options for GC detection?
                logging.error('Could not find a Global Catalog in this domain!'\
                              ' Resolving will be unreliable in forests with multiple domains')
                return False
        try:
            # Convert the hostname to an IP, this prevents ldap3 from doing it
            # which doesn't use our custom nameservers
            logging.info('Connecting to GC LDAP server: %s' % initial_server)
            q = self.ad.dnsresolver.query(initial_server, tcp=self.ad.dns_tcp)
            for r in q:
                ip = r.address
        except (resolver.NXDOMAIN, resolver.Timeout):
            for server in self.ad.gcs():
                # Skip the one we already tried
                if server == initial_server:
                    continue
                try:
                    # Convert the hostname to an IP, this prevents ldap3 from doing it
                    # which doesn't use our custom nameservers
                    logging.info('Connecting to GC LDAP server: %s' % server)
                    q = self.ad.dnsresolver.query(server, tcp=self.ad.dns_tcp)
                    for r in q:
                        ip = r.address
                        break
                except (resolver.NXDOMAIN, resolver.Timeout):
                    continue

        self.gcldap = self.ad.auth.getLDAPConnection(hostname=ip, gc=True,
                                                     baseDN=self.ad.baseDN, protocol=protocol)
        return self.gcldap is not None 
Example #15
Source File: url.py    From sync-engine with GNU Affero General Public License v3.0 4 votes vote down vote up
def provider_from_address(email_address, dns_resolver=_dns_resolver):
    if not EMAIL_REGEX.match(email_address):
        raise InvalidEmailAddressError('Invalid email address')

    domain = email_address.split('@')[1].lower()
    mx_domains = get_mx_domains(domain, dns_resolver)
    ns_records = []
    try:
        ns_records = dns_resolver().query(domain, 'NS')
    except NoNameservers:
        log.error('NoNameservers', domain=domain)
    except NXDOMAIN:
        log.error('No such domain', domain=domain)
    except Timeout:
        log.error('Time out during resolution', domain=domain)
    except NoAnswer:
        log.error('No answer from provider', domain=domain)

    for (name, info) in providers.iteritems():
        provider_domains = info.get('domains', [])

        # If domain is in the list of known domains for a provider,
        # return the provider.
        for d in provider_domains:
            if domain.endswith(d):
                return name

    for (name, info) in providers.iteritems():
        provider_mx = info.get('mx_servers', [])

        # If a retrieved mx_domain is in the list of stored MX domains for a
        # provider, return the provider.
        if mx_match(mx_domains, provider_mx):
            return name

    for (name, info) in providers.iteritems():
        provider_ns = info.get('ns_servers', [])

        # If a retrieved name server is in the list of stored name servers for
        # a provider, return the provider.
        for rdata in ns_records:
            if str(rdata).lower() in provider_ns:
                return name

    return 'unknown'


# From tornado.httputil