Python dns.resolver.NoNameservers() Examples

The following are 18 code examples of dns.resolver.NoNameservers(). 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: checkresolvers.py    From dnsbrute with MIT License 7 votes vote down vote up
def check_for_wildcards(args, server, name, rectype, tries=4):
    """
    Verify that the DNS server doesn't return wildcard results for domains
    which don't exist, it should correctly return NXDOMAIN.
    """
    resolver = Resolver()
    resolver.timeout = args.timeout
    resolver.lifetime = args.timeout
    resolver.nameservers = [server]
    nx_names = [base64.b32encode(
                    os.urandom(
                        random.randint(8, 10))
                ).strip('=').lower() + name
                for _ in range(0, tries)]
    correct_result_count = 0
    for check_nx_name in nx_names:
        try:
            result = resolver.query(check_nx_name, rectype)            
            return False  # Any valid response = immediate fail!
        except (NXDOMAIN, NoNameservers):
            correct_result_count += 1
        except DNSException:
            continue        
    return correct_result_count > (tries / 2.0) 
Example #2
Source File: dns_handler.py    From Raccoon with MIT License 6 votes vote down vote up
def query_dns(cls, domains, records):
        """
        Query DNS records for host.
        :param domains: Iterable of domains to get DNS Records for
        :param records: Iterable of DNS records to get from domain.
        """
        results = {k: set() for k in records}
        for record in records:
            for domain in domains:
                try:
                    answers = cls.resolver.query(domain, record)
                    for answer in answers:
                        # Add value to record type
                        results.get(record).add(answer)
                except (resolver.NoAnswer, resolver.NXDOMAIN, resolver.NoNameservers):
                    # Type of record doesn't fit domain or no answer from ns
                    continue

        return {k: v for k, v in results.items() if v} 
Example #3
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 #4
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 #5
Source File: test_rbl.py    From pygameweb with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def test_rbl():
    """ handles failure ok, and handles lookups ok with dns module.
    """
    import mock
    from dns.resolver import NoAnswer, NXDOMAIN, NoNameservers

    from pygameweb.user.rbl import rbl

    assert rbl('127.0.0.1') is False
    assert rbl('') is False

    with mock.patch('dns.resolver.query') as query:
        query.side_effect = NoAnswer()
        assert rbl('192.168.0.1') is False
        query.side_effect = NXDOMAIN()
        assert rbl('192.168.0.1') is False
        query.side_effect = NoNameservers()
        assert rbl('192.168.0.1') is False

    with mock.patch('dns.resolver.query') as query:
        query.side_effect = '127.0.0.2'
        assert rbl('192.168.0.1') is True
        assert query.called 
Example #6
Source File: test_lookup_dns.py    From PyFunceble with Apache License 2.0 5 votes vote down vote up
def test_record_NoNameservers(self):
        """
        Tests the method which let us get the AAAA record for the case that
        we get a NoNameservers exception.
        """

        self.dns_lookup.resolver.query = Mock(side_effect=NoNameservers())

        expected = None
        actual = self.dns_lookup.aaaa_record(self.subject)

        self.assertEqual(expected, actual) 
Example #7
Source File: test_lookup_dns.py    From PyFunceble with Apache License 2.0 5 votes vote down vote up
def test_record_NoNameservers(self):
        """
        Tests the method which let us get the PTR record for the case that
        we get a NoNameservers exception.
        """

        self.dns_lookup.resolver.query = Mock(side_effect=NoNameservers())

        expected = None
        actual = self.dns_lookup.ptr_record(self.subject)

        self.assertEqual(expected, actual) 
Example #8
Source File: test_lookup_dns.py    From PyFunceble with Apache License 2.0 5 votes vote down vote up
def test_record_NoNameservers(self):
        """
        Tests the method which let us get the NS record for the case that
        we get a NoNameservers exception.
        """

        self.dns_lookup.resolver.query = Mock(side_effect=NoNameservers())

        expected = None
        actual = self.dns_lookup.ns_record(self.subject)

        self.assertEqual(expected, actual) 
