forked from n0fate/chainbreaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chainbreaker.py
executable file
·1398 lines (1152 loc) · 59.2 KB
/
chainbreaker.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
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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# Author : n0fate
# E-Mail [email protected], [email protected]
#
# 10/7/2020 - Significant changes made by [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 2 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
import struct
from pbkdf2 import PBKDF2
from schema import *
from schema import _APPL_DB_HEADER, _APPL_DB_SCHEMA, _TABLE_HEADER, _DB_BLOB, _GENERIC_PW_HEADER, \
_KEY_BLOB_REC_HEADER, _KEY_BLOB, _SSGP, _INTERNET_PW_HEADER, _APPLE_SHARE_HEADER, _X509_CERT_HEADER, _SECKEY_HEADER, \
_UNLOCK_BLOB, _KEYCHAIN_TIME, _INT, _FOUR_CHAR_CODE, _LV, _TABLE_ID, _RECORD_OFFSET
from pyDes import TripleDES, CBC
from binascii import unhexlify, hexlify
import logging
import base64
import string
import uuid
class Chainbreaker(object):
ATOM_SIZE = 4
KEYCHAIN_SIGNATURE = "kych"
BLOCKSIZE = 8
KEYLEN = 24
MAGIC_CMS_IV = unhexlify('4adda22c79e82105')
KEYCHAIN_LOCKED_SIGNATURE = '[Invalid Password / Keychain Locked]'
def __init__(self, filepath, unlock_password=None, unlock_key=None, unlock_file=None):
self._filepath = None
self._unlock_password = None
self._unlock_key = None
self._unlock_file = None
self._db_key = None
# Raw buffer of keychain file contents
self.kc_buffer = ''
self.header = None
self.schema_info = None
self.table_list = None
self.table_metadata = None
self.record_list = None
self.table_count = None
self.table_enum = None
self.symmetric_key_list = None
self.symmetric_key_offset = None
self.dbblob = None
self.locked = True
self.logger = logging.getLogger('Chainbreaker')
self.key_list = {}
self.db_key = None
self.filepath = filepath
if not self._is_valid_keychain():
self.logger.warning('Keychain signature does not match. are you sure this is a valid keychain file?')
self.unlock_password = unlock_password
self.unlock_key = unlock_key
self.unlock_file = unlock_file
# Returns a list of GenericPasswordRecord objects extracted from the Keychain
def dump_generic_passwords(self):
entries = []
try:
table_metadata, generic_pw_list = self._get_table_from_type(CSSM_DL_DB_RECORD_GENERIC_PASSWORD)
for generic_pw_id in generic_pw_list:
entries.append(self._get_generic_password_record(generic_pw_id))
except KeyError:
self.logger.warning('[!] Generic Password Table is not available')
return entries
# Returns a list of InterertPasswordRecord objects extracted from the Keychain
def dump_internet_passwords(self):
entries = []
try:
table_metadata, internet_pw_list = self._get_table_from_type(CSSM_DL_DB_RECORD_INTERNET_PASSWORD)
for internet_pw_id in internet_pw_list:
entries.append(self._get_internet_password_record(internet_pw_id))
except KeyError:
self.logger.warning('[!] Internet Password Table is not available')
return entries
# Returns a list of AppleshareRecord objects extracted from the Keychain
def dump_appleshare_passwords(self):
entries = []
try:
table_metadata, appleshare_pw_list = self._get_table_from_type(CSSM_DL_DB_RECORD_APPLESHARE_PASSWORD)
for appleshare_pw_offset in appleshare_pw_list:
entries.append(self._get_appleshare_record(appleshare_pw_offset))
except KeyError:
self.logger.warning('[!] Appleshare Records Table is not available')
return entries
# Returns a list of X509CertfificateRecord objects extracted from the Keychain
def dump_x509_certificates(self):
entries = []
try:
table_metadata, x509_cert_list = self._get_table_from_type(CSSM_DL_DB_RECORD_X509_CERTIFICATE)
for i, x509_cert_offset in enumerate(x509_cert_list, 1):
entries.append(self._get_x_509_record(x509_cert_offset))
except KeyError:
self.logger.warning('[!] Certificate Table is not available')
return entries
# Returns a list of PublicKeyRecord objects extracted from the Keychain
def dump_public_keys(self):
entries = []
try:
table_metadata, public_key_list = self._get_table_from_type(CSSM_DL_DB_RECORD_PUBLIC_KEY)
for public_key_offset in public_key_list:
entries.append(
self._get_public_key_record(public_key_offset))
except KeyError:
self.logger.warning('[!] Public Key Table is not available')
return entries
# Returns a list of PrivateKeyRecord objects extracted from the Keychain
def dump_private_keys(self):
entries = []
try:
table_meta, private_key_list = self._get_table_from_type(CSSM_DL_DB_RECORD_PRIVATE_KEY)
for i, private_key_offset in enumerate(private_key_list, 1):
entries.append(
self._get_private_key_record(private_key_offset))
except KeyError:
self.logger.warning('[!] Private Key Table is not available')
return entries
# Attempts to read the keychain file into self.kc_buffer
# On success it extracts out relevant information (table information, key offsets, and the DB BLob)
def _read_keychain_to_buffer(self):
try:
with open(self.filepath, 'rb') as fp:
self.kc_buffer = fp.read()
if self.kc_buffer:
self.header = _APPL_DB_HEADER(self.kc_buffer[:_APPL_DB_HEADER.STRUCT.size])
self.schema_info, self.table_list = self._get_schema_info(self.header.SchemaOffset)
self.table_metadata, self.record_list = self._get_table(self.table_list[0])
self.table_count, self.table_enum = self._get_table_name_to_list(self.record_list, self.table_list)
self.symmetric_key_offset = self.table_list[self.table_enum[CSSM_DL_DB_RECORD_METADATA]]
self.base_addr = _APPL_DB_HEADER.STRUCT.size + self.symmetric_key_offset + 0x38
self.dbblob = _DB_BLOB(self.kc_buffer[self.base_addr:self.base_addr + _DB_BLOB.STRUCT.size])
except OSError as e:
self.logger.critical("Unable to read keychain: %s" % e)
# Simple check to make sure the keychain we're looking at is valid.
# A valid keychain begins with "kych"
def _is_valid_keychain(self):
if self.kc_buffer[0:4] != Chainbreaker.KEYCHAIN_SIGNATURE:
return False
return True
# When the keychain is successfully decrypted ("unlocked"), we can obtain a list of encryption keys
# used to encrypt individual records, indexed off of the SSGB label.
def _generate_key_list(self):
table_meta_data, symmetric_key_list = self._get_table_from_type(CSSM_DL_DB_RECORD_SYMMETRIC_KEY)
for symmetric_key_record in symmetric_key_list:
keyblob, ciphertext, iv, return_value = self._get_keyblob_record(symmetric_key_record)
if return_value == 0:
password = Chainbreaker.keyblob_decryption(ciphertext, iv, self.db_key)
if password != '':
self.key_list[keyblob] = password
return len(self.key_list)
# Returns basic schema (table count, size) and a list of the tables from the Keychain file.
def _get_schema_info(self, offset):
table_list = []
schema_info = _APPL_DB_SCHEMA(self.kc_buffer[offset:offset + _APPL_DB_SCHEMA.STRUCT.size])
for i in xrange(schema_info.TableCount):
base_addr = _APPL_DB_HEADER.STRUCT.size + _APPL_DB_SCHEMA.STRUCT.size
table_list.append(_TABLE_ID(self.kc_buffer[base_addr + (Chainbreaker.ATOM_SIZE * i):base_addr + (
Chainbreaker.ATOM_SIZE * i) + Chainbreaker.ATOM_SIZE]).Value)
return schema_info, table_list
# Given a table name, return the offset for the table
def _get_table_offset(self, table_name):
return self.table_list[self.table_enum[table_name]]
# Returns a table, given the AppleFileDL CSSM_DB_RECORDTYPE from schema.py
# (e.g. CSSM_DL_DB_RECORD_GENERIC_PASSWORD)
def _get_table_from_type(self, table_type):
return self._get_table(self._get_table_offset(table_type))
# Returns a both the metadata and a record list for a table, given an offset.
def _get_table(self, offset):
record_list = []
base_addr = _APPL_DB_HEADER.STRUCT.size + offset
table_metadata = _TABLE_HEADER(self.kc_buffer[base_addr:base_addr + _TABLE_HEADER.STRUCT.size])
record_offset_base = base_addr + _TABLE_HEADER.STRUCT.size
record_count = 0
offset = 0
while table_metadata.RecordCount != record_count:
record_offset = _RECORD_OFFSET(self.kc_buffer[
record_offset_base + (Chainbreaker.ATOM_SIZE * offset):record_offset_base + (
Chainbreaker.ATOM_SIZE * offset) + Chainbreaker.ATOM_SIZE]).Value
if (record_offset != 0x00) and (record_offset % 4 == 0):
record_list.append(record_offset)
record_count += 1
offset += 1
return table_metadata, record_list
# Returns a dict of table indexes keyed off of the TableId
def _get_table_name_to_list(self, record_list, table_list):
table_dict = {}
for count in xrange(len(record_list)):
table_metadata, generic_list = self._get_table(table_list[count])
table_dict[table_metadata.TableId] = count # extract valid table list
return len(record_list), table_dict
def _get_keyblob_record(self, record_offset):
base_addr = self._get_base_address(CSSM_DL_DB_RECORD_SYMMETRIC_KEY, record_offset)
key_blob_record_header = _KEY_BLOB_REC_HEADER(
self.kc_buffer[base_addr:base_addr + _KEY_BLOB_REC_HEADER.STRUCT.size])
record = self.kc_buffer[
base_addr + _KEY_BLOB_REC_HEADER.STRUCT.size:base_addr + key_blob_record_header.RecordSize]
key_blob_record = _KEY_BLOB(record[:+_KEY_BLOB.STRUCT.size])
if SECURE_STORAGE_GROUP != str(record[key_blob_record.TotalLength + 8:key_blob_record.TotalLength + 8 + 4]):
return '', '', '', 1
cipher_len = key_blob_record.TotalLength - key_blob_record.StartCryptoBlob
if cipher_len % Chainbreaker.BLOCKSIZE != 0:
self.logger.debug("Bad ciphertext length.")
return '', '', '', 1
cipher_text = record[key_blob_record.StartCryptoBlob:key_blob_record.TotalLength]
# match data, keyblob_ciphertext, Initial Vector, success
return record[
key_blob_record.TotalLength + 8:key_blob_record.TotalLength + 8 + 20], cipher_text, key_blob_record.IV, 0
# Get a timestamp from the keychain buffer
def _get_keychain_time(self, base_addr, pcol):
if pcol <= 0:
return ''
else:
return _KEYCHAIN_TIME(self.kc_buffer[base_addr + pcol:base_addr + pcol + _KEYCHAIN_TIME.STRUCT.size]).Time
# Get an integer from the keychain buffer
def _get_int(self, base_addr, pcol):
if pcol <= 0:
return 0
else:
return _INT(self.kc_buffer[base_addr + pcol:base_addr + pcol + 4]).Value
# Get 4 character code from the keychain buffer
def _get_four_char_code(self, base_addr, pcol):
if pcol <= 0:
return ''
else:
return _FOUR_CHAR_CODE(self.kc_buffer[base_addr + pcol:base_addr + pcol + 4]).Value
# Get an lv from the keychain buffer
def _get_lv(self, base_addr, pcol):
if pcol <= 0:
return ''
str_length = _INT(self.kc_buffer[base_addr + pcol:base_addr + pcol + 4]).Value
# 4byte arrangement
if (str_length % 4) == 0:
real_str_len = (str_length / 4) * 4
else:
real_str_len = ((str_length / 4) + 1) * 4
try:
data = _LV(self.kc_buffer[base_addr + pcol + 4:base_addr + pcol + 4 + real_str_len], real_str_len).Value
except struct.error:
self.logger.debug('LV string length is too long.')
return ''
return data
#
# # http://opensource.apple.com/source/libsecurity_keychain/libsecurity_keychain-55044/lib/KeyItem.cpp
def _private_key_decryption(self, encryptedblob, iv):
plain = Chainbreaker._kcdecrypt(self.db_key, Chainbreaker.MAGIC_CMS_IV, encryptedblob)
if plain.__len__() == 0:
return '', ''
# now we handle the unwrapping. we need to take the first 32 bytes,
# and reverse them.
revplain = ''
for i in range(len(plain)):
revplain += plain[len(plain) - 1 - i]
# now the real key gets found. */
plain = Chainbreaker._kcdecrypt(self.db_key, iv, revplain)
keyname = plain[:12] # Copied Buffer when user click on right and copy a key on Keychain Access
keyblob = plain[12:]
return keyname, keyblob
# ## Documents : http://www.opensource.apple.com/source/securityd/securityd-55137.1/doc/BLOBFORMAT
def _generate_master_key(self, pw):
return str(PBKDF2(pw, str(bytearray(self.dbblob.Salt)), 1000, Chainbreaker.KEYLEN))
# ## find DBBlob and extract Wrapping key
def _find_wrapping_key(self, master):
# get cipher text area
ciphertext = self.kc_buffer[
self.base_addr + self.dbblob.StartCryptoBlob:self.base_addr + self.dbblob.TotalLength]
# decrypt the key
plain = Chainbreaker._kcdecrypt(master, self.dbblob.IV, ciphertext)
if plain.__len__() < Chainbreaker.KEYLEN:
return ''
dbkey = plain[:Chainbreaker.KEYLEN]
# return encrypted wrapping key
return dbkey
# Extract the Cyphertext, IV, and Salt for the keychain file, for use with offline cracking (e.g. Hashcat)
# Returns a KeychainPasswordHash object
def dump_keychain_password_hash(self):
cyphertext = hexlify(
self.kc_buffer[self.base_addr + self.dbblob.StartCryptoBlob:self.base_addr + self.dbblob.TotalLength])
iv = hexlify(self.dbblob.IV)
salt = hexlify(self.dbblob.Salt)
return self.KeychainPasswordHash(salt, iv, cyphertext)
# Given a base address and offset (ID) of a record,
def _get_appleshare_record(self, record_offset):
base_addr = self._get_base_address(CSSM_DL_DB_RECORD_APPLESHARE_PASSWORD, record_offset)
record_meta = _APPLE_SHARE_HEADER(self.kc_buffer[base_addr:base_addr + _APPLE_SHARE_HEADER.STRUCT.size])
buffer = self.kc_buffer[base_addr + _APPLE_SHARE_HEADER.STRUCT.size:base_addr + record_meta.RecordSize]
ssgp, dbkey = self._extract_ssgp_and_dbkey(record_meta, buffer)
return self.AppleshareRecord(
created=self._get_keychain_time(base_addr, record_meta.CreationDate & 0xFFFFFFFE),
last_modified=self._get_keychain_time(base_addr, record_meta.ModDate & 0xFFFFFFFE),
description=self._get_lv(base_addr, record_meta.Description & 0xFFFFFFFE),
comment=self._get_lv(base_addr, record_meta.Comment & 0xFFFFFFFE),
creator=self._get_four_char_code(base_addr, record_meta.Creator & 0xFFFFFFFE),
type=self._get_four_char_code(base_addr, record_meta.Type & 0xFFFFFFFE),
print_name=self._get_lv(base_addr, record_meta.PrintName & 0xFFFFFFFE),
alias=self._get_lv(base_addr, record_meta.Alias & 0xFFFFFFFE),
protected=self._get_lv(base_addr, record_meta.Protected & 0xFFFFFFFE),
account=self._get_lv(base_addr, record_meta.Account & 0xFFFFFFFE),
volume=self._get_lv(base_addr, record_meta.Volume & 0xFFFFFFFE),
server=self._get_lv(base_addr, record_meta.Server & 0xFFFFFFFE),
protocol_type=self._get_four_char_code(base_addr, record_meta.Protocol & 0xFFFFFFFE),
address=self._get_lv(base_addr, record_meta.Address & 0xFFFFFFFE),
signature=self._get_lv(base_addr, record_meta.Signature & 0xFFFFFFFE),
ssgp=ssgp,
dbkey=dbkey
)
def _get_private_key_record(self, record_offset):
record = self._get_key_record(self._get_table_offset(CSSM_DL_DB_RECORD_PRIVATE_KEY), record_offset)
if not self.db_key:
keyname = privatekey = Chainbreaker.KEYCHAIN_LOCKED_SIGNATURE
else:
keyname, privatekey = self._private_key_decryption(record[10], record[9])
return self.PrivateKeyRecord(
print_name=record[0],
label=record[1],
key_class=KEY_TYPE[record[2]],
private=record[3],
key_type=record[4],
key_size=record[5],
effective_key_size=record[6],
extracted=record[7],
cssm_type=record[8],
iv=record[9],
key=record[10],
key_name=keyname,
private_key=privatekey,
)
def _get_public_key_record(self, record_offset):
record = self._get_key_record(self._get_table_offset(CSSM_DL_DB_RECORD_PUBLIC_KEY), record_offset)
return self.PublicKeyRecord(
print_name=record[0],
label=record[1],
key_class=KEY_TYPE[record[2]],
private=record[3],
key_type=record[4],
key_size=record[5],
effective_key_size=record[6],
extracted=record[7],
cssm_type=record[8],
iv=record[9],
public_key=record[10],
)
def _get_key_record(self, table_name, record_offset): ## PUBLIC and PRIVATE KEY
base_addr = self._get_base_address(table_name, record_offset)
record_meta = _SECKEY_HEADER(self.kc_buffer[base_addr:base_addr + _SECKEY_HEADER.STRUCT.size])
key_blob = self.kc_buffer[
base_addr + _SECKEY_HEADER.STRUCT.size:base_addr + _SECKEY_HEADER.STRUCT.size + record_meta.BlobSize]
iv, key = Chainbreaker._get_encrypted_data_in_blob(key_blob)
return [self._get_lv(base_addr, record_meta.PrintName & 0xFFFFFFFE),
self._get_lv(base_addr, record_meta.Label & 0xFFFFFFFE),
self._get_int(base_addr, record_meta.KeyClass & 0xFFFFFFFE),
self._get_int(base_addr, record_meta.Private & 0xFFFFFFFE),
CSSM_ALGORITHMS[self._get_int(base_addr, record_meta.KeyType & 0xFFFFFFFE)],
self._get_int(base_addr, record_meta.KeySizeInBits & 0xFFFFFFFE),
self._get_int(base_addr, record_meta.EffectiveKeySize & 0xFFFFFFFE),
self._get_int(base_addr, record_meta.Extractable & 0xFFFFFFFE),
STD_APPLE_ADDIN_MODULE[
str(self._get_lv(base_addr, record_meta.KeyCreator & 0xFFFFFFFE)).split('\x00')[0]],
iv,
key]
def _get_x_509_record(self, record_offset):
base_addr = self._get_base_address(CSSM_DL_DB_RECORD_X509_CERTIFICATE, record_offset)
record_meta = _X509_CERT_HEADER(self.kc_buffer[base_addr:base_addr + _X509_CERT_HEADER.STRUCT.size])
return self.X509CertificateRecord(
type=self._get_int(base_addr, record_meta.CertType & 0xFFFFFFFE),
encoding=self._get_int(base_addr, record_meta.CertEncoding & 0xFFFFFFFE),
print_name=self._get_lv(base_addr, record_meta.PrintName & 0xFFFFFFFE),
alias=self._get_lv(base_addr, record_meta.Alias & 0xFFFFFFFE),
subject=self._get_lv(base_addr, record_meta.Subject & 0xFFFFFFFE),
issuer=self._get_lv(base_addr, record_meta.Issuer & 0xFFFFFFFE),
serial_number=self._get_lv(base_addr, record_meta.SerialNumber & 0xFFFFFFFE),
subject_key_identifier=self._get_lv(base_addr, record_meta.SubjectKeyIdentifier & 0xFFFFFFFE),
public_key_hash=self._get_lv(base_addr, record_meta.PublicKeyHash & 0xFFFFFFFE),
certificate=self.kc_buffer[
base_addr + _X509_CERT_HEADER.STRUCT.size:base_addr + _X509_CERT_HEADER.STRUCT.size + record_meta.CertSize]
)
def _extract_ssgp_and_dbkey(self, record_meta, buffer):
ssgp = None
dbkey = None
if record_meta.SSGPArea != 0:
ssgp = _SSGP(buffer[:record_meta.SSGPArea])
dbkey_index = ssgp.Magic + ssgp.Label
if dbkey_index in self.key_list:
dbkey = self.key_list[dbkey_index]
return ssgp, dbkey
def _get_internet_password_record(self, record_offset):
base_addr = self._get_base_address(CSSM_DL_DB_RECORD_INTERNET_PASSWORD, record_offset)
record_meta = _INTERNET_PW_HEADER(self.kc_buffer[base_addr:base_addr + _INTERNET_PW_HEADER.STRUCT.size])
buffer = self.kc_buffer[base_addr + _INTERNET_PW_HEADER.STRUCT.size:base_addr + record_meta.RecordSize]
ssgp, dbkey = self._extract_ssgp_and_dbkey(record_meta, buffer)
return self.InternetPasswordRecord(
created=self._get_keychain_time(base_addr, record_meta.CreationDate & 0xFFFFFFFE),
last_modified=self._get_keychain_time(base_addr, record_meta.ModDate & 0xFFFFFFFE),
description=self._get_lv(base_addr, record_meta.Description & 0xFFFFFFFE),
comment=self._get_lv(base_addr, record_meta.Comment & 0xFFFFFFFE),
creator=self._get_four_char_code(base_addr, record_meta.Creator & 0xFFFFFFFE),
type=self._get_four_char_code(base_addr, record_meta.Type & 0xFFFFFFFE),
print_name=self._get_lv(base_addr, record_meta.PrintName & 0xFFFFFFFE),
alias=self._get_lv(base_addr, record_meta.Alias & 0xFFFFFFFE),
protected=self._get_lv(base_addr, record_meta.Protected & 0xFFFFFFFE),
account=self._get_lv(base_addr, record_meta.Account & 0xFFFFFFFE),
security_domain=self._get_lv(base_addr, record_meta.SecurityDomain & 0xFFFFFFFE),
server=self._get_lv(base_addr, record_meta.Server & 0xFFFFFFFE),
protocol_type=self._get_four_char_code(base_addr, record_meta.Protocol & 0xFFFFFFFE),
auth_type=self._get_lv(base_addr, record_meta.AuthType & 0xFFFFFFFE),
port=self._get_int(base_addr, record_meta.Port & 0xFFFFFFFE),
path=self._get_lv(base_addr, record_meta.Path & 0xFFFFFFFE),
ssgp=ssgp,
dbkey=dbkey
)
def _get_generic_password_record(self, record_offset):
base_addr = self._get_base_address(CSSM_DL_DB_RECORD_GENERIC_PASSWORD, record_offset)
record_meta = _GENERIC_PW_HEADER(self.kc_buffer[base_addr:base_addr + _GENERIC_PW_HEADER.STRUCT.size])
buffer = self.kc_buffer[
base_addr + _GENERIC_PW_HEADER.STRUCT.size:base_addr + record_meta.RecordSize]
ssgp, dbkey = self._extract_ssgp_and_dbkey(record_meta, buffer)
return self.GenericPasswordRecord(
created=self._get_keychain_time(base_addr, record_meta.CreationDate & 0xFFFFFFFE),
last_modified=self._get_keychain_time(base_addr, record_meta.ModDate & 0xFFFFFFFE),
description=self._get_lv(base_addr, record_meta.Description & 0xFFFFFFFE),
creator=self._get_four_char_code(base_addr, record_meta.Creator & 0xFFFFFFFE),
type=self._get_four_char_code(base_addr, record_meta.Type & 0xFFFFFFFE),
print_name=self._get_lv(base_addr, record_meta.PrintName & 0xFFFFFFFE),
alias=self._get_lv(base_addr, record_meta.Alias & 0xFFFFFFFE),
account=self._get_lv(base_addr, record_meta.Account & 0xFFFFFFFE),
service=self._get_lv(base_addr, record_meta.Service & 0xFFFFFFFE),
ssgp=ssgp,
dbkey=dbkey)
return record
def _get_base_address(self, table_name, offset=None):
if table_name >= 0x4000 and table_name < 0x5000:
table_name = 15
if table_name >= 0x5000 and table_name < 0x6000:
table_name = 16
base_address = _APPL_DB_HEADER.STRUCT.size + self._get_table_offset(table_name)
if offset:
base_address += offset
return base_address
@property
def filepath(self):
return self._filepath
@filepath.setter
def filepath(self, value):
self._filepath = value
if self._filepath:
self._read_keychain_to_buffer()
@property
def unlock_password(self):
return self._unlock_password
@unlock_password.setter
def unlock_password(self, unlock_password):
self._unlock_password = unlock_password
if self._unlock_password:
master_key = self._generate_master_key(self._unlock_password)
self.db_key = self._find_wrapping_key(master_key)
@property
def unlock_key(self):
return self._unlock_key
@unlock_key.setter
def unlock_key(self, unlock_key):
self._unlock_key = unlock_key
if self._unlock_key:
self.db_key = self._find_wrapping_key(unhexlify(self._unlock_key))
@property
def unlock_file(self):
return self._unlock_file
@unlock_file.setter
def unlock_file(self, filepath):
self._unlock_file = filepath
if self._unlock_file:
try:
with open(self._unlock_file, mode='rb') as uf:
file_content = uf.read()
unlock_key_blob = _UNLOCK_BLOB(file_content)
self.db_key = self._find_wrapping_key(unlock_key_blob.MasterKey)
except OSError:
logger.warning("Unable to read unlock file: %s" % self._unlock_file)
@property
def db_key(self):
return self._db_key
@db_key.setter
def db_key(self, key):
self._db_key = key
if self._db_key:
# Even after finding a db_key, we need to try and load the key list.
# If we don't find any keys, but we do find a db_key, then we've likely
# found a hash collision
if self._generate_key_list() > 0:
self.locked = False
# SOURCE : extractkeychain.py
@staticmethod
def _kcdecrypt(key, iv, data):
logger = logging.getLogger('Chainbreaker')
if len(data) == 0:
logger.debug("Encrypted data is 0.")
return ''
if len(data) % Chainbreaker.BLOCKSIZE != 0:
return ''
cipher = TripleDES(key, CBC, str(bytearray(iv)))
plain = cipher.decrypt(data)
# now check padding
pad = ord(plain[-1])
if pad > 8:
logger.debug("Bad padding byte. Keychain password might be incorrect.")
return ''
for z in plain[-pad:]:
if ord(z) != pad:
logger.debug("Bad padding byte. Keychain password might be incorrect.")
return ''
plain = plain[:-pad]
return plain
@staticmethod
def _get_encrypted_data_in_blob(blob_buffer):
key_blob = _KEY_BLOB(blob_buffer[:_KEY_BLOB.STRUCT.size])
if key_blob.CommonBlob.Magic != _KEY_BLOB.COMMON_BLOB_MAGIC:
return '', ''
key_data = blob_buffer[key_blob.StartCryptoBlob:key_blob.TotalLength]
return key_blob.IV, key_data # IV, Encrypted Data
# ## decrypted dbblob area
# ## Documents : http://www.opensource.apple.com/source/securityd/securityd-55137.1/doc/BLOBFORMAT
# ## http://www.opensource.apple.com/source/libsecurity_keychain/libsecurity_keychain-36620/lib/StorageManager.cpp
# def _ssgp_decryption(self, ssgp, dbkey):
# return Chainbreaker._kcdecrypt(dbkey, _SSGP(ssgp).IV, ssgp[_SSGP.STRUCT.size:])
# Documents : http://www.opensource.apple.com/source/securityd/securityd-55137.1/doc/BLOBFORMAT
# source : http://www.opensource.apple.com/source/libsecurity_cdsa_client/libsecurity_cdsa_client-36213/lib/securestorage.cpp
# magicCmsIV : http://www.opensource.apple.com/source/Security/Security-28/AppleCSP/AppleCSP/wrapKeyCms.cpp
@staticmethod
def keyblob_decryption(encryptedblob, iv, dbkey):
logger = logging.getLogger('Chainbreaker')
# magicCmsIV = unhexlify('4adda22c79e82105')
plain = Chainbreaker._kcdecrypt(dbkey, Chainbreaker.MAGIC_CMS_IV, encryptedblob)
if plain.__len__() == 0:
return ''
# now we handle the unwrapping. we need to take the first 32 bytes,
# and reverse them.
revplain = ''
for i in range(32):
revplain += plain[31 - i]
# now the real key gets found. */
plain = Chainbreaker._kcdecrypt(dbkey, iv, revplain)
keyblob = plain[4:]
if len(keyblob) != Chainbreaker.KEYLEN:
logger.debug("Decrypted key length is not valid")
return ''
return keyblob
class KeychainRecord(object):
def __init__(self):
self.logger = logging.getLogger('Chainbreaker')
def write_to_disk(self, output_directory):
# self.exportable contains the content we should write to disk. If it isn't implemented we can't
# then writing to disk via this method isn't currently supported.
try:
export_content = self.exportable
except NotImplementedError:
self.logger.warning('Attempted to export a non-exportable record.')
return False
# Create out export directory if it doesn't exist.
if not os.path.exists(output_directory):
try:
os.makedirs(output_directory)
except OSError:
self.logger.critical('Unable to create export directory: %s' % output_directory)
# Generate our filepath, making sure the file doesn't already exist. If it does,
# add a number, e.g. PrivateKey.1.key
file_name = self.FileName + self.FileExt
iteration = 1
while os.path.exists(os.path.join(output_directory, file_name)):
file_name = "%s.%s%s" % (self.FileName, iteration, self.FileExt)
iteration += 1
file_path = os.path.join(output_directory, file_name)
# Finish exporting the record.
try:
with open(file_path, 'wb') as fp:
self.logger.info('\t [-] Exported: %s' % file_path)
fp.write(export_content)
return True
except OSError, e:
self.logger.critical('Exception while attempting to export %s: %s' % (file_path, e))
@property
def FileName(self):
return str(uuid.uuid4())
@property
def FileExt(self):
return '.txt'
class KeychainPasswordHash(KeychainRecord):
KEYCHAIN_PASSWORD_HASH_FORMAT = "$keychain$*%s*%s*%s"
def __init__(self, salt, iv, cyphertext):
self.salt = salt
self.iv = iv
self.cypher_text = cyphertext
Chainbreaker.KeychainRecord.__init__(self)
def __str__(self):
return Chainbreaker.KeychainPasswordHash.KEYCHAIN_PASSWORD_HASH_FORMAT % (
self.salt, self.iv, self.cypher_text)
@property
def exportable(self):
return self.__str__()
@property
def FileName(self):
return "keychain_password_hash"
class PublicKeyRecord(KeychainRecord):
def __init__(self, print_name=None, label=None, key_class=None, private=None, key_type=None, key_size=None,
effective_key_size=None, extracted=None, cssm_type=None, public_key=None, iv=None, key=None):
self.PrintName = print_name
self.Label = label
self.KeyClass = key_class
self.Private = private
self.KeyType = key_type
self.KeySize = key_size
self.EffectiveKeySize = effective_key_size
self.Extracted = extracted
self.CSSMType = cssm_type
self.PublicKey = public_key
self.IV = iv
self.Key = key
Chainbreaker.KeychainRecord.__init__(self)
def __str__(self):
output = '[+] Public Key\n'
output += ' [-] Print Name: %s\n' % self.PrintName
# output += ' [-] Label: %s\n' % self.Label
output += ' [-] Key Class: %s\n' % self.KeyClass
output += ' [-] Private: %s\n' % self.Private
output += ' [-] Key Type: %s\n' % self.KeyType
output += ' [-] Key Size: %s\n' % self.KeySize
output += ' [-] Effective Key Size: %s\n' % self.EffectiveKeySize
output += ' [-] Extracted: %s\n' % self.Extracted
output += ' [-] CSSM Type: %s\n' % self.CSSMType
output += ' [-] Base64 Encoded Public Key: %s\n' % base64.b64encode(self.PublicKey)
return output
@property
def exportable(self):
return self.PublicKey
@property
def FileName(self):
return "".join(x for x in self.PrintName if x.isalnum())
@property
def FileExt(self):
return '.pub'
class PrivateKeyRecord(KeychainRecord):
def __init__(self, print_name=None, label=None, key_class=None, private=None, key_type=None, key_size=None,
effective_key_size=None, extracted=None, cssm_type=None, key_name=None, private_key=None, iv=None,
key=None):
self.PrintName = print_name
self.Label = label
self.KeyClass = key_class
self.Private = private
self.KeyType = key_type
self.KeySize = key_size
self.EffectiveKeySize = effective_key_size
self.Extracted = extracted
self.CSSMType = cssm_type
self.KeyName = key_name
self.PrivateKey = private_key
self.IV = iv
self.Key = key
Chainbreaker.KeychainRecord.__init__(self)
def __str__(self):
output = '[+] Private Key\n'
output += ' [-] Print Name: %s\n' % self.PrintName
# output += ' [-] Label: %s\n' % self.Label
output += ' [-] Key Class: %s\n' % self.KeyClass
# output += ' [-] Private: %s\n' % self.Private
output += ' [-] Key Type: %s\n' % self.KeyType
output += ' [-] Key Size: %s\n' % self.KeySize
output += ' [-] Effective Key Size: %s\n' % self.EffectiveKeySize
# output += ' [-] Extracted: %s\n' % self.Extracted
output += ' [-] CSSM Type: %s\n' % self.CSSMType
# output += ' [-] KeyName: %s\n' % self.KeyName
output += ' [-] Base64 Encoded PrivateKey: '
if self.PrivateKey == Chainbreaker.KEYCHAIN_LOCKED_SIGNATURE:
output += "%s\n" % Chainbreaker.KEYCHAIN_LOCKED_SIGNATURE
else:
output += "%s\n" % base64.b64encode(self.PrivateKey)
return output
@property
def exportable(self):
return self.PrivateKey
@property
def FileName(self):
return "".join(x for x in self.PrintName if x.isalnum())
@property
def FileExt(self):
return '.key'
class X509CertificateRecord(KeychainRecord):
def __init__(self, type=None, encoding=None, print_name=None, alias=None, subject=None, issuer=None,
serial_number=None, subject_key_identifier=None, public_key_hash=None, certificate=None):
self.Type = type
self.Encoding = encoding
self.PrintName = print_name
self.Alias = alias
self.Subject = subject
self.Issuer = issuer
self.Serial_Number = serial_number
self.Subject_Key_Identifier = subject_key_identifier
self.Public_Key_Hash = public_key_hash
self.Certificate = certificate
Chainbreaker.KeychainRecord.__init__(self)
def __str__(self):
output = '[+] X509 Certificate\n'
# output += " [-] Type: %s\n" % self.Type
# output += " [-] Encoding: %s\n" % self.Encoding
output += " [-] Print Name: %s\n" % self.PrintName
# output += " [-] Alias: %s\n" % self.Alias
# output += " [-] Subject: %s\n" % self.Subject
# output += " [-] Issuer: %s\n" % self.Issuer
# output += " [-] Serial Number: %s\n" % self.Serial_Number
# output += " [-] Subject Key Identifier: %s\n" % self.Subject_Key_Identifier
# output += " [-] Public Key Hash: %s\n" % self.Public_Key_Hash
output += " [-] Certificate: %s\n" % base64.b64encode(self.Certificate)
return output
@property
def exportable(self):
return self.Certificate
@property
def FileName(self):
return "".join(x for x in self.PrintName if x.isalnum())
@property
def FileExt(self):
return '.crt'
class SSGBEncryptedRecord(KeychainRecord):
def __init__(self):
self._password = None
self.locked = True
self.password_b64_encoded = False
Chainbreaker.KeychainRecord.__init__(self)
def decrypt_password(self):
try:
if self.SSGP and self.DBKey:
self._password = Chainbreaker._kcdecrypt(self.DBKey, self.SSGP.IV, self.SSGP.EncryptedPassword)
if not all(c in string.printable for c in self._password):
self._password = base64.b64encode(self._password)
self.password_b64_encoded = True
self.locked = False
except KeyError:
if not self._password:
self.locked = True
self._password = None
return self._password
def get_password_output_str(self):
password = self.Password
if self.password_b64_encoded:
return ' [-] Base64 Encoded Password: {}\n'.format(password)
else:
return ' [-] Password: {}\n'.format(password)
@property
def Password(self):
if not self._password:
self.decrypt_password()
if self.locked:
self._password = Chainbreaker.KEYCHAIN_LOCKED_SIGNATURE
return self._password
@property
def exportable(self):
return self.__str__()
@property
def FileName(self):
return "".join(x for x in self.PrintName if x.isalnum())
@property
def FileExt(self):
return '.txt'
class GenericPasswordRecord(SSGBEncryptedRecord):
def __init__(self, created=None, last_modified=None, description=None, creator=None, type=None, print_name=None,
alias=None, account=None, service=None, key=None, ssgp=None, dbkey=None):
self.Created = created
self.LastModified = last_modified
self.Description = description
self.Creator = creator
self.Type = type
self.PrintName = print_name
self.Alias = alias
self.Account = account
self.Service = service
self.Key = key
self.SSGP = ssgp
self.DBKey = dbkey
Chainbreaker.SSGBEncryptedRecord.__init__(self)
def __str__(self):
output = '[+] Generic Password Record\n'
output += ' [-] Create DateTime: %s\n' % self.Created # 16byte string
output += ' [-] Last Modified DateTime: %s\n' % self.LastModified # 16byte string
output += ' [-] Description: %s\n' % self.Description
output += ' [-] Creator: %s\n' % self.Creator
output += ' [-] Type: %s\n' % self.Type
output += ' [-] Print Name: %s\n' % self.PrintName
output += ' [-] Alias: %s\n' % self.Alias
output += ' [-] Account: %s\n' % self.Account
output += ' [-] Service: %s\n' % self.Service
output += self.get_password_output_str()
return output
class InternetPasswordRecord(SSGBEncryptedRecord):
def __init__(self, created=None, last_modified=None, description=None, comment=None, creator=None, type=None,
print_name=None, alias=None, protected=None, account=None, security_domain=None, server=None,
protocol_type=None, auth_type=None, port=None, path=None, ssgp=None, dbkey=None):