forked from openwall/john
-
Notifications
You must be signed in to change notification settings - Fork 0
/
itunes_backup2john.pl
executable file
·279 lines (191 loc) · 5.58 KB
/
itunes_backup2john.pl
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
#!/usr/bin/env perl
# Author: philsmd
# License: public domain
# Date: January 2017
# Version:
# 0.01
# Date released:
# January 2017
# Last updated:
# 25th January 2017
# https://github.com/philsmd/itunes_backup2hashcat
# Explanation of the output format:
# 1. all binary data is outputted in hexadeximal form
# 2. there are actually 2 formats, one for IOS backups < 10.0 and one for backups starting with 10.x
#
# version less than 10:
# $itunes_backup$*9*wpky*iter*salt**
# version 10.x hashes:
# $itunes_backup$*10*wpky*iter*salt*dpic*dpsl
use strict;
use warnings;
#use Data::Plist::BinaryReader;
#
# Constants
#
my $MAX_PLIST_SEARCH_DISTANCE = 256;
#
# Helper functions
#
sub read_plist_file
{
my $file_name = shift;
my $file_content = "";
{
local $/ = undef;
my $PLIST_FH;
if (! open ($PLIST_FH, "<$file_name"))
{
print STDERR "Could not open file '$file_name'.\n";
return "";
}
binmode ($PLIST_FH);
$file_content = <$PLIST_FH>;
close ($PLIST_FH);
}
return $file_content;
}
sub parse_manifest_file
{
my $data = shift;
my ($wpky, $salt, $iter, $dpic, $dpsl);
# my $plist = Data::Plist::BinaryReader->new;
# my $parsed_data = $plist->open_string ($data);
# my $backup_key_bag = $parsed_data->{data}[1]->{BackupKeyBag}[1];
my $data_len = length ($data);
return (undef, undef, undef) if (length ($data) < 4 + 4 + 4 + 4 + 4 + 4);
my @salt_matches = ($data =~ /SALT..../g);
# okay, I admit this is some strange parsing, but it seems to work all the times (for me)
# for instance, it assumes that the order is always like this: 1. salt, 2. iter, 3. wpky
my $idx_glob = 0;
for (my $i = 0; $i < scalar (@salt_matches); $i++)
{
my $idx_salt = index ($data, "SALT", $idx_glob + 0);
last if ($idx_salt == -1);
my $idx_iter = index ($data, "ITER", $idx_salt + 1);
last if ($idx_iter == -1);
my $idx_wpky = index ($data, "WPKY", $idx_iter + 1);
last if ($idx_wpky == -1);
# special case:
last if ($data_len - $idx_wpky < 8); # too close to the EOF
if ($idx_wpky - $idx_salt < $MAX_PLIST_SEARCH_DISTANCE) # some sane distance between the items
{
my $salt_len = substr ($data, $idx_salt + 4, 4);
my $iter_len = substr ($data, $idx_iter + 4, 4);
my $wpky_len = substr ($data, $idx_wpky + 4, 4);
$idx_salt += 8;
$idx_iter += 8;
$idx_wpky += 8;
$salt = substr ($data, $idx_salt, unpack ("L>", $salt_len));
$iter = substr ($data, $idx_iter, unpack ("L>", $iter_len));
$wpky = substr ($data, $idx_wpky, unpack ("L>", $wpky_len));
# iter is a special case, needs to be converted to a number
$iter = unpack ("L>", $iter);
last;
}
$idx_glob = $idx_wpky + 1;
}
# optional also search for DPIC and DPSL (iOS 10.2+ ?)
my @dpsl_matches = ($data =~ /DPSL..../g);
$idx_glob = 0;
for (my $i = 0; $i < scalar (@dpsl_matches); $i++)
{
my $idx_dpic = index ($data, "DPIC", $idx_glob + 0);
last if ($idx_dpic == -1);
my $idx_dpsl = index ($data, "DPSL", $idx_dpic + 1);
last if ($idx_dpsl == -1);
if ($idx_dpsl - $idx_dpic < $MAX_PLIST_SEARCH_DISTANCE)
{
my $dpic_len = substr ($data, $idx_dpic + 4, 4);
my $dpsl_len = substr ($data, $idx_dpsl + 4, 4);
$idx_dpic += 8;
$idx_dpsl += 8;
$dpic = substr ($data, $idx_dpic, unpack ("L>", $dpic_len));
$dpsl = substr ($data, $idx_dpsl, unpack ("L>", $dpsl_len));
$dpic = unpack ("L>", $dpic);
last;
}
$idx_glob = $idx_dpsl + 1;
}
return ($wpky, $salt, $iter, $dpic, $dpsl);
}
sub itunes_plist_get_hash
{
my $file_name = shift;
my $hash = "";
my $file_content = read_plist_file ($file_name);
if (length ($file_content) > 0)
{
my ($WPKY, $SALT, $ITER, $DPIC, $DPSL) = parse_manifest_file ($file_content);
if (! defined ($WPKY))
{
print "ERROR: WPKY could not be found in '$file_name'\n";
return "";
}
if (! defined ($SALT))
{
print "ERROR: SALT could not be found in '$file_name'\n";
return "";
}
if (! defined ($ITER))
{
print "ERROR: ITER could not be found in '$file_name'\n";
return "";
}
if (length ($WPKY) != 40)
{
print "ERROR: the WPKY within the file '$file_name' should be exactly 40 bytes long\n";
return "";
}
if (length ($SALT) != 20)
{
print "ERROR: the SALT within the file '$file_name' should be exactly 20 bytes long\n";
return "";
}
if (defined ($DPSL))
{
if (int ($DPIC) < 1)
{
print "ERROR: the DPIC within the file '$file_name' has an invalid value ($DPIC)\n";
return "";
}
if (length ($DPSL) != 20)
{
print "ERROR: the DPSL within the file '$file_name' should be exactly 20 bytes long\n";
return "";
}
$hash = sprintf ("\$itunes_backup\$*10*%s*%i*%s*%i*%s", unpack ("H*", $WPKY), $ITER, unpack ("H*", $SALT), $DPIC, unpack ("H*", $DPSL));
}
else
{
$hash = sprintf ("\$itunes_backup\$*9*%s*%i*%s**", unpack ("H*", $WPKY), $ITER, unpack ("H*", $SALT));
}
}
return $hash;
}
sub usage
{
my $program_name = shift;
print STDERR "Usage: $program_name <Manifest.plist file>...\n";
}
#
# Start
#
if (scalar (@ARGV) lt 1)
{
usage ($0);
exit (1);
}
foreach my $file_name (@ARGV)
{
if (! -e $file_name)
{
print STDERR "WARNING: could not open file '$file_name'\n";
next;
}
my $hash_buf = itunes_plist_get_hash ($file_name);
next unless (defined ($hash_buf));
next unless (length ($hash_buf) > 0);
print $hash_buf . "\n";
}
exit (0);