This repository has been archived by the owner on Jun 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
elf.py
574 lines (451 loc) · 20.2 KB
/
elf.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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# -*- coding: utf-8 -*-
# This file is part of Viper - https://github.com/viper-framework/viper
# See the file 'LICENSE' for copying permission.
import os
import math
from io import BytesIO, open
try:
from elftools.elf.elffile import ELFFile
from elftools.elf.sections import SymbolTableSection
from elftools.elf.descriptions import describe_sh_flags
from elftools.elf.descriptions import describe_p_flags
from elftools.elf.descriptions import describe_symbol_type
from elftools.elf.dynamic import DynamicSection
HAVE_ELFTOOLS = True
except ImportError:
HAVE_ELFTOOLS = False
from viper.common.out import bold
from viper.common.abstracts import Module
from viper.core.session import __sessions__
from viper.core.database import Database
from viper.core.storage import get_sample_path
# Have a look at scripts/readelf.py - pyelftools
class ELF(Module):
cmd = 'elf'
description = 'Extract information from ELF headers'
authors = ['emdel']
categories = ["linux"]
def __init__(self):
super(ELF, self).__init__()
subparsers = self.parser.add_subparsers(dest='subname')
subparsers.add_parser('sections', help='List ELF sections')
subparsers.add_parser('segments', help='List ELF segments')
subparsers.add_parser('symbols', help='List ELF symbols')
subparsers.add_parser('interpreter', help='Get the program interpreter')
subparsers.add_parser('dynamic', help='Show the dynamic section')
parser_ep = subparsers.add_parser('entrypoint', help='Show the ELF entry point')
parser_ep.add_argument('-a', '--all', action='store_true', help='Print the entry point of all files in the project')
parser_ep.add_argument('-c', '--cluster', action='store_true', help='Cluster all files in the project')
parser_ep.add_argument('-s', '--scan', action='store_true', help='Scan repository for matching samples')
parser_machine = subparsers.add_parser('machine', help='Show the ELF machine')
parser_machine.add_argument('-a', '--all', action='store_true', help='Print the machine for all files in the project')
parser_machine.add_argument('-c', '--cluster', action='store_true', help='Cluster all files in the project')
parser_machine.add_argument('-s', '--scan', action='store_true', help='Scan repository for matching samples')
parser_type = subparsers.add_parser('type', help='Show the ELF type')
parser_type.add_argument('-a', '--all', action='store_true', help='Print the type of all files in the project')
parser_type.add_argument('-c', '--cluster', action='store_true', help='Cluster all files in the project')
parser_type.add_argument('-s', '--scan', action='store_true', help='Scan repository for matching samples')
parser_ent = subparsers.add_parser('entropy', help='Show the ELF entropy')
parser_ent.add_argument('-a', '--all', action='store_true', help='Print the entropy of all files in the project')
parser_ent.add_argument('-c', '--cluster', action='store_true', help='Calculate and cluster all files in the project')
parser_ent.add_argument('-s', '--scan', action='store_true', help='Scan repository for matching samples')
self.elf = None
def __check_session(self):
if not __sessions__.is_set():
self.log('error', "No open session. This command expects a file to be open.")
return False
if not self.elf:
try:
self.elf = ELFFile(BytesIO(__sessions__.current.file.data))
except Exception as e:
self.log('error', "Unable to parse ELF file: {0}".format(e))
return False
return True
def segments(self):
if not self.__check_session():
return
rows = []
for segment in self.elf.iter_segments():
rows.append([
segment['p_type'],
segment['p_vaddr'],
hex(segment['p_filesz']),
hex(segment['p_memsz']),
describe_p_flags(segment['p_flags']),
self.get_entropy(segment.data())
])
self.log('info', "ELF Segments:")
self.log('table', dict(header=['Type', 'VirtAddr', 'FileSize', 'MemSize', 'Flags', 'Entropy'], rows=rows))
def sections(self):
if not self.__check_session():
return
rows = []
# TODO: Add get_entropy in pyelftools sections
for section in self.elf.iter_sections():
rows.append([
section.name,
hex(section['sh_addr']),
hex(section['sh_size']),
section['sh_type'],
describe_sh_flags(section['sh_flags']),
self.get_entropy(section.data())
])
self.log('info', "ELF Sections:")
self.log('table', dict(header=['Name', 'Addr', 'Size', 'Type', 'Flags', 'Entropy'], rows=rows))
def symbols(self):
if not self.__check_session():
return
rows = []
for section in self.elf.iter_sections():
if not isinstance(section, SymbolTableSection):
continue
for cnt, symbol in enumerate(section.iter_symbols()):
rows.append([
cnt,
hex(symbol['st_value']),
hex(symbol['st_size']),
describe_symbol_type(symbol['st_info']['type']),
symbol.name
])
self.log('info', "ELF Symbols:")
self.log('table', dict(header=['Num', 'Value', 'Size', 'Type', 'Name'], rows=rows))
def interpreter(self):
if not self.__check_session():
return
interp = None
for segment in self.elf.iter_segments():
if segment['p_type'] == 'PT_INTERP':
interp = segment
break
if interp:
self.log('', "Program interpreter: {0}".format(interp.get_interp_name()))
else:
self.log('error', "No PT_INTERP entry found")
def dynamic(self):
if not self.__check_session():
return
for section in self.elf.iter_sections():
if not isinstance(section, DynamicSection):
continue
for tag in section.iter_tags():
if tag.entry.d_tag != "DT_NEEDED":
continue
self.log('info', tag.needed)
def entrypoint(self):
if not self.__check_session():
return
ep = self.elf.header.e_entry
self.log('info', "Entry point: {0:#x}".format(ep))
if self.args.scan and self.args.cluster:
self.log('error', "You selected two exclusive options, pick one")
return
if self.args.all:
db = Database()
samples = db.find(key='all')
rows = []
for sample in samples:
sample_path = get_sample_path(sample.sha256)
if not os.path.exists(sample_path):
continue
try:
with open(sample_path, 'rb') as fd:
elfo = ELFFile(fd)
cur_ep = elfo.header.e_entry
except Exception:
continue
rows.append([sample.md5, sample.name, hex(cur_ep)])
self.log('table', dict(header=['MD5', 'Name', 'Entry point'], rows=rows))
return
if self.args.cluster:
db = Database()
samples = db.find(key='all')
cluster = {}
for sample in samples:
sample_path = get_sample_path(sample.sha256)
if not os.path.exists(sample_path):
continue
try:
with open(sample_path, 'rb') as fd:
elfo = ELFFile(fd)
cur_ep = hex(elfo.header.e_entry)
except Exception as e:
self.log('error', "Error {0} for sample {1}".format(e, sample.sha256))
continue
if cur_ep not in cluster:
cluster[cur_ep] = []
cluster[cur_ep].append([sample.md5, sample.name])
for cluster_name, cluster_members in cluster.items():
# Skipping clusters with only one entry.
if len(cluster_members) == 1:
continue
self.log('info', "ELF entry point cluster {0} with {1} elements".format(bold(cluster_name), len(cluster_members)))
self.log('table', dict(header=['MD5', 'Name'], rows=cluster_members))
if self.args.scan:
db = Database()
samples = db.find(key='all')
rows = []
for sample in samples:
if sample.sha256 == __sessions__.current.file.sha256:
continue
sample_path = get_sample_path(sample.sha256)
if not os.path.exists(sample_path):
continue
try:
with open(sample_path, 'rb') as fd:
elfo = ELFFile(fd)
cur_ep = elfo.header.e_entry
except Exception:
continue
if ep == cur_ep:
rows.append([sample.md5, sample.name])
if len(rows) > 0:
self.log('info', "Following are samples with Entry point {0}".format(bold(ep)))
self.log('table', dict(header=['MD5', 'Name'], rows=rows))
def machine(self):
if not self.__check_session():
return
machine = self.elf.header.e_machine
self.log('info', "Machine: {0:s}".format(machine))
if self.args.scan and self.args.cluster:
self.log('error', "You selected two exclusive options, pick one")
return
if self.args.all:
db = Database()
samples = db.find(key='all')
rows = []
for sample in samples:
sample_path = get_sample_path(sample.sha256)
if not os.path.exists(sample_path):
continue
try:
with open(sample_path, 'rb') as fd:
elfo = ELFFile(fd)
cur_machine = elfo.header.e_machine
except Exception:
continue
rows.append([sample.md5, sample.name, cur_machine])
self.log('table', dict(header=['MD5', 'Name', 'e_machine'], rows=rows))
return
if self.args.cluster:
db = Database()
samples = db.find(key='all')
cluster = {}
for sample in samples:
sample_path = get_sample_path(sample.sha256)
if not os.path.exists(sample_path):
continue
try:
with open(sample_path, 'rb') as fd:
elfo = ELFFile(fd)
cur_machine = elfo.header.e_machine
except Exception as e:
self.log('error', "Error {0} for sample {1}".format(e, sample.sha256))
continue
if cur_machine not in cluster:
cluster[cur_machine] = []
cluster[cur_machine].append([sample.md5, sample.name])
for cluster_name, cluster_members in cluster.items():
# Skipping clusters with only one entry.
if len(cluster_members) == 1:
continue
self.log('info', "ELF e_machine cluster {0} with {1} elements".format(bold(cluster_name), len(cluster_members)))
self.log('table', dict(header=['MD5', 'Name'], rows=cluster_members))
if self.args.scan:
db = Database()
samples = db.find(key='all')
rows = []
for sample in samples:
if sample.sha256 == __sessions__.current.file.sha256:
continue
sample_path = get_sample_path(sample.sha256)
if not os.path.exists(sample_path):
continue
try:
with open(sample_path, 'rb') as fd:
elfo = ELFFile(fd)
cur_machine = elfo.header.e_machine
except Exception:
continue
if machine == cur_machine:
rows.append([sample.md5, sample.name])
if len(rows) > 0:
self.log('info', "Following are samples with e_machine {0}".format(bold(machine)))
self.log('table', dict(header=['MD5', 'Name'], rows=rows))
def elftype(self):
if not self.__check_session():
return
e_type = self.elf.header.e_type
self.log('info', "ELF type: {0:s}".format(e_type))
if self.args.scan and self.args.cluster:
self.log('error', "You selected two exclusive options, pick one")
return
if self.args.all:
db = Database()
samples = db.find(key='all')
rows = []
for sample in samples:
sample_path = get_sample_path(sample.sha256)
if not os.path.exists(sample_path):
continue
try:
with open(sample_path, 'rb') as fd:
elfo = ELFFile(fd)
cur_e_type = elfo.header.e_type
except Exception:
continue
rows.append([sample.md5, sample.name, cur_e_type])
self.log('table', dict(header=['MD5', 'Name', 'e_type'], rows=rows))
return
if self.args.cluster:
db = Database()
samples = db.find(key='all')
cluster = {}
for sample in samples:
sample_path = get_sample_path(sample.sha256)
if not os.path.exists(sample_path):
continue
try:
with open(sample_path, 'rb') as fd:
elfo = ELFFile(fd)
cur_e_type = elfo.header.e_type
except Exception as e:
self.log('error', "Error {0} for sample {1}".format(e, sample.sha256))
continue
if cur_e_type not in cluster:
cluster[cur_e_type] = []
cluster[cur_e_type].append([sample.md5, sample.name])
for cluster_name, cluster_members in cluster.items():
# Skipping clusters with only one entry.
if len(cluster_members) == 1:
continue
self.log('info', "ELF e_type cluster {0} with {1} elements".format(bold(cluster_name), len(cluster_members)))
self.log('table', dict(header=['MD5', 'Name'], rows=cluster_members))
if self.args.scan:
db = Database()
samples = db.find(key='all')
rows = []
for sample in samples:
if sample.sha256 == __sessions__.current.file.sha256:
continue
sample_path = get_sample_path(sample.sha256)
if not os.path.exists(sample_path):
continue
try:
with open(sample_path, 'rb') as fd:
elfo = ELFFile(fd)
cur_e_type = elfo.header.e_machine
except Exception:
continue
if e_type == cur_e_type:
rows.append([sample.md5, sample.name])
if len(rows) > 0:
self.log('info', "Following are samples with ELF type {0}".format(bold(e_type)))
self.log('table', dict(header=['MD5', 'Name'], rows=rows))
def elfentropy(self):
if not self.__check_session():
return
ent = self.get_entropy(__sessions__.current.file.data)
self.log('info', "Entropy {0}".format(ent))
if ent > 7:
self.log('warning', "Probably packed. High entropy.")
if self.args.scan and self.args.cluster:
self.log('error', "You selected two exclusive options, pick one")
return
if self.args.all:
db = Database()
samples = db.find(key='all')
rows = []
for sample in samples:
sample_path = get_sample_path(sample.sha256)
if not os.path.exists(sample_path):
continue
try:
with open(sample_path, 'rb') as fd:
cur_ent = self.get_entropy(fd.read())
except Exception as e:
self.log('error', "Error {0} for sample {1}".format(e, sample.sha256))
continue
rows.append([sample.md5, sample.name, cur_ent])
self.log('table', dict(header=['MD5', 'Name', 'Entropy'], rows=rows))
return
if self.args.cluster:
db = Database()
samples = db.find(key='all')
cluster = {}
for sample in samples:
sample_path = get_sample_path(sample.sha256)
if not os.path.exists(sample_path):
continue
try:
with open(sample_path, 'rb') as fd:
cur_ent = self.get_entropy(fd.read())
except Exception as e:
self.log('error', "Error {0} for sample {1}".format(e, sample.sha256))
continue
if cur_ent not in cluster:
cluster[cur_ent] = []
cluster[cur_ent].append([sample.md5, sample.name])
for cluster_name, cluster_members in cluster.items():
# Skipping clusters with only one entry.
if len(cluster_members) == 1:
continue
self.log('info', "ELF entropy cluster {0} with {1} elements".format(bold(cluster_name), len(cluster_members)))
self.log('table', dict(header=['MD5', 'Name'], rows=cluster_members))
if self.args.scan:
db = Database()
samples = db.find(key='all')
rows = []
for sample in samples:
if sample.sha256 == __sessions__.current.file.sha256:
continue
sample_path = get_sample_path(sample.sha256)
if not os.path.exists(sample_path):
continue
try:
with open(sample_path, 'rb') as fd:
cur_ent = self.get_entropy(fd.read())
except Exception:
continue
if ent == cur_ent:
rows.append([sample.md5, sample.name])
if len(rows) > 0:
self.log('info', "Following are samples with entropy {0}".format(bold(ent)))
self.log('table', dict(header=['MD5', 'Name'], rows=rows))
def get_entropy(self, data):
if not data:
return 0
entropy = 0
for x in range(256):
p_x = float(data.count(bytes(x))) / len(data)
if p_x > 0:
entropy += - p_x * math.log(p_x, 2)
ent = "%.4f" % entropy
return float(ent)
def run(self):
super(ELF, self).run()
if self.args is None:
return
if not HAVE_ELFTOOLS:
self.log('error', "Missing dependency, install pyelftools (`pip install pyelftools`)")
return
if self.args.subname == "sections":
self.sections()
elif self.args.subname == "segments":
self.segments()
elif self.args.subname == "symbols":
self.symbols()
elif self.args.subname == "interpreter":
self.interpreter()
elif self.args.subname == "dynamic":
self.dynamic()
elif self.args.subname == "entrypoint":
self.entrypoint()
elif self.args.subname == "machine":
self.machine()
elif self.args.subname == "type":
self.elftype()
elif self.args.subname == "entropy":
self.elfentropy()
else:
self.log('error', 'At least one of the parameters is required')
self.usage()