-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
223 lines (177 loc) · 7.39 KB
/
main.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import json
from apscheduler.schedulers.blocking import BlockingScheduler
import configparser
from datetime import datetime
import ast
import fileinput
import hashlib
import logging
import os
import pathlib
import yaml
from whoisapi import *
import smtp as mail
# Global paths
working_dir = pathlib.Path(__file__).parent.resolve()
log_file = working_dir.joinpath('watcher.log')
domains_yaml = working_dir.joinpath('domains.yaml')
parsed_dict = working_dir.joinpath('parsed.dict')
conf_file = working_dir.joinpath('conf.yaml')
logging.basicConfig(filename=log_file, level=logging.DEBUG, format='%(asctime)s %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p')
# Configuration files
def load_conf(file_path):
with open(file_path, "r") as f:
return yaml.safe_load(f)
# Load config
conf = load_conf(conf_file)
RECEIPT = conf.get('EMAIL').get('RECEIPT')
SENDER_PASSWD = conf.get('EMAIL').get('PASSWD')
API_KEY = conf.get('API').get('API_KEY')
''' Change the API key to yours,
This can be read from a script.conf property file'''
client = Client(api_key=str(API_KEY))
'''
@return a list of the domains based on a domains.yaml file
'''
def read_domain_list_from_yaml():
yaml_file = open(str(domains_yaml))
domains = yaml.load(yaml_file, Loader=yaml.FullLoader)
return domains.get("domains")
def build_dictionary(response):
name = str(response['domain_name'])
if name == "buildarat.com" or name == "pionsecar.com" or name == "cryptotradexinvest.com":
administrative_root = str(response['administrative_contact'])
registrant_root = str(response['registrant'])
technical_root = str(response['technical_contact'])
contact_email = str(response['contact_email'])
administrative_dict = ast.literal_eval(administrative_root.replace('\n', ''))
registrant_dict = ast.literal_eval(registrant_root.replace('\n', ''))
technical_dict = ast.literal_eval(technical_root.replace('\n', ''))
dictionary = {
"domain_record": {
"whoisCreatedDate": str(response['created_date']),
"whoisUpdatedDate": str(response['updated_date']),
"whoisExpiresDate": str(response['expires_date']),
"registrantName": str(response['registrar_name']),
"domain_name": str(response['domain_name']),
"emails:": {
"registrant": administrative_dict['email'],
"administrativeContact": registrant_dict['email'],
"technicalContact": technical_dict['email'],
"contactEmail": contact_email
}
}
}
return dictionary
else:
administrative_root = str(response['administrative_contact'])
registrant_root = str(response['registrant'])
technical_root = str(response['technical_contact'])
contact_email = str(response['contact_email'])
dictionary = {
"domain_record": {
"whoisCreatedDate": str(response['created_date']),
"whoisUpdatedDate": str(response['updated_date']),
"whoisExpiresDate": str(response['expires_date']),
"registrantName": str(response['registrar_name']),
"domain_name": str(response['domain_name']),
"emails:": {
"registrant": registrant_root,
"administrativeContact": administrative_root,
"technicalContact": technical_root,
"contactEmail": contact_email
}
}
}
return dictionary
def normalizer(response):
dictionary = build_dictionary(response)
domain_name = dictionary.get('domain_record').get('domain_name')
if check_cache():
append_dict_to_file(dictionary)
logging.info('[INFO] Saving first results to a file.')
else:
logging.info('[INFO] Checking cache...')
result = compare_results(dictionary, domain_name)
if result == 1:
logging.info('[INFO] Saving first result for this domain into disk')
append_dict_to_file(dictionary)
def check_cache():
cache = os.path.getsize(str(parsed_dict))
if cache <= 0:
return True
else:
return False
def compare_results(dictionary, domain_name):
archived_cached_hash = calculate_dict_hash_from_cache(domain_name)
# if not found return 1
if archived_cached_hash == 1:
return 1
current_hash = calculate_hash(dictionary)
logging.info("[INFO] validating archived hash " + archived_cached_hash)
logging.info("[INFO] validating current hash " + current_hash)
if archived_cached_hash == current_hash:
logging.info("[INFO] Values are the identical from cache... this is good.")
message = mail.build_body_info_email(domain_name)
mail.send_email(message, 0, str(RECEIPT), str(SENDER_PASSWD))
else:
logging.warning("[WARN] Data has been tampered!")
archived_file = retrieve_archived_whois(domain_name)
logging.warning("[WARN] Updating registers...")
update_dict_file(dictionary, domain_name)
message = mail.build_body_alert_email(archived_cached_hash, current_hash, json.dumps(dictionary, indent=4), json.dumps(archived_file, indent=4),
domain_name)
logging.warning("[WARN] Sending alert via email...")
mail.send_email(message, 1, str(RECEIPT), str(SENDER_PASSWD))
def append_dict_to_file(dictionary):
with open(str(parsed_dict), "a") as fp:
fp.write(str(dictionary) + '\n')
fp.close()
def update_dict_file(new_dictionary, domain_name):
dict_file = fileinput.input(files=str(parsed_dict), inplace=1)
for line in dict_file:
if domain_name in line:
line = new_dictionary
print(line)
dict_file.close()
def calculate_dict_hash_from_cache(domain_name):
dict_file = open(str(parsed_dict))
for x in dict_file:
if not x.strip() == '':
file = ast.literal_eval(x.replace('\n', ''))
root_element = file['domain_record']
current_domain_name = root_element['domain_name']
if current_domain_name == domain_name:
dict_file.close()
return calculate_hash(file)
# if not found return 1
dict_file.close()
return 1
def retrieve_archived_whois(domain_name):
dict_file = open(str(parsed_dict))
for x in dict_file:
if not x.strip() == '':
file = ast.literal_eval(x.replace('\n', ''))
root_element = file['domain_record']
current_domain_name = root_element['domain_name']
if current_domain_name == domain_name:
return file
def calculate_hash(file):
hash_object = hashlib.sha256(str(file).encode())
return hash_object.hexdigest()
def do_rpc():
domains = read_domain_list_from_yaml()
try:
for domain in domains:
# Work smart .data method already validates empty values for the whois record
response = client.data(str(domain))
normalizer(response)
except ValueError:
logging.error("[ERROR] Response was empty for " + domains)
if __name__ == '__main__':
logging.debug("[DEBUG] doing first do_rpc")
do_rpc() # first execution requires call
scheduler = BlockingScheduler()
scheduler.add_job(do_rpc, 'interval', hours=24)
scheduler.start()