forked from alibaba/iOSSecAudit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice.py
More file actions
722 lines (558 loc) · 24.2 KB
/
device.py
File metadata and controls
722 lines (558 loc) · 24.2 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
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
#author: june
import re
import os
import numpy
import select
import paramiko
from abstracttool import Tool
from minisftp import Minisftp
from application import App
from sysapp import SysApp
from BinaryCookieReader import BinaryUtil
from KeychainUtil import KeychainUtil
from SshUtil import SSHOptionUtil
from CerticateUtil import CertficateUtil
from prettytable import PrettyTable
import globals as G
########################################################################
class Device(Tool):
""""""
apps_dir_ios_pre8 = '/private/var/mobile/Applications'
apps_dir_ios_8 = '/private/var/mobile/Containers/Bundle/Application'
data_dir_ios_8 = '/private/var/mobile/Containers/Data/Application'
#----------------------------------------------------------------------
def __init__(self, host, username, password, sftp_port):
"""Constructor"""
super(self.__class__, self).__init__()
self.host = host
self.username = username
self.password = password
self.sftp_port = sftp_port
self.sshopt = SSHOptionUtil(self.host, self.username, self.password, self.sftp_port)
self.ssh = self.sshopt.ssh
if self.sshopt.ssh is None:
return
#connect sftp
self.minisftp = self.sshopt.minisftp#Minisftp(self.host, self.sftp_port, self.username, self.password)
#self.minisftp.sftp_conn()
if self.sshopt.minisftp is None:
return
#check ios version
stat = self.minisftp.file_exists(self.apps_dir_ios_8)
self.cache_dir = self.tmp_path()
self.applist = []
self.sysapplist = []
self.keychaineditor = None
self.cert = None
if not stat:
self.ios_version = 7
self.apps_dir = self.apps_dir_ios_pre8
self.data_dir = self.apps_dir_ios_pre8
else:
self.ios_version = 8
self.apps_dir = self.apps_dir_ios_8
self.data_dir = self.data_dir_ios_8
self.sys_apps_dir = '/Applications'
'''
#----------------------------------------------------------------------
def ssh_conn(self):
""""""
self.ssh = paramiko.SSHClient()
self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.ssh.connect(self.host, username=self.username, password=self.password)
#----------------------------------------------------------------------
def ssh_exec(self, cmd, buffsize=-1, timeout=None, get_pty=False):
""""""
try:
stdin, stdout, stderr = self.ssh.exec_command(cmd, buffsize, timeout, get_pty)
return stdout.readlines()
except paramiko.SSHException:
return -1
#----------------------------------------------------------------------
def ssh_exec_last(self, cmd, buffsize=-1, timeout=None, get_pty=False):
""""""
try:
stdin, stdout, stderr = self.ssh.exec_command(cmd, buffsize, timeout, get_pty)
return stdout
except paramiko.SSHException:
return -1
#----------------------------------------------------------------------
def ssh_close(self):
""""""
self.minisftp.sftp_close()
self.ssh.close()
'''
#----------------------------------------------------------------------
def list_all_applications(self):
""""""
#third party app list
self.applist = self.install_app_list()
#system app list
self.sysapplist = self.system_app_list()
#----------------------------------------------------------------------
def find_app_by_pattern(self, pattern):
"""
function: find app(including third party app and system app)
"""
app = self.find_sysapp_by_pattern(pattern)
if app is None:
app = self.app_by_bundle_id(pattern)
return app
#----------------------------------------------------------------------
def find_sysapp_by_pattern(self, pattern):
"""
function: find sysapp
"""
#find sysapp list first
self.sysapplist = self.system_app_list()
for sysapp in self.sysapplist:
if pattern == sysapp.basename[:sysapp.basename.rfind('.app')] or pattern == sysapp.bundle_identifier:
return sysapp
return None
#----------------------------------------------------------------------
def system_app_list(self):
""""""
if self.sysapplist != []:
return self.sysapplist
#get system application dir list
l = self.minisftp.list_dir(self.sys_apps_dir)
if l is None:
G.log(G.INFO, "No applications found.")
return None
for basename in l:
sysapp = SysApp(basename, self)
if sysapp.binary_name is None:
continue
self.sysapplist.append(sysapp)
return self.sysapplist
#----------------------------------------------------------------------
def install_app_list(self):
""""""
#if self.applist != []:
# return self.applist
if self.ios_version == 8:
# need to refresh iOS uicache in case app was installed after last reboot.
# Otherwise the /var/mobile/Library/MobileInstallation/LastLaunchServicesMap.plist will be out of date
G.log(G.INFO, 'Refresh LastLaunchServicesMap...')
self.sshopt.ssh_exec(self.cmd_refresh_uicache)
#get uuid list
l = self.minisftp.list_dir(self.apps_dir)
if l is None:
G.log(G.INFO, "No applications found.")
return None
#add who
for uuid in l:
app = App(uuid, self)
if app.binary_name is None:
continue
self.applist.append(app)
return self.applist
#----------------------------------------------------------------------
def app_by_bundle_id(self, bundle_id):
"""
function : find third party application
"""
res = None
refresh_flag = False
if self.applist is None:
refresh_flag = True
self.applist = self.install_app_list()
if self.applist is None:
G.log(G.INFO, 'Faild to find any application.')
res = None
for e in self.applist:
if e.bundle_identifier == bundle_id:
res = e
break
if not refresh_flag and res is None:
self.applist = self.install_app_list()
if self.applist is None:
G.log(G.INFO, 'Faild to find any application.')
res = None
for e in self.applist:
if e.bundle_identifier == bundle_id:
res = e
break
return res
#----------------------------------------------------------------------
def _remote2cache_path(self, path):
""""""
local_path = self.cache_dir + re.sub("/private/var/mobile/Applications", "", re.sub("/private/var/mobile/Containers/Data/Application", "", re.sub(self.apps_dir, "", path)))# os.path.basename(path)#os.path.dirname(tmp_path)
#local_path = re.sub(".app", "_app", local_cache_dir)
return local_path
#----------------------------------------------------------------------
def _cache_file(self, remote_path):
""""""
local_path = self._remote2cache_path(remote_path)
#local_path = re.sub(".app", "_app", path)
self.mkdir_p(local_path[:local_path.rfind("/")])
if not os.path.exists(local_path):
self.minisftp.getfile(remote_path, local_path, None)
return os.path.realpath(local_path.encode("utf-8").strip())
#----------------------------------------------------------------------
def read_binary_cookie(self):
""""""
binary_cookie_remote_path = "/private/var/mobile/Library/Cookies/Cookies.binarycookies"
binary_cookie_path = self._cache_file(binary_cookie_remote_path)
binary_cookie = BinaryUtil(binary_cookie_path)
binary_cookie.read_binary_cookie()
#----------------------------------------------------------------------
def read_keyboard_cache(self):
""""""
keyboard_cache_remote_file = '/private/var/mobile/Library/Keyboard/dynamic-text.dat'
keyboard_cache_file = self._cache_file(keyboard_cache_remote_file)
#myarray = numpy.fromfile(keyboard_cache_file, dtype=float)
#f = open(keyboard_cache_file, "rb")
res = ""
with open(keyboard_cache_file, "rb") as f:
byte = f.read(1)
while byte:
#ASCII printable
if ord(byte) >= 0x20 and ord(byte) <= 0x7E:
res += byte
else:
res += ' '
byte = f.read(1)
return res
#----------------------------------------------------------------------
def logging(self, stop_event, _filter=None):
""""""
if not self.tool_installed('logtool'):
G.log(G.INFO, 'logtool not installed, try \'chenv\' first.')
cmd = self.tool_path['logtool']
transport = self.ssh.get_transport()
channel = transport.open_session()
channel.get_pty()
channel.exec_command(cmd)
self.content = ''
while True:
if stop_event.is_set():
break
if channel.exit_status_ready():
break
rl, wl, xl = select.select([channel], [], [], 0.0)
if len(rl) > 0:
l = channel.recv(1024)
if _filter is not None and _filter in str(l).decode("utf-8").strip():
print l,
else:
print l,
self.content += l
'''
if _filter is None:
cmd = "idevicesyslog"
else:
cmd = 'idevicesyslog | awk -F\'{}\[[0-9]+\] *\' \'/{}\[[0-9]+\]/{}\''.format(_filter, _filter, '{print $2}')
#cmd = 'idevicesyslog | grep \'{}\''.format(_filter)
stdout_ = self.exec_shell_last(cmd)
l = stdout_.readline()
while l:
print l,
l = stdout_.readline()
'''
#----------------------------------------------------------------------
#pb_names pb_names should be split by space, e.g."a b c d e"
def watch_pasteboard(self, pb_names=None):
""""""
if pb_names is None:
cmd = "{} 1".format(self.tool_path['pbwatcher'])
#cmd = "ls"
else:
cmd = "{} 1 {}".format(self.tool_path['pbwatcher'], pb_names)
transport = self.ssh.get_transport()
channel = transport.open_session()
channel.get_pty()
channel.exec_command(cmd)
content = ''
while True:
if channel.exit_status_ready():
break
rl, wl, xl = select.select([channel], [], [], 0.0)
if len(rl) > 0:
l = channel.recv(1024).split(" ")
date = l[0]
time = l[1]
irange = l[2]
#do print when pastedboard changed
if content != l[3]:
content = l[3]
print date, time, content,
#----------------------------------------------------------------------
def install_ipa(self, ipa_path):
""""""
filename, file_extension = os.path.splitext(ipa_path)
if os.path.isfile(ipa_path) is not True:
return False
if file_extension != '.ipa' and file_extension != '.IPA':
return False
#best check if bundle_id has been installed or not
#but first should read ipa file with python to get bundle_id, so let it go
#get remote file full path
remote_path = '/var/root/sec_audit_install.ipa' #+ os.path.basename(ipa_path)
#should check if remotefile exits
cmd_clear_ipa = "rm {}".format(remote_path)
if self.minisftp.file_exists(remote_path):
#if exists, rm remote file
r = self.sshopt.ssh_exec(cmd_clear_ipa)
#upload ipa file
attr = self.minisftp.putfile(remote_path, ipa_path, None)
#exec install
cmd = "{} {}".format(self.tool_path['appinst'], remote_path)
transport = self.ssh.get_transport()
channel = transport.open_session()
channel.get_pty()
channel.exec_command(cmd)
while True:
if channel.exit_status_ready():
break
rl, wl, xl = select.select([channel], [], [], 0.0)
if len(rl) > 0:
l = channel.recv(4096).split(" ")
r = ' '.join(l).split('\n')
for e in r:
#best to check (if ' installed bundle_id' in e: )
#but first should read ipa file with python to get bundle_id, so let it go
if ' installed ' in e and 'have installed appinst' not in e:
#clear ipa file
r = self.sshopt.ssh_exec(cmd_clear_ipa)
return True
if 'not exist' in e or 'failed to' in e or 'cannot read ipa file entry' in e:
G.log(G.INFO, e)
#clear ipa file
r = self.sshopt.ssh_exec(cmd_clear_ipa)
return False
#----------------------------------------------------------------------
def launch_app(self, bundle_id):
""""""
pid = str(0)
cmd = "{} {}".format(self.tool_path['open'], bundle_id)
transport = self.ssh.get_transport()
channel = transport.open_session()
channel.get_pty()
channel.exec_command(cmd)
while True:
if channel.exit_status_ready():
break
rl, wl, xl = select.select([channel], [], [], 0.0)
if len(rl) > 0:
l = channel.recv(1024).split(" ")
r = ' '.join(l)#.split('\n')
return False, r, pid
#get pid
app = self.find_app_by_pattern(bundle_id)
if app is None:
return False, '', str(0)
pid = app.get_pid()
return True, '', pid
#----------------------------------------------------------------------
def tool_installed(self, tool):
""""""
if self.tool_path.has_key(tool):
return self.minisftp.file_exists(self.tool_path[tool])
else:
return False
#----------------------------------------------------------------------
def cache_file(self, remote_path, local_path):
""""""
self.mkdir_p(local_path[:local_path.rfind("/")])
if not os.path.exists(local_path):
self.minisftp.getfile(remote_path, local_path, None)
return os.path.realpath(local_path.encode("utf-8").strip())
#----------------------------------------------------------------------
def cycript_installed(self):
""""""
return self.tool_installed('cycript')
#----------------------------------------------------------------------
def file_exists(self, remote_path):
""""""
return self.minisftp.file_exists(remote_path)
#----------------------------------------------------------------------
def dump_keychain(self):
""""""
#if self.keychaineditor is None:
# self.keychaineditor = KeychainUtil(self.sshopt)
self.keychaineditor = KeychainUtil(self.sshopt)
return self.keychaineditor.parse()
#----------------------------------------------------------------------
def edit_keychain(self, dataID, newData, with_base64):
""""""
#if self.keychaineditor is None:
self.keychaineditor = KeychainUtil(self.sshopt)
return self.keychaineditor.edit_by_id(dataID, newData, with_base64)
#----------------------------------------------------------------------
def delete_keychain(self, dataID):
""""""
#if self.keychaineditor is None:
self.keychaineditor = KeychainUtil(self.sshopt)
return self.keychaineditor.delete_by_ID(dataID)
#----------------------------------------------------------------------
def grep_keychain(self, pattern):
""""""
#if self.keychaineditor is None:
self.keychaineditor = KeychainUtil(self.sshopt)
return self.keychaineditor.grep_keychain(pattern)
#----------------------------------------------------------------------
def CA_install_burp(self):
""""""
if self.cert is None:
self.cert = CertficateUtil(self.sshopt)
return self.cert.install_burp_cert()
#----------------------------------------------------------------------
def CA_list_cert(self):
""""""
if self.cert is None:
self.cert = CertficateUtil(self.sshopt)
return self.cert.list_certs()
#----------------------------------------------------------------------
def CA_delete_cert(self, cert_id):
""""""
if self.cert is None:
self.cert = CertficateUtil(self.sshopt)
return self.cert.delete_cert(cert_id)
#----------------------------------------------------------------------
def CA_add_cert(self, cert_path):
""""""
if self.cert is None:
self.cert = CertficateUtil(self.sshopt)
if not self.local_file_exists(cert_path):
G.log(G.INFO, 'File \'{}\' not exists.'.format(cert_path))
return False
return self.cert.add_cert(cert_path)
#----------------------------------------------------------------------
def CA_export_cert(self, path):
""""""
if self.cert is None:
self.cert = CertficateUtil(self.sshopt)
return self.cert.export_cert(path)
#----------------------------------------------------------------------
def open_url(self, url):
""""""
if self.tool_installed('uiopen'):
cmd = "{} {}".format(self.tool_path['uiopen'], url)
self.sshopt.ssh_exec(cmd)
#----------------------------------------------------------------------
def kill_by_name(self, name):
""""""
cmd = "killall -9 {}".format(name)
self.sshopt.ssh_exec(cmd)
#----------------------------------------------------------------------
def install_tool(self, tool, remote_path):
""""""
#----get current directory path-----
#print os.getcwd()
#print os.path.abspath(os.curdir)
#print os.path.abspath('.')
p = os.path.split(os.path.realpath(__file__))[0]
index = p.rfind('/')
path = p[0:index]
local_path = path + os.sep + 'bin' + os.sep + tool
self.minisftp.putfile(remote_path, local_path, None)
cmd = "chmod a+x {}".format(remote_path)
self.sshopt.ssh_exec(cmd)
#print local_path
#----------------------------------------------------------------------
def check_env(self):
""""""
G.log(G.INFO, 'Checking enviroment...')
keys = self.tool_path.keys()
'''
for key in keys:
if not self.tool_installed(key):
print "\'{}\' => NO".format(key),
if str(self.tool_path[key]).find('/var/root/') != -1:
print "=> installing \'{}\'...".format(key),
self.install_tool(key, str(self.tool_path[key]))
print "=> complete."
else:
print "=> Please install it in Cydia."
else:
print "\'{}\' => YES ".format(key)
'''
result = PrettyTable(["Tool", "Status", "Solution"])
result.align["Solution"] = "l"
result.align["Tool"] = "l"
tool = 'None'
status = 'NO'
solution = 'None'
for key in keys:
tool = key
if not self.tool_installed(key):
if str(self.tool_path[key]).find('/var/root/') != -1:
self.install_tool(key, str(self.tool_path[key]))
status = 'Ready'
solution = 'None'
else:
status = 'NOT Ready'
solution = 'Install in Cydia'
else:
status = 'Ready'
solution = 'None'
result.add_row([tool, status, solution])
print result
#----------------------------------------------------------------------
def download_dir(self, remote_dir, save_path, clearFirst=True, clearAfter=True):
""""""
#check remote file exists
if not self.file_exists(remote_dir):
G.log(G.INFO, 'Dir \'{}\' not exists.'.format(remote_dir))
return False
#check remote path is dir
if not self.minisftp.isdir(remote_dir):
G.log(G.INFO, '\'{}\' is not dir.'.format(remote_dir))
return False
#make tar and gzip package name
#gz_name is the final download file
tar_name = '/var/root/_tmp_iSecAudit_dir_download.tar'
gz_name = '{}.gz'.format(tar_name)
if os.path.isdir(os.path.abspath(os.path.expanduser(save_path))):
save_path = os.path.join(save_path, os.path.basename(gz_name))
#mkdir_p
self.mkdir_p(os.path.abspath(os.path.expanduser(os.path.dirname(save_path))))
#check local file exists
idx = 0
_local_path = save_path
while os.path.exists(_local_path):
tmp = _local_path
_local_path = '_'.join([save_path, str(idx)])
idx += 1
G.log(G.INFO, 'Local file \'{}\' already exists, try path: \'{}\'.'.format(tmp, _local_path))
#clear first
if clearFirst:
G.log(G.INFO, 'Do clear job at beginning...')
if self.file_exists(tar_name):
cmd = 'rm {}'.format(tar_name)
self.sshopt.ssh_exec(cmd)
if self.file_exists(gz_name):
cmd = 'rm {}'.format(gz_name)
self.sshopt.ssh_exec(cmd)
G.log(G.INFO, 'Package result...')
if remote_dir.endswith(os.path.sep):
remote_res_path = remote_dir[:-1]
else:
remote_res_path = remote_dir
#tar target dir
index = remote_res_path.rfind(os.path.sep)
tmp_dir = remote_res_path[:index]
target_dir = remote_res_path[index + 1:]
cmd = 'cd \'{}\' && tar cvf \'{}\' \'{}\''.format(tmp_dir, tar_name, target_dir)
self.sshopt.ssh_exec(cmd)
#gzip target tar
G.log(G.INFO, 'Gzip package...')
cmd = 'cd ~ && gzip \'{}\''.format(tar_name)
self.sshopt._ssh_exec(cmd)
#download zip file
G.log(G.INFO, 'Download result...')
r = False
r = self.minisftp.getfile(gz_name, _local_path, None)
if r == False:
G.log(G.INFO, 'Target dir maybe an empty dir...')
#clear after
if clearAfter:
G.log(G.INFO, 'Do clear job at last...')
if self.file_exists(tar_name):
cmd = 'rm {}'.format(tar_name)
self.sshopt.ssh_exec(cmd)
if self.file_exists(gz_name):
cmd = 'rm {}'.format(gz_name)
self.sshopt.ssh_exec(cmd)
return _local_path, r