-
Notifications
You must be signed in to change notification settings - Fork 0
/
jaccard_HDBSCAN_ransomware.py
550 lines (447 loc) · 20 KB
/
jaccard_HDBSCAN_ransomware.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#Script to compute jaccard distance and use HDBSCAN
import sys
import time
import heapq
import pickle
import ast
import csv
import random
import hdbscan
import datetime
import numpy as np
import pandas as pd
import multiprocessing
import functools
from UltraDict import UltraDict
from sklearn.utils.random import sample_without_replacement
from sklearn.metrics import pairwise_distances
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.metrics.pairwise import euclidean_distances
from sklearn.metrics import jaccard_score
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import minimum_spanning_tree
from scipy.sparse.csgraph import connected_components
from hdbscan.plots import SingleLinkageTree
from hdbscan._hdbscan_tree import condense_tree, compute_stability
from hdbscan._hdbscan_tree import get_clusters, outlier_scores
from hdbscan._hdbscan_linkage import label as slt_label
from sklearn.metrics import silhouette_score
from sklearn.datasets import make_blobs
def series_to_set(df_series):
"""Convert a series in a dataframe to a set, dropping -1"""
s = ast.literal_eval(df_series)
s.discard(-1)
#if len(s) <= 10:
if len(s) <= 10:
return None
return s
def convert_to_set(row):
"""Convert to a set"""
try:
#Convert string to list and remove empty entries
lst = [int(item) for item in row.strip('[]').split(',') if item]
return set(lst)
except:
return set() # Return an empty set in case of error
#Applying Jaccard similarity
def get_file_similarities(idx_range):
eps = 1e-5
shm_file_rare_functions = UltraDict(name="file_rare_functions", create=False, shared_lock=True)
shm_function_files = UltraDict(name="function_files", create=False, shared_lock=True)
shm_function_matrix = UltraDict(name="function_matrix", create=False, shared_lock=True)
start_idx, end_idx = idx_range
dists = {}
for file1_idx in range(start_idx, end_idx+1):
if file1_idx % 1000 == 0:
print(file1_idx, time.time())
sys.stdout.flush()
# Skip files with no rare functions
if shm_file_rare_functions.get(file1_idx) is None:
continue
# Get set of rare function IDs in file
rare_func_idxs = shm_file_rare_functions[file1_idx]
# Build a set of files which share a rare function with file1
file2_idxs = set()
for func_idx in rare_func_idxs:
file2_idxs.update(function_files[func_idx])
# To avoid duplicate work, only consider files with an index greater than file1's
file2_idxs = [idx for idx in file2_idxs if idx > file1_idx]
# Compute jaccard distance between file1 and its related files
file1_file2s = []
file1_dists = []
for file2_idx in file2_idxs:
intersection = len(shm_function_matrix[file1_idx].intersection(shm_function_matrix[file2_idx]))
union = len(shm_function_matrix[file1_idx].union(shm_function_matrix[file2_idx]))
jaccard_similarity = intersection / union
# Convert to distance and add very small value to keep distance nonzero
# Only keep files with >= 95% similar set of functions
if jaccard_similarity > 1:
print(intersection, union, jaccard_similarity)
if jaccard_similarity >= 0.95:
jaccard_dist = 1 - jaccard_similarity + eps
file1_file2s.append(file2_idx)
file1_dists.append(jaccard_dist)
if dists.get(file2_idx) is None:
dists[file2_idx] = []
dists[file2_idx].append([file1_idx, jaccard_dist])
dists[file1_idx] = list(zip(file1_file2s, file1_dists))
return dists
#Calculate the distance for the pe_header information
def get_pe_feature_similarities(idx_range):
eps = 1e-5
shm_pe_data = UltraDict(name="file_pe_features", create=False, shared_lock=True)
start_idx, end_idx = idx_range
dists = {}
for idx1 in range(start_idx, end_idx + 1):
#set1 = pe_feature_data[idx1]
if idx1 % 1000 == 0:
print(idx1, time.time())
sys.stdout.flush()
#skip files with no pe data
if shm_pe_data.get(idx1) is None:
continue
pe_data = shm_pe_data[idx1]
file1_file2s = []
file1_dists = []
idx2_count = 0 # Counter
# To avoid duplicate work, only consider files with an index greater than idx1's
for idx2 in range(idx1 + 1, len(shm_pe_data)):
if idx2_count >= 25: # Stop adding more idx2 values once we reach 25
break
set2 = shm_pe_data[idx2]
intersection = len(pe_data.intersection(set2))
union = len(pe_data.union(set2))
jaccard_similarity = intersection / union # Can add eps to avoid division by zero
if jaccard_similarity > 1:
print(intersection, union, jaccard_similarity)
if jaccard_similarity == 1: # Applying a threshold
jaccard_dist = 1 - jaccard_similarity + eps
'''
if dists.get(idx1) is None:
dists[idx1] = []
if dists.get(idx2) is None:
dists[idx2] = []
dists[idx1].append((idx2, jaccard_dist))
dists[idx2].append((idx1, jaccard_dist))
'''
file1_file2s.append(idx2)
file1_dists.append(jaccard_dist)
if dists.get(idx2) is None:
dists[idx2] = []
dists[idx2].append([idx1, jaccard_dist])
idx2_count += 1 # Increment the counter
dists[idx1] = list(zip(file1_file2s, file1_dists))
idx2_count = 0
return dists
#Calculate the average of both Jaccard similarities between imports and pe headers.
def combine_ultradicts_batch(idx_range):
shm_X_temp = UltraDict(name="X_temp", create=False, shared_lock=True)
shm_X_pe = UltraDict(name="X_pe", create=False, shared_lock=True)
start_idx, end_idx = idx_range
partial_combined_dict = {}
for key in range(start_idx, end_idx + 1):
if key % 1000 == 0:
print(key, time.time())
sys.stdout.flush()
if key in shm_X_temp or key in shm_X_pe:
combined_values = []
dict1 = {idx: dist for idx, dist in shm_X_temp.get(key, [])}
dict2 = {idx: dist for idx, dist in shm_X_pe.get(key, [])}
all_indices = set(dict1.keys()).union(dict2.keys())
for idx in all_indices:
dist1 = dict1.get(idx, 1) # Default distance if not present
dist2 = dict2.get(idx, 1) # Default distance if not present
averaged_distance = (dist1 + dist2) / 2
#print(averaged_distance)
combined_values.append((idx, averaged_distance))
partial_combined_dict[key] = combined_values
return partial_combined_dict
#Calcualte the reach scores
def get_reach_scores(idx_range):
reach_scores = []
file1_idxs = []
file2_idxs = []
start_idx, end_idx = idx_range
shm_X = UltraDict(name="X", create=False, shared_lock=True)
shm_core_dists = UltraDict(name="core_dists", create=False, shared_lock=True)
for file1_idx in range(start_idx, end_idx):
if file1_idx % 1000 == 0:
print(file1_idx)
sys.stdout.flush()
if shm_X.get(file1_idx) is None:
continue
file2_info = shm_X[file1_idx]
for file2_idx, dist in file2_info:
reach_score = max([shm_core_dists[file1_idx], shm_core_dists[file2_idx], dist])
reach_scores.append(reach_score)
file1_idxs.append(file1_idx)
file2_idxs.append(file2_idx)
return reach_scores, file1_idxs, file2_idxs
for name in ["file_rare_functions", "function_files", "function_matrix", "X", "core_dists", "X_temp", "X_pe", "file_pe_features"]:
UltraDict.unlink_by_name(name, ignore_errors=True)
UltraDict.unlink_by_name(f'{name}_memory', ignore_errors=True)
#main function
if __name__ == "__main__":
print("Start", time.time())
sys.stdout.flush()
# Read dictionary pkl file
with open('func_maps.pkl', 'rb') as fp:
function = pickle.load(fp)
#Upload the filtered file here
file_id_map = pd.read_csv('results.csv', sep='\t')
# Make the 'mapped_functions' column contain sets of function IDs
function_matrix = file_id_map['mapped_functions'].apply(series_to_set)
print(len(function_matrix))
sys.stdout.flush()
keep_df = file_id_map.iloc[[i for i, s in enumerate(function_matrix) if s is not None]]
print(len(keep_df))
print("Converted pandas dataframe to sets of function IDs", time.time())
#print(keep_df.shape)
print(len(keep_df))
sys.stdout.flush()
# Discard elements with < 10 imports
function_matrix = [s for s in function_matrix if s is not None]
# Randomly keep num_keep points in dataset
'''
num_keep = 100000
rand_idxs = sample_without_replacement(len(function_matrix), num_keep)
function_matrix = {i: function_matrix[idx] for i, idx in enumerate(rand_idxs)}
keep_df = keep_df.iloc[rand_idxs]
'''
num_samples = len(function_matrix)
# Comment this out if you uncomment the random keep
function_matrix = {i: function_matrix[i] for i in range(num_samples)}
print(keep_df.columns)
md5s = keep_df["md5"].tolist()
print(keep_df.keys())
funcs = keep_df["dll_imports"].tolist()
tokens = keep_df["av_tokens"].tolist()
#Consider the pe informaation of only the keep_df
# Extract the column data as a list of sets and store in Ultradict
data_list = keep_df["mapped_pe_features"].apply(convert_to_set)
try:
data_list_dict = {i: s for i, s in enumerate(data_list)}
buffer_size = sys.getsizeof(data_list) + 1000
shm_pe_data = UltraDict(data_list_dict, name="file_pe_features",
buffer_size=buffer_size, create=True, shared_lock=True)
except UltraDict.Exceptions.AlreadyExists as e:
print(f"Memory already exists: {e}")
# Map each function to set of files it occurs in
function_files = {}
#num_samples = len(function_matrix)
for file_idx in range(num_samples):
for func_idx in function_matrix[file_idx]:
if function_files.get(func_idx) is None:
function_files[func_idx] = set()
function_files[func_idx].add(file_idx)
# Get list of functions that occur in less than some % of files
rare_func_threshold = num_samples * 0.05 # 5% threshold
rare_func_files = {}
for func_idx, file_idxs in function_files.items():
if len(file_idxs) <= rare_func_threshold:
rare_func_files[func_idx] = file_idxs
# Map files back to only their rare functions
file_rare_functions = {}
for func_idx, file_idxs in rare_func_files.items():
for file_idx in file_idxs:
if file_rare_functions.get(file_idx) is None:
file_rare_functions[file_idx] = set()
file_rare_functions[file_idx].add(func_idx)
# Compare files which contain the same rare function
eps = 1e-5 # Small value to add to each distance so it's nonzero
print("Num samples", num_samples)
#Handle the exception memory already exists
try:
# UltraDict for files -> rare functions
buffer_size = sys.getsizeof(file_rare_functions) + 1000
shm_file_rare_functions = UltraDict(file_rare_functions, name="file_rare_functions",
buffer_size=buffer_size, create=True, shared_lock=True)
# Ultradict for functions -> file IDs
buffer_size = sys.getsizeof(function_files) + 1000
shm_function_files = UltraDict(function_files, name="function_files",
buffer_size=buffer_size, create=True, shared_lock=True)
# Ultradict for file IDs -> functions
buffer_size = sys.getsizeof(function_matrix) + 1000
shm_function_matrix = UltraDict(function_matrix, name="function_matrix",
buffer_size=buffer_size, create=True, shared_lock=True)
except UltraDict.Exceptions.AlreadyExists as e:
print(f"Memory already exists: {e}")
# Compute jaccard distances in parallel
pool = multiprocessing.Pool(32) # Use 32 cores
batch_size = 1000
start_idxs = list(range(0, num_samples, batch_size))
end_idxs = list(range(batch_size-1, num_samples+batch_size-1, batch_size))
end_idxs[-1] = num_samples-1
idx_ranges = list(zip(start_idxs, end_idxs))
results = pool.map(get_file_similarities, idx_ranges)
print(len(results))
# Construct nested dict storing distances
X = {file1_idx: [] for file1_idx in range(num_samples)}
for dists in results:
for file1_idx, dist_info in dists.items():
X[file1_idx].extend(dist_info)
print("Finished jaccard distance calculations", time.time())
print(len(X))
print(sys.getsizeof(X))
sys.stdout.flush()
# Ultradict for all distances
try:
buffer_size = sys.getsizeof(X) + 1000
shm_X_temp = UltraDict(X, name="X_temp", buffer_size=buffer_size, create=True, shared_lock=True)
except UltraDict.Exceptions.AlreadyExists as e:
print(f"Memory already exists: {e}")
#Also compute jaccard similarity of pe information
results_pe = pool.map(get_pe_feature_similarities, idx_ranges)
print(len(results_pe))
# Combine results
combined_results = {file1_idx: [] for file1_idx in range(num_samples)}
for dists in results_pe:
for file1_idx, dist_info in dists.items():
combined_results[file1_idx].extend(dist_info)
print("Finished jaccard distance calculations for pe information", time.time())
with open('check_combined_files.csv', 'w') as csv_file:
writer = csv.writer(csv_file)
for key, value in combined_results.items():
writer.writerow([key, value])
print(len(combined_results))
print(sys.getsizeof(combined_results))
sys.stdout.flush()
#Ultradict for distances for pe
try:
buffer_size = sys.getsizeof(combined_results) + 1000
shm_X_pe = UltraDict(combined_results, name="X_pe", buffer_size=buffer_size, create=True, shared_lock=True)
print("Finished storing in dictionary")
sys.stdout.flush()
except UltraDict.Exceptions.AlreadyExists as e:
print(f"Memory already exists: {e}")
#Combine both Ultradicts
#CHECK: Try reducing the number of cores for these just incase this is where the memory is exceeding and the program is terminating
pool = multiprocessing.Pool(16) # Tried 24 disn't work now trying 16
results_merge = pool.map(combine_ultradicts_batch, idx_ranges)
# Combine results
combined_ultradicts = {file1_idx: [] for file1_idx in range(num_samples)}
for m_results in results_merge:
for file1_idx, merge_results in m_results.items():
combined_ultradicts[file1_idx].extend(merge_results)
print("Finished jaccard distance calculations for commbining dictionaries", time.time())
print(sys.getsizeof(combined_ultradicts))
sys.stdout.flush()
# Ultradict for all distances
try:
buffer_size = sys.getsizeof(combined_ultradicts) + 1000
shm_X = UltraDict(combined_ultradicts, name="X", buffer_size=buffer_size, create=True, shared_lock=True)
except UltraDict.Exceptions.AlreadyExists as e:
print(f"Memory already exists: {e}")
print("Completed all distance calculations")
sys.stdout.flush()
pool = multiprocessing.Pool(32) # Use 32 cores
# HDBSCAN hyperparameters
k = 5
min_samples = 10
min_cluster_size = 10
# Get core distance of each point
core_dists = {}
for file_idx in range(num_samples):
if X.get(file_idx) is None:
continue
dists = [d for d, _ in X[file_idx]]
n_smallest = [eps]
if len(dists):
n_smallest = heapq.nsmallest(k, dists)
if len(n_smallest) > k:
core_dists[file_idx] = n_smallest[k]
else:
core_dists[file_idx] = n_smallest[-1]
try:
buffer_size = sys.getsizeof(core_dists) + 1000
shm_core_dists = UltraDict(core_dists, name="core_dists",
buffer_size=buffer_size, create=True, shared_lock=True)
except UltraDict.Exceptions.AlreadyExists as e:
print(f"Memory already exists: {e}")
print("Computed all core distances", time.time())
sys.stdout.flush()
# Compute mutual reachablity distances between scans
reach_scores = []
file1_idxs = []
file2_idxs = []
results = pool.map(get_reach_scores, idx_ranges)
for r, f1, f2 in results:
#print("Enters this for loop to store values")
reach_scores.extend(r)
file1_idxs.extend(f1)
file2_idxs.extend(f2)
# Construct CSR matrix for reachability scores
reach_matrix = csr_matrix((reach_scores, (file1_idxs, file2_idxs)), shape=(num_samples, num_samples))
print("Constructed reachability matrix", time.time())
sys.stdout.flush()
# Compute Minimum Spanning Forest (MSF) of reachability matrix
msf = minimum_spanning_tree(reach_matrix)
msf_idxs = msf.nonzero()
# Get all connected components in MSF
msf_vals = msf[msf_idxs].A1
msf_sparse = csr_matrix((msf_vals, msf_idxs), shape=(num_samples, num_samples))
n_components, conn_labels = connected_components(msf_sparse)
all_idxs = np.arange(num_samples)
all_labels = np.array([-1]*num_samples)
num_labels = 0
# Iterate over each connected component
for i in range(n_components):
# Skip components smaller than min_cluster_size
keep_idxs = all_idxs[conn_labels == i]
if len(keep_idxs) < min_cluster_size:
continue
print("Component size:", len(keep_idxs))
# Get Minimum Spanning Tree (MST) for component
keep_rows = np.array([i for i, idx in enumerate(msf_idxs[0]) if idx in keep_idxs])
mst_idxs = np.array(msf_idxs)[:, keep_rows]
# label() will break if the set of idxs is not contiguous
sorted_mst_idxs = sorted(keep_idxs)
trans_idxs = {idx: i for i, idx in enumerate(sorted_mst_idxs)}
# Sort MST edges by highest->lowest distance
mst_vals = msf[mst_idxs[0], mst_idxs[1]][0].A1
row_idxs = [trans_idxs[idx] for idx in mst_idxs[0]]
col_idxs = [trans_idxs[idx] for idx in mst_idxs[1]]
mst = np.vstack((row_idxs,) + (col_idxs,) + (mst_vals,)).T
mst = mst[np.argsort(mst.T[2]), :]
print("Got MST", time.time())
sys.stdout.flush()
# Get single linkage tree from MST
slt = slt_label(mst)
print("Got single linkage tree", time.time())
sys.stdout.flush()
# Condensing Single Linkage Tree and compute stability
condensed_tree = condense_tree(slt, min_cluster_size)
stability_dict = compute_stability(condensed_tree)
print("Condensed tree, computed stability", time.time())
sys.stdout.flush()
# Get cluster labels
cluster_labels, _, _ = get_clusters(
condensed_tree,
stability_dict,
"leaf", # Selection Method
True, # Allow single cluster
False, # Match Reference implementation
0.0, # Cluster selection epsilon
0 # Max cluster size
)
print("Got cluster labels", time.time())
sys.stdout.flush()
# Update global labels with this clustering
for i, label in enumerate(cluster_labels):
if label != -1:
label += num_labels
idx = keep_idxs[i]
all_labels[idx] = label
num_labels += len(set(cluster_labels) - set([-1]))
# TODO: Save to results folder!!!
df_clusters = pd.DataFrame({
'Cluster_Labels': all_labels,
'md5s': md5s,
'mapped_functions': funcs,
'av_tokens': tokens,
})
#replace filename with something else
df_clusters.to_csv('HDBSCAN_sparse_results_rerun_v2.csv')
print("Done")
print(time.time())
sys.stdout.flush()