Exemplo n.º 1
0
    def query(self, qname, rdtype=A, rdclass=IN, **kwargs):
        assert self.cache

        if isinstance(qname, StringTypes):
            qname = from_text(qname)

        answer = self.cache.get((qname, rdtype, rdclass))
        if answer is not None:
            if isinstance(answer, NXAnswer):
                raise NXDOMAIN(qnames=[qname],
                               responses={qname: answer.response})
            elif isinstance(answer, DNSException):
                raise answer

        if DNSPYTHON_2:  # pragma: no cover
            return super(ExceptionCachingResolver, self).query(qname,
                                                               rdtype=rdtype,
                                                               rdclass=rdclass,
                                                               **kwargs)

        try:
            return super(ExceptionCachingResolver, self).query(qname,
                                                               rdtype=rdtype,
                                                               rdclass=rdclass,
                                                               **kwargs)
        except NoMetaqueries:
            raise
        except DNSException as e:
            self._cache_exception(e, qname, rdtype, rdclass)
            raise
Exemplo n.º 2
0
 def test_get_nxdomain_exception(self):
     with mock.patch('dns.resolver.query') as query:
         query.side_effect = NXDOMAIN()
         with app.flask_app.test_client() as client:
             response = client.get('/mxrecords/example.com')
             json_data = response.get_json()
             self.assertEqual(json_data['exception'],
                              'NXDOMAIN Exception -- mx record not found')
             self.assertEqual(response.status_code, 500)
             self.assertRaises(exceptions.InternalServerError)
Exemplo n.º 3
0
def test_gsuite_error_handling(mock_dns_resolver, mock_log,
                               mock_azure_network):
    mock_dns_response = Mock(spec=dns.resolver.Answer)

    mock_dns_response = ["ip4:35.190.247.0/24"]

    mock_dns_resolver.return_value = mock_dns_response
    mock_dns_resolver.side_effect = NXDOMAIN()

    result = mock_azure_network.get_gsuite_smtp_ipv4_cidrs()

    assert result == set()
Exemplo n.º 4
0
    def test_record_NXDOMAIN(self):
        """
        Tests the method which let us get the PTR record for the case that
        we get a NXDOMAIN exception.
        """

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

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

        self.assertEqual(expected, actual)
Exemplo n.º 5
0
 def ip(self):
     # Determine the domain's IP address
     try:
         req = query(self.dom)
     except NXDOMAIN:
         raise NXDOMAIN("Check if %r exists." % self.dom)
     except NoNameservers:
         raise NoNameservers('No Nameservers found for domain: %r' %
                             self.dom)
     except NoAnswer:
         raise NoAnswer('No IP available for domain: %s' % self.dom)
     self.addr = [str(ip.address) for ip in req]
Exemplo n.º 6
0
def lookup(domain):
    'Lookup if domain is using CDN. This is "bad" CDN lookup coz it use known CDN list to find if the domain use CDN.'
    print('Looking up via DNS..')
    CDNs = []
    print('Checking Multiple IP...')
    try:
        asw = query(domain)
    except NXDOMAIN:
        raise NXDOMAIN("Are you sure %r exist?" % domain)
    except NoNameservers:
        raise NoNameservers('No nameserver found for %r' % domain)
    except NoAnswer:
        raise NoAnswer('No IP for %s!' % domain)
    print('Found IP: %s' % [str(a.address) for a in asw])

    #print('Probably %s is using CDN (maybe :P)!'%domain)
    # check whether domain is using CNAME (but doesn't mean it's use CDN..)
    print('Checking CNAME...')
    try:
        asw = query(domain, 'cname')
        domains = [a.to_text() for a in asw]
        print('Found that %s have another domains: %s' %
              (domain, ','.join(domains)))
        for d in domains:
            cdn_found = find_cdn(d)
            if cdn_found:
                print('Got CDN from CNAME!')
                if cdn_found not in CDNs:
                    CDNs.append(cdn_found)

    except NoAnswer as err:
        print('No CNAME found!')

    print('Checking NS...')
    try:
        asw = query(domain, 'ns')
        print('Found Nameservers: %s' % ','.join([a.to_text() for a in asw]))
    except NoAnswer:
        print('No Nameserver? Perhaps you can try SOA query instead')
    else:
        print('Checking CDN from NS..')
        for a in asw:
            ns = a.to_text()
            cdn_found = find_cdn(ns)
            if cdn_found:
                print('Got CDN from NS!')
                if cdn_found not in CDNs:
                    CDNs.append(cdn_found)
    if CDNs:
        print('CDN used by %s : %s' % (domain, ','.join(CDNs)))
    else:
        print('No CDN used by %s' % domain)
Exemplo n.º 7
0
def A(domain):
    'this func sometime helpful :)'
    print('Checking Multiple IP...')
    try:
        asw = query(domain)
    except NXDOMAIN:
        raise NXDOMAIN("Are you sure %r exist?" % domain)
    except NoNameservers:
        raise NoNameservers('No nameserver found for %r' % domain)
    except NoAnswer:
        raise NoAnswer('No IP for %s!' % domain)
    print('Found IP: %s' % [str(a.address) for a in asw])
    print('Checking CNAME...')
    try:
        asw = query(domain, 'cname')
        domains = [a.to_text() for a in asw]
        print('Found that %s have another domains: %s' %
              (domain, ','.join(domains)))
    except Exception as e:
        print(e)
    print()
