File: macho_reader.py

package info (click to toggle)
lief 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 16,036 kB
  • sloc: cpp: 76,013; python: 6,167; ansic: 3,355; pascal: 404; sh: 98; makefile: 32
file content (866 lines) | stat: -rw-r--r-- 28,216 bytes parent folder | download
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Description
# -----------
# Print information about a Mach-O binary

import sys
import os
import argparse
import traceback
import lief
from lief import MachO

from lief import Logger
Logger.set_level(lief.LOGGING_LEVEL.FATAL)

terminal_rows, terminal_columns = 100, 100
try:
    terminal_rows, terminal_columns = os.popen('stty size', 'r').read().split()
except ValueError:
    pass

terminal_columns = int(terminal_columns)
terminal_rows    = int(terminal_rows)
EXIT_STATUS = 0

class exceptions_handler(object):
    func = None

    def __init__(self, exceptions, on_except_callback=None):
        self.exceptions         = exceptions
        self.on_except_callback = on_except_callback

    def __call__(self, *args, **kwargs):
        if self.func is None:
            self.func = args[0]
            return self
        try:
            return self.func(*args, **kwargs)
        except self.exceptions as e:
            global EXIT_STATUS
            print("{} raised: {}".format(self.func.__name__, e))
            EXIT_STATUS = 1
            if self.on_except_callback is not None:
                self.on_except_callback(e)
            else:
                print("-" * 60)
                print("Exception in {}: {}".format(self.func.__name__, e))
                exc_type, exc_value, exc_traceback = sys.exc_info()
                traceback.print_tb(exc_traceback)
                print("-" * 60)



@exceptions_handler(Exception)
def print_information(binary):
    print("== Information ==")
    format_str = "{:<30} {:<30}"
    format_hex = "{:<30} 0x{:<28x}"
    format_dec = "{:<30} {:<30d}"
    print(format_str.format("Name:",         binary.name))
    print(format_hex.format("Address base:", binary.imagebase))
    print(format_str.format("PIE:",          str(binary.is_pie)))
    print(format_str.format("NX:",           str(binary.has_nx)))
    print("")

@exceptions_handler(Exception)
def print_header(binary):
    format_str = "{:<33} {:<30}"
    format_hex = "{:<33} 0x{:<28x}"
    format_dec = "{:<33} {:<30d}"

    print("== Header ==")
    header = binary.header
    flags_str =  " - ".join([str(s).split(".")[-1] for s in header.flags_list])

    print(format_str.format("Magic:",              str(header.magic).split(".")[-1]))
    print(format_str.format("CPU Type:",           str(header.cpu_type).split(".")[-1]))
    print(format_hex.format("CPU sub-type:",       header.cpu_subtype))
    print(format_str.format("File Type:",          str(header.file_type).split(".")[-1]))
    print(format_str.format("Flags:",              flags_str))
    print(format_dec.format("Number of commands:", header.nb_cmds))
    print(format_hex.format("Size of commands:",   header.sizeof_cmds))
    print(format_hex.format("Reserved:",           header.reserved))

@exceptions_handler(Exception)
def print_commands(binary):

    f_title = "|{:<20}|{:<11}|{:<11}|"
    f_value = "|{:<20}|0x{:08x} |0x{:08x} |"
    print("== Commands ==")
    print(f_title.format("Command", "Offset", "Size"))
    for command in binary.commands:
        print(f_value.format(
            str(command.command).split(".")[-1],
            command.command_offset,
            command.size))
    print("")

@exceptions_handler(Exception)
def print_libraries(binary):

    f_title = "|{:<30}|{:<10}|{:<16}|{:<22}|"
    f_value = "|{:<30}|{:<10d}|{:<16}|{:<22}|"
    print("== Libraries ==")
    print(f_title.format("Name", "Timestamp", "Current Version", "Compatibility Version"))
    for library in binary.libraries:
        current_version_str = "{:d}.{:d}.{:d}".format(*library.current_version)
        compatibility_version_str = "{:d}.{:d}.{:d}".format(*library.compatibility_version)
        print(f_value.format(
            library.name,
            library.timestamp,
            current_version_str,
            compatibility_version_str))
    print("")

