forked from n0fate/iChainbreaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keybag.py
381 lines (311 loc) · 11.7 KB
/
keybag.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
from crypto.aes import AESdecryptCBC
from crypto.aes import AESencryptCBC
from crypto.aeswrap import aes_unwrap_key
import hmac
from crypto.pbkdf2 import pbkdf2
import hashlib
sha256 = hashlib.sha256
sha1 = hashlib.sha1
import sys
import struct
import uuid
from binascii import hexlify, unhexlify
from ctypes import *
KEYBAG_DATA = '>4sI'
KEYBAG_DATA_SIZE = 8
KEYBAG_HEADER = '>4sII4sII4sI16s4sI40s4sII4sI20s4sII'
KEYBAG_HEADER_SIZE = 148
KEYBAG_SIGN = '>4sI20s'
KEYBAG_SIGN_SIZE = 28
class _tlvint(BigEndianStructure):
_fields_ = [
("type", c_char*4),
("length", c_uint32),
("value", c_uint32)
]
class _tlvuuid(BigEndianStructure):
_fields_ = [
("type", c_char*4),
("length", c_uint32),
("value", c_ubyte*16)
]
class _tlvsalt(BigEndianStructure):
_fields_ = [
("type", c_char*4),
("length", c_uint32),
("value", c_ubyte*20)
]
class _tlvhmac(BigEndianStructure):
_fields_ = [
("type", c_char*4),
("length", c_uint32),
("value", c_ubyte*40)
]
class _keybag_sign(BigEndianStructure):
_fields_ = [
("type", c_char*4),
("length", c_uint32),
("value", c_ubyte*20)
]
class _keybag_header(BigEndianStructure):
_fields_ = [
("vers", _tlvint),
("type", _tlvint),
("uuid", _tlvuuid),
("hmck", _tlvhmac),
("wrap", _tlvint),
("salt", _tlvsalt),
("iter", _tlvint)
]
class _keybag_data(BigEndianStructure):
_fields_ = [
("data", c_char*4),
("datasize", c_uint32)
]
def _memcpy(buf, fmt):
return cast(c_char_p(buf), POINTER(fmt)).contents
def tlvs(data):
'''TLVs parser generator'''
while data:
try:
type, length = struct.unpack('!4sI', data[:8])
value = struct.unpack('!%is'%length, data[8:8+length])[0]
except:
print "Unproper TLV structure found: ", (data,)
break
yield type, value
data = data[8+length:]
# 'kc_parse_keyclass' in binary named 'secd'
def get_class_name(classnum):
if classnum == 1:
return 'NSFileProtectionComplete'
elif classnum == 2:
return 'NSFileProtectionCompleteUnlessOpen'
elif classnum == 3:
return 'NSFileProtectionCompleteUtilUserAuthentication'
elif classnum == 4:
return 'NSFileProtectionNone'
elif classnum == 5:
return 'NSFileProtectionRecovery'
elif classnum == 6:
return 'kSecAttrAccessibleWhenUnlocked'
elif classnum == 7:
return 'kSecAttrAccessibleAfterFirstUnlock'
elif classnum == 8:
return 'kSecAttrAccessibleAlways'
elif classnum == 9:
return 'kSecAttrAccessibleWhenUnlockedThisDeviceOnly'
elif classnum == 10:
return 'kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly'
elif classnum == 11:
return 'kSecAttrAccessibleAlwaysThisDeviceOnly'
elif classnum == 12:
return 'kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly'
else:
return 'unknown'
def get_key_type(keytypenum):
if keytypenum == 0:
return 'AES with GCM'
elif keytypenum == 1:
return 'Curve25519'
else:
return 'unknown'
def get_wrap_name(wrapnum):
if wrapnum == 1:
return 'AES encrypted with device key'
elif wrapnum == 3:
return 'Wrapped key after AES encrypted with device key'
else:
return 'unknown'
class Keybag():
def __init__(self, filepath):
#self.filepath = filepath
#self.fileoffset = 0
#fhandle = ''
self.devicekey = ''
self.endofdata = 0
self.keybag = {}
self.keyring = {} # Password Set
try:
fhandle = open(filepath, 'rb')
except:
print '[-] Keybag open failed'
sys.exit()
self.fbuf = fhandle.read()
fhandle.close()
def Decryption(self):
dict = {}
for type, data in tlvs(self.fbuf[KEYBAG_DATA_SIZE+KEYBAG_HEADER_SIZE:self.endofdata]):
#if type == 'UUID':
#print '%s : %s'%(type, uuid.UUID(bytes=data) )
if type == 'CLAS':
#print ' [-] %s : %s'%(type, get_class_name(int(hexlify(data), 16) ))
dict['CLAS'] = int(hexlify(data), 16)
elif type == 'WRAP':
#print '%s : %s'%(type, get_wrap_name(int(hexlify(data), 16) ))
dict['WRAP'] = int(hexlify(data), 16)
#elif type == 'KTYP':
#print '%s : %s'%(type, get_key_type(int(hexlify(data), 16) ))
elif type == 'WPKY':
decryptedkey = ''
if dict['WRAP'] == 1:
decryptedkey = AESdecryptCBC(data, self.devicekey)
#print ' [-] Decrypted Key : %s'%hexlify(decryptedkey)
elif dict['WRAP'] == 3:
try:
unwrapped = aes_unwrap_key(self.passcodekey, data)
except ValueError:
print '[!] Invalid Password. Enter the valid user password'
sys.exit()
decryptedkey = AESdecryptCBC(unwrapped, self.devicekey)
#print ' [-] Decrypted Key : %s'%hexlify(decryptedkey)
self.keyring[dict['CLAS']] = decryptedkey # Key
def GetKeybyClass(self, classnum):
key = ''
try:
key = self.keyring[classnum]
except:
key = ''
return key
def load_keybag_header(self):
keybag_data = _memcpy(self.fbuf[:sizeof(_keybag_data)], _keybag_data)
endofdata = sizeof(keybag_data) + keybag_data.datasize
self.endofdata = endofdata
self.keybag['data'] = self.fbuf[sizeof(_keybag_data):endofdata]
keybag_sign = _memcpy(self.fbuf[endofdata:endofdata+sizeof(_keybag_sign)], _keybag_sign)
self.keybag['sign'] = hexlify(keybag_sign.value)
keybag_header = _memcpy(self.fbuf[sizeof(_keybag_data):sizeof(_keybag_data)+sizeof(_keybag_header)], _keybag_header)
#if keybag_header[0] == 'VERS':
self.keybag['version'] = keybag_header.vers.value
#if keybag_header[3] == 'TYPE':
self.keybag['type'] = keybag_header.type.value
#if keybag_header[6] == 'UUID':
self.keybag['uuid'] = keybag_header.uuid.value
#if keybag_header[9] == 'HMCK':
self.keybag['hmck'] = hexlify(keybag_header.hmck.value)
#if keybag_header[12] == 'WRAP':
self.keybag['wrap'] = get_wrap_name(keybag_header.wrap.value)
#if keybag_header[15] == 'SALT':
self.keybag['salt'] = hexlify(keybag_header.salt.value)
#if keybag_header[18] == 'ITER':
self.keybag['iter'] = keybag_header.iter.value
def get_keybag_type(self, typenum):
if typenum == 0:
return 'System Keybag'
elif typenum == 1:
return 'Backup Keybag'
elif typenum == 2:
return 'Escrow Keybag'
elif typenum == 3:
return 'iCloud Keybag'
def get_wrap_type(self, wraptype):
if wraptype == 0:
return 'unknown'
elif wraptype == 1:
return 'AES encrypted with device key'
def debug_print_header(self):
print '[+] Keybag Header'
print ' [-] versions : %d'%self.keybag['version']
print ' [-] type : %s'%self.get_keybag_type(self.keybag['type'])
print ' [-] uuid : %s'%uuid.UUID(bytes=str(bytearray(self.keybag['uuid'])))
print ' [-] hmac key : %s'%self.keybag['hmck']
print ' [-] wrap : %s'%(self.get_wrap_type(self.keybag['wrap']))
print ' [-] salt : %s'%self.keybag['salt']
print ' [-] iteration count : %d'%self.keybag['iter']
def generatepasscodekey(self, passcode):
passcodekey_prf = pbkdf2(passcode, unhexlify(self.keybag['salt']), 1, 32, sha1)
#print 'The PRF passcode key is %s'%hexlify(passcodekey_prf)
self.passcodekey = self.tangle_with_hardware(passcodekey_prf, 32, self.keybag['iter'])
return self.passcodekey
def xor(self, data, key):
index = len(data) % 4
size = (4, 1, 2, 1)[index]
type = ('L', 'B', 'H', 'B')[index]
key_len = len(key)/size
data_len = len(data)/size
key_fmt = "<" + str(key_len) + type
data_fmt = "<" + str(data_len) + type
key_list = struct.unpack(key_fmt, key)
data_list = struct.unpack(data_fmt, data)
result = []
for i in range(data_len):
result.append (key_list[i % key_len] ^ data_list[i])
return struct.pack(data_fmt, *result)
def fill_buffer(self, BufSize, PRFKey, PRFKeyLen, xorKey):
NewHeap = ''
NewXorKey = xorKey
Count = 0
while Count < BufSize:
xoredPRFKey = ''
for inOffset in range(0, PRFKeyLen, 4):
xoredPRFKey += self.xor(PRFKey[inOffset:inOffset+4], struct.pack('i', NewXorKey))
NewXorKey += 1
NewHeap += xoredPRFKey
Count += PRFKeyLen
return NewXorKey, NewHeap
def hw_crypt_aligned(self, EncryptMethod, Unknown, filled_buf, BufSize, Version):
encrypted_buf = AESencryptCBC(filled_buf, self.devicekey)
return encrypted_buf
def xor_buffer(self, encryptedbuf, BUFSize, passcodePRFKey, Count):
KeySize = 32
NewHeap = passcodePRFKey
sCount = 0
while sCount < Count:
NewHeap = self.xor(NewHeap, encryptedbuf[sCount*KeySize:sCount*KeySize+KeySize])
sCount += 1
return NewHeap
# tangle with hardware
def tangle_with_hardware(self, PRFKey, PRFKeyLen, itercount):
DERIVATION_BUFFER_SIZE = 4096
nBlock = DERIVATION_BUFFER_SIZE / PRFKeyLen # 4096 / 32 = 128
xorkey = 1
passcodePRFKey = PRFKey
while itercount > 0:
newxorkey, filled_buf = self.fill_buffer(DERIVATION_BUFFER_SIZE, PRFKey, PRFKeyLen, xorkey)
xorkey = newxorkey
encrypted_buf = self.hw_crypt_aligned(1, 0, filled_buf, DERIVATION_BUFFER_SIZE, 2)
Count = nBlock
if Count >= itercount:
Count = itercount
passcodePRFKey = self.xor_buffer(encrypted_buf, PRFKeyLen*nBlock, passcodePRFKey, Count)
itercount -= Count
return passcodePRFKey
# 16bit input -> 32bit output
# Code : AppleKeyStore.kext -> AppleKeyStore::device_key_init()
def device_key_init(self, IOPlatformUUID):
salt = ''
self.devicekey = pbkdf2(IOPlatformUUID, salt, 50000, 32, sha256)
return self.devicekey
def device_key_validation(self):
if self.devicekey == '':
return False
if self.keybag['sign']:
hmackey = aes_unwrap_key(self.devicekey, unhexlify(self.keybag['hmck']))
sigcheck = hmac.new(key=hmackey, msg=self.keybag['data'], digestmod=sha1).digest()
if hexlify(sigcheck) != self.keybag['sign']:
return False
else:
return False
return True
def main():
try:
if len(sys.argv) != 2:
print 'index error'
sys.exit()
except IndexError:
print 'index error'
sys.exit()
try:
f = open(sys.argv[1], 'rb')
except IOError:
print '[+] WARNING!! Can not open keybag.'
sys.exit()
keybag = Keybag(sys.argv[1])
keybag.load_keybag_header()
keybag.debug_print_header()
devicekey = keybag.device_key_init()
print 'device key is %s'%hexlify(devicekey)
keybag.device_key_validation()
keybag.generatepasscodekey(sys.argv[3])
if __name__ == "__main__":
main()