Skip to content

Instantly share code, notes, and snippets.

@ezr
Created November 29, 2019 21:52
Show Gist options
  • Save ezr/d79dbe4714f39c454844463840e1929c to your computer and use it in GitHub Desktop.
Save ezr/d79dbe4714f39c454844463840e1929c to your computer and use it in GitHub Desktop.
basic script to look up DNS entries using DNS over HTTPS. Similar dig/nslookup.
#!/usr/bin/env python3
import argparse
import base64
import dnslib # https://github.com/paulc/dnslib
import requests
parser = argparse.ArgumentParser(description='script to look up DNS records using DNS over HTTPs')
parser.add_argument('-s', '--server', help='the server to query', required=False)
parser.add_argument('-q', '--question', help='usually a hostname', required=True)
parser.add_argument('-t', '--qtype', help='RR type (e.g. "A" or "NS")', required=False)
parser.set_defaults(server="cloudflare-dns.com", qtype="A")
args = vars(parser.parse_args())
d = dnslib.DNSRecord.question(args['question'], qtype=args['qtype'])
questionB64 = base64.b64encode(d.pack()).decode("utf-8").rstrip("=")
url = "https://%s/dns-query" % args['server']
params = {'dns': questionB64}
headers = {'accept': 'application/dns-message'}
res = requests.get(url, params=params, headers=headers)
if res.status_code != 200:
print("[*] error - recieved status code %s" % res.status_code)
print(res.text)
exit(2)
res.close()
answer = dnslib.DNSRecord.parse(res.content)
print(answer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment