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
//! Data structures and logic for iOS backup handling.
pub mod crypto;
pub mod device;
pub mod models;
pub(crate) mod util;
use std::{
collections::HashSet,
fs::{File, read},
io::BufReader,
path::{Path, PathBuf},
};
use crate::{
backup::{
crypto::{AesCbcDecryptReader, aes_decrypt_cbc_with_padding, aes_kw_unwrap},
models::{
auth::Authentication,
file::BackupFileEntry,
keyring::EncryptionKey,
manifest::{
app::Application,
lockdown::ManifestLockdownInfo,
manifest_plist::{Manifest, ManifestData},
},
manifest_db::ManifestDb,
},
util::hex::hex_encode,
},
error::{BackupError, Result},
};
/// Main entry point for working with an iOS backup.
///
/// Provides methods to initialize, configure, and extract data from a backup,
/// including metadata loading, manifest database access, and file decryption.
#[derive(Debug)]
pub struct Backup {
/// Filesystem path to the specific device backup folder
pub backup_path: PathBuf,
/// Parsed manifest and decryption state
pub manifest: Manifest,
/// Decrypted manifest database details
pub manifest_db: ManifestDb,
}
impl Backup {
/// Create a new [`Backup`] instance, loading manifest data.
///
/// # Arguments
///
/// * `backup_path` - Filesystem path to a specific device backup folder (the UDID directory).
/// * `auth` - [`Authentication`] specifying password or derived key.
///
/// # Errors
/// Returns [`BackupError`] if paths are invalid, manifest loading fails, or decryption fails.
///
/// # Examples
///
/// ```no_run
/// use crabapple::{Backup, Authentication};
///
/// let backup = Backup::open(
/// "/path/to/backup",
/// &Authentication::Password("pass".into()),
/// )?;
///
/// println!("UDID: {}", backup.udid()?);
/// # Ok::<(), crabapple::error::BackupError>(())
/// ```
pub fn open<P: AsRef<Path>>(backup_path: P, auth: &Authentication) -> Result<Self> {
let device_backup_path = backup_path.as_ref().to_path_buf();
if !device_backup_path.is_dir() {
return Err(BackupError::InvalidBackupRoot(
device_backup_path.display().to_string(),
));
}
let manifest_plist = device_backup_path.join("Manifest.plist");
// Ensure that the manifest plist file exists
if !manifest_plist.exists() {
return Err(BackupError::ManifestPlistNotFound(
device_backup_path
.join("Manifest.plist")
.display()
.to_string(),
));
}
// Load `Manifest.plist` and extract necessary keys and info
let manifest_data = ManifestData::from_plist(&manifest_plist)?;
let manifest = Manifest::from_manifest_data(manifest_data, auth)?;
let manifest_db = ManifestDb::new(&device_backup_path.join("Manifest.db"), &manifest)?;
Ok(Self {
backup_path: device_backup_path,
manifest,
manifest_db,
})
}
/// Returns the current device `UDID` (the backup folder name).
///
/// # Errors
/// Returns [`BackupError::InvalidBackupRoot`] if the `UDID` cannot be retrieved as a string.
///
/// # Examples
///
/// ```no_run
/// use crabapple::{Backup, Authentication};
///
/// let backup = Backup::open(
/// "/path/to/backup",
/// &Authentication::Password("pass".into()),
/// )?;
///
/// let udid = backup.udid()?;
/// println!("UDID: {}", udid);
/// # Ok::<(), crabapple::error::BackupError>(())
/// ```
pub fn udid(&self) -> Result<&str> {
self.backup_path
.file_name()
.and_then(|os| os.to_str())
.ok_or_else(|| BackupError::InvalidBackupRoot(self.backup_path.display().to_string()))
}
/// Returns device metadata from `Manifest.plist`.
///
/// # Returns
/// Manifest lockdown information parsed from `Manifest.plist`.
///
/// # Examples
///
/// ```no_run
/// use crabapple::{Backup, Authentication};
///
/// let backup = Backup::open(
/// "/path/to/backup",
/// &Authentication::Password("pass".into()),
/// )?;
///
/// let lockdown = backup.lockdown();
/// println!("Device name: {}", lockdown.device_name);
/// # Ok::<(), crabapple::error::BackupError>(())
#[must_use]
pub fn lockdown(&self) -> &ManifestLockdownInfo {
&self.manifest.manifest_data.lockdown
}
/// Indicates whether the backup is encrypted.
///
/// # Returns
/// `true` if the backup is encrypted, `false` otherwise.
///
/// # Examples
///
/// ```no_run
/// use crabapple::{Backup, Authentication};
///
/// let backup = Backup::open(
/// "/path/to/backup",
/// &Authentication::Password("pass".into()),
/// )?;
///
/// println!("Encrypted?: {}", backup.is_encrypted());
/// # Ok::<(), crabapple::error::BackupError>(())
#[must_use]
pub fn is_encrypted(&self) -> bool {
self.manifest.manifest_data.is_encrypted
}
/// Get number of applications in the backup.
///
/// # Returns
/// The number of applications in the backup.
///
/// # Examples
///
/// ```no_run
/// use crabapple::{Backup, Authentication};
///
/// let backup = Backup::open(
/// "/path/to/backup",
/// &Authentication::Password("pass".into()),
/// )?;
///
/// println!("Backup contains {} apps!", backup.num_apps());
/// # Ok::<(), crabapple::error::BackupError>(())
pub fn num_apps(&self) -> usize {
self.manifest.manifest_data.applications.len()
}
/// Get a reference to the applications in the backup.
///
/// # Returns
/// A reference to a vector of [`Application`] objects parsed from the manifest.
///
/// # Examples
///
/// ```no_run
/// use crabapple::{Backup, Authentication};
///
/// let backup = Backup::open(
/// "/path/to/backup",
/// &Authentication::Password("pass".into()),
/// )?;
///
/// let apps = backup.apps();
/// for app in apps {
/// println!("App: {}", app.bundle_id);
/// }
/// # Ok::<(), crabapple::error::BackupError>(())
pub fn apps(&self) -> &[Application] {
&self.manifest.manifest_data.applications
}
/// Returns the main decryption key as a hex string, if the backup is encrypted.
///
/// # Returns
/// An [`Option<String>`] containing the decryption key in hexadecimal representation,
/// or [`None`] if the backup is not encrypted.
///
/// # Examples
///
/// ```no_run
/// use crabapple::{Backup, Authentication};
///
/// let backup = Backup::open(
/// "/path/to/backup",
/// &Authentication::Password("pass".into()),
/// )?;
///
/// if let Some(key_hex) = backup.decryption_key_hex() {
/// println!("Key: {}", key_hex);
/// }
/// # Ok::<(), crabapple::error::BackupError>(())
/// ```
#[must_use]
pub fn decryption_key_hex(&self) -> Option<String> {
self.manifest
.main_decryption_key
.as_ref()
.map(|v| hex_encode(v))
}
/// Retrieve the raw 32-byte decryption key, if available.
///
/// # Returns
/// An `Option<KeyEncryptionKey>` containing the main decryption key, or `None` if not encrypted.
///
/// # Examples
///
/// ```no_run
/// use crabapple::{Backup, Authentication};
///
/// let backup = Backup::open(
/// "/path/to/backup",
/// &Authentication::Password("pass".into()),
/// )?;
///
/// if let Some(key) = backup.decryption_key() {
/// println!("Key: {:?}", key);
/// }
/// # Ok::<(), crabapple::error::BackupError>(())
/// ```
#[must_use]
pub fn decryption_key(&self) -> Option<EncryptionKey> {
self.manifest.main_decryption_key.clone()
}
/// Get all domains present in the backup's manifest database.
///
/// Some common domains, in no particular order, include:
///
/// * `AppDomain`
/// * `AppDomainGroup`
/// * `AppDomainPlugin`
/// * `CameraRollDomain`
/// * `DatabaseDomain`
/// * `HealthDomain`
/// * `HomeDomain`
/// * `HomeKitDomain`
/// * `InstallDomain`
/// * `KeyboardDomain`
/// * `KeychainDomain`
/// * `ManagedPreferencesDomain`
/// * `MediaDomain`
/// * `MobileDeviceDomain`
/// * `NetworkDomain`
/// * `ProtectedDomain`
/// * `RootDomain`
/// * `SysContainerDomain`
/// * `SysSharedContainerDomain`
/// * `SystemPreferencesDomain`
/// * `TonesDomain`
/// * `WirelessDomain`
///
/// # Returns
/// A [`HashSet<String>`] containing each unique domain present in the backup.
///
/// # Errors
/// Returns [`BackupError::ManifestDbNotFound`] if the manifest database is unavailable,
/// or [`BackupError::Database`] if the database query fails.
///
/// # Examples
///
/// ```no_run
/// use crabapple::{Backup, Authentication};
///
/// let backup = Backup::open(
/// "/path/to/backup",
/// &Authentication::Password("pass".into()),
/// )?;
///
/// let domains = backup.query_all_domains()?;
/// println!("Domains: {:?}", domains);
/// # Ok::<(), crabapple::error::BackupError>(())
/// ```
pub fn query_all_domains(&self) -> Result<HashSet<String>> {
self.manifest_db.query_all_domains()
}
/// Get the filesystem path to the decrypted (or raw) `Manifest.db` file.
///
/// # Returns
/// A [`Path`] pointing to the location of the manifest database file.
///
/// # Examples
///
/// ```no_run
/// use crabapple::{Backup, Authentication};
///
/// let backup = Backup::open(
/// "/path/to/backup",
/// &Authentication::Password("pass".into()),
/// )?;
///
/// let db_path = backup.manifest_db_path();
/// println!("Manifest.db path: {:?}", db_path);
/// # Ok::<(), crabapple::error::BackupError>(())
/// ```
pub fn manifest_db_path(&self) -> &Path {
&self.manifest_db.db_path
}
/// List all files recorded in `Manifest.db`.
///
/// # Errors
/// Returns [`BackupError::Database`] if the database cannot be accessed.
///
/// # Examples
///
/// ```no_run
/// use crabapple::{Backup, Authentication};
///
/// let backup = Backup::open(
/// "/path/to/backup",
/// &Authentication::Password("pass".into()),
/// )?;
///
/// let entries = backup.entries()?;
/// for entry in entries {
/// println!("{:?}", entry);
/// }
/// # Ok::<(), crabapple::error::BackupError>(())
/// ```
pub fn entries(&self) -> Result<Vec<BackupFileEntry>> {
self.manifest_db.query_all_entries()
}
/// Get a single file entry by its file ID.
///
/// # Arguments
/// * `file_id` - The file's unique identifier (`SHA1` hash).
///
/// # Errors
/// Returns [`BackupError::FileNotFoundInBackup`] if the specified file ID is not found,
/// or [`BackupError::Database`] if the database query fails.
///
/// # Examples
///
/// ```no_run
/// use crabapple::{Backup, Authentication};
///
/// let backup = Backup::open(
/// "/path/to/backup",
/// &Authentication::Password("pass".into()),
/// )?;
///
/// let entry = backup.get_file("fileid")?;
/// println!("File encryption key: {:?}", entry.metadata.encryption_key);
/// # Ok::<(), crabapple::error::BackupError>(())
/// ```
pub fn get_file(&self, file_id: &str) -> Result<BackupFileEntry> {
self.manifest_db
.query_file_by_id(file_id)?
.ok_or_else(|| BackupError::FileNotFoundInBackup(file_id.to_string()))
}
/// Decrypt the file represented by [`BackupFileEntry`], returning plaintext bytes.
///
/// All operations are performed in memory, and the decrypted data is returned as a byte vector.
///
/// # Arguments
/// * `entry` - A [`BackupFileEntry`] containing metadata and encrypted file ID.
///
/// # Returns
/// Plaintext data as a byte vector.
///
/// # Errors
/// Returns [`BackupError::Crypto`] on decryption errors or missing keys.
///
/// # Examples
///
/// ```no_run
/// use crabapple::{Backup, Authentication};
///
/// let backup = Backup::open(
/// "/path/to/backup",
/// &Authentication::Password("pass".into()),
/// )?;
///
/// let entry = backup.get_file("fileid")?;
/// let data = backup.decrypt_entry(&entry)?;
/// println!("Data size: {} bytes", data.len());
/// # Ok::<(), crabapple::error::BackupError>(())
/// ```
pub fn decrypt_entry(&self, entry: &BackupFileEntry) -> Result<Vec<u8>> {
if !self.is_encrypted() {
return Err(BackupError::NotEncrypted);
}
let source = self
.backup_path
.join(&entry.file_id[0..2])
.join(&entry.file_id);
let ciphertext = read(&source)?;
let key = self.unwrap_key_for_entry(entry)?;
aes_decrypt_cbc_with_padding(&ciphertext, &key)
}
/// Decrypt the file represented by [`BackupFileEntry`], returning a streaming reader.
///
/// All operations are streamed from the disk, and the decrypted data is returned as a reader.
///
/// # Arguments
/// * `entry` - A [`BackupFileEntry`] containing metadata and encrypted file ID.
///
/// # Returns
/// A streaming reader implementing `std::io::Read` that yields plaintext as it's read.
///
/// # Errors
/// Returns [`BackupError::Crypto`] on decryption errors or missing keys.
///
/// # Examples
///
/// ```no_run
/// use std::{fs::File, io::copy};
/// use crabapple::{Backup, Authentication};
///
/// let backup = Backup::open(
/// "/path/to/backup",
/// &Authentication::Password("pass".into()),
/// )?;
///
/// let file = backup.get_file("41ee3469300471004e6d526ebd09c051c19f8a39")?;
/// let mut reader = backup.decrypt_entry_stream(&file)?;
/// let mut plain = Vec::new();
/// copy(&mut reader, &mut plain)?;
/// # Ok::<(), crabapple::error::BackupError>(())
/// ```
pub fn decrypt_entry_stream(
&self,
entry: &BackupFileEntry,
) -> Result<AesCbcDecryptReader<BufReader<File>>> {
if !self.is_encrypted() {
return Err(BackupError::NotEncrypted);
}
let ciphertext = File::open(self.backup_path.join(entry.source()))?;
let key = self.unwrap_key_for_entry(entry)?;
AesCbcDecryptReader::from(ciphertext, &key)
}
/// Unwrap the encryption key for a specific file entry.
///
/// # Arguments
/// * `entry` - A [`BackupFileEntry`] containing metadata and encrypted file ID.
///
/// # Returns
/// A streaming reader implementing `std::io::Read` that yields plaintext as it's read.
///
/// # Errors
/// Returns [`BackupError::Crypto`] on decryption errors or missing keys.
fn unwrap_key_for_entry(&self, entry: &BackupFileEntry) -> Result<EncryptionKey> {
let class_key_entry = self
.manifest
.get_class_key(entry.metadata.protection_class)?;
let key = aes_kw_unwrap(
&class_key_entry.key,
&entry
.metadata
.encryption_key
.as_ref()
.ok_or(BackupError::NotEncrypted)?
.file_key,
)?;
Ok(key)
}
}