-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathimage.py
170 lines (146 loc) · 15.6 KB
/
image.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
#!/usr/bin/env python3
#
# Copyright (C) 2017 Freek van Tienen <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import sys
import getopt
import re
import os
import hashlib
import binascii
import configparser
import itertools
from Crypto.Cipher import AES
from ctypes import *
from time import gmtime, strftime, strptime
from calendar import timegm
from os.path import basename
# All found keys
keys = {
# Encryption keys
"RREK": bytes([0x37, 0xD6, 0xD9, 0x13, 0xE5, 0xD0, 0x80, 0x17, 0xE5, 0x12, 0x15, 0x45, 0x0C, 0x1E, 0x16, 0xE7]),
"RIEK": bytes([0xF1, 0x69, 0xC0, 0xF3, 0x8B, 0x2D, 0x9A, 0xDC, 0x65, 0xEE, 0x0C, 0x57, 0x83, 0x32, 0x94, 0xE9]),
"RUEK": bytes([0x9C, 0xDA, 0xF6, 0x27, 0x4E, 0xCB, 0x78, 0xF3, 0xED, 0xDC, 0xE5, 0x26, 0xBC, 0xEC, 0x66, 0xF8]),
"PUEK": bytes([0x70, 0xe0, 0x03, 0x08, 0xe0, 0x4b, 0x0a, 0xe2, 0xce, 0x8e, 0x07, 0xd4, 0xd6, 0x21, 0x4b, 0xb6]),
"DRAK": bytes([0x6f, 0x70, 0x7f, 0x29, 0x62, 0x35, 0x1d, 0x75, 0xbc, 0x08, 0x9a, 0xc3, 0x4d, 0xa1, 0x19, 0xfa]),
"SAAK": bytes([0x6f, 0x40, 0x2f, 0xb8, 0x62, 0x52, 0x05, 0xce, 0x9b, 0xdd, 0x58, 0x02, 0x17, 0xd2, 0x18, 0xd8]),
# RSA keys
"PRAK": bytes([0x40, 0x00, 0x00, 0x00, 0xC3, 0x15, 0x16, 0x41, 0x15, 0x7D, 0x30, 0x44, 0x8F, 0xEE, 0x89, 0x58, 0xD6, 0x84, 0x33, 0x2E, 0x8B, 0x28, 0x21, 0x3C, 0xDB, 0x05, 0xC9, 0x23, 0xE0, 0x6A, 0xFE, 0x2D, 0x13, 0x37, 0x1B, 0x48, 0x87, 0xC2, 0x87, 0x2F, 0x7F, 0xD6, 0x74, 0x49, 0x0E, 0x25, 0x00, 0x17, 0x18, 0x3A, 0x9F, 0xCF, 0xB4, 0x10, 0x9F, 0xDD, 0xD8, 0x6A, 0x55, 0x5F, 0xC8, 0x74, 0xB0, 0x8D, 0x64, 0x19, 0xC4, 0xB7, 0xFA, 0x7E, 0x03, 0xB8, 0xF1, 0x06, 0xA0, 0x8F, 0x57, 0x1E, 0x8C, 0x26, 0xA5, 0x32, 0xFC, 0x23, 0xE1, 0xDD, 0x0D, 0x7F, 0xE4, 0xD4, 0x96, 0x52, 0x3B, 0x08, 0xBC, 0x50, 0xD9, 0x23, 0x8A, 0x6B, 0xAA, 0xB5, 0x7D, 0x37, 0xA1, 0x3F, 0x3A, 0xFD, 0x91, 0x28, 0x4C, 0x8B, 0x98, 0xE2, 0xB4, 0x5E, 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args, file=sys.stderr, **kwargs)
class ImgPkgHeader(LittleEndianStructure):
_pack_ = 1
_fields_ = [('magic', c_char * 4), #0
('version', c_uint), #4
('total_size', c_uint), #8
('unk1', c_char * 4), #12
('header_size', c_uint), #16
('rsa_sig_size', c_uint), #20
('payload_size', c_uint), #24
('unk2', c_char * 12), #28
('auth_key', c_char * 4), #40
('enc_key', c_char * 4), #44
('scramble_key', c_ubyte * 16), #48
('image_name', c_char * 32), #64
('unk3', c_char * 60), #96
('blocks_cnt', c_uint), #156
('sha256_payload', c_ubyte * 32)] #160 end is 192
def dict_export(self):
d = dict()
for (varkey, vartype) in self._fields_:
if not varkey.startswith('unk'):
d[varkey] = getattr(self, varkey)
return d
def __repr__(self):
d = self.dict_export()
from pprint import pformat
return pformat(d, indent=4, width=1)
class ImgBlkHeader(LittleEndianStructure):
_pack_ = 1
_fields_ = [('name', c_char * 4), #0
('start_offset', c_uint), #4
('output_size', c_uint), #8
('attrib', c_uint), #12
('unk3', c_char * 16)] #16 end is 32
def dict_export(self):
d = dict()
for (varkey, vartype) in self._fields_:
if not varkey.startswith('unk'):
d[varkey] = getattr(self, varkey)
return d
def __repr__(self):
d = self.dict_export()
from pprint import pformat
return pformat(d, indent=4, width=1)
def main(argv):
filname_wo_ext = os.path.splitext(basename(argv[0]))[0]
image_file = open(argv[0], "rb")
# Decode the image header
header = ImgPkgHeader()
if image_file.readinto(header) != sizeof(header):
raise EOFError("Couldn't read image file header.")
# Check the magic
if header.magic != bytes("IM*H", "utf-8"):
print("Magic isn't correct in the header")
return
print(header)
# Get the encryption keys
enc_k_str = header.enc_key.decode("utf-8")
enc_key = None
if enc_k_str in keys:
enc_key = keys[enc_k_str]
else:
print("Can't find enc_key " + enc_k_str)
# Decode the blocks of the image
blocks = []
for i in range(0, header.blocks_cnt):
block = ImgBlkHeader()
if image_file.readinto(block) != sizeof(block):
raise EOFError("Couldn't read image block " + i + " header.")
blocks.append(block)
# Output the blocks
for block in blocks:
if (block.attrib & 0x01):
image_file.seek(header.header_size + header.rsa_sig_size + block.start_offset, 0)
file_buffer = image_file.read(block.output_size)
output_file = open(filname_wo_ext + '_' + block.name.decode("utf-8") + '.bin', "wb")
output_file.write(file_buffer)
output_file.close()
print("Unpacking block " + str(block))
elif enc_key != None:
# Set the key
if header.version == 1:
cipher = AES.new(enc_key, AES.MODE_CBC, bytes([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))
scramble_key = cipher.decrypt(header.scramble_key)
cipher_scrmb = AES.new(scramble_key, AES.MODE_CBC, bytes([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))
else:
cipher_scrmb = AES.new(enc_key, AES.MODE_CBC, header.scramble_key)
# Decrypt the data
image_file.seek(header.header_size + header.rsa_sig_size + block.start_offset, 0)
enc_buffer = image_file.read(block.output_size)
pad_cnt = (AES.block_size - len(enc_buffer) % AES.block_size) % AES.block_size
enc_buffer = enc_buffer + ((bytes([0])) * pad_cnt)
dec_buffer = cipher_scrmb.decrypt(enc_buffer)
output_file = open(filname_wo_ext + '_' + block.name.decode("utf-8") + '.bin', "wb")
output_file.write(dec_buffer)
output_file.close()
print("Unpacking encrypted block " + str(block))
else:
print("Can't unpack encoded block " + str(block))
image_file.close()
if __name__ == "__main__":
main(sys.argv[1:])