-
Notifications
You must be signed in to change notification settings - Fork 38
/
itunes_backup2hashcat.pl
executable file
·310 lines (213 loc) · 6.13 KB
/
itunes_backup2hashcat.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
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
#!/usr/bin/env perl
# Author: philsmd
# License: public domain
# Date: January 2017
# Version:
# 0.02
# Date released:
# January 2017
# Last updated:
# 4th February 2017
# 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*wkpy*iter*salt**
# version 10.x hashes:
# $itunes_backup$*10*wkpy*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 globbing_on_windows
{
my @file_list = @_;
my $os = $^O;
if (($os eq "MSWin32") || ($os eq "Win32"))
{
my $windows_globbing_module = "File::Glob";
my $windows_globbing = "bsd_glob";
if (eval "require $windows_globbing_module")
{
no strict 'refs';
$windows_globbing_module->import ($windows_globbing);
my @new_file_list = ();
foreach my $item (@file_list)
{
push (@new_file_list, $windows_globbing-> ($item));
}
@file_list = @new_file_list;
}
}
return @file_list;
}
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);
}
my @file_list = globbing_on_windows (@ARGV);
foreach my $file_name (@file_list)
{
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);