Created
January 4, 2017 00:12
-
-
Save CreateRemoteThread/a96e5cd970a6ebb211e57710c53e731e to your computer and use it in GitHub Desktop.
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/python | |
| # dnsdump.py | |
| # ---------------------------------- | |
| # derived from github/PaulSec/API-dnsdumpster.com | |
| import requests | |
| import re | |
| import sys | |
| from bs4 import BeautifulSoup | |
| class DNSDumpsterAPI(object): | |
| """DNSDumpsterAPI Main Handler""" | |
| def __init__(self, verbose=False): | |
| self.verbose = verbose | |
| def display_message(self, s): | |
| if self.verbose: | |
| print('[verbose] %s' % s) | |
| def retrieve_results(self, table): | |
| res = [] | |
| trs = table.findAll('tr') | |
| for tr in trs: | |
| tds = tr.findAll('td') | |
| pattern_ip = r'([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})' | |
| ip = re.findall(pattern_ip, tds[1].text)[0] | |
| domain = tds[0].text.replace('\n', '').split(' ')[0] | |
| header = ' '.join(tds[0].text.replace('\n', '').split(' ')[1:]) | |
| reverse_dns = tds[1].find('span', attrs={}).text | |
| additional_info = tds[2].text | |
| country = tds[2].find('span', attrs={}).text | |
| autonomous_system = additional_info.split(' ')[0] | |
| provider = ' '.join(additional_info.split(' ')[1:]) | |
| provider = provider.replace(country, '') | |
| data = {'domain': domain, | |
| 'ip': ip, | |
| 'reverse_dns': reverse_dns, | |
| 'as': autonomous_system, | |
| 'provider': provider, | |
| 'country': country, | |
| 'header': header} | |
| res.append(data) | |
| return res | |
| def retrieve_txt_record(self, table): | |
| res = [] | |
| for td in table.findAll('td'): | |
| res.append(td.text) | |
| return res | |
| def search(self, domain): | |
| dnsdumpster_url = 'https://dnsdumpster.com/' | |
| s = requests.session() | |
| req = s.get(dnsdumpster_url) | |
| soup = BeautifulSoup(req.content, 'html.parser') | |
| csrf_middleware = soup.findAll('input', attrs={'name': 'csrfmiddlewaretoken'})[0]['value'] | |
| self.display_message('Retrieved token: %s' % csrf_middleware) | |
| cookies = {'csrftoken': csrf_middleware} | |
| headers = {'Referer': dnsdumpster_url} | |
| data = {'csrfmiddlewaretoken': csrf_middleware, 'targetip': domain} | |
| req = s.post(dnsdumpster_url, cookies=cookies, data=data, headers=headers) | |
| if req.status_code != 200: | |
| print( | |
| u"Unexpected status code from {url}: {code}".format( | |
| url=dnsdumpster_url, code=req.status_code) | |
| ) | |
| raise | |
| if 'error' in req.content.decode('utf-8'): | |
| print("There was an error getting results") | |
| raise | |
| soup = BeautifulSoup(req.content, 'html.parser') | |
| tables = soup.findAll('table') | |
| # removed xlsx and image code | |
| res = {} | |
| res['domain'] = domain | |
| res['dns_records'] = {} | |
| res['dns_records']['dns'] = self.retrieve_results(tables[0]) | |
| res['dns_records']['mx'] = self.retrieve_results(tables[1]) | |
| res['dns_records']['txt'] = self.retrieve_txt_record(tables[2]) | |
| res['dns_records']['host'] = self.retrieve_results(tables[3]) | |
| return res | |
| import sys | |
| import getopt | |
| if __name__ == "__main__": | |
| if len(sys.argv) == 1: | |
| print " [!] usage: ./dnsdump.py [domain]" | |
| sys.exit(0) | |
| dnsBase = [] | |
| output = "csv" | |
| optlist, args = getopt.getopt(sys.argv[1:],'ctd:',['dns=','csv','tsv']) | |
| for (opt,value) in optlist: | |
| if opt in ("--dns","-d"): | |
| dnsBase.append( [value] ) | |
| elif opt in ("--csv","-c"): | |
| output = "csv" | |
| elif opt in ("--tsv","-t"): | |
| output = "tsv" | |
| if len(dnsBase) == 0 and len(args) != 0: | |
| dnsBase = args | |
| for dnsEntry in dnsBase: | |
| res = DNSDumpsterAPI().search(dnsEntry) | |
| for entry in res['dns_records']['host']: | |
| if output == "csv": | |
| print "%s,%s" % (entry['ip'],entry['domain']) | |
| elif output == "tsv": | |
| print "%s\t%s" % (entry['ip'],entry['domain']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment