secynic / ipwhois

Retrieve and parse whois data for IPv4 and IPv6 addresses
https://ipwhois.readthedocs.io/en/latest
BSD 2-Clause "Simplified" License
555 stars 121 forks source link

Incorrect results for DNS based asn lookups when a local searchdomain is in effect #324

Closed MrSeccubus closed 3 weeks ago

MrSeccubus commented 9 months ago

In net.py the cymru domain is set as follows

IPV4_DNS_ZONE = '{0}.origin.asn.cymru.com'

IPV6_DNS_ZONE = '{0}.origin6.asn.cymru.com'

It should be set to this:

IPV4_DNS_ZONE = '{0}.origin.asn.cymru.com.'

IPV6_DNS_ZONE = '{0}.origin6.asn.cymru.com.'

On my system there is a local search domain set which is appended after cymru.net and leads to the following error.

Traceback (most recent call last):
  File "/Users/fbreedijk/repos/scripts/venv/lib/python3.11/site-packages/ipwhois/asn.py", line 146, in parse_fields_dns
    ret = {'asn_registry': temp[3].strip(' \n')}
                           ~~~~^^^
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/fbreedijk/repos/scripts/abuse2sqlite/./debug_whois.py", line 51, in <module>
    print(abuse_from_whois("160.50.220.0"))
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fbreedijk/repos/scripts/abuse2sqlite/./debug_whois.py", line 8, in abuse_from_whois
    rdap = obj.lookup_rdap(depth=2)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fbreedijk/repos/scripts/venv/lib/python3.11/site-packages/ipwhois/ipwhois.py", line 295, in lookup_rdap
    asn_data = self.ipasn.lookup(
               ^^^^^^^^^^^^^^^^^^
  File "/Users/fbreedijk/repos/scripts/venv/lib/python3.11/site-packages/ipwhois/asn.py", line 464, in lookup
    asn_data_list.append(self.parse_fields_dns(
                         ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/fbreedijk/repos/scripts/venv/lib/python3.11/site-packages/ipwhois/asn.py", line 167, in parse_fields_dns
    raise ASNParseError('Parsing failed for "{0}" with exception: {1}.'
ipwhois.exceptions.ASNParseError: Parsing failed for ""v=spf1 -all"" with exception: list index out of range.

I will make a PR for this change.

MrSeccubus commented 9 months ago

The same mistake is present in line 260:

        zone = '{0}.asn.cymru.com'.format(asn)

Should be

        zone = '{0}.asn.cymru.com.'.format(asn)
MrSeccubus commented 9 months ago

Line 74 as well.

secynic commented 3 weeks ago

Thanks. Tested and in dev.