-
Notifications
You must be signed in to change notification settings - Fork 2
/
file_relay.py
141 lines (124 loc) · 4.19 KB
/
file_relay.py
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
#!/usr/bin/env python
# -*- coding: utf8 -*-
#
# $Id$
#
# Copyright (c) 2012-2014 "dark[-at-]gotohack.org"
#
# This file is part of pymobiledevice2
#
# pymobiledevice2 is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#
import os
import zlib
import gzip
import logging
from pymobiledevice2.lockdown import LockdownClient
from pymobiledevice2.util.cpio import CpioArchive
from pymobiledevice2.util import MultipleOption
from pprint import pprint
from tempfile import mkstemp
from optparse import OptionParser
from io import BytesIO
SRCFILES = """Baseband
CrashReporter
MobileAsset
VARFS
HFSMeta
Lockdown
MobileBackup
MobileDelete
MobileInstallation
MobileNotes
Network
UserDatabases
WiFi
WirelessAutomation
NANDDebugInfo
SystemConfiguration
Ubiquity
tmp
WirelessAutomation"""
class DeviceVersionNotSupported(Exception):
pass
class FileRelay(object):
def __init__(self, lockdown=None, serviceName="com.apple.mobile.file_relay",
udid=None, logger=None):
self.logger = logger or logging.getLogger(__name__)
self.lockdown = lockdown if lockdown else LockdownClient(udid=udid)
ProductVersion = self.lockdown.getValue("", "ProductVersion")
if ProductVersion[0] >= "8":
raise DeviceVersionNotSupported
self.service = self.lockdown.startService(serviceName)
self.packet_num = 0
def stop_session(self):
self.logger.info("Disconecting...")
self.service.close()
def request_sources(self, sources=["UserDatabases"]):
self.service.sendPlist({"Sources": sources})
while 1:
res = self.service.recvPlist()
if res:
s = res.get("Status")
if s == "Acknowledged":
z = ""
while True:
x = self.service.recv()
if not x:
break
z += x
return z
else:
print(res.get("Error"))
break
return None
if __name__ == "__main__":
parser = OptionParser(option_class=MultipleOption,usage="%prog")
parser.add_option("-s", "--sources",
action="extend",
dest="sources",
metavar='SOURCES',
choices=SRCFILES.split("\n"),
help="comma separated list of file relay source to dump")
parser.add_option("-e", "--extract",dest="extractpath" , default=False,
help="Extract archive to specified location", type="string")
parser.add_option("-o", "--output", dest="outputfile", default=False,
help="Output location", type="string")
(options, args) = parser.parse_args()
sources = []
if options.sources:
sources = options.sources
else:
sources = ["UserDatabases"]
print("Downloading: %s" % "".join([str(item)+" " for item in sources]))
fc = None
try:
fc = FileRelay()
except:
print("Device with product vertion >= 8.0 does not allow access to fileRelay service")
exit()
data = fc.request_sources(sources)
if data:
if options.outputfile:
path = options.outputfile
else:
_,path = mkstemp(prefix="fileRelay_dump_",suffix=".gz",dir=".")
open(path,'wb').write(data)
self.logger.info("Data saved to: %s ", path)
if options.extractpath:
with open(path, 'r') as f:
gz = gzip.GzipFile(mode='rb', fileobj=f)
cpio = CpioArchive(fileobj=BytesIO(gz.read()))
cpio.extract_files(files=None,outpath=options.extractpath)