Reverse DNS Lookups with dnspython
Sorry once again for the long lull in posting. School has not been kind towards my desire to blog. I will hopefully be posting more frequently in the coming weeks and months.
Anyways, now that the apologies are out of the way, here's a little something I was messing around with this morning during class. It's often useful to be able to do reverse DNS lookups of a given IP range to find hosts with interesting domain names, whether they're interesting because it looks like a network administrator has forgotten about them, or because they look like they weren't meant to be found (you'd be surprised how many machines rely on this sort of security through obscurity), or just because they have something like "mail" or "proxy" in their name. A simple way to do this is to write up a short bash script that uses the host or dig commands. However, this is slow (because you have to spawn a ton of processes), and I don't get to use Python.
Enter dnspython. dnspython is a great tool for working with DNS, so I'd suggest you look through their site if you're interested in messing around with DNS at all. Doing a reverse lookup of an IP address is quite easy:
from dns import resolver,reversename addr=reversename.from_address("192.168.0.1") str(resolver.query(addr,"PTR"))
This will probably throw a NXDOMAIN error, being a local address and all, but you get the idea. Taking this, it's obviously very easy to make a fast, effective script for scanning large ranges of IP addresses to find potentially interesting hosts.
I took a bit and wrote up a short python script using this technique (and a small reverse dns module that you can use in other programs) . I have attached it in case anyone would find that useful. Usage instructions are included: revdns.tar.gz . Be sure that you have dnspython installed, or else this will probably not work :P .
Hopefully I'll be back soon enough with some more interesting and in depth things I've been working on.
UPDATE 04/24/10: So yeah, I just realized that I mistakenly referenced "PyDNS" as the name of the module I used, when in fact it was the incredibly useful dnspython module. My bad. That's what I get for not checking my posts thoroughly. I updated all the references to it accordingly, but I figured for the sake of honesty I would clarify here as well. I also updated the source to deal with lookup timeouts a little better, if you care. Happy hacking!