-
Notifications
You must be signed in to change notification settings - Fork 27
/
misp-check-domains-available.py
137 lines (118 loc) · 5.38 KB
/
misp-check-domains-available.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
import json
import requests
import re
import whois
from pyfaup.faup import Faup
import sys
import codecs
import datetime
import smtplib
key = ''
url = ''
eventurl = ''
timeframe='1d'
ignore_eventid = []
output_domain_file = '/tmp/possibledomains.txt'
def checkDomain(domain):
response = requests.get("https://dns.google/resolve?name={}".format(domain))
try:
response_json = json.loads(response.text)
if response_json['Status'] != 0:
return True
return False
except:
return False
def getmisp_eventdetails(key, url, eventid):
headers = {'Authorization': '{}'.format(key), 'Content-type': 'application/json', 'Accept': 'application/json'}
payload = '{ "returnFormat": "json", "eventid": "%s", "enforceWarninglist": true, "metadata": true }' % eventid
response = requests.post(url, headers=headers, data=payload, verify=False)
json_response = json.loads(response.text)
try:
eventdetails = json_response['response'][0]['Event']['info']
return eventdetails
except:
return False
def getmisp_domains(key, url, timeframe):
response_domains = []
headers = {'Authorization': '{}'.format(key), 'Content-type': 'application/json', 'Accept': 'application/json'}
payload = '{ "returnFormat": "json", "type": "domain", "last": "%s", "enforceWarninglist": true }' % timeframe
response = requests.post(url, headers=headers, data=payload, verify=False)
json_response = json.loads(response.text)
fp = Faup()
try:
for attr in json_response['response']['Attribute']:
url = attr['value']
eventid = attr['event_id']
if eventid not in ignore_eventid:
fp.decode(url)
domain = fp.get_domain()
category = attr['category']
comment = attr['comment']
eventinfo = getmisp_eventdetails(key, eventurl, eventid)
timestamp = datetime.datetime.utcfromtimestamp(int(attr['timestamp'])).strftime('%Y-%m-%d')
response_domains.append({'domain': domain, 'eventid': eventid, 'category': category, 'timestamp': timestamp, 'comment': comment, 'event': eventinfo})
return response_domains
except:
return response_domains
def getmisp_urls(key, url, timeframe):
response_domains = []
headers = {'Authorization': '{}'.format(key), 'Content-type': 'application/json', 'Accept': 'application/json'}
payload = '{ "returnFormat": "json", "type": "url", "last": "%s", "enforceWarninglist": true }' % timeframe
response = requests.post(url, headers=headers, data=payload, verify=False)
json_response = json.loads(response.text)
fp = Faup()
try:
for attr in json_response['response']['Attribute']:
url = attr['value']
eventid = attr['event_id']
if eventid not in ignore_eventid:
category = attr['category']
comment = attr['comment']
eventinfo = getmisp_eventdetails(key, eventurl, eventid)
timestamp = datetime.datetime.utcfromtimestamp(int(attr['timestamp'])).strftime('%Y-%m-%d')
fp.decode(url)
domain = fp.get_domain()
if not re.match(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", domain):
response_domains.append({'domain': domain, 'eventid': eventid, 'category': category, 'timestamp': timestamp, 'comment': comment, 'event': eventinfo})
return response_domains
except:
return response_domains
def inCheckRegister(domain, domainlist):
for el in domainlist:
if domain == el['domain']:
return True
return False
def checkRegister(domains):
check_to_register = []
if len(domains) > 0:
for domain in domains:
# First check the DNS
if not inCheckRegister(domain['domain'], check_to_register) and checkDomain(domain['domain']):
# Now check whois
try:
whois_result = whois.query(domain['domain'])
if whois_result.creation_date is None:
check_to_register.append({'domain': domain['domain'], 'eventid': domain['eventid'], 'reason': 'No DNS. No Whois', 'category': domain['category'], 'timestamp': domain['timestamp'], 'comment': domain['comment'], 'event': domain['event']})
except ValueError:
continue
except Exception as e:
reason = str(e).split('\n')[0]
check_to_register.append({'domain': domain['domain'], 'eventid': domain['eventid'], 'reason': reason, 'category': domain['category'], 'timestamp': domain['timestamp'], 'comment': domain['comment'], 'event': domain['event']})
continue
return check_to_register
# Read all domains
res_urls = []
res_domains = []
res_urls = checkRegister(getmisp_urls(key, url, timeframe))
res_domains = checkRegister(getmisp_domains(key, url, timeframe))
message = "Subject: MISP Domains available for registration\n\n\n"
if len(res_urls) > 0:
for domain in res_urls:
message = message + json.dumps(domain) + "\n"
if len(res_domains) > 0:
for domain in res_domains:
message = message + json.dumps(domain) + "\n"
smtp_server = '127.0.0.1'
sender_email = 'MAIL_SENDER_RCPT'
with smtplib.SMTP(smtp_server) as server:
server.sendmail(sender_email, sender_email, message)