/DNSDumpsterAPI.py

https://github.com/m0rtem/CloudFail · Python · 84 lines · 74 code · 5 blank · 5 comment · 0 complexity · e96615b8aa5f232c153fbaad4ffb5161 MD5 · raw file

  1. """
  2. This is the (unofficial) Python API for dnsdumpster.com Website.
  3. Using this code, you can retrieve subdomains
  4. Author: https://github.com/PaulSec/
  5. """
  6. from __future__ import print_function
  7. import re
  8. import sys
  9. import requests
  10. from bs4 import BeautifulSoup
  11. class DNSDumpsterAPI(object):
  12. """DNSDumpsterAPI Main Handler"""
  13. def __init__(self, verbose=False):
  14. self.verbose = verbose
  15. def display_message(self, string):
  16. if self.verbose:
  17. print('[verbose] %s' % string)
  18. def retrieve_results(self, table):
  19. res = []
  20. trs = table.findAll('tr')
  21. for tr in trs:
  22. tds = tr.findAll('td')
  23. pattern_ip = r'([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})'
  24. ip = re.findall(pattern_ip, tds[1].text)[0]
  25. domain = tds[0].text.replace('\n', '')
  26. additional_info = tds[2].text
  27. country = tds[2].find('span', attrs={}).text
  28. autonomous_system = additional_info.split(' ')[0]
  29. provider = ' '.join(additional_info.split(' ')[1:])
  30. provider = provider.replace(country, '')
  31. data = {'domain': domain, 'ip': ip, 'as': autonomous_system, 'provider': provider, 'country': country}
  32. res.append(data)
  33. return res
  34. def retrieve_txt_record(self, table):
  35. res = []
  36. for td in table.findAll('td'):
  37. res.append(td.text)
  38. return res
  39. def search(self, domain):
  40. dnsdumpster_url = 'https://dnsdumpster.com/'
  41. s = requests.session()
  42. req = s.get(dnsdumpster_url)
  43. soup = BeautifulSoup(req.content, 'html.parser')
  44. csrf_middleware = soup.findAll('input', attrs={'name': 'csrfmiddlewaretoken'})[0]['value']
  45. self.display_message('Retrieved token: %s' % csrf_middleware)
  46. cookies = {'csrftoken': csrf_middleware}
  47. headers = {'Referer': dnsdumpster_url}
  48. data = {'csrfmiddlewaretoken': csrf_middleware, 'targetip': domain}
  49. req = s.post(dnsdumpster_url, cookies=cookies, data=data, headers=headers)
  50. if req.status_code != 200:
  51. print(
  52. u"Unexpected status code from {url}: {code}".format(
  53. url=dnsdumpster_url, code=req.status_code),
  54. file=sys.stderr,
  55. )
  56. return []
  57. if 'error getting results' in req.content.decode('utf-8'):
  58. print("There was an error getting results", file=sys.stderr)
  59. return []
  60. soup = BeautifulSoup(req.content, 'html.parser')
  61. tables = soup.findAll('table')
  62. res = {'domain': domain, 'dns_records': {}}
  63. res['dns_records']['dns'] = self.retrieve_results(tables[0])
  64. res['dns_records']['mx'] = self.retrieve_results(tables[1])
  65. res['dns_records']['txt'] = self.retrieve_txt_record(tables[2])
  66. res['dns_records']['host'] = self.retrieve_results(tables[3])
  67. return res