Exemplo n.º 8
0
    def resolve(self,
                qname,
                rdtype=A,
                rdclass=IN,
                tcp=False,
                source=None,
                raise_on_no_answer=True,
                source_port=0,
                lifetime=None,
                search=None):
        assert self.cache

        if isinstance(qname, StringTypes):
            qname = from_text(qname)

        answer = self.cache.get((qname, rdtype, rdclass))
        if answer is not None:
            if isinstance(answer, NXAnswer):
                raise NXDOMAIN(qnames=[qname],
                               responses={qname: answer.response})
            elif isinstance(answer, DNSException):
                raise answer

        try:
            return super(ExceptionCachingResolver, self).resolve(
                qname,
                rdtype,
                rdclass,
                tcp,
                source,
                raise_on_no_answer,
                source_port,
                lifetime,
            )
        except NoMetaqueries:
            raise
        except DNSException as e:
            self._cache_exception(e, qname, rdtype, rdclass)
            raise
Exemplo n.º 9
0
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
Exemplo n.º 10
0
                    Name("bad-response.example.com".split(".")))
_DNE_MX_RECORD = MX("IN", "MX", 10,
                    Name("does-not-exist.example.com".split(".")))
_MX_RECORDS = [_MX_RECORD_1]
_IP_SET_RECORDS = [RRset()]

_IPV4_RECORD_1 = (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("192.0.2.1", 25))
_IPV4_RECORD_2 = (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("192.0.2.2", 25))
_IPV6_RECORD = (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("2001:db8::1", 25))
_BAD_IP_RECORD = (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("BAD", 25))
_IP_RECORDS = [_IPV4_RECORD_1, _IPV6_RECORD]

_POSSIBLE_DNS_RESULTS = {
    "test3.com": [_MX_RECORD_2],
    "no-mx.example.com": NoAnswer(),
    "does-not-exist.example.com": NXDOMAIN(),
    "timeout.example.com": Timeout(),
    "no-ns-servers.example.com": NoNameservers(),
    "bad-response.example.com": [_BAD_MX_RECORD],
    "no-lookup.example.com": [_DNE_MX_RECORD],
    "no-answer.example.com": [_DNE_MX_RECORD],
    "dns-checks.com": [_MX_RECORD_2],
    "invalid-mx.com": [_MX_RECORD_3]
}

_POSSIBLE_DNS_RESULTS_NO_MX = {
    "does-not-exist.example.com": NXDOMAIN(),
    "mx2.example.com": NXDOMAIN(),
    "no-lookup.example.com": NXDOMAIN(),
    "no-answer.example.com": NoAnswer(),
    "bad-response.example.com": [RRsetInvalid()],
 def test_register_fails_on_weird_named_ports(self, *args):
     with patch.object(dns.resolver, 'query', side_effect=NXDOMAIN()):
         self.assertRaises(ValueError, self.r.register, self.service, self.nport, 'non-existing-service-port-name')
 def test_register_fails_if_not_protocol_match(self, *args):
     with patch.object(dns.resolver, 'query', side_effect=NXDOMAIN()):
         self.assertRaises(ValueError, self.r.register, self.service, self.nport, self.sport, protocol="udp")
Exemplo n.º 13
0
_BAD_MX_RECORD = MX("IN", "MX", 10,
                    Name("bad-response.example.com".split(".")))
_DNE_MX_RECORD = MX("IN", "MX", 10,
                    Name("does-not-exist.example.com".split(".")))
_MX_RECORDS = [_MX_RECORD_1]

_IPV4_RECORD_1 = (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("192.0.2.1", 25))
_IPV4_RECORD_2 = (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("192.0.2.2", 25))
_IPV6_RECORD = (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("2001:db8::1", 25))
_BAD_IP_RECORD = (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("BAD", 25))
_IP_RECORDS = [_IPV4_RECORD_1, _IPV6_RECORD]

_POSSIBLE_DNS_RESULTS = {
    "test3.com": [_MX_RECORD_2],
    "no-mx.example.com": NoAnswer(),
    "does-not-exist.example.com": NXDOMAIN(),
    "timeout.example.com": Timeout(),
    "no-ns-servers.example.com": NoNameservers(),
    "bad-response.example.com": [_BAD_MX_RECORD],
    "no-lookup.example.com": [_DNE_MX_RECORD],
}

_POSSIBLE_IP_RESULTS = {
    "test3.com": [_IPV4_RECORD_2],
    "mx2.example.com": [_IPV4_RECORD_2],
    "bad-response.example.com": [_BAD_IP_RECORD],
    "does-not-exist.example.com": socket.gaierror(),
    "192.0.2.254": socket.gaierror(),
    "2001:db8:254": socket.gaierror(),
}