-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdriver.py
More file actions
1007 lines (934 loc) · 35 KB
/
Copy pathdriver.py
File metadata and controls
1007 lines (934 loc) · 35 KB
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Part of the VexIR2Vec Project, under the AGPL V3.0 License. See the
# LICENSE file or <https://www.gnu.org/licenses/> for license information.
"""Main driver script for embedding generation and triplets generation"""
# Usage: python driver.py -b /path/to/binary -o /path/to/output.data -v /path/to/seed/embedding -d -n <Normalisation> -fchunks <no-of-func-chunks> -edb /path/to/DB_path/during/db/genaration/.db -mode <db/non-db>
import json
from colorama import Fore, Style
import pickle
from posixpath import basename
import os
import sys
SCRIPT_DIR_1 = os.path.dirname(os.path.abspath(__file__))
IMPORT_DIR = os.path.join(SCRIPT_DIR_1, "./embedding-gen")
sys.path.append(os.path.normpath(IMPORT_DIR))
import triplets as T
import argparse
import angr
import pdb
import re
import glob
import pyvex as py
import numpy as np
import pickle
from angr.analyses.forward_analysis.visitors.call_graph import CallGraphVisitor as CGV
from multiprocessing import Manager, Lock, Queue, Process
from pebble import ProcessPool
from functools import partial
import string
import time, sys, psutil
from collections import defaultdict
from function_normalizer import functionNormalizer
from angr.angrdb.db import AngrDB
import networkx as nx
import scipy.sparse
import pathlib
import sys
import logging
import sqlalchemy as db
from sqlalchemy.orm import scoped_session, sessionmaker
SCRIPT_DIR_2 = os.path.dirname(os.path.abspath(__file__))
IMPORT_DIR_1 = os.path.join(SCRIPT_DIR_2, "./db-gen")
sys.path.append(os.path.normpath(IMPORT_DIR_1))
from binary_db_model import binaryMetaData, binaryMetaDataUnstripped
import pickle
import pandas as pd
import embeddings as emb
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
VEXNET_PATH = os.path.join(SCRIPT_DIR, "../vexNet")
sys.path.append(os.path.normpath(VEXNET_PATH))
# print(sys.path)
import warnings
from utils import *
warnings.filterwarnings("ignore")
skipFuncs = [
"_init",
"UnresolvableCallTarget",
"_start",
"_dl_relocate_static_pie",
"deregister_tm_clones",
"UnresolvableJumpTarget",
"register_tm_clones",
"frame_dummy",
"_fini",
]
skipLibs = ["libstdc++.so"]
freq_dict = {}
freq_weight_dict = None
# Caller -> [Callee1, Callee2, ...] map
edges = {}
# For external functions
extern_edges = {}
# Function -> [prefix, strRefs, sbVecs] map
func_vec_map = {}
# Set of functions that are called
isCalled = set()
ext_lib_fn_names = {}
str_fn_embs = {}
cgv_nodes = None
unstr_df = None
def removeEntity(text, entity_list):
for entity in entity_list:
if entity in text:
text = text.replace(entity, " ")
return text.strip()
def addCalleeVecs(orig_addr, edges, func_vec_map, ext_lib_fn_names):
map_entry = func_vec_map[orig_addr]
for callee_addr in edges:
map_entry[1] = map_entry[1] + func_vec_map[callee_addr][1]
if orig_addr in ext_lib_fn_names and callee_addr in ext_lib_fn_names:
ext_lib_fn_names[orig_addr] = (
ext_lib_fn_names[orig_addr] + ext_lib_fn_names[callee_addr]
)
elif (
callee_addr in ext_lib_fn_names
): # check needed as there can be no external lib func for a func
ext_lib_fn_names[orig_addr] = ext_lib_fn_names[callee_addr]
map_entry[2] += func_vec_map[callee_addr][2] # for func level O embed
map_entry[3] += func_vec_map[callee_addr][3] # for func level T embed
map_entry[4] += func_vec_map[callee_addr][4] # for func level A embed
func_vec_map[orig_addr] = map_entry
return (map_entry, ext_lib_fn_names)
def parallelFunc(
func_chunk,
addr_to_line,
enablecfg,
isUnstripped,
vocabulary,
vec_map,
lock,
outFile,
normalize,
ext_global=None,
edges_pre=None,
isCalledd=None,
mode="",
):
for func in func_chunk:
prefix = ""
prefix = str(func.addr) + "\t"
ofile = open(outFile, "a")
if addr_to_line and func.addr in addr_to_line:
# logic added only for openssl unstr data gen
src_key = str(addr_to_line[func.addr])
# Replace config directory in key string for openssl
src_key = re.sub(
r"(x86|arm)-(clang|arm-linux-gnueabi-gcc|gcc)-(1?[0-9]|bcf|fla|sub|hybrid)-O[0-3|s]",
"build-dir",
src_key,
)
prefix += src_key + "\t"
else:
prefix += "NO_FILE_SRC \t"
prefix += func.name + "\t"
strRefs = []
if mode != "pre":
try:
strEmbed = str_fn_embs[func.addr]
except KeyError:
strEmbed = np.array2string(np.zeros(1), max_line_width=99999999)
else:
try:
for _, strRef in func.string_references():
if isUnstripped:
break
"""
preprocess stringrefs for cleaning
1. removing everything other than alphabets
2. removing strings containing paths
3. removing format specifiers
4. lowercasing everything
5. convert to separate tokens
"""
# print("Debug StrRef: ",strRef)
strRef = strRef.decode("latin-1")
if "http" in strRef:
continue
format_specifiers = [
"%c",
"%s",
"%hi",
"%h",
"%Lf",
"%n",
"%d",
"%i",
"%o",
"%x",
"%p",
"%f",
"%u",
"%e",
"%E",
"%%",
"%#lx",
"%lu",
"%ld",
"__",
"_",
]
punctuations = list(string.punctuation)
strRef = removeEntity(strRef, format_specifiers)
strRef = removeEntity(strRef, punctuations)
strRef = re.sub("[^a-zA-Z]", " ", strRef).lower().strip().split()
if strRef:
strRefs.extend(strRef)
except angr.errors.SimEngineError as e:
print("CONTINUING IN EXCEPT")
pass
strEmbed = getStremb("^".join(strRefs))
embeddings = emb.symbolicEmbeddings(vocabulary)
if isUnstripped:
bbwalkVecs = np.array2string(np.zeros(1), max_line_width=99999999)
else:
if args.bb_freq_weight:
bbwalkVecs_O, bbwalkVecs_T, bbwalkVecs_A = embeddings.processFunc(
func, normalize, bbfreq_dict=freq_weight_dict
)
else:
bbwalkVecs_O, bbwalkVecs_T, bbwalkVecs_A = embeddings.processFunc(
func, normalize
)
try:
if isUnstripped:
vec = prefix + str(bbwalkVecs) + "\n"
lock.acquire()
ofile.write(vec)
lock.release()
# Dump function to file if function is neither a callee nor caller
# To save on memory usage
elif func.addr not in isCalledd and not edges_pre[func.addr]:
if func.addr in ext_global:
if mode != "pre":
if unstr_df.isin([func.addr]).any().any() and not isUnstripped:
# exit(0)
unstr_df.loc[
unstr_df.addr == func.addr,
["strRefs", "extlibs", "embed_O", "embed_T", "embed_A"],
] = [
str(strEmbed).replace("\n", ""),
str(ext_global[func.addr]).replace("\n", ""),
str(bbwalkVecs_O).replace("\n", ""),
str(bbwalkVecs_T).replace("\n", ""),
str(bbwalkVecs_A).replace("\n", ""),
]
vec = (
prefix
+ str(strEmbed).replace("\n", "")
+ "\t"
+ str(ext_global[func.addr]).replace("\n", "")
+ "\t"
+ str(bbwalkVecs_O).replace("\n", "")
+ "\t"
+ str(bbwalkVecs_T).replace("\n", "")
+ "\t"
+ str(bbwalkVecs_A).replace("\n", "")
+ "\n"
)
else:
vec = (
prefix
+ str(strEmbed).replace("\n", "")
+ "\t"
+ "\t"
+ str(bbwalkVecs_O).replace("\n", "")
+ "\t"
+ str(bbwalkVecs_T).replace("\n", "")
+ "\t"
+ str(bbwalkVecs_A).replace("\n", "")
+ "\n"
)
lock.acquire()
ofile.write(vec)
lock.release()
else:
vec_map[func.addr] = [
prefix,
strEmbed,
bbwalkVecs_O,
bbwalkVecs_T,
bbwalkVecs_A,
]
except KeyError:
continue
def processBinary(
proj,
addr_to_line,
outFile,
vocabulary,
enablecfg=False,
printTriplets=False,
isUnstripped=False,
func_chunks=32,
dump_cfg=False,
normalize=1,
mode="",
):
if printTriplets:
ofile = open(outFile, "w")
triplets = T.triplets()
# print(triplets)
print(proj)
proj.analyses.CFGFast(normalize=True)
cg = proj.kb.functions.callgraph
print(cg)
func_norm = functionNormalizer(proj)
funcs = []
cgv = CGV(cg)
nodes = cgv.sort_nodes()
traverse = True
funcs = functionTraversal(
proj,
nodes,
isUnstripped,
func_norm,
skipFuncs,
funcs,
addr_to_line=addr_to_line,
)
# print(funcs)
# exit(0)
for i in funcs:
funcTriplets = triplets.processFunc(i)
# print(funcTriplets)
ofile.write(funcTriplets)
ofile.close()
exit(0)
if mode == "pre":
extern_edges = {}
edgess = {}
isCalledd = set()
ext_lib_fn_names_global = {}
ext_lib_fn_names_l = {}
str_fn_embs = {}
loadFasttextInit()
symbols = proj.loader.symbols
ext_lib_functions = [
symbol.name for symbol in symbols if symbol.is_function and symbol.is_extern
]
while True:
if psutil.virtual_memory().percent < 45:
break
proj.analyses.CFGFast(normalize=True)
cg = proj.kb.functions.callgraph
func_norm = functionNormalizer(proj)
funcs = []
cgv = CGV(cg)
nodes = cgv.sort_nodes()
traverse = True
funcs = functionTraversal(
proj,
nodes,
isUnstripped,
func_norm,
skipFuncs,
funcs,
addr_to_line=addr_to_line,
)
if mode == "pre":
func_addr_list = [func.addr for func in funcs]
processExtlibCalls(
func_addr_list,
funcs,
proj,
ext_lib_functions,
ext_lib_fn_names_l,
extern_edges,
edgess,
isCalledd,
)
for func in funcs:
if func.addr in ext_lib_fn_names_l:
libEmbed = getExtlibemb("^".join(ext_lib_fn_names_l[func.addr]))
else:
ext_lib_fn_names_l[func.addr] = np.nan
libEmbed = getExtlibemb(ext_lib_fn_names_l[func.addr])
libEmbed = libEmbed.reshape(
-1,
)
ext_lib_fn_names_global[func.addr] = libEmbed
strRefs = processStringReferences(func, isUnstripped)
strEmbed = getStremb(strRefs)
str_fn_embs[func.addr] = strEmbed
if printTriplets:
pass
else:
ofile = open(outFile, "a")
embeddings = emb.symbolicEmbeddings(vocabulary)
if enablecfg:
first = True
for func in funcs:
jsonFnObj = {}
print("processing function - ", func.name, func.addr)
jsonFnObj["addr"] = str(func.addr)
jsonFnObj["name"] = func.name
if args.dump_dataset and func.addr in addr_to_line:
src_key = str(addr_to_line[func.addr])
# Replace config directory in key string for openssl
src_key = re.sub(
r"(x86|arm)-(clang|arm-linux-gnueabi-gcc|gcc)-(1?[0-9]|bcf|fla|sub|hybrid)-O[0-3|s]",
"build-dir",
src_key,
)
jsonFnObj["key"] = src_key
else:
jsonFnObj["key"] = "NO_FILE_SRC"
strRefs = []
for _, strRef in func.string_references():
"""
preprocess stringrefs for cleaning
1. removing everything other than alphabets
2. removing strings containing paths
3. removing format specifiers
4. lowercasing everything
5. convert to separate tokens
"""
strRef = strRef.decode("latin-1")
if "http" in strRef:
continue
format_specifiers = [
"%c",
"%s",
"%hi",
"%h",
"%Lf",
"%n",
"%d",
"%i",
"%o",
"%x",
"%p",
"%f",
"%u",
"%e",
"%E",
"%%",
"%#lx",
"%lu",
"%ld",
"__",
"_",
]
punctuations = list(string.punctuation)
strRef = removeEntity(strRef, format_specifiers)
strRef = removeEntity(strRef, punctuations)
strRef = re.sub("[^a-zA-Z]", " ", strRef).lower().strip().split()
if strRef:
strRefs.extend(strRef)
sbVecs = embeddings.processFunc(
func, enablecfg=enablecfg, genSBVec=True
)
json_edges = {}
for n in proj.kb.functions[func.name].graph:
json_edges[n.addr] = [i.addr for i in n.successors()]
jsonFnObj["nodes"] = sbVecs
jsonFnObj["edges"] = json_edges
strVec = strRefs = "^".join(strRefs)
jsonFnObj["strings"] = strVec
jsonStr = json.dumps(jsonFnObj)
if first:
ofile.write("[")
first = False
else:
ofile.write(",\n")
ofile.write(jsonStr)
ofile.write("]\n")
ofile.close()
else:
if dump_cfg == True:
for func in funcs:
G = proj.kb.functions[func.name].graph
adj_matrix = nx.adjacency_matrix(G).toarray()
if str(adj_matrix.tolist()) == "nan":
continue
vec = str(func.addr) + "\t" + str(adj_matrix.tolist()) + "\n"
ofile.write(vec)
ofile.write("\n")
return
global unstr_df, ext_lib_fn_names
func_addr_list = [func.addr for func in funcs]
if not isUnstripped and mode != "pre":
unstr_df = unstr_df[unstr_df["addr"].isin(func_addr_list)]
unstr_df.reset_index(drop=True, inplace=True)
m = Manager()
lock = m.Lock()
func_vec_map = m.dict()
if mode != "pre":
ext_lib_fn_names_local = ext_lib_fn_names.copy()
if mode != "pre":
parallelFunc_part = partial(
parallelFunc,
addr_to_line=addr_to_line,
enablecfg=enablecfg,
isUnstripped=isUnstripped,
vocabulary=vocabulary,
vec_map=func_vec_map,
lock=lock,
outFile=outFile,
normalize=normalize,
isCalledd=isCalled,
edges_pre=edges,
ext_global=ext_lib_fn_names,
)
else:
parallelFunc_part = partial(
parallelFunc,
addr_to_line=addr_to_line,
enablecfg=enablecfg,
isUnstripped=isUnstripped,
vocabulary=vocabulary,
vec_map=func_vec_map,
lock=lock,
outFile=outFile,
normalize=normalize,
ext_global=ext_lib_fn_names_global,
edges_pre=edgess,
isCalledd=isCalledd,
mode="pre",
)
workers = []
k = func_chunks
reversed_func_list = funcs
reversed_func_list.reverse()
func_chunks = [
reversed_func_list[i : i + k]
for i in range(0, len(reversed_func_list), k)
]
max_jobs_running = 32
jobs_running = 0
for chunk in func_chunks:
while True:
if psutil.virtual_memory().percent < 45:
break
p = Process(target=parallelFunc_part, args=(chunk,))
p.start()
workers.append(p)
jobs_running += 1
if jobs_running >= max_jobs_running:
while jobs_running >= max_jobs_running:
jobs_running = 0
for p in workers:
jobs_running += p.is_alive()
for p in workers:
p.join()
p.close()
if mode != "pre":
for func in reversed_func_list:
if isUnstripped:
break
try:
# Ensure `func.addr` exists in `isCalled` or `edges`
if func.addr in isCalled or func.addr in edges:
if func.addr not in func_vec_map.keys():
continue
# Call the addCalleeVecs function
map_entry, ext_lib_fn_names_local = addCalleeVecs(
func.addr,
edges.get(
func.addr, []
), # Safely get edges for `func.addr`
func_vec_map,
ext_lib_fn_names_local,
)
# print("this is map_entry",map_entry)
ext_fns = ""
if func.addr in ext_lib_fn_names_local:
ext_fns = str(ext_lib_fn_names_local[func.addr])
else:
ext_fns = ""
if mode != "pre":
if (
unstr_df.isin([func.addr]).any().any()
and not isUnstripped
):
unstr_df.loc[
unstr_df.addr == func.addr,
[
"strRefs",
"extlibs",
"embed_O",
"embed_T",
"embed_A",
],
] = [
str(map_entry[1]).replace("\n", ""),
ext_fns.replace("\n", ""),
str(map_entry[2]).replace("\n", ""),
str(map_entry[3]).replace("\n", ""),
str(map_entry[4]).replace("\n", ""),
]
vec = (
map_entry[0]
+ str(map_entry[1]).replace("\n", "")
+ "\t"
+ ext_fns.replace("\n", "")
+ "\t"
+ str(map_entry[2]).replace("\n", "")
+ "\t"
+ str(map_entry[3]).replace("\n", "")
+ "\t"
+ str(map_entry[4]).replace("\n", "")
+ "\n"
)
ofile.write(vec)
except KeyError:
continue
ofile.write("\n")
ofile.close()
if not isUnstripped and mode != "pre":
unstr_df = unstr_df[
unstr_df["embed_T"].notnull()
] # Use `notnull()` for better pandas practice
unstr_df.reset_index(drop=True, inplace=True)
return
else:
for func in reversed_func_list:
if isUnstripped:
break
try:
# Ensure `func.addr` exists in `isCalled` or `edges`
if func.addr in isCalledd or func.addr in edgess:
if func.addr not in func_vec_map.keys():
continue
# Call the addCalleeVecs function
map_entry, ext_lib_fn_names_global = addCalleeVecs(
func.addr,
edgess.get(
func.addr, []
), # Safely get edges for `func.addr`
func_vec_map,
ext_lib_fn_names_global,
)
ext_fns = ""
if func.addr in ext_lib_fn_names_global:
ext_fns = str(ext_lib_fn_names_global[func.addr])
else:
ext_fns = ""
vec = (
map_entry[0]
+ str(map_entry[1]).replace("\n", "")
+ "\t"
+ ext_fns.replace("\n", "")
+ "\t"
+ str(map_entry[2]).replace("\n", "")
+ "\t"
+ str(map_entry[3]).replace("\n", "")
+ "\t"
+ str(map_entry[4]).replace("\n", "")
+ "\n"
)
ofile.write(vec)
except KeyError:
continue
ofile.write("\n")
ofile.close()
if not isUnstripped and mode != "pre":
unstr_df = unstr_df[
unstr_df["embed_T"].notnull()
] # Use `notnull()` for better pandas practice
unstr_df.reset_index(drop=True, inplace=True)
return
def loadBinary(binary, args):
if args.mode == "db":
print("\033[31mDB Flow Activated\033[0m")
global str_fn_embs, ext_lib_fn_names, isCalled, edges, cgv_nodes
isUnstripped = False
if "unstripped" in binary:
isUnstripped = True
META_DB_PATH = os.path.abspath(args.embedding_db_path)
engine = db.create_engine(f"sqlite:///{META_DB_PATH}")
session = sessionmaker(bind=engine)
connection = session()
# print("I have gone here")
# print(args.dump_dataset)
# exit(0)
if args.dump_dataset:
binary_db = binary.replace("stripped", DB_PATH).replace(".out", ".db")
# print(binary_db)
# exit(0)
proj_db = AngrDB()
proj = proj_db.load(binary_db)
db_bin_key = "_".join(binary.split("/")[-4:])
if isUnstripped:
result = (
connection.query(binaryMetaDataUnstripped)
.filter(binaryMetaDataUnstripped.id == db_bin_key)
.all()
)
# print(result)
addr_to_line = pickle.loads(result[0].addr_to_line)
cgv_nodes = pickle.loads(result[0].cgv_nodes)
else:
result = (
connection.query(binaryMetaData)
.filter(binaryMetaData.id == db_bin_key)
.all()
)
ext_lib_fn_names = pickle.loads(result[0].ext_lib_functions)
str_fn_embs = pickle.loads(result[0].string_func_embedding)
isCalled = pickle.loads(result[0].isCalled)
edges = pickle.loads(result[0].func_callee_edges)
addr_to_line = None
cgv_nodes = pickle.loads(result[0].cgv_nodes)
else:
binary_db = binary.replace("stripped", DB_PATH).replace(".out", ".db")
proj_db = AngrDB()
proj = proj_db.load(binary_db)
if args.output:
outFile = args.output
elif args.output_dir_name:
if args.dump_freq:
outputDir = os.path.dirname(binary).replace(
"Datafiles", args.output_dir_name
)
if not os.path.exists(outputDir):
os.makedirs(outputDir, exist_ok=True)
outFile = os.path.join(
outputDir, os.path.basename(binary)[:-4] + ".pickle"
)
else:
outputDir = os.path.join(
os.path.dirname(os.path.dirname(binary)), args.output_dir_name
)
if not os.path.exists(outputDir):
os.mkdir(outputDir)
outFile = os.path.join(
outputDir, os.path.basename(binary)[:-4] + ".data"
)
if os.path.exists(outFile):
print(outFile, " already exists")
return
if not isUnstripped:
unstr_file = os.path.abspath(outFile.replace("stripped", "unstripped"))
if not os.path.exists(unstr_file):
print("Unstripped Counter Part not present, Generate it first. Exiting")
exit(0)
global unstr_df
col_names_unstr = ["addr", "key", "fnName", "embed_unst"]
unstr_df = pd.read_csv(
unstr_file, sep="\t", names=col_names_unstr, header=None
)
unstr_df = unstr_df[unstr_df["key"].str.contains("NO_FILE_SRC") == False]
unstr_df.reset_index(drop=True, inplace=True)
unstr_df = unstr_df[unstr_df["fnName"].str.startswith("sub_") == False]
unstr_df.reset_index(drop=True, inplace=True)
unstr_df.drop("embed_unst", axis=1, inplace=True)
unstr_df.key = unstr_df.key + unstr_df.fnName
unstr_df.drop("fnName", axis=1, inplace=True)
unstr_df = unstr_df.assign(
strRefs=None, extlibs=None, embed_O=None, embed_T=None, embed_A=None
)
print("Processing", binary)
if args.dump_dataset:
status = processBinary(
proj,
addr_to_line,
outFile,
args.vocabulary,
args.enable_cfg,
args.dump_triplets,
isUnstripped,
args.func_chunks,
args.dump_cfg,
args.normalize,
)
else:
status = processBinary(
proj,
None,
outFile,
args.vocabulary,
args.enable_cfg,
args.dump_triplets,
isUnstripped,
args.func_chunks,
args.dump_cfg,
args.normalize,
)
if status == -1:
print("Angr Error:", binary)
if not isUnstripped:
unstr_df.to_csv(outFile.replace(".data", ".csv"), sep="\t")
if args.mode == "non-db":
print("\033[31mNon-DB Flow Activated\033[0m")
if args.dump_dataset:
proj = angr.Project(binary, auto_load_libs=False, load_debug_info=True)
addr_to_line = proj.loader.main_object.addr_to_line
else:
proj = angr.Project(binary, auto_load_libs=False)
symbols = proj.loader.symbols
isUnstripped = False
if "unstripped" in binary:
isUnstripped = True
if args.output:
outFile = args.output
elif args.output_dir_name:
if args.dump_freq:
outputDir = os.path.dirname(binary).replace(
"Datafiles", args.output_dir_name
)
if not os.path.exists(outputDir):
os.makedirs(outputDir, exist_ok=True)
outFile = os.path.join(
outputDir, os.path.basename(binary)[:-4] + ".pickle"
)
else:
outputDir = os.path.join(
os.path.dirname(os.path.dirname(binary)), args.output_dir_name
)
if not os.path.exists(outputDir):
os.mkdir(outputDir)
outFile = os.path.join(
outputDir, os.path.basename(binary)[:-4] + ".data"
)
if os.path.exists(outFile):
print(outFile, " already exists")
return
print("Processing", binary)
if args.dump_dataset:
status = processBinary(
proj,
addr_to_line,
outFile,
args.vocabulary,
args.enable_cfg,
args.dump_triplets,
isUnstripped,
args.func_chunks,
args.dump_cfg,
args.normalize,
"pre",
)
else:
status = processBinary(
proj,
None,
outFile,
args.vocabulary,
args.enable_cfg,
args.dump_triplets,
isUnstripped,
args.func_chunks,
args.dump_cfg,
args.normalize,
"pre",
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Tool to generate VEXIR2Vec representations"
)
group_input = parser.add_mutually_exclusive_group(required=True)
group_output = parser.add_mutually_exclusive_group(required=True)
group_input.add_argument("-b", "--binary", type=str, help="Input binary")
group_input.add_argument(
"-b_dir",
"--binary_dir",
type=str,
help="Directory containing input binaries or subdirectories of input binaries",
)
parser.add_argument(
"-threads",
"--num_threads",
type=int,
default=1,
help="Number of parallel Pool workers",
)
parser.add_argument(
"-fchunks",
"--func_chunks",
type=int,
default=32,
help="Number of function chunks to be processed in parallel",
)
group_output.add_argument("-o", "--output", type=str, help="Output file path")
group_output.add_argument(
"-o_dir", "--output_dir_name", type=str, help="Output directory name"
)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
"-t",
"--dump-triplets",
dest="dump_triplets",
required=False,
default=False,
action="store_true",
help="Print triplets to the file",
)
group.add_argument("-v", "--vocabulary", help="path to the vocabulary")
parser.add_argument(
"-d",
"--dump-dataset",
dest="dump_dataset",
required=False,
default=False,
action="store_true",
help="Print dataset to the file",
)
group_freq = parser.add_mutually_exclusive_group(required=False)
parser.add_argument(
"-cfg",
"--enable-cfg",
dest="enable_cfg",
required=False,
default=False,
action="store_true",
help="Generate data with cfg edges",
)
parser.add_argument(
"-dc",
"--dump-cfg",
dest="dump_cfg",
required=False,
default=False,
action="store_true",
help="Dump only fn address and adj matrix coresponding to its cfg",
)
parser.add_argument(
"-n",
"--normalize",
type=int,
default=1,
help="The normalisation",
)
parser.add_argument(
"-w",
"--bb-freq-weight",
dest="bb_freq_weight",
required=False,
help="Path to frequency json to add weights to embeddings",
)
parser.add_argument(
"-edb",
"--embedding-db",
dest="embedding_db_path",
type=str,
required=False,
help="Path to DB containing extlib and string embeddings",
)
parser.add_argument(
"-mode",
dest="mode",
type=str,
required=False,
help="Specify the mode as a string",
)
args = parser.parse_args()
if args.bb_freq_weight:
with open(args.bb_freq_weight, "rb") as f:
freq_weight_dict = pickle.load(f)