-
Notifications
You must be signed in to change notification settings - Fork 5
/
plist_service.py
executable file
·112 lines (96 loc) · 3.51 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
#
# pymobiledevice - Jython implementation of libimobiledevice
#
# Copyright (C) 2014 Taconut <https://github.com/Triforce1>
# Copyright (C) 2014 PythEch <https://github.com/PythEch>
# Copyright (C) 2013 GotoHack <https://github.com/GotoHack>
#
# pymobiledevice is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pymobiledevice 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with pymobiledevice. If not, see <http://www.gnu.org/licenses/>.
import os
import plistlib
import struct
from re import sub
from usbmux import usbmux
from util.bplist import BPlistReader
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(5.0)
dev = None
while not dev and mux.devices:
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.socket = mux.connect(dev, self.port)
except:
raise Exception("Connection to device port %d failed" % self.port)
return dev.serial
def close(self):
self.socket.close()
def recv(self, len=4096):
return self.socket.recv(len)
def send(self, data):
return self.socket.send(data)
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(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()
#print '<<<<<<<<',payload
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)
#print '>>>>',payload
l = struct.pack(">L", len(payload))
self.send(l + payload)
def sendRequest(self, plist):
self.sendPlist(plist)
return self.recvPlist()
def start_ssl(self, keyfile):
if os._name == 'nt':
self.socket = usbmux.SSLSocket(self.socket.sock._sock._get_jsocket(), keyfile)
else:
self.socket = usbmux.SSLSocket(self.socket.sock, keyfile)