@exceptions_handler(Exception)
def print_segments(binary):

    f_title = "|{:<20}|{:<16}|{:<16}|{:<16}|{:16}|{:16}|{:16}|{}"
    f_value = "|{:<20}|0x{:<13x} |0x{:<13x} |0x{:<13x} |0x{:<13x} |0x{:<13x} |0x{:<13x} |{}"
    print("== Segments ==")
    print(f_title.format(
        "Name", "Virtual Address", "Virtual Size",
        "Offset", "Size", "Max Protection",
        "Init Protection", "Sections"))
    for segment in binary.segments:
        sections = ", ".join(map(lambda s : s.name, segment.sections))
        print(f_value.format(
            segment.name,
            segment.virtual_address,
            segment.virtual_size,
            segment.file_offset,
            segment.file_size,
            segment.max_protection,
            segment.init_protection,
            sections))

    print("")

@exceptions_handler(Exception)
def print_sections(binary):

    f_title = "|{:<20}|{:<16}|{:<16}|{:<16}|{:16}|{:22}|{:19}|{:25}|"
    f_value = "|{:<20}|0x{:<13x} |0x{:<13x} |0x{:<13x} |0x{:<13x} |0x{:<19x} |0x{:<16x} |{:<25}|"
    print("== Sections ==")
    print(f_title.format(
        "Name", "Virtual Address", "Offset", "Size",
        "Alignement", "Number of Relocations", "Relocation offset",
        "Type"))
    for section in binary.sections:
        print(f_value.format(
            section.name,
            section.virtual_address,
            section.offset,
            section.size,
            section.alignment,
            section.numberof_relocations,
            section.relocation_offset,
            str(section.type).split(".")[-1]))
        if len(section.relocations) > 0:
            for idx, reloc in enumerate(section.relocations):
                name = reloc.symbol.name if reloc.has_symbol else ""
                secname = " - " + reloc.section.name if reloc.has_section else ""
                type = str(reloc.type)
                if reloc.architecture == MachO.CPU_TYPES.x86:
                    type = str(MachO.X86_RELOCATION(reloc.type))

                if reloc.architecture == MachO.CPU_TYPES.x86_64:
                    type = str(MachO.X86_64_RELOCATION(reloc.type))

                if reloc.architecture == MachO.CPU_TYPES.ARM:
                    type = str(MachO.ARM_RELOCATION(reloc.type))

                if reloc.architecture == MachO.CPU_TYPES.ARM64:
                    type = str(MachO.ARM64_RELOCATION(reloc.type))

                if reloc.architecture == MachO.CPU_TYPES.POWERPC:
                    type = str(MachO.PPC_RELOCATION(reloc.type))


                print("    [Reloc #{:d} {section}] {name:<10} 0x{address:<6x} {type:<20} {size:d} {pcrel} {scat}".format(
                    idx,
                    section=secname,
                    name=name,
                    address=reloc.address,
                    type=type.split(".")[-1],
                    size=reloc.size,
                    pcrel=str(reloc.pc_relative),
                    scat=str(reloc.is_scattered)))
            print("")


    print("")

@exceptions_handler(Exception)
def print_symbols(binary):
    symbols = binary.symbols
    if len(symbols) == 0:
        return
    try:
        maxsize = max([len(symbol.demangled_name) for symbol in symbols])
    except:
        maxsize = max([len(symbol.name) for symbol in symbols])
    maxsize = min(maxsize, terminal_columns - 90) if terminal_columns > 90 else terminal_columns

    f_title = "|{:<" + str(maxsize) + "} |{:<6}|{:<19}|{:<16}|{:16}| {:s}"
    f_value = "|{:<" + str(maxsize) + "} |0x{:<3x} |0x{:<16x} |0x{:<13x} |0x{:<13x} | {:s}"
    print("== Symbols ==")
    print(f_title.format(
        "Name", "Type", "Number of Sections", "Description", "Value", "Library"))
    for symbol in binary.symbols:
        libname = ""
        if symbol.has_binding_info and symbol.binding_info.has_library:
            libname = symbol.binding_info.library.name


        symbol_value = symbol.value if symbol.value > 0 or not symbol.has_binding_info else symbol.binding_info.address

        try:
            symbol_name = symbol.demangled_name
        except:
            symbol_name = symbol.name
        print(f_value.format(
            symbol_name,
            symbol.type,
            symbol.numberof_sections,
            symbol.description,
            symbol_value,
            libname))
    print("")

