-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdump.py
executable file
·311 lines (245 loc) · 8.11 KB
/
dump.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
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
#!/usr/bin/env python3
import argparse
import os
import platform
import shutil
import subprocess
import sys
import time
from zipfile import ZipFile
from urllib.request import urlretrieve
try:
import paramiko
except ImportError:
raise
host_os = platform.system()
def getimg4tool():
cwd = os.getcwd()
if host_os == 'Linux':
print('Ensuring needed dependencies are installed!')
subprocess.run(
('sudo',
'apt',
'install',
'automake',
'g++',
'openssl',
'pkg-config',
'libtool',
'python-dev',
'cmake'))
elif host_os == 'Darwin':
pass
if not os.path.exists('.tmp/libplist'):
subprocess.run(
('git',
'clone',
'https://github.com/libimobiledevice/libplist',
'.tmp/libplist'),
stdout=subprocess.DEVNULL)
os.chdir('.tmp/libplist')
subprocess.run(
'./autogen.sh --without-cython && make && sudo make install',
stdout=subprocess.DEVNULL,
shell=True)
os.chdir(cwd)
if not os.path.exists('.tmp/libgeneral'):
subprocess.run(
('git',
'clone',
'https://github.com/tihmstar/libgeneral',
'.tmp/libgeneral'),
stdout=subprocess.DEVNULL)
os.chdir('.tmp/libgeneral')
subprocess.run(
'./autogen.sh && make && sudo make install',
stdout=subprocess.DEVNULL,
shell=True)
os.chdir(cwd)
if not os.path.exists('.tmp/lzfse'):
subprocess.run(
('git',
'clone',
'https://github.com/lzfse/lzfse',
'.tmp/lzfse'),
stdout=subprocess.DEVNULL)
os.chdir('.tmp/lzfse')
if not os.path.exists('build'):
os.mkdir('build')
os.chdir('build')
subprocess.run(
'cmake .. && make && sudo make install',
stdout=subprocess.DEVNULL,
shell=True)
os.chdir(cwd)
if not os.path.exists('.tmp/img4tool'):
subprocess.run(
('git',
'clone',
'--recursive',
'https://github.com/tihmstar/img4tool',
'.tmp/img4tool'),
stdout=subprocess.DEVNULL)
os.chdir('.tmp/img4tool')
subprocess.run(
'./autogen.sh && make && sudo make install',
stdout=subprocess.DEVNULL,
shell=True)
os.chdir(cwd)
if host_os == 'Linux':
subprocess.run(('sudo', 'ldconfig'))
try:
subprocess.run(('which', 'img4tool'), stdout=subprocess.DEVNULL)
except FileNotFoundError:
print('Seems like img4tool was not compiled!')
raise
def getldid():
url = 'https://github.com/xerub/ldid/releases/download/42/ldid.zip'
filename = os.path.basename(url)
path = '.tmp/{}'.format(filename)
ldid_path = '.tmp/ldid'
if not os.path.exists('.tmp'):
os.mkdir('.tmp')
if not os.path.exists(path):
print('Downloading ldid!')
urlretrieve(url, path)
if not os.path.exists(ldid_path):
with ZipFile(path, 'r') as f:
if host_os == 'Darwin':
print('Extracting MacOS ldid...')
f.extract('ldid', '.tmp')
os.chmod(ldid_path, 0o755)
elif host_os == 'Linux':
with f.open('linux64/ldid') as yeet, open(ldid_path, 'wb') as yort:
print('Extracting Linux ldid...')
shutil.copyfileobj(yeet, yort)
os.chmod(ldid_path, 0o755)
def dump(host, port, user, password):
ldid_path = '.tmp/ldid'
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(
host,
port=port,
username=user,
password=password,
look_for_keys=False,
allow_agent=False)
except ConnectionError:
raise
else:
bad_string = b"dd: failed to open '/dev/disk1': Operation not permitted\n"
shsh_dump_path = '/tmp/shsh_dump.bin'
ssh.invoke_shell()
sftp_client = ssh.open_sftp()
dd_in, dd_out, dd_err = ssh.exec_command(
'dd if=/dev/disk1 of={}'.format(shsh_dump_path))
if dd_err.read() == bad_string:
print('dd failed to dump, applying entitlements!')
# NOTE This is very important, so, seems like dropbear doesn't work.
# This means that we absolutely need something that'll work with
# paramiko. Thankfully, having OpenSSH installed fixes this issue.
print('Downloading dd!')
sftp_client.get('/bin/dd', '.tmp/dd')
print('Applying entitlements from ent.plist to dd!')
subprocess.run((ldid_path, '-Sent.plist', '.tmp/dd'))
print('Uploading modified dd!')
sftp_client.put('.tmp/dd', '/bin/dd-dump')
ssh.exec_command('chmod +x /bin/dd-dump')
dd_dump_in, dd_dump_out, dd_dump_err = ssh.exec_command(
'dd-dump if=/dev/disk1 of={}'.format(shsh_dump_path))
try:
sftp_client.get(shsh_dump_path, '.tmp/shsh_dump.bin')
except FileNotFoundError:
print('Somehow {} does not exist!'.format(shsh_dump_path))
raise
else:
sftp_client.close()
ssh.close()
# Ensure that '.tmp/shsh_dump.bin' was actually downloaded
try:
os.path.exists(shsh_dump_path)
except FileNotFoundError:
print('Wut? Somehow the dump does not exist!')
raise
else:
print('Seems like {} exists!'.format(shsh_dump_path))
def extract():
subprocess.run(
('img4tool',
'-s',
'.tmp/blob.shsh2',
'--convert',
'.tmp/shsh_dump.bin'),
stdout=subprocess.DEVNULL)
try:
os.path.exists('.tmp/blob.shsh2')
except FileNotFoundError:
print('Something went wrong extracting the shsh/ticket from the dump!')
raise
else:
print('Seems like everything worked! Enjoy your dumped shsh :P -Merc')
def go():
argv = sys.argv
if not os.path.exists('.tmp'):
has_plist = b'Compiled with plist: YES'
img4tool_check = subprocess.run(
('which',
'img4tool'),
stdout=subprocess.PIPE)
if img4tool_check.returncode == 1 or has_plist not in img4tool_check.stdout:
getimg4tool()
ldid_check = subprocess.run(
('which',
'ldid'),
stdout=subprocess.DEVNULL)
if ldid_check.returncode == 1:
getldid()
if not os.path.exists('.tmp/shsh_dump.bin'):
print('Starting dumping process!')
dump(argv[2], argv[4], argv[6], argv[8])
extract()
else:
if not os.path.exists('.tmp/blob.shsh2'):
print('Seems like we already have a dump, just extracting!')
extract()
else:
sys.exit('Nothing to do!')
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'--ip',
help='Specify address of your device',
metavar='\b',
nargs=1,
required=True)
parser.add_argument(
'--port',
help='Use a different port for SSH',
metavar='\b',
nargs=1,
required=True)
parser.add_argument(
'--user',
help='Username to connect with (default should be root)',
metavar='\b',
nargs=1,
required=True)
parser.add_argument(
'--password',
help='Password of the user you choose (if using root, default password is alpine)',
metavar='\b',
nargs=1,
required=True)
try:
args = parser.parse_args()
except Exception:
sys.exit(parser.print_help(sys.stderr))
else:
start = time.time()
go()
end = time.time() - start
print('We took {:.2f} seconds!'.format(end))
if __name__ == '__main__':
main()