forked from iOSForensics/pymobiledevice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plist_service.py
executable file
·121 lines (104 loc) · 3.45 KB
/
plist_service.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
#!/usr/bin/env python
# -*- coding: utf8 -*-
#
# $Id$
#
# Copyright (c) 2012-2014 "dark[-at-]gotohack.org"
#
# This file is part of pymobiledevice
#
# pymobiledevice 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/>.
#
#
from usbmux import usbmux
from util.bplist import BPlistReader
import plistlib
import ssl
import struct
from pprint import pprint
from re import sub
class PlistService(object):
def __init__(self, port, udid=None):
self.port = port
self.connect(udid)
def connect(self, udid=None):
mux = usbmux.USBMux()
mux.process(1.0)
dev = None
while not dev and mux.devices :
mux.process(1.0)
if udid:
for d in mux.devices:
if d.serial == udid:
dev = d
print "Connecting to device: " + dev.serial
else:
dev = mux.devices[0]
print "Connecting to device: " + dev.serial
try:
self.s = mux.connect(dev, self.port)
except:
raise Exception("Connexion to device port %d failed" % self.port)
return dev.serial
def close(self):
self.s.close()
def recv(self, length=4096):
return self.s.recv(length)
def send(self, data):
try:
self.s.send(data)
except:
print "Sending data to device failled"
return -1
return 0
def sendRequest(self, data):
res = None
if self.sendPlist(data) >= 0:
res = self.recvPlist()
return res
def recv_exact(self, l):
data = ""
while l > 0:
d = self.recv(l)
if not d or len(d) == 0:
break
data += d
l -= len(d)
return data
def recv_raw(self):
l = self.recv_exact(4)
if not l or len(l) != 4:
return
l = struct.unpack(">L", l)[0]
return self.recv_exact(l)
def send_raw(self, data):
return self.send(struct.pack(">L", len(data)) + data)
def recvPlist(self):
payload = self.recv_raw()
if not payload:
return
if payload.startswith("bplist00"):
return BPlistReader(payload).parse()
elif payload.startswith("<?xml"):
#HAX lockdown HardwarePlatform with null bytes
payload = sub('[^\w<>\/ \-_0-9\"\'\\=\.\?\!\+]+','', payload.decode('utf-8')).encode('utf-8')
return plistlib.readPlistFromString(payload)
else:
raise Exception("recvPlist invalid data : %s" % payload[:100].encode("hex"))
def sendPlist(self, d):
payload = plistlib.writePlistToString(d)
l = struct.pack(">L", len(payload))
return self.send(l + payload)
def ssl_start(self, keyfile, certfile):
self.s = ssl.wrap_socket(self.s, keyfile, certfile, ssl_version=ssl.PROTOCOL_TLSv1)