@exceptions_handler(Exception)
def print_symbol_command(binary):
    print("== Symbol Command ==")

    scmd = binary.symbol_command

    format_str = "{:<17} {:<30}"
    format_hex = "{:<17} 0x{:<28x}"
    format_dec = "{:<17} {:<30d}"

    print(format_hex.format("Symbol offset", scmd.symbol_offset))
    print(format_dec.format("Number of symbols", scmd.numberof_symbols))

    print(format_hex.format("String offset", scmd.strings_offset))
    print(format_hex.format("String size", scmd.strings_size))

    print("")

@exceptions_handler(Exception)
def print_dynamic_symbol_command(binary):
    print("== Dynamic Symbol Command ==")

    dyscmd = binary.dynamic_symbol_command

    format_str = "{:<36} {:<30}"
    format_hex = "{:<36} 0x{:<28x}"
    format_dec = "{:<36} {:<30d}"

    print(format_dec.format("First local symbol index", dyscmd.idx_local_symbol))
    print(format_dec.format("Number of local symbols", dyscmd.nb_local_symbols))

    print(format_dec.format("External symbol index", dyscmd.idx_external_define_symbol))
    print(format_dec.format("Number of external symbols", dyscmd.nb_external_define_symbols))

    print(format_dec.format("Undefined symbol index", dyscmd.idx_undefined_symbol))
    print(format_dec.format("Number of undefined symbols", dyscmd.nb_undefined_symbols))

    print(format_dec.format("Table of content offset", dyscmd.toc_offset))
    print(format_dec.format("Number of entries in TOC", dyscmd.nb_toc))

    print(format_hex.format("Module table offset", dyscmd.module_table_offset))
    print(format_dec.format("Number of entries in module table", dyscmd.nb_module_table))

    print(format_hex.format("External reference table offset", dyscmd.external_reference_symbol_offset))
    print(format_dec.format("Number of external reference", dyscmd.nb_external_reference_symbols))

    print(format_hex.format("Indirect symbols offset", dyscmd.indirect_symbol_offset))
    print(format_dec.format("Number of indirect symbols", dyscmd.nb_indirect_symbols))

    print(format_hex.format("External relocation offset", dyscmd.external_relocation_offset))
    print(format_dec.format("Number of external relocations", dyscmd.nb_external_relocations))

    print(format_hex.format("Local relocation offset", dyscmd.local_relocation_offset))
    print(format_dec.format("Number of local relocations", dyscmd.nb_local_relocations))

    print("")


@exceptions_handler(Exception)
def print_uuid(binary):
    print("== UUID ==")
    cmd = binary.uuid
    uuid_str = " ".join(map(lambda e : "{:02x}".format(e), cmd.uuid))
    print("UUID: {}".format(uuid_str))

    print("")


@exceptions_handler(Exception)
def print_main_command(binary):

    format_str = "{:<13} {:<30}"
    format_hex = "{:<13} 0x{:<28x}"
    format_dec = "{:<13} {:<30d}"

    print("== Main Command ==")
    cmd = binary.main_command

    print(format_hex.format("Entry point:", cmd.entrypoint))
    print(format_hex.format("Stack size:", cmd.stack_size))

    print("")


@exceptions_handler(Exception)
def print_thread_command(binary):

    format_str = "{:<13} {:<30}"
    format_hex = "{:<13} 0x{:<28x}"
    format_dec = "{:<13} {:<30d}"

    print("== Thread Command ==")
    cmd = binary.thread_command

    print(format_hex.format("Flavor:", cmd.flavor))
    print(format_hex.format("Count:",  cmd.count))
    print(format_hex.format("PC:",     cmd.pc))

    print("")

