forked from TheHive-Project/Cortex-Analyzers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdnsdumpster.py
executable file
·137 lines (112 loc) · 4.98 KB
/
dnsdumpster.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
# encoding: utf-8
from __future__ import print_function
import requests
import re
import iocextract
from bs4 import BeautifulSoup
from cortexutils.analyzer import Analyzer
class DNSdumpsterAnalyzer(Analyzer):
def __init__(self):
Analyzer.__init__(self)
self.session = requests.Session()
self.baseurl = 'https://dnsdumpster.com'
def run(self):
Analyzer.run(self)
if self.data_type == 'domain':
try:
domain = self.get_param('data', None, 'Observable is missing')
result = self.dnsdumpster_query(domain)
self.report({'result': result})
except Exception as e:
self.error("Error: {}".format(e))
else:
self.error('Invalid data type')
def dnsdumpster_query(self, domain):
try:
r = self.session.get(self.baseurl)
except requests.ConnectionError as connerr:
self.error("Connection error. Error {}".format(connerr))
soup = BeautifulSoup(r.content, 'html.parser')
csrf_middleware = soup.findAll('input', attrs={'name': 'csrfmiddlewaretoken'})[0]['value']
cookies = {'csrftoken': csrf_middleware}
headers = {'Referer': self.baseurl,
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/95.0.4638.69 Safari/537.36'}
data = {'csrfmiddlewaretoken': csrf_middleware, 'targetip': domain, 'user': 'free'}
r = self.session.post(self.baseurl, cookies=cookies, data=data, headers=headers)
if r.status_code != 200:
self.error("Unexpected status code from. Status code: {}".format(r.status_code))
return []
if 'There was an error getting results' in r.content.decode('utf-8'):
self.error("There was an error getting results")
return []
soup = BeautifulSoup(r.content, 'html.parser')
tables = soup.findAll('table')
res = {'domain': domain, '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])
res['dns_records']['map_url'] = '{}/static/map/{}.png'.format(self.baseurl, domain)
return res
def retrieve_txt_record(self, table):
res = []
for td in table.findAll('td'):
res.append(td.text)
return res
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})'
try:
ip = re.findall(pattern_ip, tds[1].text)[0]
domain = str(tds[0]).split('<br/>')[0].split('>')[1].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)
except Exception as err:
self.error("Unexpected error when parsing data from DNSdumpster.com. Error {}".format(err))
return res
def artifacts(self, raw):
artifacts = []
ipv4s = list(iocextract.extract_ipv4s(str(raw)))
ipv6s = list(iocextract.extract_ipv6s(str(raw)))
domains = list(iocextract.extract_urls(str(raw)))
if ipv4s:
ipv4s = list(dict.fromkeys(ipv4s))
for i in ipv4s:
artifacts.append(self.build_artifact('ip', str(i)))
if ipv6s:
ipv6s = list(dict.fromkeys(ipv6s))
for j in ipv6s:
artifacts.append(self.build_artifact('ip', str(j)))
if domains:
domains = list(dict.fromkeys(domains))
for k in domains:
artifacts.append(self.build_artifact('url', str(k)))
return artifacts
def summary(self, raw):
taxonomies = []
level = "info"
namespace = "DNSdumpster"
predicate = "Report"
value = "{}".format("OK")
taxonomies.append(self.build_taxonomy(level, namespace, predicate, value))
return {"taxonomies": taxonomies}
if __name__ == '__main__':
DNSdumpsterAnalyzer().run()