-
Notifications
You must be signed in to change notification settings - Fork 0
/
mih.py
252 lines (233 loc) · 13.3 KB
/
mih.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import requests, argparse
from requests.auth import HTTPBasicAuth
import information
censys_url="https://search.censys.io/api/v2/hosts/search?"
def print_results_censys(search_name,search_query,hits_list):
print("Censys Search Name: " + search_name)
print("Censys Search Query: " + search_query)
print("-----------------------")
for hit in hits_list:
print(hit)
def write_results_censys_to_file(search_name, search_query, hits_list, output_file):
with open(output_file, 'a') as file:
file.write("Censys Search Name: " + search_name + "\n")
file.write("Censys Search Query: " + search_query + "\n")
file.write("-----------------------\n")
for hit in hits_list:
file.write(str(hit) + "\n")
def search_censys(store_arg,reputation_arg,files_arg):
for search in information.censys_search['threats']:
if store_arg and (reputation_arg or files_arg):
with open(store_arg, 'a') as file:
file.write("Censys Search Name: " + search['name'] + "\n")
file.write("Censys Search Query: " + search['search'] + "\n")
file.write("-----------------------\n")
elif not store_arg and (reputation_arg or files_arg):
print("Censys Search Name: " + search['name'])
print("Censys Search Query: " + search['search'])
print("-----------------------")
hits=[]
search_q=search['search']
censys_search_results=requests.get(url=censys_url+"q="+search_q,auth=HTTPBasicAuth(information.censys_id, information.censys_secret)).json()
try:
if not censys_search_results['result']['hits']==[]:
for hit in censys_search_results['result']['hits']:
hits.append(hit['ip'])
if reputation_arg:
reputation_information(hit['ip'],store_arg)
if files_arg:
communicating_files_information(hit['ip'], store_arg)
if store_arg and not files_arg and not reputation_arg:
write_results_censys_to_file(search['name'], search['search'],hits,store_arg)
elif not store_arg and not files_arg and not reputation_arg:
print_results_censys(search['name'], search['search'],hits)
else:
print("No results identified for this search. Exiting")
exit()
except Exception:
print("Censys Error: "+str(censys_search_results))
exit()
def write_reputation_information_to_file(line,vt_response, abuse_response, output_file):
suspicious = vt_response['data']['attributes']['last_analysis_stats']['suspicious']
malicious = vt_response['data']['attributes']['last_analysis_stats']['malicious']
harmless = vt_response['data']['attributes']['last_analysis_stats']['harmless']
undetected = vt_response['data']['attributes']['last_analysis_stats']['undetected']
all = suspicious + malicious + harmless + undetected
malicious_percentage = (malicious + suspicious) / all * 100
malicious_percentage = "{:.2f}".format(malicious_percentage)
domain = abuse_response['data']['domain']
countryCode = abuse_response['data']['countryCode']
usageType = abuse_response['data']['usageType']
isWhitelisted = abuse_response['data']['isWhitelisted']
isTor = abuse_response['data']['isTor']
totalReports = abuse_response['data']['totalReports']
lastReportedAt = abuse_response['data']['lastReportedAt']
abuseConfidenceScore = abuse_response['data']['abuseConfidenceScore']
with open(output_file, 'a') as file:
file.write("Reputation info for: " + str(line) + "\n")
file.write("VirusTotal info: -----" + " (" + str(malicious_percentage) + "% Malicious)\n")
file.write("Tags: " + str(vt_response['data']['attributes']['tags']) + "\n")
file.write("Suspicious Detections: " + str(suspicious) + "\n")
file.write("Malicious Detections: " + str(malicious) + "\n")
file.write("Harmless Detections: " + str(harmless) + "\n")
file.write("Undetected Detections: " + str(undetected) + "\n")
file.write("AbuseIPDB info: ----- abuseConfidenceScore: " + str(abuseConfidenceScore) + "\n")
file.write("Domain: " + str(domain) + "\n")
file.write("Usage: " + str(usageType) + "\n")
file.write("countryCode: " + str(countryCode) + "\n")
file.write("lastReportedAt: " + str(lastReportedAt) + "\n")
file.write("isWhitelisted: " + str(isWhitelisted) + "\n")
file.write("isTor: " + str(isTor) + "\n")
if totalReports > 0:
file.write("Reports: \n")
for x in abuse_response['data']['reports']:
file.write("Report Comment: " + x['comment'] + "\n")
file.write("--------------------------------------------------------------------------------------------\n")
def print_reputation_information(line,vt_response,abuse_response):
suspicious = vt_response['data']['attributes']['last_analysis_stats']['suspicious']
malicious = vt_response['data']['attributes']['last_analysis_stats']['malicious']
harmless = vt_response['data']['attributes']['last_analysis_stats']['harmless']
undetected = vt_response['data']['attributes']['last_analysis_stats']['undetected']
all = suspicious + malicious + harmless + undetected
malicious_percentage = (malicious + suspicious / all)
malicious_percentage = "{:.2f}".format(malicious_percentage)
domain = abuse_response['data']['domain']
countryCode = abuse_response['data']['countryCode']
usageType = abuse_response['data']['usageType']
isWhitelisted = abuse_response['data']['isWhitelisted']
isTor = abuse_response['data']['isTor']
totalReports = abuse_response['data']['totalReports']
lastReportedAt = abuse_response['data']['lastReportedAt']
abuseConfidenceScore = abuse_response['data']['abuseConfidenceScore']
print("Reputation info for: "+line)
print("VirusTotal info: -----" + " (" + str(malicious_percentage) + "% Malicious)")
print("Tags: " + str(vt_response['data']['attributes']['tags']))
print("Suspicious Detections: " + str(suspicious))
print("Malicious Detections: " + str(malicious))
print("Harmless Detections: " + str(harmless))
print("Undetected Detections: " + str(undetected))
print("AbuseIPDB info: ----- abuseConfidenceScore: " + str(abuseConfidenceScore))
print("Domain: " + str(domain))
print("Usage: " + str(usageType))
print("countryCode: " + str(countryCode))
print("lastReportedAt: " + str(lastReportedAt))
print("isWhitelisted: " + str(isWhitelisted))
print("isTor: " + str(isTor))
if totalReports > 0:
print("Reports: ")
for x in abuse_response['data']['reports']:
print("Report Comment: " + x['comment'])
print("--------------------------------------------------------------------------------------------")
def reputation_information(line,store_arg):
# perform a request for each ip in VT
response = requests.get('https://www.virustotal.com/api/v3/ip_addresses/' + line.strip(),
headers=information.vt_headers).json()
# abuseipdb request
abuse_response = requests.get('https://api.abuseipdb.com/api/v2/check?ipAddress=' + line.strip() + '&verbose',
headers=information.abuse_headers).json()
# if it does not exist in VT, ignore
try:
if store_arg:
write_reputation_information_to_file(line,response,abuse_response,store_arg)
else:
print_reputation_information(line,response,abuse_response)
except Exception as e:
print("Reputation Error: " +str(response))
print("Potential Reputation Error: " + str(abuse_response))
def write_communicating_files_information_to_file(line, files, output_file):
with open(output_file, 'a') as file:
file.write("VirusTotal Communicating Files\n")
file.write("------------------------------\n")
count = 0
file.write("Communicating Files for: " + line + "\n")
for file_info in files:
names = str(file_info['attributes']['names'])
suggested_threat_label = file_info['attributes']['popular_threat_classification']['suggested_threat_label']
sha256 = file_info['attributes']['sha256']
tags = str(file_info['attributes']['tags'])
suspicious = file_info['attributes']['last_analysis_stats']['suspicious']
malicious = file_info['attributes']['last_analysis_stats']['malicious']
harmless = file_info['attributes']['last_analysis_stats']['harmless']
undetected = file_info['attributes']['last_analysis_stats']['undetected']
all_detections = suspicious + malicious + harmless + undetected
malicious_percentage = (malicious + suspicious) / all_detections * 100
malicious_percentage = "{:.2f}".format(malicious_percentage)
file.write("File: " + str(count) + "\n")
file.write("VirusTotal info: -----" + " (" + str(malicious_percentage) + "% Malicious)\n")
file.write("Suggested Threat Label: " + suggested_threat_label + "\n")
file.write("Names: " + names + "\n")
file.write("SHA256: " + sha256 + "\n")
file.write("Tags: " + tags + "\n")
file.write("Suspicious Detections: " + str(suspicious) + "\n")
file.write("Malicious Detections: " + str(malicious) + "\n")
file.write("Harmless Detections: " + str(harmless) + "\n")
file.write("Undetected Detections: " + str(undetected) + "\n")
file.write("-----------------------\n")
count += 1
def print_communicating_files_information(line,files):
print("VirusTotal Communicating Files")
print("------------------------------")
print("Communicating Files for: " + line)
count=0
for file in files:
names=str(file['attributes']['names'])
suggested_threat_label=file['attributes']['popular_threat_classification']['suggested_threat_label']
sha256=file['attributes']['sha256']
tags=str(file['attributes']['tags'])
suspicious = file['attributes']['last_analysis_stats']['suspicious']
malicious = file['attributes']['last_analysis_stats']['malicious']
harmless = file['attributes']['last_analysis_stats']['harmless']
undetected = file['attributes']['last_analysis_stats']['undetected']
all = suspicious + malicious + harmless + undetected
malicious_percentage = (malicious + suspicious / all)
malicious_percentage = "{:.2f}".format(malicious_percentage)
print("File: "+ str(count))
print("VirusTotal info: -----" + " (" + str(malicious_percentage) + "% Malicious)")
print("Suggested Threat Label: " + suggested_threat_label)
print("Names: " + names)
print("SHA256: " + sha256)
print("Tags: " + tags)
print("Suspicious Detections: " + str(suspicious))
print("Malicious Detections: " + str(malicious))
print("Harmless Detections: " + str(harmless))
print("Undetected Detections: " + str(undetected))
print("-------")
count=count+1
def communicating_files_information(line,store_args):
files=requests.get('https://www.virustotal.com/api/v3/ip_addresses/' + line+'/communicating_files',
headers=information.vt_headers).json()
try:
if not files['data']==[]:
if store_args:
write_communicating_files_information_to_file(line,files['data'],store_args)
else:
print_communicating_files_information(line,files['data'])
else:
if store_args:
with open(store_args, 'a') as file:
file.write("No communicating files detected for the IPv4: "+line+"\n")
else:
print("No communicating files detected for the IPv4: "+line)
except Exception:
print("VT Communicating Files Error: " + str(files))
def main():
# Create ArgumentParser object
parser = argparse.ArgumentParser(description="Welcome to Malware Infrustructure Hunter (MIH). Created by Efstratios Lontzetidis")
# Add command-line arguments
parser.add_argument('--reputation', action='store_true', help='Include output for reputation of the Censys search results')
parser.add_argument('--files', action='store_true',help='Include output for files communicating with the Censys search results')
parser.add_argument("--store", type=str, help="Output file location for results")
# Parse the command-line arguments
args = parser.parse_args()
# Access the values of command-line arguments
files_arg = args.files
reputation_arg = args.reputation
store_arg=args.store
if store_arg:
with open(store_arg, 'a') as file:
file.write("------MalwareInfrastructureHunter starts hunting-------\n")
else:
print("------MalwareInfrastructureHunter starts hunting-------\n")
search_censys(store_arg,reputation_arg,files_arg)
if __name__ == '__main__':
main()