Example #9
Source File: test_lookup_dns.py    From PyFunceble with Apache License 2.0 5 votes vote down vote up
def test_record_NoNameservers(self):
        """
        Tests the method which let us get the MX record for the case that
        we get a NoNameservers exception.
        """

        self.dns_lookup.resolver.query = Mock(side_effect=NoNameservers())

        expected = None
        actual = self.dns_lookup.mx_record(self.subject)

        self.assertEqual(expected, actual) 
Example #10
Source File: test_lookup_dns.py    From PyFunceble with Apache License 2.0 5 votes vote down vote up
def test_record_NoNameservers(self):
        """
        Tests the method which let us get the CNAME record for the case that
        we get a NoNameservers exception.
        """

        self.dns_lookup.resolver.query = Mock(side_effect=NoNameservers())

        expected = None
        actual = self.dns_lookup.cname_record(self.subject)

        self.assertEqual(expected, actual) 
Example #11
Source File: models.py    From desec-stack with MIT License 5 votes vote down vote up
def public_suffix(self):
        try:
            public_suffix = psl.get_public_suffix(self.name)
            is_public_suffix = psl.is_public_suffix(self.name)
        except (Timeout, NoNameservers):
            public_suffix = self.name.rpartition('.')[2]
            is_public_suffix = ('.' not in self.name)  # TLDs are public suffixes
        except psl_dns.exceptions.UnsupportedRule as e:
            # It would probably be fine to treat this as a non-public suffix (with the TLD acting as the
            # public suffix and setting both public_suffix and is_public_suffix accordingly).
            # However, in order to allow to investigate the situation, it's better not catch
            # this exception. For web requests, our error handler turns it into a 503 error
            # and makes sure admins are notified.
            raise e

        if is_public_suffix:
            return public_suffix

        # Take into account that any of the parent domains could be a local public suffix. To that
        # end, identify the longest local public suffix that is actually a suffix of domain_name.
        for local_public_suffix in settings.LOCAL_PUBLIC_SUFFIXES:
            has_local_public_suffix_parent = ('.' + self.name).endswith('.' + local_public_suffix)
            if has_local_public_suffix_parent and len(local_public_suffix) > len(public_suffix):
                public_suffix = local_public_suffix

        return public_suffix 
Example #12
Source File: test_lookup_dns.py    From PyFunceble with Apache License 2.0 5 votes vote down vote up
def test_record_NoNameservers(self):
        """
        Tests the method which let us get the A record for the case that
        we get a NoNameservers exception.
        """

        self.dns_lookup.resolver.query = Mock(side_effect=NoNameservers())

        expected = None
        actual = self.dns_lookup.a_record(self.subject)

        self.assertEqual(expected, actual) 
Example #13
Source File: brute.py    From OneForAll with GNU General Public License v3.0 5 votes vote down vote up
def get_wildcard_record(domain, resolver):
    logger.log('INFOR', f'Query {domain} \'s wildcard dns record in authoritative name server')
    try:
        answer = resolver.query(domain, 'A')
    # 如果查询随机域名A记录时抛出Timeout异常则重新查询
    except Timeout as e:
        logger.log('ALERT', f'Query timeout, retrying')
        logger.log('DEBUG', e.args)
        raise tenacity.TryAgain
    except (NXDOMAIN, YXDOMAIN, NoAnswer, NoNameservers) as e:
        logger.log('DEBUG', e.args)
        logger.log('INFOR', f'{domain} dont have A record on authoritative name server')
        return None, None
    except Exception as e:
        logger.log('ERROR', e.args)
        logger.log('ERROR', f'Query {domain} wildcard dns record in authoritative name server error')
        exit(1)
    else:
        if answer.rrset is None:
            logger.log('DEBUG', f'No record of query result')
            return None, None
        name = answer.name
        ip = {item.address for item in answer}
        ttl = answer.ttl
        logger.log('INFOR', f'{domain} results on authoritative name server: {name} '
                            f'IP: {ip} TTL: {ttl}')
        return ip, ttl 
