-
Notifications
You must be signed in to change notification settings - Fork 0
/
VT_Classify.py
156 lines (119 loc) · 5.76 KB
/
VT_Classify.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
#!/usr/bin/env python3
import os
def generate_hash_files():
base_path = "/media/cape/Malware/Nico/Dataset_Resized_Malicious_Binaries"
dirs = os.listdir(base_path)
dirs.remove("dir_tree.txt")
dirs.remove("total_samples.txt")
for dir in dirs:
with open(os.path.join(base_path,dir+".txt"), "w") as file:
for hash in os.listdir(os.path.join(base_path,dir)):
file.writelines(os.path.splitext(hash)[0]+"\n")
import requests
import json
import shutil
def check_family():
base_path = "/media/cape/Malware/Nico/Dataset_Resized_Malicious_Binaries"
FAMILIES = ["babuk", "blackmatter", "cerber", "chaos", "wastedlocker", "conti", "cryptbot", "dharma", "formbook", "gandcrab", "hive", "lockbit", "medusalocker", "nitro", "phobos", "sodinokibi", "virlock", "loki", "stop", "razy", "marte", "msil", "snatch"]
txt_files = [file for file in os.listdir(base_path) if file.endswith(".txt")]
txt_files.remove("dir_tree.txt")
txt_files.remove("total_samples.txt")
for txt_file in txt_files[:1]:
# Change the name of the .txt file
txt_file = "medusalocker"
with open(os.path.join(base_path,txt_file+".txt"), "r") as file:
hashes = file.readlines()
print(len(hashes))
i=0
for hash in hashes:
families = {
"conti": 0,
"cryptbot": 0,
"dharma": 0,
"formbook": 0,
"gandcrab": 0,
"hive": 0,
"lockbit": 0,
"nitro": 0,
"phobos": 0,
"sodinokibi": 0,
"virlock": 0,
"loki": 0,
"stop": 0,
"stopcrypt": 0,
"mint": 0,
"razy": 0,
"marte": 0,
"crysis": 0,
"msil": 0,
"snatch": 0,
"medusalocker": 0,
"babuk": 0,
"blackmatter": 0,
"cerber": 0,
"chaos": 0,
"wastedlocker": 0
}
url = "https://www.virustotal.com/api/v3/files/"
url = url+hash.strip("\n")
headers = {
"accept": "application/json",
"x-apikey": "PUT_HERE_THE_API_KEY"
}
print(i, "- ", hash.strip("\n"))
json_report = json.loads(requests.get(url, headers=headers).text)
if 'popular_threat_classification' in json_report.get('data', {}).get('attributes', {}):
if 'popular_threat_name' in json_report.get('data', {}).get('attributes', {}).get('popular_threat_classification', {}):
stats = json_report['data']['attributes']['popular_threat_classification']['popular_threat_name']
stat = []
for s in stats:
stat.append(s['value'])
for s in stat:
if s in FAMILIES:
families[s] = families[s] + 1
to_remove = True
for key, value in families.items():
if value > 0:
to_remove = False
for key, value in families.items():
if key == txt_file and value > 0:
print("The file correctly belongs to its family")
break
elif key == "stopcrypt" and value > 0:
print("The file belongs to a different family")
shutil.move(os.path.join(base_path,txt_file,hash.strip("\n")+".exe"), os.path.join(base_path,"stop",hash.strip("\n")+".exe"))
print("Moved ", hash.strip("\n"), " to the correct family, stop")
break
elif key == "crysis" and value > 0:
print("The file belongs to a different family")
shutil.move(os.path.join(base_path,txt_file,hash.strip("\n")+".exe"), os.path.join(base_path,"dharma",hash.strip("\n")+".exe"))
print("Moved ", hash.strip("\n"), " to the correct family, crysis")
break
elif key in FAMILIES and value > 0:
print("The file belongs to a different family")
shutil.move(os.path.join(base_path,txt_file,hash.strip("\n")+".exe"), os.path.join(base_path,key,hash.strip("\n")+".exe"))
print("Moved ", hash.strip("\n"), " to the correct family, ", key)
break
if to_remove:
os.remove(os.path.join(base_path,txt_file,hash.strip("\n")+".exe"))
print("Removed ", hash.strip("\n"), " because it doesn't belongs to any family.")
i = i+1
else:
os.remove(os.path.join(base_path,txt_file,hash.strip("\n")+".exe"))
print("Removing because is a general ransomware")
else:
os.remove(os.path.join(base_path,txt_file,hash.strip("\n")+".exe"))
print("Removing because is a general ransomware")
def exit_program():
print("Exiting the program.")
exit()
if __name__ == "__main__":
switch_dict = {
'1': generate_hash_files,
'2': check_family,
'3': exit_program,
}
while(True):
choose = input("1: Generate the file containing all the hash\n2: Send the samples to VirusTotal\n3: Exit\n")
selected_function = switch_dict.get(choose, lambda: print("Invalid option"))
selected_function()