-
Notifications
You must be signed in to change notification settings - Fork 2
/
bcd.py
executable file
·393 lines (302 loc) · 8.93 KB
/
bcd.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
#!/usr/bin/env python3
import sys,re
import getopt
from glob import glob
import os
import shutil
import time
import tempfile
import subprocess
import json
import pprint
import pickle
from datasketch import MinHash, LeanMinHash
import itertools
start = time.time()
'''
schema of dictionary db:
<hash:List[filename_funcname,..]>
'''
MINHASHDB = {}
def debug(*args, **kwargs):
if VERBOSE:
print(*args,file=sys.stderr, **kwargs)
def elog(*args, **kwargs):
print(*args,file=sys.stderr, **kwargs)
def usage():
elog("usage:\n%s <-i/-s> [options] <path to binary or .ll files> .."%sys.argv[0] )
# print("action can include extract, tokenize, minhash, ssdeep, ssdeep_ll, simhash, simhash_ft compare, compare_ll, confusion_matrix")
elog('''
arguments:
-s : search mode, lookup similar functions
-i : index mode, indexes binaries/ll files into db pickle
-f path_to_pickle : path to pickle file of bcd
-p permutations : number of permutations for minhash
-t threshold : threshold for matching in minhash and simhash (e.g 0.5 for minhash, 10 for simhash)
-v : verbose debugging messages
''')
def tokenize(instruction):
'''
takes an llvm IR instruction and returns a list of string tokens
'''
tokens = instruction.split()
result_tokens = []
intsizes = ['i4', 'i8', 'i16', 'i32', 'i64',
'u4', 'u8', 'u16', 'u32', 'u64']
# when a token starts with a shoterner, truncate it to the shortener.
shorteners = ['%stack_var', '%dec_label', '%global', '@global']
for i in range(len(tokens)):
# run replacement rules
t = tokens[i]
replaced = False
for s in shorteners:
if t.startswith(s):
debug(f'replacing {t} with {s}')
result_tokens.append(s)
replaced = True
break
if replaced:
continue
elif t[:3] in intsizes:
debug(f'dropping {t}')
continue
elif t.startswith('%') and not ("(" in t):
# generic variable reference
newt = '%r'
debug(f'replacing {t} with {newt}')
result_tokens.append(newt)
elif t == '!insn.addr': # stop processing
break
else:
newt = t
for it in intsizes:
newt = newt.replace(it, '')
# newt = t.replace()
result_tokens.append(newt)
# can use lookahead to determine nature of token
if result_tokens != []:
#result_tokens.append(";")
debug(result_tokens)
return result_tokens # signify end of instruction
return None
def extract_functions_retdecLL(filepath):
'''
extract functions from retdec LLVM IR
return a dictionary of funcname:funccode?
'''
# function regex for llvm ir from retdec
func_re = r'define .* (@.*){\n'
pattern = re.compile(func_re)
with open(filepath) as f:
data = f.read()
debug(f"[extract_functions_retdecLL] done reading {filepath} into mem..")
res = {}
r = pattern.search(data)
prev = None
count = 0
skipCount = 0
# the goal is to dump out the entire block, by reading from end of last label match to start of current match
while r:
# print the match
# print(r.group())
# read until end of function (marked by '}')
funcEnd = data[r.start():].find('}')
# debug(f"start: {r.start()} funcEnd:{funcEnd}")
funcCode = data[r.start():r.start() + funcEnd] + '}'
fheader = funcCode.split('{')[0]
fname = fheader.split('(')[0].split(' ')[-1]
if res.get(fname) != None:
elog(f"duplicate function f{fname}")
res[fname] = funcCode
r = pattern.search(data, r.start() + 1)
count += 1
if skipCount > 0:
debug(f"skipped {skipCount} functions")
return res
def lift(binaryPath):
# if this program from retdec is not in your path, use full path
# install from https://github.com/avast/retdec
retdecDecompilerPath = "retdec-decompiler"
# make temp directory and copy file over
tmpd = tempfile.mkdtemp(prefix="tmp-"+os.path.basename(binaryPath)+'_', dir='./tmp')
newbin = shutil.copy(binaryPath, tmpd)
# decompile
if VERBOSE:
os.system(f"{retdecDecompilerPath} {newbin}")
else:
os.system(f"{retdecDecompilerPath} {newbin} >/dev/null")
# remove copied bin
os.remove(newbin)
llFile = f"{newbin}.ll"
if not os.path.exists(llFile):
elog("error - lifted LL file not found")
exit(2)
# import code
# code.interact(local=locals())
# exit(1)
return llFile
def getTuple1(t):
''''
return 1st (0 indexed) element of a tuple
'''
return t[1]
def lookupPath(path, db=MINHASHDB):
'''
decompile a binary (or all binaries in a directory), calculate hashes for each function and then look it up in the database
'''
if os.path.isdir(path):
dirpath = path
for i in os.walk(dirpath):
files = i[2]
for file in files:
filepath = os.path.join(dirpath, file)
# print(path)
lookupPath(filepath)
# lift binary using retdec
if path.endswith('.ll'):
llpath = path
else:
llpath = lift(path)
functions = extract_functions_retdecLL(llpath)
# os.remove(llpath)
lstart = time.time()
# schema: funcname:[(filefunc, match_score)]
matches = {}
# get the minhash values of each
for fname in functions:
functokens = tokenize(functions[fname])
# using LeanMinHash because the pipeline does, to be consistent
m = MinHash(num_perm=MINHASH_PERMS)
for t in functokens:
m.update(t.encode('utf8'))
# m.update(t)
lm = LeanMinHash(m)
hashvals = lm.hashvalues
# print(f'{fname}:{hashvals}')
# for each function, find all similar functions in the db (each function would be O(64) for 64 hash lookups)
# funcname: hash match
hashcounts = {}
for h in hashvals:
if db.get(h) == None: # no match
continue
for filefunc in db.get(h):
if hashcounts.get(filefunc) == None:
hashcounts[filefunc] = 0
hashcounts[filefunc] += 1
for filefunc in hashcounts:
score = hashcounts[filefunc] / MINHASH_PERMS
if score >= THRESHOLD:
if matches.get(fname) == None:
matches[fname] = []
matches[fname].append((filefunc, score))
# pprint.pprint(matches, indent=2)
# rank results based on score
for function_key in matches:
matches[function_key].sort(key=getTuple1, reverse=True)
elog("lookupPath took", (time.time() - lstart))
return matches
def indexPath(path, db=MINHASHDB):
'''
decompile a binary (or all binaries in a directory), calculate hashes for each function and then store it in the database
'''
global MINHASHDB
if os.path.isdir(path):
dirpath = path
for i in os.walk(dirpath):
files = i[2]
for file in files:
filepath = os.path.join(dirpath, file)
# print(path)
indexPath(filepath)
# lift binary using retdec
if path.endswith('.ll'):
llpath = path
else:
llpath = lift(path)
functions = extract_functions_retdecLL(llpath)
# os.remove(llpath)
lstart = time.time()
# schema: funcname:[(filefunc, match_score)]
matches = {}
# get the minhash values of each
for fname in functions:
functokens = tokenize(functions[fname])
# using LeanMinHash because the pipeline does, to be consistent
m = MinHash(num_perm=MINHASH_PERMS)
for t in functokens:
m.update(t.encode('utf8'))
# m.update(t)
lm = LeanMinHash(m)
hashvals = lm.hashvalues
# print(f'{fname}:{hashvals}')
# for each function, find all similar functions in the db (each function would be O(64) for 64 hash lookups)
# funcname: hash match
hashcounts = {}
filename_funcname = os.path.basename(path) + ":" + fname
for h in hashvals:
if db.get(h) == None:
db[h] = set()
# if filename_funcname not in db[h]
elif type(db.get(h)) == list:
# convert entry to set if its a list (old version)
db[h] = set(db[h])
db[h].add(filename_funcname)
print("indexPath took", (time.time() - lstart))
MINHASH_PERMS = 64
THRESHOLD = 0.5
VERBOSE = False
PICKLEFILE = 'db_dict.pkl'
# OUTPUT_DBPATHS = {'extract':'ll_extract.db', 'tokenize':'tokens.db', 'hash':'hashes.db'}
MINHASH_PERMS = 64
MODE = 'lookup'
# main
if __name__ == '__main__':
funcNames = None
opts, args = getopt.gnu_getopt(sys.argv[1:], 'hvisd:a:t:p:f:')
for tup in opts:
o,a = tup[0], tup[1]
if o == '-h':
usage()
exit(0)
elif o == '-i':
MODE = 'index'
elif o == '-s':
MODE = 'lookup'
elif o == '-f':
PICKLEFILE = a
elif o == '-p':
MINHASH_PERMS = int(a)
# elif o == '-a':
# ALGO = a
elif o == '-v':
VERBOSE = True
elif o == '-t':
THRESHOLD = float(a)
if len(args) < 1:
print('missing path to file.')
usage()
exit(1)
allfilefuncs = set()
if not os.path.exists(PICKLEFILE):
MINHASHDB = {}
else:
with open(PICKLEFILE,'rb') as f:
MINHASHDB = pickle.load(f)
elog(f"finished loading db dictionary, elapsed {time.time() - start}")
elog(f"hashes in db: {len(MINHASHDB)}")
for targetpath in args:
if MODE == 'lookup':
if not os.path.exists(PICKLEFILE):
elog("no db pickle file specified, can't do lookup")
exit(1)
matches = lookupPath(targetpath, MINHASHDB)
print(json.dumps(matches, indent=2))
elif MODE == 'index':
indexPath(targetpath, MINHASHDB)
elog(f"hashes in db after indexing: {len(MINHASHDB)}")
with open(PICKLEFILE,'wb') as f:
pickle.dump(MINHASHDB, f)
elog(f"updated db at {PICKLEFILE}")
elog("elapsed:", time.time() - start)
#import code
#code.interact(local=locals())