Last active
May 17, 2017 09:23
-
-
Save averagesecurityguy/663e9cfc76401447f605c7e2c5575820 to your computer and use it in GitHub Desktop.
Resolve DNS Names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
#----------------------------------------------------------------------------- | |
# This is free and unencumbered software released into the public domain. | |
# | |
# Anyone is free to copy, modify, publish, use, compile, sell, or distribute | |
# this software, either in source code form or as a compiled binary, for any | |
# purpose, commercial or non-commercial, and by any means. | |
# | |
# In jurisdictions that recognize copyright laws, the author of this software | |
# dedicates any and all copyright interest in the software to the public | |
# domain. We make this dedication for the benefit of the public at large and | |
# to the detriment of our heirs and successors. We intend this dedication to | |
# be an overt act of relinquishment in perpetuity of all present and future | |
# rights to this software under copyright law. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
# | |
# For more information, please refer to <http://unlicense.org/> | |
#----------------------------------------------------------------------------- | |
#----------------------------------------------------------------------------- | |
# To use this software you will need to install dnspython3. | |
# pip3 install dnspython3 | |
#----------------------------------------------------------------------------- | |
import sys | |
import csv | |
import dns.resolver | |
import dns.exception | |
resolver = dns.resolver.Resolver() | |
resolver.nameservers = ['208.67.222.222', '208.67.220.220'] | |
def resolve(fqdn, rtype='A'): | |
""" | |
Resolve a DNS query. | |
Query the DNS server for a record of type rtype. The default is A records. | |
""" | |
# Resolve our query and return a list of answers. If there are any | |
# exceptions, print the exception and return an empty list. | |
ips = [] | |
try: | |
ans = resolver.query(fqdn, rtype) | |
ips = [a.to_text() for a in ans] | |
except dns.exception.DNSException: | |
pass | |
return ips | |
def write_records(records): | |
with open('lookup_results.csv', 'w') as csvfile: | |
writer = csv.writer(csvfile) | |
for record in records: | |
writer.writerow([record, '|'.join(records[record])]) | |
def get_domains(filename): | |
""" | |
Get the wordlist from the file. | |
""" | |
wordlist = [] | |
with open(filename) as f: | |
for line in f: | |
line = line.rstrip() | |
if (line != '') and (line[0] != '#'): | |
wordlist.append(line) | |
return wordlist | |
def usage(): | |
print('Usage: resolver.py domain_name_list') | |
sys.exit(1) | |
if __name__ == '__main__': | |
if len(sys.argv) != 2: | |
usage() | |
domains = get_domains(sys.argv[1]) | |
records = {} | |
for domain in domains: | |
ips = resolve(domain) | |
ips.extend(resolve(domain, 'AAAA')) | |
records[domain] = ips | |
write_records(records) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ./resolver.py names.txt | |
$ cat lookup_results.csv | |
www.live.com,207.46.11.252|65.55.129.171 | |
www.asgconsulting.com,185.53.179.7 | |
www.google.com,74.205.129.15|74.205.129.38|74.205.129.29|74.205.129.44|74.205.129.27|74.205.129.42|74.205.129.23|74.205.129.49|74.205.129.59|74.205.129.57|74.205.129.53|74.205.129.34|74.205.129.45|74.205.129.30|74.205.129.19|2607:f8b0:4002:c06::6a | |
www.outlook.com,132.245.75.194|132.245.78.146|132.245.9.226|132.245.3.210|132.245.23.146|132.245.29.242|132.245.71.18|132.245.60.2|132.245.44.226|40.96.8.2|2a01:111:f400:50aa::2|2a01:111:f400:5374::2|2a01:111:f400:516d::2|2a01:111:f400:52ef::2|2a01:111:f400:2ea1::2|2a01:111:f400:53eb::2|2a01:111:f400:f20d::2|2a01:111:f400:2a::2|2a01:111:f400:16::2|2a01:111:f400:4000::2 | |
www.yahoo.com,98.139.180.149|98.139.183.24|2001:4998:58:c02::a9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment