-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclustering.py
269 lines (233 loc) · 7.95 KB
/
clustering.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# -*- coding: utf-8 -*-
"""malcluster: malware clustering analysis tool"""
__version__ = "0.1.0"
import os
import sys
import time
import timeit
import shutil
import itertools
import profile
import numpy as np
import fastcluster
import multiprocessing as mp
from scipy.cluster.hierarchy import fcluster # @UnresolvedImport
from scipy.interpolate import PiecewisePolynomial # @UnresolvedImport
from scipy.optimize import fsolve # @UnresolvedImport
import matplotlib.pyplot as plt
from plugins.mvhash import MvHash
from plugins.nghash import NgHash
from plugins.sdhash import SdHash
from plugins.bshash import BsHash
from plugins.rlhash import RlHash
p1 = None
p2 = None
malorder = []
fingerprints = {}
cwd = os.getcwd()
myhash = None
algorithms = [
BsHash(81920, 10), # BsHash works on original whole samples
NgHash(7), # NgHash works on original whole samples
RlHash(16807, 256, 1),
MvHash(512, 20, 0.7), # MvHash works on python-extracted code sequences
SdHash() # SdHash works on python-extracted code sequences
]
hash_names = ["bshash", "nghash", "rlhash", "mvhash", "sdhash"]
def timing(f):
def wrap(*args):
time1 = time.time()
ret = f(*args)
time2 = time.time()
print '%s function took %0.3f ms' % (f.func_name, (time2-time1)*1000.0)
return ret
return wrap
def hash_gen():
""" Wrapper for generating fingerprints from malware samples """
shutil.rmtree(os.path.join(cwd, "hashs/"), ignore_errors=True)
os.mkdir(os.path.join(cwd, "hashs"))
mallist = (x for x in os.listdir(os.path.join(cwd, "samples/")))
for malware in mallist:
malpath = os.path.join(cwd, "samples/" + malware)
malorder.append(malware)
fingerprints[malware] = myhash.generateHash(malpath)
shutil.rmtree(os.path.join(cwd, "hashs/"), ignore_errors=True)
def hash_comp(ab):
return myhash.compareHash(ab[0], ab[1])
def get_dmentry():
for item in itertools.combinations(malorder, 2):
yield map(fingerprints.get, item)
def get_dmlist():
""" Get all the pairwise distance matrix """
number_per_round = 10000
result = []
getdm = get_dmentry()
pool = mp.Pool(processes=mp.cpu_count())
while True:
tempresult = pool.map(hash_comp, itertools.islice(getdm, number_per_round))
if tempresult:
result.extend(tempresult)
else:
break
return np.array(result)
def hacluster(y):
""" Wrapper for the Hierarchical Clustering algorithm from fastcluster """
z = fastcluster.linkage(y, method='single')
return z
def calculate_precision(ref_lines, cdb_lines):
tp = 0
t_num = 0
for cline in cdb_lines:
clist = (cline.split(':')[1]).split()
clist_num = len(clist)
t_num += clist_num
maxcnt = 0
for rline in ref_lines:
rlist = (rline.split(':')[1]).split()
#rlist_num = len(rlist)
cnt = 0
for v in clist:
if v in rlist:
cnt += 1
if cnt > maxcnt:
maxcnt = cnt
if maxcnt == clist_num:
break
tp += maxcnt
return float(tp)/t_num
def calculate_recall(ref_lines, cdb_lines):
tp = 0
t_num = 0
for rline in ref_lines:
rlist = (rline.split(':')[1]).split()
rlist_num = len(rlist)
t_num += rlist_num
maxcnt = 0
for cline in cdb_lines:
clist = (cline.split(':')[1]).split()
#clist_num = len(clist)
cnt = 0
for v in rlist:
if v in clist:
cnt += 1
if cnt > maxcnt:
maxcnt = cnt
if maxcnt == rlist_num:
break
tp += maxcnt
return float(tp)/t_num
def evaluate(z):
shutil.rmtree(os.path.join(os.getcwd(), "eval/"), ignore_errors=True)
os.mkdir(os.path.join(os.getcwd(), "eval"))
thresholds = np.linspace(0, 1, 21)
mid = np.arange(0, 1, 0.01)
precision_set = []
recall_set = []
refset = {}
tempx = 0
for i in xrange(len(malorder)):
if malorder[i].split("-")[0] not in refset:
refset[malorder[i].split("-")[0]] = [i]
else:
refset[malorder[i].split("-")[0]].append(i)
with open("eval/refset.txt", "w") as f:
for family in refset:
f.write(family + ": " + ' '.join([str(x) for x in refset[family]]) + "\n")
for i in thresholds:
with open("eval/refset.txt") as f:
reflines = f.readlines()
hc = fcluster(z, i, 'distance')
cdblines = get_clist(hc, i)
precision = calculate_precision(reflines, cdblines)
recall = calculate_recall(reflines, cdblines)
precision_set.append(precision)
recall_set.append(recall)
global p1
p1 = PiecewisePolynomial(thresholds, np.array(precision_set)[:, np.newaxis])
global p2
p2 = PiecewisePolynomial(thresholds, np.array(recall_set)[:, np.newaxis])
for x in mid:
root, infodict, ier, mesg = fsolve(pdiff, x, full_output=True)
root = float("%.3f" % root[0])
if ier == 1 and thresholds.min() < root < thresholds.max():
tempx = root
break
tempy = p2(tempx)
if p1(tempx) > p2(tempx):
tempy = p1(tempx)
print "Best x:", tempx, "Best y:", tempy
try:
pleg1, = plt.plot(thresholds, precision_set, '-bo', label="Precision")
pleg2, = plt.plot(thresholds, recall_set, '-rx', label="Recall")
plt.legend([pleg1, pleg2], ["Precision", "Recall"], loc="center right")
plt.xlabel('Distance threshold (t)')
plt.ylabel("Precision and Recall")
plt.show()
except:
raise
finally:
plt.close()
return tempx, tempy
def pdiff(x):
return p1(x) - p2(x)
def get_clist(hc, s):
ncluster = 0
shres = '%1.3f' % s
with open("eval/"+shres+".txt", "w") as f:
for i in xrange(len(malorder)):
cid = hc[i]
if cid == -1:
continue
if cid == 0:
hc[i] = -1
f.write("C" + str(ncluster) + ": " + str(i) + "\n")
ncluster += 1
else:
f.write("C" + str(ncluster) + ": ")
for j in range(i, len(malorder)):
if hc[j] == cid:
hc[j] = -1
f.write(" " + str(j))
f.write("\n")
ncluster += 1
print "threshold: ", s, "Nclusters: ", ncluster
with open("eval/"+shres+".txt") as f:
return f.readlines()
def get_clusters(z, shresholdx):
hc = fcluster(z, shresholdx, 'distance')
clusters = {}
with open("eval/cluster_results.txt", "w") as f:
for i in xrange(hc.min(), hc.max()+1):
clusters[i] = []
for j in xrange(hc.size):
if hc[j] == i:
mal = malorder[j]
clusters[i].append(mal)
for i in clusters:
f.write("C"+str(i)+":"+"\n")
for mal in clusters[i]:
f.write(mal+"\n")
f.write("\n")
def main():
global myhash
hash_name = sys.argv[1]
if hash_name in hash_names:
myhash = algorithms[hash_names.index(hash_name)]
else:
print "Unknown hash name provided."
startedat = timeit.default_timer()
hash_gen()
hashgenat = timeit.default_timer()
print "Finish generating fingerprints", hashgenat - startedat
y = get_dmlist()
print "Max distance", np.amax(y)
getdmat = timeit.default_timer()
print "Finish generating distance matrix", getdmat - hashgenat
z = hacluster(y)
hclustat = timeit.default_timer()
shresholdx, tempy = evaluate(z)
get_clusters(z, shresholdx)
print "Finish clustering analyais", len(malorder), hclustat - getdmat
if __name__ == "__main__":
# profile.run('main()')
main()