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
/
Copy pathlief.py
2120 lines (2005 loc) · 106 KB
/
lief.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
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
# -*- coding: utf-8 -*-
# This file is part of Viper - https://github.com/viper-framework/viper
# See the file 'LICENSE' for copying permission.
import math
import os.path
import string
from viper.common.abstracts import Module
from viper.core.session import __sessions__
from datetime import datetime
try:
import lief
HAVE_LIEF = True
except Exception:
HAVE_LIEF = False
class Lief(Module):
cmd = "lief"
description = "Parse and extract information from ELF, PE, MachO, DEX, OAT, ART and VDEX formats"
authors = ["Jordan Samhi"]
categories = ["windows", "osx", "linux", "android"]
def __init__(self):
super(Lief, self).__init__()
subparsers = self.parser.add_subparsers(dest="subname")
""" Constants """
self.IS_PE = False
self.IS_ELF = False
self.IS_MACHO = False
self.IS_OAT = False
self.IS_DEX = False
self.IS_VDEX = False
self.IS_ART = False
self.FILE_PATH = None
""" Arguments parsers """
parser_pe = subparsers.add_parser("pe", help="Extract information from PE files")
parser_pe.add_argument("-A", "--architecture", action="store_true", help="Show PE architecture")
parser_pe.add_argument("-b", "--debug", action="store_true", help="Show PE debug information")
parser_pe.add_argument("-c", "--compiledate", action="store_true", help="Show PE date of compilation")
parser_pe.add_argument("-C", "--richheader", action="store_true", help="Show PE rich header")
parser_pe.add_argument("-d", "--dlls", action="store_true", help="Show PE imported dlls")
parser_pe.add_argument("-D", "--datadirectories", action="store_true", help="Show PE data directories")
parser_pe.add_argument("-e", "--entrypoint", action="store_true", help="Show PE entrypoint")
parser_pe.add_argument("-g", "--signature", action="store_true", help="Show PE signature")
parser_pe.add_argument("-G", "--dialogs", action="store_true", help="Show PE dialogs box information")
parser_pe.add_argument("-H", "--header", action="store_true", help="Show PE header")
parser_pe.add_argument("-i", "--imports", action="store_true", help="Show PE imported functions and DLLs")
parser_pe.add_argument("-I", "--impfunctions", action="store_true", help="Show PE imported functions")
parser_pe.add_argument("-j", "--expfunctions", action="store_true", help="Show PE exported functions")
parser_pe.add_argument("-l", "--loadconfiguration", action="store_true", help="Show PE load configuration")
parser_pe.add_argument("-L", "--langs", action="store_true", help="Show PE langs and sublangs used")
parser_pe.add_argument("-m", "--imphash", action="store_true", help="Show PE imported functions hash")
parser_pe.add_argument("-M", "--manifest", action="store_true", help="Show PE Manifest")
parser_pe.add_argument("-o", "--dosheader", action="store_true", help="Show PE DOS header")
parser_pe.add_argument("-O", "--icons", action="store_true", help="Show PE icons information")
parser_pe.add_argument("-r", "--relocations", action="store_true", help="Show PE relocations")
parser_pe.add_argument("-R", "--resources", action="store_true", help="Show PE resources")
parser_pe.add_argument("-s", "--sections", action="store_true", help="Show PE sections")
parser_pe.add_argument("-t", "--type", action="store_true", help="Show PE type")
parser_pe.add_argument("-T", "--tls", action="store_true", help="Show PE tls")
parser_pe.add_argument("-u", "--dosstub", action="store_true", help="Show PE DOS stub")
parser_pe.add_argument("-x", "--extracticons", nargs='?', help="Extract icons to the given path (default : ./)", const="./", metavar="path")
parser_pe.add_argument("-y", "--dynamic", action="store_true", help="Show PE dynamic libraries")
parser_pe.add_argument("-Y", "--resourcestypes", action="store_true", help="Show PE types of resources")
parser_pe.add_argument("--id", nargs=1, type=int, help="Define an id for following commands : -x", metavar="id")
parser_elf = subparsers.add_parser("elf", help="Extract information from ELF files")
parser_elf.add_argument("-A", "--architecture", action="store_true", help="Show ELF architecture")
parser_elf.add_argument("-b", "--impsymbols", action="store_true", help="Show ELF imported symbols")
parser_elf.add_argument("-B", "--staticsymbols", action="store_true", help="Show ELF static symbols")
parser_elf.add_argument("-d", "--dynamic", action="store_true", help="Show ELF dynamic libraries")
parser_elf.add_argument("-e", "--entrypoint", action="store_true", help="Show ELF entrypoint")
parser_elf.add_argument("-E", "--entropy", action="store_true", help="Show ELF entropy")
parser_elf.add_argument("-g", "--gnu_hash", action="store_true", help="Show ELF GNU hash")
parser_elf.add_argument("-H", "--header", action="store_true", help="Show ELF header")
parser_elf.add_argument("-i", "--interpreter", action="store_true", help="Show ELF interpreter")
parser_elf.add_argument("-I", "--impfunctions", action="store_true", help="Show ELF imported functions")
parser_elf.add_argument("-j", "--expfunctions", action="store_true", help="Show ELF exported functions")
parser_elf.add_argument("-k", "--expsymbols", action="store_true", help="Show ELF exported symbols")
parser_elf.add_argument("-n", "--notes", action="store_true", help="Show ELF notes")
parser_elf.add_argument("-o", "--objectrelocations", action="store_true", help="Show ELF object relocations")
parser_elf.add_argument("-r", "--relocations", action="store_true", help="Show ELF relocations")
parser_elf.add_argument("-s", "--sections", action="store_true", help="Show ELF sections")
parser_elf.add_argument("-S", "--segments", action="store_true", help="Show ELF segments")
parser_elf.add_argument("-t", "--type", action="store_true", help="Show ELF type")
parser_elf.add_argument("-T", "--dynamicentries", action="store_true", help="Strip ELF dynamic entries")
parser_elf.add_argument("-w", "--write", nargs=1, help="Write binary into file", metavar="fileName")
parser_elf.add_argument("-y", "--symbols", action="store_true", help="Show ELF symbols")
parser_elf.add_argument("-Y", "--dynamicsymbols", action="store_true", help="Show ELF dynamic symbols")
parser_elf.add_argument("-z", "--strip", action="store_true", help="Strip ELF binary")
parser_macho = subparsers.add_parser("macho", help="Extract information from MachO files")
parser_macho.add_argument("-A", "--architecture", action="store_true", help="Show MachO architecture")
parser_macho.add_argument("-c", "--commands", action="store_true", help="Show MachO commands")
parser_macho.add_argument("-C", "--codesignature", action="store_true", help="Show MachO code signature")
parser_macho.add_argument("-d", "--dynamic", action="store_true", help="Show MachO dynamic libraries")
parser_macho.add_argument("-D", "--dataincode", action="store_true", help="Show MachO data in code")
parser_macho.add_argument("-e", "--entrypoint", action="store_true", help="Show MachO entrypoint")
parser_macho.add_argument("-f", "--subframework", action="store_true", help="Show MachO sub-framework")
parser_macho.add_argument("-H", "--header", action="store_true", help="Show MachO header")
parser_macho.add_argument("-I", "--impfunctions", action="store_true", help="Show MachO imported functions")
parser_macho.add_argument("-j", "--expfunctions", action="store_true", help="Show MachO exported functions")
parser_macho.add_argument("-k", "--expsymbols", action="store_true", help="Show MachO exported symbols")
parser_macho.add_argument("-m", "--maincommand", action="store_true", help="Show MachO main command")
parser_macho.add_argument("-q", "--impsymbols", action="store_true", help="Show MachO imported symbols")
parser_macho.add_argument("-s", "--sections", action="store_true", help="Show MachO sections")
parser_macho.add_argument("-S", "--segments", action="store_true", help="Show MachO segments")
parser_macho.add_argument("-t", "--type", action="store_true", help="Show MachO type")
parser_macho.add_argument("-u", "--uuid", action="store_true", help="Show MachO uuid")
parser_macho.add_argument("-v", "--sourceversion", action="store_true", help="Show MachO source version")
parser_macho.add_argument("-y", "--symbols", action="store_true", help="Show MachO symbols")
parser_oat = subparsers.add_parser("oat", help="Extract information from OAT files")
parser_oat.add_argument("-c", "--classname", nargs=1, help="Full name of class (com.android.etc...). Used with -m", metavar="fullname", type=str)
parser_oat.add_argument("-C", "--classes", action="store_true", help="Show OAT classes")
parser_oat.add_argument("-b", "--impsymbols", action="store_true", help="Show OAT imported symbols")
parser_oat.add_argument("-B", "--staticsymbols", action="store_true", help="Show OAT static symbols")
parser_oat.add_argument("-d", "--dynamic", action="store_true", help="Show OAT dynamic libraries")
parser_oat.add_argument("-D", "--dynamicrelocations", action="store_true", help="Show OAT dynamic relocations")
parser_oat.add_argument("-e", "--entrypoint", action="store_true", help="Show OAT entrypoint")
parser_oat.add_argument("-E", "--entropy", action="store_true", help="Show OAT entropy")
parser_oat.add_argument("-f", "--dexfiles", action="store_true", help="Show OAT dex files")
parser_oat.add_argument("-g", "--gnu_hash", action="store_true", help="Show OAT GNU hash")
parser_oat.add_argument("-H", "--header", action="store_true", help="Show OAT header")
parser_oat.add_argument("-i", "--interpreter", action="store_true", help="Show OAT interpreter")
parser_oat.add_argument("-I", "--impfunctions", action="store_true", help="Show OAT imported functions")
parser_oat.add_argument("-j", "--expfunctions", action="store_true", help="Show OAT exported functions")
parser_oat.add_argument("-k", "--expsymbols", action="store_true", help="Show OAT exported symbols")
parser_oat.add_argument("-m", "--methods", action="store_true", help="Show OAT methods by class")
parser_oat.add_argument("-n", "--name", nargs=1, type=str, help="Define a name for the following commands : -m, -x", metavar="name")
parser_oat.add_argument("-N", "--notes", action="store_true", help="Show OAT notes")
parser_oat.add_argument("-o", "--objectrelocations", action="store_true", help="Show OAT object relocations")
parser_oat.add_argument("-r", "--relocations", action="store_true", help="Show OAT relocations")
parser_oat.add_argument("-s", "--sections", action="store_true", help="Show OAT sections")
parser_oat.add_argument("-S", "--segments", action="store_true", help="Show OAT segments")
parser_oat.add_argument("-t", "--type", action="store_true", help="Show OAT type")
parser_oat.add_argument("-T", "--dynamicentries", action="store_true", help="Show OAT dynamic entries")
parser_oat.add_argument("-v", "--androidversion", action="store_true", help="Show OAT android version")
parser_oat.add_argument("-w", "--write", nargs=1, help="Write binary into file", metavar="fileName")
parser_oat.add_argument("-x", "--extractdexfiles", nargs='?', help="Extract dex files to the given path (default : ./)", const="./", metavar="path")
parser_oat.add_argument("-y", "--symbols", action="store_true", help="Show OAT static and dynamic symbols")
parser_oat.add_argument("-Y", "--dynamicsymbols", action="store_true", help="Show OAT dynamic symbols")
parser_oat.add_argument("-z", "--strip", action="store_true", help="Strip OAT binary")
parser_dex = subparsers.add_parser("dex", help="Extract information from DEX files")
parser_dex.add_argument("-c", "--classname", nargs=1, help="Full name of class (com.android.etc...). Used with -m", metavar="fullname", type=str)
parser_dex.add_argument("-C", "--classes", action="store_true", help="Show DEX classes")
parser_dex.add_argument("-H", "--header", action="store_true", help="Show DEX header")
parser_dex.add_argument("-m", "--methods", action="store_true", help="Show DEX methods by class")
parser_dex.add_argument("-M", "--map", action="store_true", help="Show DEX map items")
parser_dex.add_argument("-n", "--name", nargs=1, type=str, help="Define a name for the following commands : -m", metavar="name")
parser_dex.add_argument("-s", "--strings", action="store_true", help="Show DEX strings")
parser_vdex = subparsers.add_parser("vdex", help="Extract information from VDEX files")
parser_vdex.add_argument("-f", "--dexfiles", action="store_true", help="Show VDEX dex files")
parser_vdex.add_argument("-H", "--header", action="store_true", help="Show VDEX header")
parser_vdex.add_argument("-n", "--name", nargs=1, type=str, help="Define a name for the following commands : -x", metavar="name")
parser_vdex.add_argument("-v", "--androidversion", action="store_true", help="Show VDEX android version")
parser_vdex.add_argument("-x", "--extractdexfiles", nargs='?', help="Extract dex files to the given path (default : ./)", const="./", metavar="path")
parser_art = subparsers.add_parser("art", help="Extract information from ART files")
parser_art.add_argument("-H", "--header", action="store_true", help="Show ART header")
parser_art.add_argument("-v", "--androidversion", action="store_true", help="Show ART android version")
self.lief = 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.lief:
try:
self.lief = self.parseBinary(__sessions__.current.file.path)
self.FILE_PATH = __sessions__.current.file.path
except lief.parser_error as e:
self.log("error", "Unable to parse file : {0}".format(e))
return False
return True
"""Binaries methods"""
def sections(self):
"""
Display sections of ELF, PE, Mach-O and OAT formats
"""
if not self.__check_session():
return
rows = []
if self.IS_OAT or self.IS_ELF:
for section in self.lief.sections:
rows.append([
section.name,
hex(section.offset),
hex(section.virtual_address),
"{0:<6} bytes".format(section.size),
self.liefConstToString(section.type),
':'.join(self.liefConstToString(flag) for flag in section.flags_list),
round(section.entropy, 4)
])
self.log("info", "Sections : ")
self.log("table", dict(header=["Name", "Address", "RVA", "Size", "Type", "Flags", "Entropy"], rows=rows))
elif self.IS_PE:
for section in self.lief.sections:
rows.append([
section.name,
hex(section.virtual_address),
"{0:<6} bytes".format(section.virtual_size),
hex(section.offset),
"{0:<6} bytes".format(section.size),
round(section.entropy, 4)
])
self.log("info", "PE sections : ")
self.log("table", dict(header=["Name", "RVA", "VirtualSize", "PointerToRawData", "RawDataSize", "Entropy"], rows=rows))
elif self.IS_MACHO:
for section in self.lief.sections:
rows.append([
section.name,
hex(section.virtual_address),
self.liefConstToString(section.type),
"{:<6} bytes".format(section.size),
hex(section.offset),
round(section.entropy, 4)
])
self.log("info", "MachO sections : ")
self.log("table", dict(header=["Name", "Virt Addr", "Type", "Size", "Offset", "Entropy"], rows=rows))
else:
self.log("warning", "No section found")
return
def segments(self):
"""
Display segments of ELF, Mach-O and OAT formats
"""
if not self.__check_session():
return
rows = []
if self.IS_OAT or self.IS_ELF:
for segment in self.lief.segments:
flags = []
if lief.ELF.SEGMENT_FLAGS.R in segment:
flags.append(self.liefConstToString(lief.ELF.SEGMENT_FLAGS.R))
if lief.ELF.SEGMENT_FLAGS.W in segment:
flags.append(self.liefConstToString(lief.ELF.SEGMENT_FLAGS.W))
if lief.ELF.SEGMENT_FLAGS.X in segment:
flags.append(self.liefConstToString(lief.ELF.SEGMENT_FLAGS.X))
if lief.ELF.SEGMENT_FLAGS.NONE in segment:
flags.append(self.liefConstToString(lief.ELF.SEGMENT_FLAGS.NONE))
rows.append([
self.liefConstToString(segment.type),
hex(segment.physical_address),
hex(segment.physical_size),
hex(segment.virtual_address),
hex(segment.virtual_size),
':'.join(flags),
self.getEntropy(bytes(segment.content))
])
self.log("info", "Segments : ")
self.log("table", dict(header=["Type", "PhysicalAddress", "FileSize", "VirtuAddr", "MemSize", "Flags", "Entropy"], rows=rows))
elif self.IS_MACHO:
self.log("info", "MachO segments : ")
for segment in self.lief.segments:
self.log("info", "Information of segment {0} : ".format(segment.name))
self.log("item", "{0:<18} : {1}".format("Name", segment.name)),
self.log("item", "{0:<18} : {1} bytes".format("Size", segment.file_size)),
self.log("item", "{0:<18} : {1}".format("Offset", segment.file_offset)),
self.log("item", "{0:<18} : {1}".format("Command", self.liefConstToString(segment.command))),
self.log("item", "{0:<18} : {1} bytes".format("Command size", segment.size)),
self.log("item", "{0:<18} : {1}".format("Command offset", hex(segment.command_offset))),
self.log("item", "{0:<18} : {1}".format("Number of sections", segment.numberof_sections)),
self.log("item", "{0:<18} : {1}".format("Initial protection", segment.init_protection)),
self.log("item", "{0:<18} : {1}".format("Maximum protection", segment.max_protection)),
self.log("item", "{0:<18} : {1}".format("Virtual address", hex(segment.virtual_address))),
self.log("item", "{0:<18} : {1} bytes".format("Virtual size", segment.virtual_size)),
if segment.sections:
for section in segment.sections:
rows.append([
section.name,
hex(section.virtual_address),
self.liefConstToString(section.type),
"{:<6} bytes".format(section.size),
hex(section.offset),
round(section.entropy, 4)
])
self.log("success", "Sections in segment {0} : ".format(segment.name))
self.log("table", dict(header=["Name", "Virtual address", "Type", "Size", "Offset", "Entropy"], rows=rows))
rows = []
else:
self.log("warning", "No segment found")
def type(self):
"""
Display type of ELF, PE, Mach-O and OAT formats
"""
if not self.__check_session():
return
binaryType = None
if self.IS_OAT:
binaryType = self.lief.type
elif self.IS_ELF:
binaryType = self.lief.header.file_type
elif self.IS_PE:
binaryType = lief.PE.get_type(self.FILE_PATH)
elif self.IS_MACHO:
binaryType = self.lief.header.file_type
if binaryType:
self.log("info", "Type : {0}".format(self.liefConstToString(binaryType)))
else:
self.log("warning", "No type found")
def entrypoint(self):
"""
Display entrypoint of ELF, PE, Mach-O and OAT formats
"""
if not self.__check_session():
return
entrypoint = None
if self.IS_OAT:
entrypoint = self.lief.entrypoint
elif self.IS_ELF:
entrypoint = self.lief.header.entrypoint
elif self.IS_PE:
entrypoint = self.lief.entrypoint
elif self.IS_MACHO and self.lief.has_entrypoint:
entrypoint = self.lief.entrypoint
if entrypoint:
self.log("info", "Entrypoint : {0}".format(hex(entrypoint)))
else:
self.log("warning", "No entrypoint found")
def architecture(self):
"""
Display architecture type of ELF, PE and Mach-O formats
"""
if not self.__check_session():
return
architecture = None
if self.IS_ELF:
architecture = self.lief.header.machine_type
elif self.IS_PE:
architecture = self.lief.header.machine
elif self.IS_MACHO:
architecture = self.lief.header.cpu_type
if architecture:
self.log("info", "Architecture : {0}".format(self.liefConstToString(architecture)))
else:
self.log("warning", "No architecture found")
def entropy(self):
"""
Display entropy of a binary file
"""
if not self.__check_session():
return
entropy = self.getEntropy(bytes(__sessions__.current.file.data))
self.log("info", "Entropy : {0}".format(str(entropy)))
if entropy > 7:
self.log("warning", "The binary is probably packed")
def interpreter(self):
"""
Display interpreter of ELF and OAT formats
"""
if not self.__check_session():
return
if (self.IS_OAT or self.IS_ELF) and self.lief.has_interpreter:
self.log("info", "Interpreter : {0}".format(self.lief.interpreter))
else:
self.log("warning", "No interpreter found")
def dynamic(self):
"""
Display dynamic libraries of ELF, PE, Mach-O and OAT formats
"""
if not self.__check_session():
return
rows = []
if (self.IS_OAT or self.IS_ELF or self.IS_PE) and self.lief.libraries:
self.log("info", "Dynamic libraries : ")
for lib in self.lief.libraries:
self.log("info", lib)
elif self.IS_MACHO and self.lief.libraries:
for library in self.lief.libraries:
rows.append([
self.liefConstToString(library.command),
library.name,
hex(library.command_offset),
self.listVersionToDottedVersion(library.compatibility_version),
self.listVersionToDottedVersion(library.current_version),
"{0:<6} bytes".format(library.size),
library.timestamp
])
self.log("info", "Dynamic libraries : ")
self.log("table", dict(header=["Command", "Name", "Offset", "Compatibility version", "Current version", "Size", "Timestamp"], rows=rows))
else:
self.log("warning", "No dynamic library found")
def symbols(self):
"""
Display symbols of ELF, Mach-O and OAT formats
"""
if not self.__check_session():
return
rows = []
if (self.IS_OAT or self.IS_ELF) and self.lief.symbols:
self.printElfAndOatSymbols(self.lief.symbols, "Static and dynamic symbols")
elif self.IS_MACHO and self.lief.symbols:
self.log("info", "MachO symbols : ")
for symbol in self.lief.symbols:
rows.append([
symbol.name,
hex(symbol.description),
symbol.numberof_sections,
hex(symbol.type),
hex(symbol.value),
self.liefConstToString(symbol.origin)
])
self.log("info", "Mach-O symbols : ")
self.log("table", dict(header=["Name", "Description", "Nb of sections", "Type", "Value", "Origin"], rows=rows))
else:
self.log("warning", "No symbol found")
def dlls(self):
"""
Display PE binary imported dlls if any
"""
if not self.__check_session():
return
if self.IS_PE and self.lief.libraries:
self.log("info", "PE dlls : ")
for lib in self.lief.libraries:
self.log("info", lib)
else:
self.log("error", "No DLL found")
def imports(self):
"""
Display Pe imports if any
"""
if not self.__check_session():
return
if self.IS_PE and self.lief.imports:
self.log("info", "PE imports")
for imp in self.lief.imports:
self.log("info", "{0}".format(imp.name))
for function in imp.entries:
self.log("item", "{0} : {1}".format(hex(function.iat_address), function.name))
else:
self.log("warning", "No import found")
def imphash(self):
"""
Display PE imphash
"""
if not self.__check_session():
return
if self.IS_PE:
self.log("info", "Imphash : {0}".format(lief.PE.get_imphash(self.lief)))
else:
self.log("warning", "No imphash found")
def gnu_hash(self):
"""
Display GNU hash of ELF and OAT formats
"""
if not self.__check_session():
return
if (self.IS_OAT and self.lief.use_gnu_hash) or (self.IS_ELF and not self.IS_OAT and self.lief.gnu_hash):
bloomFilters = ""
hashBuckets = ""
hashValues = ""
for fil in self.lief.gnu_hash.bloom_filters:
bloomFilters += str(hex(fil))
if fil != self.lief.gnu_hash.bloom_filters[len(self.lief.gnu_hash.bloom_filters)-1]:
bloomFilters += ", "
for bucket in self.lief.gnu_hash.buckets:
hashBuckets += str(hex(bucket))
if bucket != self.lief.gnu_hash.buckets[len(self.lief.gnu_hash.buckets)-1]:
hashBuckets += ", "
for h in self.lief.gnu_hash.hash_values:
hashValues += str(hex(h))
if h != self.lief.gnu_hash.hash_values[len(self.lief.gnu_hash.hash_values)-1]:
hashValues += ", "
self.log("info", "GNU hash : ")
self.log("item", "{0} : {1}".format("Number of buckets", self.lief.gnu_hash.nb_buckets))
self.log("item", "{0} : {1}".format("First symbol index", hex(self.lief.gnu_hash.symbol_index)))
self.log("item", "{0} : {1}".format("Bloom filters", bloomFilters))
self.log("item", "{0} : {1}".format("Hash buckets", hashBuckets))
self.log("item", "{0} : {1}".format("Hash values", hashValues))
else:
self.log("warning", "No GNU hash found")
def compileDate(self):
"""
Display PE compilation date
"""
if not self.__check_session():
return
if self.IS_PE:
self.log("info", "Compilation date : {0}".format(self.fromTimestampToDate(self.lief.header.time_date_stamps)))
else:
self.log("warning", "No compilation date found")
def strip(self):
"""
Strip ELF and OAT formats
"""
if not self.__check_session():
return
if self.IS_OAT or self.IS_ELF:
self.lief.strip()
self.log("success", "The binary has been stripped")
self.log("warning", "Do not forget --write (-w) option if you want your stripped binary to be saved")
else:
self.log("warning", "Binary must be of type ELF or OAT")
def write(self):
"""
Write the open binary into another file, useful after being stripped
A destination folder can be set (default ./)
"""
if not self.__check_session():
return
fileName = self.args.write[0]
destFolder = './' if '/' not in fileName else fileName[:fileName.rfind('/') + 1]
if os.path.isfile(fileName):
self.log("error", "File already exists")
elif not os.access(destFolder, os.X_OK | os.W_OK):
self.log("error", "Cannot write into folder : {0}".format(destFolder))
elif fileName[len(fileName) - 1] == '/':
self.log("error", "Please enter a file name")
else:
self.lief.write(fileName)
self.log("success", "File successfully saved")
def notes(self):
"""
Display ELF and OAT notes
"""
if not self.__check_session():
return
if (self.IS_OAT or self.IS_ELF) and self.lief.has_notes:
self.log("info", "Notes : ")
for note in self.lief.notes:
description = ""
for desc in note.description:
description += str(hex(desc))[2:]
self.log("success", "Information of {0} note : ".format(note.name))
self.log("item", "{0} : {1}".format("Name", note.name))
self.log("item", "{0} : {1}".format("Description", description))
self.log("item", "{0} : {1}".format("Type", self.liefConstToString(note.type)))
if note.type == lief.ELF.NOTE_TYPES.ABI_TAG:
note_abi = note.details
self.log("item", "{0} : {1}".format("ABI", self.liefConstToString(note_abi.abi)))
self.log("item", "{0} : {1}".format("Version", self.listVersionToDottedVersion(note_abi.version)))
else:
self.log("warning", "No note found")
def map(self):
"""
Display DEX map items
"""
if not self.__check_session():
return
rows = []
if self.IS_DEX and self.lief.map:
for item in self.lief.map.items:
rows.append([
self.liefConstToString(item.type),
hex(item.offset),
"{0:<5} bytes".format(item.size)
])
self.log("info", "DEX map items : ")
self.log("table", dict(header=["Type", "Offset", "Size"], rows=rows))
else:
self.log("warning", "No map found")
def header(self):
"""
Display header of ELF, PE, Mach-O, OAT, DEX, VDEX and ART formats
"""
if not self.__check_session():
return
if self.IS_ART:
self.log("info", "ART header : ")
self.log("item", "{0:<17} : {1}".format("Magic", self.formatMagicList(self.lief.header.magic)))
self.log("item", "{0:<17} : {1}".format("Version", self.lief.header.version))
self.log("item", "{0:<17} : {1}".format("Image begin", hex(self.lief.header.image_begin)))
self.log("item", "{0:<17} : {1} bytes".format("Image size", self.lief.header.image_size))
self.log("item", "{0:<17} : {1}".format("Checksum", hex(self.lief.header.oat_checksum)))
self.log("item", "{0:<17} : {1}".format("OAT file begin", hex(self.lief.header.oat_file_begin)))
self.log("item", "{0:<17} : {1}".format("OAT file end", hex(self.lief.header.oat_file_end)))
self.log("item", "{0:<17} : {1}".format("Patch delta", self.lief.header.patch_delta))
self.log("item", "{0:<17} : {1} bytes".format("Pointer size", self.lief.header.pointer_size))
self.log("item", "{0:<17} : {1}".format("Compile pic", "Yes" if self.lief.header.compile_pic else "No"))
self.log("item", "{0:<17} : {1}".format("Nb of sections", self.lief.header.nb_sections))
self.log("item", "{0:<17} : {1}".format("Nb of methods", self.lief.header.nb_methods))
self.log("item", "{0:<17} : {1}".format("Boot image begin", hex(self.lief.header.boot_image_begin)))
self.log("item", "{0:<17} : {1} bytes".format("Boot image size", self.lief.header.boot_image_size))
self.log("item", "{0:<17} : {1}".format("Boot OAT begin", hex(self.lief.header.boot_oat_begin)))
self.log("item", "{0:<17} : {1} bytes".format("Boot OAT size", self.lief.header.boot_oat_size))
self.log("item", "{0:<17} : {1}".format("Storage mode", self.liefConstToString(self.lief.header.storage_mode)))
self.log("item", "{0:<17} : {1} bytes".format("Data size", self.lief.header.data_size))
elif self.IS_VDEX:
self.log("info", "VDEX header : ")
self.log("item", "{0:<22} : {1}".format("Magic", self.formatMagicList(self.lief.header.magic)))
self.log("item", "{0:<22} : {1}".format("Nb of DEX files", self.lief.header.nb_dex_files))
self.log("item", "{0:<22} : {1} bytes".format("Size of info section", self.lief.header.quickening_info_size))
self.log("item", "{0:<22} : {1} bytes".format("Size of deps section", self.lief.header.verifier_deps_size))
self.log("item", "{0:<22} : {1} bytes".format("Size of all DEX files", self.lief.header.dex_size))
self.log("item", "{0:<22} : {1}".format("Version", self.lief.header.version))
elif self.IS_DEX:
signature = ""
for sig in self.lief.header.signature:
signature += str(hex(sig))[2:]
self.log("info", "DEX header : ")
self.log("item", "{0:<17} : {1}".format("Magic", self.formatMagicList(self.lief.header.magic)))
self.log("item", "{0:<17} : {1}".format("Checksum", hex(self.lief.header.checksum)))
self.log("item", "{0:<17} : {1}".format("Endianness", hex(self.lief.header.endian_tag)))
self.log("item", "{0:<17} : {1}".format("Location", self.lief.location if self.lief.location else '-'))
self.log("item", "{0:<17} : {1} bytes".format("Size", self.lief.header.file_size))
self.log("item", "{0:<17} : {1} bytes".format("Header size", self.lief.header.header_size))
self.log("item", "{0:<17} : {1}".format("Map offset", hex(self.lief.header.map_offset)))
self.log("item", "{0:<17} : {1}".format("Signature", signature))
self.log("item", "{0:<17} : {1}".format("DEX version", self.lief.version))
self.log("item", "{0:<17} : {1}".format("Nb of Prototypes", "{0:<6} => id : {1}".format(self.lief.header.prototypes[1], hex(self.lief.header.prototypes[0]))))
self.log("item", "{0:<17} : {1}".format("Nb of Strings", "{0:<6} => id : {1}".format(self.lief.header.strings[1], hex(self.lief.header.strings[0]))))
self.log("item", "{0:<17} : {1}".format("Nb of Classes", "{0:<6} => id : {1}".format(self.lief.header.classes[1], hex(self.lief.header.classes[0]))))
self.log("item", "{0:<17} : {1}".format("Nb of Fields", "{0:<6} => id : {1}".format(self.lief.header.fields[1], hex(self.lief.header.fields[0]))))
self.log("item", "{0:<17} : {1}".format("Nb of Methods", "{0:<6} => id : {1}".format(self.lief.header.methods[1], hex(self.lief.header.methods[0]))))
self.log("item", "{0:<17} : {1}".format("Nb of Types", "{0:<6} => id : {1}".format(self.lief.header.types[1], hex(self.lief.header.types[0]))))
self.log("item", "{0:<17} : {1}".format("Nb of Data", "{0:<6} => id : {1}".format(self.lief.header.data[1], hex(self.lief.header.data[0]))))
self.log("item", "{0:<17} : {1}".format("Nb of Link", "{0:<6} => id : {1}".format(self.lief.header.link[1], hex(self.lief.header.link[0]))))
elif self.IS_OAT:
self.log("info", "OAT header : ")
self.log("item", "{0:<37} : {1}".format("Magic", self.formatMagicList(self.lief.header.magic)))
self.log("item", "{0:<37} : {1}".format("Checksum", hex(self.lief.header.checksum)))
self.log("item", "{0:<37} : {1}".format("ImageBase", hex(self.lief.imagebase) if self.lief.imagebase else '-'))
self.log("item", "{0:<37} : {1}".format("Executable offset", hex(self.lief.header.executable_offset)))
self.log("item", "{0:<37} : {1}".format("I2c code bridge offset", hex(self.lief.header.i2c_code_bridge_offset)))
self.log("item", "{0:<37} : {1}".format("I2c bridge offset", hex(self.lief.header.i2i_bridge_offset)))
self.log("item", "{0:<37} : {1}".format("Image file location oat checksum", hex(self.lief.header.image_file_location_oat_checksum)))
self.log("item", "{0:<37} : {1}".format("Image file location of data", hex(self.lief.header.image_file_location_oat_data_begin)))
self.log("item", "{0:<37} : {1}".format("Image patch delta", self.lief.header.image_patch_delta))
self.log("item", "{0:<37} : {1}".format("Insctruction set", self.liefConstToString(self.lief.header.instruction_set)))
self.log("item", "{0:<37} : {1}".format("JNI DLSYM lookup offset", hex(self.lief.header.jni_dlsym_lookup_offset)))
self.log("item", "{0:<37} : {1} bytes".format("Key value size", self.lief.header.key_value_size))
self.log("item", "{0:<37} : {1}".format("Keys", ", ".join(self.liefConstToString(key) for key in self.lief.header.keys)))
self.log("item", "{0:<37} : {1}".format("Number of dex files", self.lief.header.nb_dex_files))
self.log("item", "{0:<37} : {1}".format("Oat dex files offset", hex(self.lief.header.oat_dex_files_offset)))
self.log("item", "{0:<37} : {1}".format("Quick generic JNI trampoline offset", hex(self.lief.header.quick_generic_jni_trampoline_offset)))
self.log("item", "{0:<37} : {1}".format("Quick IMT conflict trampoline offset", hex(self.lief.header.quick_imt_conflict_trampoline_offset)))
self.log("item", "{0:<37} : {1}".format("Quick resolution trampoline offset", hex(self.lief.header.quick_resolution_trampoline_offset)))
self.log("item", "{0:<37} : {1}".format("Quick to interpreter bridge offset", hex(self.lief.header.quick_to_interpreter_bridge_offset)))
self.log("item", "{0:<37} : {1}".format("Version", self.lief.header.version))
elif self.IS_MACHO:
self.log("info", "MachO header : ")
self.log("item", "{0:<15} : {1}".format("CPU type", self.liefConstToString(self.lief.header.cpu_type)))
self.log("item", "{0:<15} : {1}".format("File type", self.liefConstToString(self.lief.header.file_type)))
self.log("item", "{0:<15} : {1}".format("Number of cmds", self.lief.header.nb_cmds))
self.log("item", "{0:<15} : {1} bytes".format("Size of cmds", self.lief.header.sizeof_cmds))
self.log("item", "{0:<15} : {1}".format("Flags", ':'.join(self.liefConstToString(flag) for flag in self.lief.header.flags_list)))
elif self.IS_PE:
self.log("info", "PE header : ")
self.log("item", "{0:<28} : {1}".format("Magic", self.formatMagicList(self.lief.header.signature)))
self.log("item", "{0:<28} : {1}".format("Type", self.liefConstToString(self.lief.header.machine)))
self.log("item", "{0:<28} : {1}".format("Number of sections", self.lief.header.numberof_sections))
self.log("item", "{0:<28} : {1}".format("Number of symbols", self.lief.header.numberof_symbols))
self.log("item", "{0:<28} : {1}".format("Pointer to symbol table", hex(self.lief.header.pointerto_symbol_table)))
self.log("item", "{0:<28} : {1}".format("Date of compilation", self.fromTimestampToDate(self.lief.header.time_date_stamps)))
self.log("item", "{0:<28} : {1:<6} bytes".format("Size of optional header", self.lief.header.sizeof_optional_header))
if self.lief.header.sizeof_optional_header > 0:
self.log("success", "Optional header : ")
self.log("item", "{0:<28} : {1}".format("Entrypoint", hex(self.lief.optional_header.addressof_entrypoint)))
self.log("item", "{0:<28} : {1}".format("Base of code", hex(self.lief.optional_header.baseof_code)))
self.log("item", "{0:<28} : {1}".format("Checksum", hex(self.lief.optional_header.checksum)))
self.log("item", "{0:<28} : {1}".format("Base of image", hex(self.lief.optional_header.imagebase)))
self.log("item", "{0:<28} : {1}".format("Magic", self.liefConstToString(self.lief.optional_header.magic)))
self.log("item", "{0:<28} : {1}".format("Subsystem", self.liefConstToString(self.lief.optional_header.subsystem)))
self.log("item", "{0:<28} : {1}".format("Min OS version", self.lief.optional_header.minor_operating_system_version))
self.log("item", "{0:<28} : {1}".format("Max OS version", self.lief.optional_header.major_operating_system_version))
self.log("item", "{0:<28} : {1}".format("Min Linker version", self.lief.optional_header.minor_linker_version))
self.log("item", "{0:<28} : {1}".format("Max Linker version", self.lief.optional_header.major_linker_version))
self.log("item", "{0:<28} : {1}".format("Min Image version", self.lief.optional_header.minor_image_version))
self.log("item", "{0:<28} : {1}".format("Max Image version", self.lief.optional_header.major_image_version))
self.log("item", "{0:<28} : {1:<8} bytes".format("Size of code", self.lief.optional_header.sizeof_code))
self.log("item", "{0:<28} : {1:<8} bytes".format("Size of headers", self.lief.optional_header.sizeof_headers))
self.log("item", "{0:<28} : {1:<8} bytes".format("Size of heap commited", self.lief.optional_header.sizeof_heap_commit))
self.log("item", "{0:<28} : {1:<8} bytes".format("Size of heap reserved", self.lief.optional_header.sizeof_heap_reserve))
self.log("item", "{0:<28} : {1:<8} bytes".format("Size of image", self.lief.optional_header.sizeof_image))
self.log("item", "{0:<28} : {1:<8} bytes".format("Size of Initialized data", self.lief.optional_header.sizeof_initialized_data))
self.log("item", "{0:<28} : {1:<8} bytes".format("Size of Uninitialized data", self.lief.optional_header.sizeof_uninitialized_data))
self.log("item", "{0:<28} : {1:<8} bytes".format("Size of stack commited", self.lief.optional_header.sizeof_stack_commit))
self.log("item", "{0:<28} : {1:<8} bytes".format("Size of stack reserved", self.lief.optional_header.sizeof_stack_reserve))
elif self.IS_ELF:
if self.lief.header.mips_flags_list:
mipsFlags = ':'.join(self.liefConstToString(flag) for flag in self.lief.header.mips_flags_list)
else:
mipsFlags = "No flags"
self.log("info", "ELF header : ")
self.log("item", "{0:<26} : {1}".format("Magic", self.formatMagicList(self.lief.header.identity)))
self.log("item", "{0:<26} : {1}".format("Type", self.liefConstToString(self.lief.header.file_type)))
self.log("item", "{0:<26} : {1}".format("Entrypoint", hex(self.lief.header.entrypoint)))
self.log("item", "{0:<26} : {1}".format("ImageBase", hex(self.lief.imagebase) if self.lief.imagebase else '-'))
self.log("item", "{0:<26} : {1} bytes".format("Header size", self.lief.header.header_size))
self.log("item", "{0:<26} : {1}".format("Endianness", self.liefConstToString(self.lief.header.identity_data)))
self.log("item", "{0:<26} : {1}".format("Class", self.liefConstToString(self.lief.header.identity_class)))
self.log("item", "{0:<26} : {1}".format("OS/ABI", self.liefConstToString(self.lief.header.identity_os_abi)))
self.log("item", "{0:<26} : {1}".format("Version", self.liefConstToString(self.lief.header.identity_version)))
self.log("item", "{0:<26} : {1}".format("Architecture", self.liefConstToString(self.lief.header.machine_type)))
self.log("item", "{0:<26} : {1}".format("MIPS Flags", mipsFlags))
self.log("item", "{0:<26} : {1}".format("Number of sections", self.lief.header.numberof_sections))
self.log("item", "{0:<26} : {1}".format("Number of segments", self.lief.header.numberof_segments))
self.log("item", "{0:<26} : {1}".format("Program header offet", hex(self.lief.header.program_header_offset)))
self.log("item", "{0:<26} : {1} bytes".format("Program header size", self.lief.header.program_header_size))
self.log("item", "{0:<26} : {1}".format("Section Header offset", hex(self.lief.header.section_header_offset)))
self.log("item", "{0:<26} : {1} bytes".format("Section header size", self.lief.header.section_header_size))
else:
self.log("warning", "No header found")
def codeSignature(self):
"""
Display Mach-O code signature if any
"""
if not self.__check_session():
return
if self.IS_MACHO and self.lief.has_code_signature:
rows = []
rows.append([
self.liefConstToString(self.lief.code_signature.command),
hex(self.lief.code_signature.command_offset),
"{:<6} bytes".format(self.lief.code_signature.size),
hex(self.lief.code_signature.data_offset),
"{:<6} bytes".format(self.lief.code_signature.data_size)
])
self.log("info", "MachO code signature : ")
self.log("table", dict(header=["Command", "Cmd offset", "Cmd size", "Data offset", "Date size"], rows=rows))
else:
self.log("warning", "No code signature found")
def exportedFunctions(self):
"""
Display ELf, PE, Mach-O and OAT exported functions if any
"""
if not self.__check_session():
return
if ((self.IS_MACHO and self.lief.exported_functions)
or (self.IS_OAT and self.lief.exported_functions)
or (self.IS_ELF and self.lief.exported_functions)
or (self.IS_PE and self.lief.exported_functions)):
self.log("info", "Exported functions : ")
for function in self.lief.exported_functions:
self.log("info", function)
else:
self.log("warning", "No exported function found")
def exportedSymbols(self):
"""
Display ELF, Mach-O and OAT exported symbols if any
"""
if not self.__check_session():
return
if (self.IS_OAT or self.IS_ELF) and self.lief.exported_symbols:
self.printElfAndOatSymbols(self.lief.exported_symbols, "Exported symbols")
elif self.IS_MACHO and self.lief.exported_symbols:
rows = []
for symbol in self.lief.exported_symbols:
rows.append([
symbol.name,
symbol.numberof_sections,
hex(symbol.value),
self.liefConstToString(symbol.origin)
])
self.log("info", "MachO exported symbols : ")
self.log("table", dict(header=["Name", "Nb section(s)", "Value", "Origin"], rows=rows))
else:
self.log("warning", "No exported symbol found")
def importedFunctions(self):
"""
Display ELF, PE, Mach-O and OAT imported functions if any
"""
if not self.__check_session():
return
if ((self.IS_MACHO and self.lief.imported_functions)
or (self.IS_OAT and self.lief.imported_functions)
or (self.IS_ELF and self.lief.imported_functions)
or (self.IS_PE and self.lief.imported_functions)):
self.log("info", "Imported functions : ")
for function in self.lief.imported_functions:
self.log("info", function)
else:
self.log("warning", "No imported function found")
def importedSymbols(self):
"""
Display ELF, Mach-O and OAT imported symbols if any
"""
if not self.__check_session():
return
rows = []
if (self.IS_OAT or self.IS_ELF) and self.lief.imported_symbols:
self.printElfAndOatSymbols(self.lief.imported_symbols, "Imported symbols")
elif self.IS_MACHO and self.lief.imported_symbols:
for symbol in self.lief.imported_symbols:
rows.append([
symbol.name,
symbol.numberof_sections,
hex(symbol.value),
self.liefConstToString(symbol.origin)
])
self.log("info", "MachO imported symbols : ")
self.log("table", dict(header=["Name", "Nb section(s)", "Value", "Origin"], rows=rows))
else:
self.log("warning", "No imported symbol found")
def sourceVersion(self):
"""
Display Mach-O source version if any
"""
if not self.__check_session():
return
if self.IS_MACHO and self.lief.has_source_version:
self.log("info", "Source version : ")
self.log("item", "{0:<10} : {1}".format("command", self.liefConstToString(self.lief.source_version.command)))
self.log("item", "{0:<10} : {1}".format("Offset", hex(self.lief.source_version.command_offset)))
self.log("item", "{0:<10} : {1} bytes".format("size", self.lief.source_version.size))
self.log("item", "{0:<10} : {1}".format("Version", self.listVersionToDottedVersion(self.lief.source_version.version)))
else:
self.log("warning", "No source version found")
def subFramework(self):
"""
Display Mach-O sub-framework if any
"""
if not self.__check_session():
return
if self.IS_MACHO and self.lief.has_sub_framework:
self.log("info", "Sub-framework : ")
self.log("item", "{0:<10} : {1}".format("Command", self.liefConstToString(self.lief.sub_framework.command)))
self.log("item", "{0:<10} : {1}".format("Offset", hex(self.lief.sub_framework.command_offset)))
self.log("item", "{0:<10} : {1} bytes".format("Size", self.lief.sub_framework.size))
self.log("item", "{0:<10} : {1}".format("Umbrella", self.lief.sub_framework.umbrella))
else:
self.log("warning", "No sub-framework found")
def uuid(self):
"""
Display Mach-O uuid if any
"""
if not self.__check_session():
return
if self.IS_MACHO and self.lief.has_uuid:
self.log("info", "Uuid : ")
self.log("item", "{0:<10} : {1}".format("Command", self.liefConstToString(self.lief.uuid.command)))
self.log("item", "{0:<10} : {1}".format("Offset", hex(self.lief.uuid.command_offset)))
self.log("item", "{0:<10} : {1} bytes".format("Size", self.lief.uuid.size))
self.log("item", "{0:<10} : {1}".format("Uuid", self.listUuidToUuid(self.lief.uuid.uuid)))
else:
self.log("warning", "No uuid found")
def dataInCode(self):
"""
Display Mach-O data in code if any
"""
if not self.__check_session():
return
if self.IS_MACHO and self.lief.has_data_in_code:
self.log("info", "Data in code : ")
self.log("item", "{0:<12} : {1}".format("Command", self.liefConstToString(self.lief.data_in_code.command)))
self.log("item", "{0:<12} : {1}".format("Offset", hex(self.lief.data_in_code.command_offset)))
self.log("item", "{0:<12} : {1} bytes".format("Size", self.lief.data_in_code.size))
self.log("item", "{0:<12} : {1}".format("Data Offset", hex(self.lief.data_in_code.data_offset)))
else:
self.log("warning", "No data in code found")
def mainCommand(self):
"""
Display Mach-O main command if any
"""
if not self.__check_session():
return
if self.IS_MACHO and self.lief.has_main_command:
self.log("info", "Main command : ")
self.log("item", "{0:<12} : {1}".format("Command", self.liefConstToString(self.lief.main_command.command)))
self.log("item", "{0:<12} : {1}".format("Offset", hex(self.lief.main_command.command_offset)))
self.log("item", "{0:<12} : {1} bytes".format("Size", self.lief.main_command.size))
self.log("item", "{0:<12} : {1}".format("Entrypoint", hex(self.lief.main_command.entrypoint)))
self.log("item", "{0:<12} : {1} bytes".format("Stack size", self.lief.main_command.stack_size))
else:
self.log("warning", "No main command found")
def commands(self):
"""
Display all Mach-O commands
"""
if not self.__check_session():
return
rows = []
if self.IS_MACHO and self.lief.commands:
for command in self.lief.commands:
rows.append([
self.liefConstToString(command.command),
"{0:<6} bytes".format(command.size),
hex(command.command_offset),
])
self.log("info", "MachO commands : ")
self.log("table", dict(header=["Command", "Size", "Offset"], rows=rows))
else:
self.log("warning", "No command found")
def dosHeader(self):
"""
Display PE DOS header
"""
if not self.__check_session():
return
if self.IS_PE:
self.log("info", "DOS header : ")
self.log("item", "{0:<28} : {1}".format("Magic", hex(self.lief.dos_header.magic)))
self.log("item", "{0:<28} : {1}".format("Address of new EXE header", hex(self.lief.dos_header.addressof_new_exeheader)))
self.log("item", "{0:<28} : {1}".format("Address of relocation table", hex(self.lief.dos_header.addressof_relocation_table)))
self.log("item", "{0:<28} : {1}".format("Checksum", hex(self.lief.dos_header.checksum)))
self.log("item", "{0:<28} : {1}".format("File size in pages", self.lief.dos_header.file_size_in_pages))
self.log("item", "{0:<28} : {1}".format("Header size in paragraphs", self.lief.dos_header.header_size_in_paragraphs))
self.log("item", "{0:<28} : {1}".format("Initial IP", self.lief.dos_header.initial_ip))
self.log("item", "{0:<28} : {1}".format("Initial relative CS", self.lief.dos_header.initial_relative_cs))
self.log("item", "{0:<28} : {1}".format("Initial relative SS", self.lief.dos_header.initial_relative_ss))
self.log("item", "{0:<28} : {1}".format("Initial SP", self.lief.dos_header.initial_sp))
self.log("item", "{0:<28} : {1}".format("Maximum extra paragraphs", self.lief.dos_header.maximum_extra_paragraphs))
self.log("item", "{0:<28} : {1}".format("Minimum extra paragraphs", self.lief.dos_header.minimum_extra_paragraphs))
self.log("item", "{0:<28} : {1}".format("Number of relocation", self.lief.dos_header.numberof_relocation))
self.log("item", "{0:<28} : {1}".format("OEM ID", self.lief.dos_header.oem_id))
self.log("item", "{0:<28} : {1}".format("OEM Info", self.lief.dos_header.oem_info))
self.log("item", "{0:<28} : {1}".format("Overlay number", self.lief.dos_header.overlay_number))
self.log("item", "{0:<28} : {1}".format("Used bytes in last page", self.lief.dos_header.used_bytes_in_the_last_page))
else:
self.log("warning", "No DOS header found")
def datadirectories(self):
"""
Display PE data directories if any
"""
if not self.__check_session():
return
rows = []
if self.IS_PE and self.lief.data_directories:
for datadirectory in self.lief.data_directories:
rows.append([
hex(datadirectory.rva),
"{0:<7} bytes".format(datadirectory.size),
self.liefConstToString(datadirectory.type),
datadirectory.section.name if datadirectory.has_section else '-'
])
self.log("info", "Data directories")
self.log("table", dict(header=["RVA", "Size", "Type", "Section"], rows=rows))
else:
self.log("warning", "No data directory found")
def dosStub(self):
"""
Disaply PE DOS stub
"""
if not self.__check_session():
return
if self.IS_PE:
rawDosStub = ""
for stub in self.lief.dos_stub:
if chr(stub) in string.printable.replace(string.whitespace, ''):
rawDosStub += chr(stub)
else:
rawDosStub += '.'
printableDosStub = ""
for i in range(0, len(rawDosStub), 16):
printableDosStub += rawDosStub[i:i + 16]
printableDosStub += '\n'
self.log("info", "{0}{1}".format('DOS stub : \n', printableDosStub))
else:
self.log("warning", "No DOS stub found")
def debug(self):
"""
Display PE debug information
"""
if not self.__check_session():
return
if self.IS_PE and self.lief.has_debug:
signature = ""
debug = self.lief.debug[0]
for sig in debug.code_view.signature:
signature += str(hex(sig))[2:]
self.log("info", "Debug information : ")
self.log("item", "{0:<28} : {1}".format("Address of Raw data", hex(debug.addressof_rawdata)))
self.log("item", "{0:<28} : {1}".format("Minor version of debug data", debug.minor_version))
self.log("item", "{0:<28} : {1}".format("Major version of debug data", debug.major_version))