forked from willstruggle/john
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeychain2john.py
More file actions
executable file
·114 lines (94 loc) · 2.89 KB
/
keychain2john.py
File metadata and controls
executable file
·114 lines (94 loc) · 2.89 KB
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
#!/usr/bin/env python
# keychain2john processes input Mac OS X keychain files into a format suitable
# for use with JtR.
#
# This software is Copyright (c) 2014, Dhiru Kholia <dhiru [at] openwall.com>
# and (c) 2004 Matt Johnston <matt @ ucc asn au>,
#
# This code may be freely used and modified for any purpose.
#
# How it works:
#
# The parts of the keychain we're interested in are "blobs" (see ssblob.h in
# Apple's code). There are two types - DbBlobs and KeyBlobs.
#
# Each blob starts with the magic hex string FA DE 07 11 - so we search for
# that. There's only one DbBlob (at the end of the file), and that contains the
# file encryption key (amongst other things), encrypted with the master key.
# The master key is derived purely from the user's password, and a salt, also
# found in the DbBlob. PKCS #5 2 pbkdf2 is used for deriving the master key.
#
# DbBlob format:
#
# The offsets from the start of the blob are as follows:
#
# 0 0xfade0711 - magic number
# 4 version
# 8 crypto-offset - offset of the encryption and signing key
# 12 total len
# 16 signature (16 bytes)
# 32 sequence
# 36 idletimeout
# 40 lockonsleep flag
# 44 salt (20 bytes)
# 64 iv (8 bytes)
# 72 blob signature (20)
#
# Output Format: filename:$keychain$*salt*iv*ciphertext
import sys
import struct
import os
from binascii import hexlify
SALTLEN = 20
IVLEN = 8
CTLEN = 48
magic = b"\xfa\xde\x07\x11"
def process_file(filename):
f = open(filename, "rb")
f.seek(-4, 2)
while True:
f.seek(-8, 1)
data = f.read(4)
if len(data) < 4:
msg = "%s : Couldn't find db key. Is it a keychain file?\n"
sys.stderr.write(msg % filename)
sys.exit(1)
if data == magic:
break
pos = f.tell() - 4
# ciphertext offset
f.seek(pos + 8, 0)
cipheroff = struct.unpack(">I", f.read(4))[0]
# salt
f.seek(pos + 44, 0)
salt = f.read(SALTLEN)
if len(salt) != SALTLEN:
sys.stderr.write("Something went wrong - fread(salt) error\n")
sys.exit(1)
# IV
f.seek(pos + 64, 0)
iv = f.read(IVLEN)
if len(iv) != IVLEN:
sys.stderr.write("Something went wrong - fread(iv) error\n")
sys.exit(1)
# ciphertext
f.seek(pos + cipheroff, 0)
ct = f.read(CTLEN)
if len(ct) != CTLEN:
sys.stderr.write("Something went wrong - fread(ct) error\n")
sys.exit(1)
# output
sys.stdout.write("%s:$keychain$*" % os.path.basename(filename))
sys.stdout.write(hexlify(salt).decode("ascii"))
sys.stdout.write("*")
sys.stdout.write(hexlify(iv).decode("ascii"))
sys.stdout.write("*")
sys.stdout.write(hexlify(ct).decode("ascii"))
sys.stdout.write("\n")
f.close()
if __name__ == "__main__":
if len(sys.argv) < 2:
sys.stdout.write("Usage: keychain2john [keychain files]\n")
sys.exit(-1)
for i in range(1, len(sys.argv)):
process_file(sys.argv[i])