Skip to content

Instantly share code, notes, and snippets.

@toland
Created September 27, 2009 00:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toland/194530 to your computer and use it in GitHub Desktop.
Save toland/194530 to your computer and use it in GitHub Desktop.
Perl script to extract data from iPhone backups
#!/usr/bin/perl -w
use strict;
# This file clumsily extracts the DB's hidden in the iPhone backup files
# Usage: perl -w bkupextract.pl /Users/flip/Library/Application\ Support/MobileSync/Backup/*/*
my %seen;
foreach my $filename (@ARGV) {
# Slurp File Contents
open(FILE, "<$filename") or die "Can't open $filename: $!";
my $data = do {local $/; binmode FILE; <FILE>};
close(FILE);
my $type;
my $outfilename;
# skip non-SQLite Files
if ($data =~ m|SQLite|) {
# identify the file and use it to name the output
$type = ($data =~ m!([^/]*?)\.(sqlitedb|db)!) ? $1 : 'unknown';
$seen{$type}++;
$outfilename = sprintf "%s_%02d.db", $type, $seen{$type};
# Helpful progress report
printf STDERR "%-60s to %-25s\n", $filename, $type, $outfilename;
# dump the part between "SQLite format 3" and end
# FIXME -- is there cruft at end? I don't know the SQLite format. Maybe you do?
open(OUTFILE, ">$outfilename") or die "Can't open $outfilename: $!";
binmode OUTFILE;
my $sqlitedb = $data;
$sqlitedb =~ s/^(.*?SQLite format 3)/SQLite format 3/;
print OUTFILE $sqlitedb;
close(OUTFILE);
} elsif ($data =~ m|Exif|) {
# identify the file and use it to name the output
$type = 'IMG';
$seen{$type}++;
$outfilename = sprintf "%s_%02d.jpg", $type, $seen{$type};
# Helpful progress report
printf STDERR "%-60s to %-25s\n", $filename, $type, $outfilename;
# dump the part between "SQLite format 3" and end
# FIXME -- is there cruft at end? I don't know the SQLite format. Maybe you do?
open(OUTFILE, ">$outfilename") or die "Can't open $outfilename: $!";
binmode OUTFILE;
print OUTFILE $data;
close(OUTFILE);
} elsif ($data =~ m|PNG|) {
# identify the file and use it to name the output
$type = 'PNG';
$seen{$type}++;
$outfilename = sprintf "%s_%02d.png", $type, $seen{$type};
# Helpful progress report
printf STDERR "%-60s to %-25s\n", $filename, $type, $outfilename;
# dump the part between "SQLite format 3" and end
# FIXME -- is there cruft at end? I don't know the SQLite format. Maybe you do?
open(OUTFILE, ">$outfilename") or die "Can't open $outfilename: $!";
binmode OUTFILE;
print OUTFILE $data;
close(OUTFILE);
} elsif ($data =~ m|plist|) {
# identify the file and use it to name the output
$type = 'plist';
$seen{$type}++;
$outfilename = sprintf "%s_%02d.plist", $type, $seen{$type};
# Helpful progress report
printf STDERR "%-60s to %-25s\n", $filename, $type, $outfilename;
# dump the part between "SQLite format 3" and end
# FIXME -- is there cruft at end? I don't know the SQLite format. Maybe you do?
open(OUTFILE, ">$outfilename") or die "Can't open $outfilename: $!";
binmode OUTFILE;
print OUTFILE $data;
close(OUTFILE);
} else {
printf STDERR "type of %s is unknown\n", $filename
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment