-
Notifications
You must be signed in to change notification settings - Fork 59
/
run_yextend
executable file
·463 lines (373 loc) · 18.5 KB
/
run_yextend
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
#!/usr/bin/env python
"""
run statement:
./run_yextend -r rule_entity -t target_file_entity [-j]
I label them as 'entity' because they can each be
either a directory or a file
"""
import subprocess
import sys
import os
import fnmatch
import time
import getopt
import json
from types import *
from collections import OrderedDict
########################################################
def usage():
print "\n{}\n".format("Usage:")
print "{} {} {} {}\n".format("./run_yextend", "-r rule_entity", "-t target_file_entity", "[-j]")
print " {}".format("-r RULES_FILE = Yara ruleset file [*required]")
print " {}".format("-t TARGET_ENTITY = file or directory [*required]")
print " {}".format("-j output in JSON format and nothing more [optional]")
print "".format("\n\n")
def get_file_list(thedir='', thepattern='*'):
if not thedir.endswith('/'):
thedir = thedir + '/'
f_list = []
if thedir:
for root, dirnames, filenames in os.walk(thedir):
for name in fnmatch.filter(filenames, thepattern):
complete_fname = "{}{}".format(root, name)
if os.path.isfile(complete_fname):
if complete_fname not in f_list:
f_list.append(complete_fname)
return f_list
def my_print(thestr='', thecolor=''):
if thestr:
format_str = '\033[1;{}m{}\033[1;m'
if thecolor == 'gray':
return format_str.format('30', thestr)
elif thecolor == 'red':
return format_str.format('31', thestr)
elif thecolor == 'green':
return format_str.format('32', thestr)
elif thecolor == 'yellow':
return format_str.format('33', thestr)
elif thecolor == 'blue':
return format_str.format('34', thestr)
elif thecolor == 'magenta':
return format_str.format('35', thestr)
elif thecolor == 'cyan':
return format_str.format('36', thestr)
elif thecolor == 'white':
return format_str.format('37', thestr)
elif thecolor == 'crimson':
return format_str.format('38', thestr)
elif thecolor == 'highlighted_red':
return format_str.format('41', thestr)
elif thecolor == 'highlighted_red':
return format_str.format('42', thestr)
elif thecolor == 'highlighted_brown':
return format_str.format('43', thestr)
elif thecolor == 'highlighted_blue':
return format_str.format('44', thestr)
elif thecolor == 'highlighted_magenta':
return format_str.format('45', thestr)
elif thecolor == 'highlighted_cyan':
return format_str.format('46', thestr)
elif thecolor == 'highlighted_gray':
return format_str.format('47', thestr)
elif thecolor == 'highlighted_crimson':
return format_str.format('48', thestr)
def which(program=""):
def is_exe(fpath):
return os.path.exists(fpath) and os.access(fpath, os.X_OK)
def ext_candidates(fpath):
yield fpath
for ext in os.environ.get("PATHEXT", "").split(os.pathsep):
yield fpath + ext
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
tarr = os.environ["PATH"].split(os.pathsep)
tarr.append("/sbin")
tarr.append("./")
for path in tarr:
exe_file = os.path.join(path, program)
for candidate in ext_candidates(exe_file):
if is_exe(candidate):
return candidate
return None
def print_yextend_output(out=''):
if out:
for the_out in out.split('\n'):
if the_out.startswith('='):
print my_print(thestr=the_out, thecolor='white')
elif the_out.startswith("Yara "):
# print the_out
t_o = the_out.split(': ')
print "{}: ".format(my_print(thestr=t_o[0], thecolor='white'))
for rule_data in t_o[1].split(', '):
if ':[' in rule_data:
rule_id, rule_meta = rule_data.split(':[')
rule_meta = rule_meta[:-1]
print "\tRule ID: {}".format(my_print(thestr=rule_id, thecolor='red'))
print "\tRule META:"
for rmeta in rule_meta.split(','):
rmeta = rmeta.replace('=', ' = ')
if rmeta.startswith('detected'):
rm_label, rm_dat = rmeta.split(' = ')
print "\t\t{}".format(my_print(thestr=rm_label + ':', thecolor='blue'))
rm_dat_spl = rm_dat.split('-')
for rmdspl in rm_dat_spl:
try:
rmdspl_offset, rmdspl_label = rmdspl.strip().split(':')
except ValueError:
pass
rmdspl_out = "{} at {}".format(my_print(thestr=rmdspl_label, thecolor='red'), my_print(rmdspl_offset, thecolor='red'))
print "\t\t\t{}".format(my_print(thestr=rmdspl_out, thecolor='blue'))
else:
print "\t\t{}".format(my_print(thestr=rmeta, thecolor='blue'))
else:
if rule_data == "No hit from Yara ruleset":
print "\t{}".format(my_print(thestr=rule_data, thecolor='red'))
else:
print "\tRule ID: {}".format(my_print(thestr=rule_data, thecolor='red'))
print
elif the_out == '\n':
print the_out
else:
try:
t_o = the_out.split(': ')
print "{}: {}".format(my_print(thestr=t_o[0], thecolor='white'), my_print(thestr=t_o[1], thecolor='green'))
except:
def_out = my_print(thestr=the_out, thecolor='white')
if def_out:
print my_print(thestr=the_out, thecolor='white')
else:
print the_out
def print_yextend_json_output(out=''):
if out:
try:
string_to_json = json.loads(out)
for rule_file in string_to_json:
print '\n{}'.format(my_print(thestr='===============================ALPHA===================================', thecolor='white'))
print '{} : {}'.format(my_print(thestr="File Name", thecolor='white'), my_print(thestr=rule_file['file_name'], thecolor='green'))
print '{} : {}'.format(my_print(thestr="File Size", thecolor='white'), my_print(thestr=rule_file['file_size'], thecolor='green'))
print '{} : {}'.format(my_print(thestr="File Signature (MD5)", thecolor='white'), my_print(thestr=rule_file['file_signature_MD5'], thecolor='green'))
if 'yara_ruleset_file_name' in rule_file.keys():
print '{} : {}'.format(my_print(thestr="Yara Ruleset File Name", thecolor='white'), my_print(thestr=rule_file['yara_ruleset_file_name'], thecolor='green'))
print '\n{}\n'.format(my_print(thestr='=======================================================================', thecolor='white'))
print my_print(thestr='Yara Result(s):', thecolor='white')
if ('scan_results' in rule_file) and (rule_file['scan_results'] is not None):
for result in rule_file['scan_results']:
res_keys = result.keys()
if result['yara_matches_found']:
print '\tRule ID: {}'.format(my_print(thestr=result['yara_rule_id'], thecolor='red'))
print '\tRule META:'
for key, value in result.iteritems():
if key == 'detected offsets':
constructed_out = key + ':'
print '\t\t{}'.format(my_print(thestr=constructed_out, thecolor='blue'))
for detect in value:
if (":") in detect:
offset,label = detect.split(':')
else:
offset = detect
label = "UNK"
constructed_out = '{} at {}'.format(my_print(thestr=label, thecolor='red'), my_print(offset, thecolor='red'))
print '\t\t\t{}'.format(my_print(thestr=constructed_out, thecolor='blue'))
else:
if key not in ['scan_type', 'file_name', 'file_signature_MD5', 'yara_rule_id', 'yara_matches_found']:
constructed_pair = key + ' = ' + str(value)
print '\t\t{}'.format(my_print(thestr=constructed_pair, thecolor='blue'))
if len(result) <= 2:
print '\t\t{}\n'.format('None')
if 'scan_type' in res_keys:
print '\n{} : {}'.format(my_print(thestr="Scan Type", thecolor='white'), my_print(thestr=result['scan_type'], thecolor='green'))
if 'file_name' in res_keys:
print '{} : {}'.format(my_print(thestr="File Name", thecolor='white'), my_print(thestr=result['file_name'], thecolor='green'))
if 'child_file_name' in res_keys:
print '{} : {}'.format(my_print(thestr="Child File Name", thecolor='white'), my_print(thestr=result['child_file_name'], thecolor='green'))
if 'parent_file_name' in res_keys:
print '{} : {}'.format(my_print(thestr="Parent File Name", thecolor='white'), my_print(thestr=result['parent_file_name'], thecolor='green'))
if 'file_size' in res_keys:
print '{} : {}'.format(my_print(thestr="File Size", thecolor='white'), my_print(thestr=result['file_size'], thecolor='green'))
if 'file_signature_MD5' in res_keys:
print '{} : {}\n'.format(my_print(thestr="File Signature (MD5)", thecolor='white'), my_print(thestr=result['file_signature_MD5'], thecolor='green'))
else:
print '\tRule ID: {}\n'.format('None')
if 'scan_type' in res_keys:
print '{} : {}'.format(my_print(thestr="Scan Type", thecolor='white'), my_print(thestr=result['scan_type'], thecolor='green'))
if 'non_archive_file_name' in res_keys:
print '{} : {}'.format(my_print(thestr="Non-Archive File Name", thecolor='white'), my_print(thestr=result['non_archive_file_name'], thecolor='green'))
if 'file_name' in res_keys:
print '{} : {}'.format(my_print(thestr="File Name", thecolor='white'), my_print(thestr=result['file_name'], thecolor='green'))
if 'parent_file_name' in res_keys:
print '{} : {}'.format(my_print(thestr="Parent File Name", thecolor='white'), my_print(thestr=result['parent_file_name'], thecolor='green'))
if 'child_file_name' in res_keys:
print '{} : {}'.format(my_print(thestr="Child File Name", thecolor='white'), my_print(thestr=result['child_file_name'], thecolor='green'))
if 'file_size' in res_keys:
print '{} : {}'.format(my_print(thestr="File Size", thecolor='white'), my_print(thestr=result['file_size'], thecolor='green'))
if 'file_signature_MD5' in res_keys:
print '{} : {}\n'.format(my_print(thestr="File Signature (MD5)", thecolor='white'), my_print(thestr=result['file_signature_MD5'], thecolor='green'))
print '{}\n'.format(my_print(thestr='===============================OMEGA===================================', thecolor='white'))
except:
# when file can't be read
t_o = out.split(': ')
print '{}: {}'.format(my_print(thestr=t_o[0], thecolor='white'), my_print(thestr=t_o[1].strip("null"), thecolor='green'))
def format_child(child):
if "yara_rule_id" in child:
rule_id = child["yara_rule_id"]
try:
yara_results = {rule_id: {"description":""}}
if "description" in child:
yara_results[rule_id]["description"] = child["description"]
del child["description"]
if "detected offsets" in child:
yara_results[rule_id]["offsets"] = child["detected offsets"]
del child["detected offsets"]
if "hit_count" in child:
yara_results[rule_id]["hit_count"] = child["hit_count"]
del child["hit_count"]
del child["yara_rule_id"]
child["yara_results"] = yara_results
except Exception, e:
print e
else:
# clean out empty k/v pairs
for k,v in child.items():
if v == "":
del child[k]
return child
def advanced_print(out="", ruleset_name=""):
scan_results_key = 'scan_results'
out = json.loads(out)
r_final = []
j_final = []
files_level1 = {}
for file_dict in out:
files = []
new_files = dict(file_dict)
del new_files[scan_results_key]
files.append(format_child(new_files))
for child in file_dict[scan_results_key]:
child_keys = child.keys()
new_child = dict(child)
if "child_file_name" in child_keys:
new_child["file_name"] = new_child.pop("child_file_name")
child["file_name"] = child.pop("child_file_name")
for f in reversed(files):
f_keys = f.keys()
if "file_name" in f_keys:
if f["file_name"] == child["parent_file_name"]:
try:
f["children"].append(format_child(new_child))
except:
f["children"] = [format_child(new_child)]
del child["parent_file_name"]
del new_child["parent_file_name"]
files.append(format_child(child))
elif "non_archive_file_name" in child_keys:
new_child["file_name"] = new_child.pop("non_archive_file_name")
try:
files[0]["children"].append(format_child(new_child))
except:
files[0]["children"] = [format_child(new_child)]
else:
try:
files[0]["children"].append(format_child(new_child))
except:
files[0]["children"] = [format_child(new_child)]
for x in files:
if "children" in x:
if x not in j_final:
j_final.append(x)
#r_final.append(x)
if len(j_final) > 0:
files_level1.update(j_final[0])
files_level1[scan_results_key] = []
cnt = 0
for i in j_final:
if cnt > 0:
files_level1[scan_results_key].append(j_final[cnt])
cnt = cnt + 1
r_final.append(files_level1)
return r_final
########################################################
RULES_ENTITY = ''
FILE_ENTITY = ''
RULES_ENTITY_DIR = False
YEXTEND = which(program="yextend")
USRLOCLIB = "{}".format("\/usr\/local\/lib")
try:
proc = subprocess.Popen([YEXTEND],
env={LD_LIBRARY_PATH:USRLOCLIB},
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
out, err = proc.communicate()
except:
USRLOCLIB = "{}".format("/usr/local/lib")
LD_LIBRARY_PATH = "{}".format("LD_LIBRARY_PATH")
JSON_OUT = False
ADVANCED = False
try:
options, rem = getopt.getopt(sys.argv[1:], 'r:t:ja', ['ruleset=','target='])
except getopt.GetoptError as err:
print str(err)
usage()
sys.exit()
for opt, arg in options:
if opt in ('-r', '--ruleset'):
RULES_ENTITY = "{}".format(arg)
elif opt in ('-t', '--target'):
FILE_ENTITY = "{}".format(arg)
elif opt in ('-j'):
JSON_OUT = True
elif opt in ('-a'):
ADVANCED = True
else:
assert False, "unhandled option"
if os.path.isdir(RULES_ENTITY):
RULES_ENTITY_DIR = True
if not RULES_ENTITY or not FILE_ENTITY:
usage()
sys.exit()
'''
we will force the JSON option to yextend
as the default run mode going forward
function 'print_yextend_json_output' presents
the results of a run to stdout the same way
we always have except that under the hood it
is transforming the JSON output from yextend
instead of transforming the legacy output
format (i.e. non-JSON)
'''
JSON_OUT_FORCE = "-j"
if JSON_OUT:
JSON_OUT = "-j"
else:
JSON_OUT = ""
ruleset_pool = []
''' ruleset = file '''
if not RULES_ENTITY_DIR:
ruleset_pool.append(RULES_ENTITY)
else:
for r in get_file_list(thedir=RULES_ENTITY):
ruleset_pool.append(r)
for lpol in ruleset_pool:
out = ""
err = ""
proc = subprocess.Popen([YEXTEND, "-r", lpol, "-t", FILE_ENTITY, JSON_OUT_FORCE],
env={LD_LIBRARY_PATH:USRLOCLIB},
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
out, err = proc.communicate()
if out:
if JSON_OUT:
if ADVANCED:
print json.dumps(advanced_print(out=out, ruleset_name=lpol), indent=4)
else:
#print_yextend_json_output(out=out)
print out
else:
#print_yextend_output(out=out)
print_yextend_json_output(out=out)
if err:
print "ERR: %s" % err