@exceptions_handler(Exception)
def print_rpath_command(binary):

    format_str = "{:<13} {:<30}"
    format_hex = "{:<13} 0x{:<28x}"
    format_dec = "{:<13} {:<30d}"

    print("== Rpath Command ==")
    cmd = binary.rpath
    print("Path: {}".format(cmd.path))


    print("")



@exceptions_handler(Exception)
def print_dylinker(binary):
    print("== Dylinker ==")
    print("Path: {}".format(binary.dylinker.name))

    print("")

@exceptions_handler(Exception)
def print_function_starts(binary):
    format_str = "{:<13} {:<30}"
    format_hex = "{:<13} 0x{:<28x}"
    format_dec = "{:<13} {:<30d}"

    print("== Function Starts ==")

    fstarts = binary.function_starts

    print(format_hex.format("Offset:", fstarts.data_offset))
    print(format_hex.format("Size:",   fstarts.data_size))
    print("Functions: ({:d})".format(len(fstarts.functions)))
    for idx, address in enumerate(fstarts.functions):
        print("    [{:d}] __TEXT + 0x{:x}".format(idx, address))

    print("")

@exceptions_handler(Exception)
def print_data_in_code(binary):
    format_str = "{:<13} {:<30}"
    format_hex = "{:<13} 0x{:<28x}"
    format_dec = "{:<13} {:<30d}"

    print("== Data In Code ==")

    datacode = binary.data_in_code

    print(format_hex.format("Offset:", datacode.data_offset))
    print(format_hex.format("Size:",   datacode.data_size))
    print("")
    for entry in datacode.entries:
        type_str = str(entry.type).split(".")[-1]
        print("- {:<14}: 0x{:x} ({:d} bytes)".format(type_str, entry.offset, entry.length))
    print("")


@exceptions_handler(Exception)
def print_segment_split_info(binary):
    format_str = "{:<13} {:<30}"
    format_hex = "{:<13} 0x{:<28x}"
    format_dec = "{:<13} {:<30d}"

    print("== Segment Split Info ==")

    sinfo = binary.segment_split_info

    print(format_hex.format("Offset:", sinfo.data_offset))
    print(format_hex.format("Size:",   sinfo.data_size))

    print("")


@exceptions_handler(Exception)
def print_sub_framework(binary):
    format_str = "{:<13} {:<30}"
    format_hex = "{:<13} 0x{:<28x}"
    format_dec = "{:<13} {:<30d}"

    print("== Sub Framework ==")

    sinfo = binary.sub_framework
    print(format_str.format("Umbrella:", sinfo.umbrella))

    print("")

@exceptions_handler(Exception)
def print_dyld_environment(binary):
    format_str = "{:<13} {:<30}"
    format_hex = "{:<13} 0x{:<28x}"
    format_dec = "{:<13} {:<30d}"

    print("== Dyld Environment ==")

    env = binary.dyld_environment
    print(format_str.format("Value:", env.value))

    print("")


@exceptions_handler(Exception)
def print_dyld_info(binary):
    print("== Dyld Info ==")
    f_title = "|{:<12}|{:<11}|{:<11}|"
    f_value = "|{:<12}|0x{:<8x} |0x{:<8x} |"

    dyld_info = binary.dyld_info

    print(f_title.format("Type", "Offset", "Size"))
    print(f_value.format("Rebase",    dyld_info.rebase[0],      dyld_info.rebase[1]))
    print(f_value.format("Bind",      dyld_info.bind[0],        dyld_info.bind[1]))
    print(f_value.format("Weak Bind", dyld_info.weak_bind[0],   dyld_info.weak_bind[1]))
    print(f_value.format("Lazy Bind", dyld_info.lazy_bind[0],   dyld_info.lazy_bind[1]))
    print(f_value.format("Export",    dyld_info.export_info[0], dyld_info.export_info[1]))

    print("")

    print("Bindings")
    print("--------")
    for idx, binfo in enumerate(dyld_info.bindings):
        print("{:10}: {}".format("Class", str(binfo.binding_class).split(".")[-1]))
        print("{:10}: {}".format("Type", str(binfo.binding_type).split(".")[-1]))
        print("{:10}: {:x}".format("Address", binfo.address))

        if binfo.has_symbol:
            print("{:10}: {}".format("Symbol", binfo.symbol.name))

        if binfo.has_segment:
            print("{:10}: {}".format("Segment", binfo.segment.name))

        if binfo.has_library:
            print("{:10}: {}".format("Library", binfo.library.name))

        print("")

    print("")

    print("Exports")
    print("-------")
    for idx, einfo in enumerate(dyld_info.exports):
        print("{:10}: {:<10x}".format("Address", einfo.address))
        print("{:10}: {:<10x}".format("Flags", einfo.flags))

        if binfo.has_symbol:
            print("{:10}: {}".format("Symbol", einfo.symbol.name))

        print("")



    print("")


@exceptions_handler(Exception)
def print_rebase_opcodes(binary):
    print("== Rebase opcodes ==")

    print(binary.dyld_info.show_rebases_opcodes)

    print("")

@exceptions_handler(Exception)
def print_bind_opcodes(binary):
    print("== Bind opcodes ==")

    print(binary.dyld_info.show_bind_opcodes)

    print("")

@exceptions_handler(Exception)
def print_weak_bind_opcodes(binary):
    print("== Weak bind opcodes ==")

    print(binary.dyld_info.show_weak_bind_opcodes)

    print("")

@exceptions_handler(Exception)
def print_lazy_bind_opcodes(binary):
    print("== Lazy bind opcodes ==")

    print(binary.dyld_info.show_lazy_bind_opcodes)

    print("")

@exceptions_handler(Exception)
def print_export_trie(binary):
    print("== Export trie ==")

    print(binary.dyld_info.show_export_trie)

    print("")

@exceptions_handler(Exception)
def print_source_version(binary):
    print("== Source Version ==")

    version = binary.source_version.version

    print("Version: {:d}.{:d}.{:d}.{:d}.{:d}".format(*version))

    print("")


@exceptions_handler(Exception)
def print_version_min(binary):
    print("== Version Min ==")

    version = binary.version_min.version
    sdk     = binary.version_min.sdk

    print("Version: {:d}.{:d}.{:d}".format(*version))
    print("SDK: {:d}.{:d}.{:d}".format(*sdk))

    print("")


@exceptions_handler(Exception)
def print_relocations(binary):
    print("== Relocations ==")

    f_value = "|0x{address:<10x} | {size:<4d} | {type:<15} | {pcrel:<11} | {secseg:<23} | {symbol}"
    f_title = "|{address:<12} | {size:<4} | {type:<15} | {pcrel:<11} | {secseg:<23} | {symbol}"

    print(f_title.format(
        address="Address",
        size="Size",
        type="Type",
        pcrel="PC Relative",
        secseg="Section/Section",
        symbol="Symbol"))


    for reloc in binary.relocations:
        type_str = ""
        if reloc.origin == lief.MachO.RELOCATION_ORIGINS.DYLDINFO:
            type_str = str(lief.MachO.REBASE_TYPES(reloc.type)).split(".")[-1]

        if reloc.origin == lief.MachO.RELOCATION_ORIGINS.RELOC_TABLE:
            if reloc.architecture == MachO.CPU_TYPES.x86:
                type_str = str(MachO.X86_RELOCATION(reloc.type))

            if reloc.architecture == MachO.CPU_TYPES.x86_64:
                type_str = str(MachO.X86_64_RELOCATION(reloc.type))

            if reloc.architecture == MachO.CPU_TYPES.ARM:
                type_str = str(MachO.ARM_RELOCATION(reloc.type))

            if reloc.architecture == MachO.CPU_TYPES.ARM64:
                type_str = str(MachO.ARM64_RELOCATION(reloc.type))

            if reloc.architecture == MachO.CPU_TYPES.POWERPC:
                type_str = str(MachO.PPC_RELOCATION(reloc.type))

            type_str = type_str.split(".")[-1]

        symbol_name = ""
        if reloc.has_symbol:
            symbol_name = reloc.symbol.name

        secseg_name = ""
        if reloc.has_segment and reloc.has_section:
            secseg_name = "{}.{}".format(reloc.segment.name, reloc.section.name)
        else:
            if reloc.has_segment:
                secseg_name = reloc.segment.name

            if reloc.has_section:
                secseg_name = reloc.section.name

        print(f_value.format(
            address=reloc.address,
            size=reloc.size,
            type=type_str,
            pcrel=str(reloc.pc_relative),
            secseg=secseg_name,
            symbol=symbol_name))


    print("")