Example #14
Source File: brute.py    From OneForAll with GNU General Public License v3.0 5 votes vote down vote up
def do_query_a(domain, resolver):
    try:
        answer = resolver.query(domain, 'A')
    # If resolve random subdomain raise timeout error, try again
    except Timeout as e:
        logger.log('ALERT', f'DNS resolve timeout, retrying')
        logger.log('DEBUG', e.args)
        raise tenacity.TryAgain
    # If resolve random subdomain raise NXDOMAIN, YXDOMAIN, NoAnswer, NoNameservers error
    # It means that there is no A record of random subdomain and not use wildcard dns record
    except (NXDOMAIN, YXDOMAIN, NoAnswer, NoNameservers) as e:
        logger.log('DEBUG', e.args)
        logger.log('INFOR', f'{domain} seems not use wildcard dns record')
        return False
    except Exception as e:
        logger.log('ALERT', f'Detect {domain} wildcard dns record error')
        logger.log('FATAL', e.args)
        exit(1)
    else:
        if answer.rrset is None:
            logger.log('ALERT', f'DNS resolve dont have result, retrying')
            raise tenacity.TryAgain
        ttl = answer.ttl
        name = answer.name
        ips = {item.address for item in answer}
        logger.log('ALERT', f'{domain} use wildcard dns record')
        logger.log('ALERT', f'{domain} resolve to: {name} '
                            f'IP: {ips} TTL: {ttl}')
        return True 
Example #15
Source File: endpoints.py    From OpenResolve with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get(self, rdtype, domain):
        t1 = time.time()

        rdtype = rdtype.upper()
        current_app.logger.info(
            'Request from %s - %s', request.remote_addr, rdtype)
        self.valid_args(rdtype, domain)

        # Iterate through nameservers so that we can tell which one gets used.
        nameservers = current_app.config['RESOLVERS']
        for nameserver in nameservers:
            dns_resolver.nameservers = [nameserver]
            try:
                answer = dns_resolver.query(
                    domain, rdtype, raise_on_no_answer=False)
                # Successful query
                break
            except (NoNameservers, NXDOMAIN):
                # TODO: this should still follow the RFC
                return {'message': "No nameservers found for provided domain"}, 404
            except Timeout as e:
                # Communication fail or timeout - try next nameserver
                if nameserver is nameservers[-1]:
                    current_app.logger.info(e)
                    return {'message': 'All nameservers timed out.'}, 503
                continue
            except Exception as e:
                current_app.logger.error(e)
                return {'message': 'An unexpected error occured.'}, 500

        t2 = time.time()
        duration = t2 - t1

        return parse_query(answer, nameserver, duration) 
Example #16
Source File: verifier.py    From email-verifier with MIT License 4 votes vote down vote up
def verify(self, email):
        """
        method that performs the verification on the passed
        email address.
        """
        lookup = {
            'address': None,
            'valid_format': False,
            'deliverable': False,
            'full_inbox': False,
            'host_exists': False,
            'catch_all': False,
        }
        try:
            lookup['address'] = self._parse_address(email)
            lookup['valid_format'] = True
        except EmailFormatError:
            lookup['address'] = f"{email}"
            return lookup
        
        # look for mx record and create a list of mail exchanges
        try:
            mx_record = resolver.query(lookup['address'].domain, 'MX')
            mail_exchangers = [exchange.to_text().split() for exchange in mx_record]
            lookup['host_exists'] = True
        except (resolver.NoAnswer, resolver.NXDOMAIN, resolver.NoNameservers):
            lookup['host_exists'] = False
            return lookup

        for exchange in mail_exchangers:
            try:
                host_exists, deliverable, catch_all = self._can_deliver(exchange, lookup['address'])
                if deliverable:
                    lookup['host_exists'] = host_exists
                    lookup['deliverable'] = deliverable
                    lookup['catch_all'] = catch_all
                    break
            except SMTPRecepientException as err:
                # Error handlers return a dict that is then merged with 'lookup'
                kwargs = handle_error.get(err.code, handle_unrecognised)(err.response)
                # This expression merges the lookup dict with kwargs
                lookup = {**lookup, **kwargs}

            except smtplib.SMTPServerDisconnected as err:
                lookup['message'] = "Internal Error"
            except smtplib.SMTPConnectError as err:
                lookup['message'] = "Internal Error. Maybe blacklisted"

        return lookup 
Example #17
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 #18
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