aboutsummaryrefslogtreecommitdiff
path: root/scripts/parser.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/parser.py')
-rwxr-xr-xscripts/parser.py139
1 files changed, 139 insertions, 0 deletions
diff --git a/scripts/parser.py b/scripts/parser.py
new file mode 100755
index 0000000..e01c3c1
--- /dev/null
+++ b/scripts/parser.py
@@ -0,0 +1,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")