paulc / dnslib

A Python library to encode/decode DNS wire-format packets
https://github.com/paulc/dnslib
BSD 2-Clause "Simplified" License
300 stars 85 forks source link

reverse lookup #68

Closed anpic closed 2 months ago

anpic commented 3 months ago

thanks for the cool project :) how do I make a reverse dns lookup?

domain = "github.com" q = DNSRecord.question(domain, qtype = "A") p = q.send("8.8.8.8", 53) d = DNSRecord.parse(p) print(len(d.rr)) # 1 ip = str(d.rr[0].rdata) q = DNSRecord.question(ip, qtype = "PTR") p = q.send("8.8.8.8", 53) d = DNSRecord.parse(p) print(len(d.rr)) # 0

anpic commented 3 months ago

So I understand that need to create a reverse address on my own. Can you add an option so that automatically convert IPv4 and IPv6 addresses to the desired form?

anpic commented 3 months ago

and why?

domain = "microsoft.com" q = DNSRecord.question(domain, qtype = "AAAA") p = q.send("8.8.8.8", 53) d = DNSRecord.parse(p) print(d.rr) # [<DNS RR: 'microsoft.com.' rtype=AAAA rclass=IN ttl=3343 rdata='2603:1030:c02:8::14'>, <DNS RR: 'microsoft.com.' rtype=AAAA rclass=IN ttl=3343 rdata='2603:1020:201:10::10f'>, <DNS RR: 'microsoft.com.' rtype=AAAA rclass=IN ttl=3343 rdata='2603:1010:3:3::5b'>, <DNS RR: 'microsoft.com.' rtype=AAAA rclass=IN ttl=3343 rdata='2603:1030:b:3::152'>, <DNS RR: 'microsoft.com.' rtype=AAAA rclass=IN ttl=3343 rdata='2603:1030:20e:3::23c'>]

domain = "4.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.0.0.0.2.0.c.0.0.3.0.1.3.0.6.2.ip6.arpa" # reverse form for '2603:1030:c02:8::14' q = DNSRecord.question(domain, qtype = "PTR") p = q.send("8.8.8.8", 53) d = DNSRecord.parse(p) print(d.rr) # []

paulc commented 2 months ago

The ipaddress module in the standard library provides this functionality:

>>> import ipaddress
>>> ipaddress.ip_address('2603:1020:201:10::10f').reverse_pointer
'f.0.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.1.0.0.1.0.2.0.0.2.0.1.3.0.6.2.ip6.arpa'
anpic commented 2 months ago

The ipaddress module in the standard library provides this functionality:

Moreover you can easily implement automatic conversion ;) But why is an empty list given in the second example?