Repository navigation
Expand file tree
/
Copy pathfolder_backup.py
More file actions
217 lines (188 loc) · 8.66 KB
/
Copy pathfolder_backup.py
File metadata and controls
217 lines (188 loc) · 8.66 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
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
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# iOS Backup Explorer — a backup that was already extracted to folders
# Copyright (C) 2026 slammingprogramming and contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Opening a backup that has already been decrypted and extracted.
Tools that decrypt a backup (and this app's own *Extract Entire Backup*)
leave a folder with one sub-folder per domain
(``HomeDomain/Library/SMS/sms.db``, ``AppDomain-com.example.app/...``) and
no ``Manifest.db``. :class:`FolderBackend` presents such a folder to the
rest of the app the way a real backup looks: it builds an index in memory
(the same ``Files`` table a ``Manifest.db`` has) from what is on disk and
reads the files from where they are. The folder is never written to.
"""
import contextlib
import hashlib
import os
import plistlib
import shutil
import sqlite3
import tempfile
_SUFFIXES = ("Domain", "DomainGroup", "DomainPlugin")
def looks_like_domain(name):
"""Whether a folder name is a backup domain: ``HomeDomain``,
``AppDomain-com.apple.mobilesafari``, ``AppDomainGroup-group...``,
``SysSharedContainerDomain-systemgroup...``."""
return name.split("-", 1)[0].endswith(_SUFFIXES)
KEY_BAG_FILE = "Manifest.plist"
def read_key_bag(path):
"""The backup's key bag (the bytes of ``BackupKeyBag``) from the
``Manifest.plist`` in the folder *path*, or None. An extraction made by
this program keeps that file so the keychain can still be opened."""
try:
with open(os.path.join(path, KEY_BAG_FILE), "rb") as handle:
manifest = plistlib.load(handle)
except (OSError, ValueError, plistlib.InvalidFileException):
return None
bag = manifest.get("BackupKeyBag") if isinstance(manifest, dict) else None
return bytes(bag) if isinstance(bag, (bytes, bytearray)) else None
def looks_extracted(path):
"""Whether *path* holds an extracted backup: sub-folders named like
backup domains, and no ``Manifest`` files of a real backup. (A
``Manifest.plist`` holding only a key bag, which an extraction keeps for
the keychain, does not make it a real backup.)"""
try:
names = os.listdir(path)
except OSError:
return False
if any(n in ("Manifest.db", "Manifest.mbdb") for n in names):
return False
if KEY_BAG_FILE in names and read_key_bag(path) is None:
return False
return any(looks_like_domain(n) and os.path.isdir(os.path.join(path, n))
for n in names)
def file_record(size, mtime, birth=None):
"""A ``file`` blob like the ones in ``Manifest.db`` (an archived
dictionary), holding what the app reads from it."""
record = {"Size": int(size), "Mode": 0o100644}
if mtime:
record["LastModified"] = int(mtime)
if birth:
record["Birth"] = int(birth)
return plistlib.dumps(
{"$archiver": "NSKeyedArchiver", "$objects": ["$null", record],
"$top": {"root": plistlib.UID(1)}, "$version": 100000},
fmt=plistlib.FMT_BINARY)
def file_id_for(domain, relative_path):
"""The ID a real backup would give the file: SHA-1 of ``domain-path``."""
return hashlib.sha1(f"{domain}-{relative_path}".encode("utf-8",
"surrogateescape")
).hexdigest()
class FolderBackend:
"""An extracted backup folder, read-only."""
def __init__(self, root, fs_path=lambda path: path):
self._root = os.path.abspath(root)
self._fs = fs_path
self._conn = sqlite3.connect(":memory:")
self._conn.execute(
"CREATE TABLE Files (fileID TEXT PRIMARY KEY, domain TEXT, "
"relativePath TEXT, flags INTEGER, file BLOB)")
self._conn.execute("CREATE INDEX by_domain ON Files (domain)")
self._populate()
# -- the index --------------------------------------------
def _populate(self):
rows = []
for name in sorted(os.listdir(self._fs(self._root))):
top = os.path.join(self._root, name)
if looks_like_domain(name) and os.path.isdir(self._fs(top)):
self._walk(name, top, "", rows)
self._conn.executemany("INSERT OR IGNORE INTO Files VALUES "
"(?, ?, ?, ?, ?)", rows)
self._conn.commit()
def _walk(self, domain, folder, prefix, rows):
try:
entries = list(os.scandir(self._fs(folder)))
except OSError:
return # unreadable folder: leave it out
for entry in entries:
relative = prefix + entry.name
try:
if entry.is_symlink():
continue # (never follow a link out of the tree)
if entry.is_dir(follow_symlinks=False):
rows.append((file_id_for(domain, relative), domain,
relative, 2, file_record(0, 0)))
self._walk(domain, os.path.join(folder, entry.name),
relative + "/", rows)
elif entry.is_file(follow_symlinks=False):
stat = entry.stat(follow_symlinks=False)
birth = getattr(stat, "st_birthtime", None) or (
stat.st_ctime if os.name == "nt" else None)
rows.append((file_id_for(domain, relative), domain,
relative, 1,
file_record(stat.st_size, stat.st_mtime,
birth)))
except OSError:
continue
@contextlib.contextmanager
def manifest_db_cursor(self):
cur = self._conn.cursor()
try:
yield cur
finally:
cur.close()
# -- the files --------------------------------------------
def _source(self, domain, rel_path):
"""Where a file is on disk. *domain* and *rel_path* come from the
index built from the disk, but are checked all the same."""
parts = [domain] + rel_path.split("/")
# a backslash counts everywhere: it separates folders on Windows, and
# a name holding one could not be written out there
separators = {x for x in (os.sep, os.altsep, "\\") if x}
if any(p in ("", ".", "..") or any(x in p for x in separators)
for p in parts):
raise ValueError("unsafe path in the extracted backup")
return os.path.join(self._root, *parts)
def extract(self, cur, file_id, domain, rel_path, mtime, out_path):
source = self._fs(self._source(domain, rel_path))
handle, partial = tempfile.mkstemp(dir=os.path.dirname(out_path))
os.close(handle)
try:
shutil.copyfile(source, partial)
os.replace(partial, out_path)
except BaseException:
if os.path.exists(partial):
os.remove(partial)
raise
if mtime:
os.utime(out_path, (mtime, mtime))
def materialize(self, cur, file_id, domain, rel_path, size, mtime,
cache_dir):
"""A path to the file's data. Nothing is copied: the file is
already where it is."""
if size == 0:
path = os.path.join(cache_dir, file_id)
if not os.path.exists(path):
os.makedirs(cache_dir, exist_ok=True)
open(path, "wb").close()
return path
source = self._source(domain, rel_path)
if not os.path.isfile(self._fs(source)):
raise FileNotFoundError(source)
return source
def keychain_source(self):
"""``("keybag", bytes)`` if the folder kept the backup's key bag (the
password is then needed), else ``("none", why)``."""
bag = read_key_bag(self._root)
if bag is not None:
return "keybag", bag
return "none", (
"This folder was extracted without the backup's key bag "
"(Manifest.plist), so the keychain cannot be opened. Extract "
"the whole backup again with this program, or open the "
"original encrypted backup.")
def close(self):
self._conn.close()