aboutsummaryrefslogtreecommitdiff
path: root/scripts/parser.py
blob: e01c3c1ac78e7d69cdf9c6dfbe768212b6e9cebe (plain)
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
#!/usr/bin/python

# All credits to the one writing it up here:
# http://rotlogix.com/2015/11/28/writing-a-simple-binary-parser-with-python-ctypes
import sys
from ctypes import *


class DFUInitPacket(Structure):

    """

    struct dfu_init_packet {
      uint16_t      device_type;
      uint16_t    device_rev;
      uint32_t app_version;
      uint16_t      softdevice_len;
      uint16_t      softdevice;
      uint32_t      EXT_IDENTIFIER;
      uint32_t      EXT_IMAGE_LENGTH;
      uint32_t      hash_0_3;
      uint32_t      hash_4_7;
      uint32_t      hash_8_11;
      uint32_t      hash_12_15;
      uint32_t      hash_16_19;
      uint32_t      hash_20_23;
      uint32_t      hash_24_27;
      uint32_t      hash_28_31;
      uint32_t      sig_r_0_3;
      uint32_t      sig_r_4_7;
      uint32_t      sig_r_8_11;
      uint32_t      sig_r_12_15;
      uint32_t      sig_r_16_19;
      uint32_t      sig_r_20_23;
      uint32_t      sig_r_24_27;
      uint32_t      sig_r_28_31;
      uint32_t      sig_s_0_3;
      uint32_t      sig_s_4_7;
      uint32_t      sig_s_8_11;
      uint32_t      sig_s_12_15;
      uint32_t      sig_s_16_19;
      uint32_t      sig_s_20_23;
      uint32_t      sig_s_24_27;
      uint32_t      sig_s_28_31;
    };

    """

    _fields_ = [

        ("device_type", c_ushort),
        ("device_rev", c_ushort),
        ("app_version", c_uint),
        ("softdevice_len", c_ushort),
        ("softdevice", c_ushort),
        ("EXT_IDENTIFIER", c_uint),
        ("EXT_IMAGE_LENGTH", c_uint),
        ("hash_0_3", c_uint),
        ("hash_4_7", c_uint),
        ("hash_8_11", c_uint),
        ("hash_12_15", c_uint),
        ("hash_16_19", c_uint),
        ("hash_20_23", c_uint),
        ("hash_24_27", c_uint),
        ("hash_28_31", c_uint),
        ("sig_r_0_3", c_uint),
        ("sig_r_4_7", c_uint),
        ("sig_r_8_11", c_uint),
        ("sig_r_12_15", c_uint),
        ("sig_r_16_19", c_uint),
        ("sig_r_20_23", c_uint),
        ("sig_r_24_27", c_uint),
        ("sig_r_28_31", c_uint),
        ("sig_s_0_3", c_uint),
        ("sig_s_4_7", c_uint),
        ("sig_s_8_11", c_uint),
        ("sig_s_12_15", c_uint),
        ("sig_s_16_19", c_uint),
        ("sig_s_20_23", c_uint),
        ("sig_s_24_27", c_uint),
        ("sig_s_28_31", c_uint)
    ]

def run_parser(b):

    binary = None

    try:
        with open(b, "rb") as f:
            binary = bytearray(f.read())
            print("[*] Loading : {0}".format(b))
            f.close()
    except IOError:
        print("[*] Cannot open {0} (!) ".format(b))

    # Copy the binary into our DFUInitPacket structure
    dfu_init_packet = DFUInitPacket.from_buffer_copy(binary)

    print("device_type:      %s" % hex(dfu_init_packet.device_type))
    print("device_rev:       %s" % hex(dfu_init_packet.device_rev))
    print("app_version:      %s" % hex(dfu_init_packet.app_version))
    print("softdevice_len:   %s" % hex(dfu_init_packet.softdevice_len))
    print("softdevice:       %s" % hex(dfu_init_packet.softdevice))
    print("EXT_IDENTIFIER    %s" % hex(dfu_init_packet.EXT_IDENTIFIER))
    print("EXT_IMAGE_LENGTH  %s (%d) <- length of the bin-file" % (hex(dfu_init_packet.EXT_IMAGE_LENGTH), int(dfu_init_packet.EXT_IMAGE_LENGTH)))
    print("\033[1;35;40mhash[0-3]         0x%08x" % dfu_init_packet.hash_0_3)
    print("hash[4-7]         0x%08x" % dfu_init_packet.hash_4_7)
    print("hash[8-11]        0x%08x" % dfu_init_packet.hash_8_11)
    print("hash[12-15]       0x%08x" % dfu_init_packet.hash_12_15)
    print("hash[16-19]       0x%08x" % dfu_init_packet.hash_16_19)
    print("hash[20-23]       0x%08x" % dfu_init_packet.hash_20_23)
    print("hash[24-27]       0x%08x" % dfu_init_packet.hash_24_27)
    print("hash[28-31]       0x%08x" % dfu_init_packet.hash_28_31)
    print("\033[1;33;40msig_r[0-3]        0x%08x" % dfu_init_packet.sig_r_0_3)
    print("sig_r[4-7]        0x%08x" % dfu_init_packet.sig_r_4_7)
    print("sig_r[8-11]       0x%08x" % dfu_init_packet.sig_r_8_11)
    print("sig_r[12-15]      0x%08x" % dfu_init_packet.sig_r_12_15)
    print("sig_r[16-19]      0x%08x" % dfu_init_packet.sig_r_16_19)
    print("sig_r[20-23]      0x%08x" % dfu_init_packet.sig_r_20_23)
    print("sig_r[24-27]      0x%08x" % dfu_init_packet.sig_r_24_27)
    print("sig_r[28-31]      0x%08x" % dfu_init_packet.sig_r_28_31)
    print("\033[1;39;40msig_s[0-3]        0x%08x" % dfu_init_packet.sig_s_0_3)
    print("sig_s[4-7]        0x%08x" % dfu_init_packet.sig_s_4_7)
    print("sig_s[8-11]       0x%08x" % dfu_init_packet.sig_s_8_11)
    print("sig_s[12-15]      0x%08x" % dfu_init_packet.sig_s_12_15)
    print("sig_s[16-19]      0x%08x" % dfu_init_packet.sig_s_16_19)
    print("sig_s[20-23]      0x%08x" % dfu_init_packet.sig_s_20_23)
    print("sig_s[24-27]      0x%08x" % dfu_init_packet.sig_s_24_27)
    print("sig_s[28-31]      0x%08x" % dfu_init_packet.sig_s_28_31)

if __name__ == "__main__":
    if sys.argv[1]:
        try:
            run_parser(sys.argv[1])
        except KeyboardInterrupt:
            sys.exit(0)
    else:
        print("[*] Not enough arguments (!) ")
        print("[*] Usage : parser.py bin")