@exceptions_handler(Exception)
def print_encryption_info(binary):

    format_str = "{:<13} {:<30}"
    format_hex = "{:<13} 0x{:<28x}"
    format_dec = "{:<13} {:<30d}"

    print("== Encryption Info ==")
    cmd = binary.encryption_info

    print(format_hex.format("Offset:", cmd.crypt_offset))
    print(format_hex.format("Size:",   cmd.crypt_size))
    print(format_dec.format("ID:",     cmd.crypt_id))

    print("")



def main():
    parser = argparse.ArgumentParser(usage='%(prog)s [options] <macho-file>')
    parser.add_argument('-a', '--all',
            action='store_true', dest='show_all',
            help='Show all information')

    parser.add_argument('-c', '--commands',
            action='store_true', dest='show_commands',
            help='Display Commands')

    parser.add_argument('-H', '--header',
            action='store_true', dest='show_header',
            help='Display header')

    parser.add_argument('-L', '--libraries',
            action='store_true', dest='show_libraries',
            help='Display Imported Libraries')

    parser.add_argument('-l', '--segments',
            action='store_true', dest='show_segments',
            help='Display Segments')

    parser.add_argument('-r', '--relocations',
            action='store_true', dest='show_relocs',
            help='Display the relocations (if present)')

    parser.add_argument('-s', '--symbols',
            action='store_true', dest='show_symbols',
            help='Display Symbols')

    parser.add_argument('-S', '--sections',
            action='store_true', dest='show_sections',
            help='Display Sections')

    parser.add_argument('--uuid',
            action='store_true', dest='show_uuid',
            help='Display the UUID command')

    parser.add_argument('--main',
            action='store_true', dest='show_main',
            help='Display the Main command')

    parser.add_argument('--dylinker',
            action='store_true', dest='show_dylinker',
            help='Display the Dylinker command')

    parser.add_argument('--dyldinfo',
            action='store_true', dest='show_dyldinfo',
            help='Display the DyldInfo command')

    parser.add_argument('--function-starts',
            action='store_true', dest='show_function_starts',
            help='Display the FunctionStarts command')

    parser.add_argument('--rebase-opcodes',
            action='store_true', dest='show_rebase_opcodes',
            help='Display the "Rebase" opcodes')

    parser.add_argument('--source-version',
            action='store_true', dest='show_source_version',
            help="Display the 'Source Version' command")

    parser.add_argument('--version-min',
            action='store_true', dest='show_version_min',
            help="Display the 'Version Min' command")

    parser.add_argument('--thread-command',
            action='store_true', dest='show_thread_command',
            help="Display the 'Thread Command' command")

    parser.add_argument('--rpath-command',
            action='store_true', dest='show_rpath_command',
            help="Display the 'Rpath Command' command")

    parser.add_argument('--symbol-command',
            action='store_true', dest='show_symbol_command',
            help="Display the 'Symbol Command' command")

    parser.add_argument('--dynamic-symbol-command',
            action='store_true', dest='show_dynamic_symbol_command',
            help="Display the 'Symbol Command' command")

    parser.add_argument('--data-in-code',
            action='store_true', dest='show_data_in_code',
            help="Display the 'Data In Code' command")

    parser.add_argument('--segment-split-info',
            action='store_true', dest='show_segment_split_info',
            help="Display the 'Segment Split Info' command")

    parser.add_argument('--sub-framework',
            action='store_true', dest='show_sub_framework',
            help="Display the 'Sub Framework' command")

    parser.add_argument('--dyld-environment',
            action='store_true', dest='show_dyld_env',
            help="Display the 'Dyld Environment' command")

    parser.add_argument('--encryption-info',
            action='store_true', dest='show_encrypt_info',
            help="Display the 'Encryption Info' command")

    parser.add_argument('--bind-opcodes',
            action='store_true', dest='show_bind_opcodes',
            help='Display the "Bind" opcodes')

    parser.add_argument('--weak-bind-opcodes',
            action='store_true', dest='show_weak_bind_opcodes',
            help='Display the "Weak Bind" opcodes')

    parser.add_argument('--lazy-bind-opcodes',
            action='store_true', dest='show_lazy_bind_opcodes',
            help='Display the "Lazy Bind" opcodes')

    parser.add_argument('--export-trie',
            action='store_true', dest='show_export_trie',
            help='Display the export trie')

    parser.add_argument('--opcodes',
            action='store_true', dest='show_opcodes',
            help='Display the bind and rebase opcodes')

    parser.add_argument("binary",
            metavar="<macho-file>",
            help='Target Mach-O File')

    args = parser.parse_args()


    binaries = None
    try:
        binaries = MachO.parse(args.binary)
    except lief.exception as e:
        print(e)
        sys.exit(1)

    for binary in binaries:
        print_information(binary)

        if args.show_header or args.show_all:
            print_header(binary)

        if args.show_commands or args.show_all:
            print_commands(binary)

        if args.show_libraries or args.show_all:
            print_libraries(binary)

        if args.show_segments or args.show_all:
            print_segments(binary)

        if args.show_sections or args.show_all:
            print_sections(binary)

        if args.show_symbols or args.show_all:
            print_symbols(binary)

        if (args.show_uuid or args.show_all) and binary.has_uuid:
            print_uuid(binary)

        if (args.show_main or args.show_all) and binary.has_main_command:
            print_main_command(binary)

        if (args.show_dylinker or args.show_all) and binary.has_dylinker:
            print_dylinker(binary)

        if (args.show_dyldinfo or args.show_all) and binary.has_dyld_info:
            print_dyld_info(binary)

        if (args.show_function_starts or args.show_all) and binary.has_function_starts:
            print_function_starts(binary)

        if (args.show_source_version or args.show_all) and binary.has_source_version:
            print_source_version(binary)

        if (args.show_version_min or args.show_all) and binary.has_version_min:
            print_version_min(binary)

        if (args.show_relocs or args.show_all) and len(binary.relocations) > 0:
            print_relocations(binary)

        if (args.show_thread_command or args.show_all) and binary.has_thread_command:
            print_thread_command(binary)

        if (args.show_rpath_command or args.show_all) and binary.has_rpath:
            print_rpath_command(binary)

        if (args.show_symbol_command or args.show_all) and binary.has_symbol_command:
            print_symbol_command(binary)

        if (args.show_dynamic_symbol_command or args.show_all) and binary.has_dynamic_symbol_command:
            print_dynamic_symbol_command(binary)

        if (args.show_data_in_code or args.show_all) and binary.has_data_in_code:
            print_data_in_code(binary)

        if (args.show_segment_split_info or args.show_all) and binary.has_segment_split_info:
            print_segment_split_info(binary)

        if (args.show_sub_framework or args.show_all) and binary.has_sub_framework:
            print_sub_framework(binary)

        if (args.show_dyld_env or args.show_all) and binary.has_dyld_environment:
            print_dyld_environment(binary)

        if (args.show_encrypt_info or args.show_all) and binary.has_encryption_info:
            print_encryption_info(binary)

        if (args.show_rpath_command or args.show_all) and binary.has_rpath:
            print_rpath_command(binary)

        if (args.show_rebase_opcodes or args.show_opcodes) and binary.has_dyld_info:
            print_rebase_opcodes(binary)

        if (args.show_bind_opcodes  or args.show_opcodes) and binary.has_dyld_info:
            print_bind_opcodes(binary)

        if (args.show_weak_bind_opcodes or args.show_opcodes) and binary.has_dyld_info:
            print_weak_bind_opcodes(binary)

        if (args.show_lazy_bind_opcodes or args.show_opcodes) and binary.has_dyld_info:
            print_lazy_bind_opcodes(binary)

        if (args.show_export_trie or args.show_opcodes) and binary.has_dyld_info:
            print_export_trie(binary)

        sys.exit(EXIT_STATUS)


if __name__ == "__main__":
    main()