-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
mcinstall.go
306 lines (279 loc) · 8.63 KB
/
mcinstall.go
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
package mcinstall
import (
"fmt"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/pkcs12"
"io"
ios "github.com/danielpaulus/go-ios/ios"
)
const serviceName string = "com.apple.mobile.MCInstall"
type Connection struct {
deviceConn ios.DeviceConnectionInterface
plistCodec ios.PlistCodec
}
func New(device ios.DeviceEntry) (*Connection, error) {
deviceConn, err := ios.ConnectToService(device, serviceName)
if err != nil {
return &Connection{}, err
}
var mcInstallConn Connection
mcInstallConn.deviceConn = deviceConn
mcInstallConn.plistCodec = ios.NewPlistCodec()
return &mcInstallConn, nil
}
type ProfileInfo struct {
Identifier string
Manifest ProfileManifest
Metadata ProfileMetadata
Status string
}
type ProfileMetadata struct {
PayloadDescription string
PayloadDisplayName string
PayloadRemovalDisallowed bool
PayloadUUID string
PayloadVersion uint64
}
type ProfileManifest struct {
Description string
IsActive bool
}
func (mcInstallConn *Connection) readExchangeResponse(reader io.Reader) ([]ProfileInfo, error) {
responseBytes, err := mcInstallConn.plistCodec.Decode(reader)
if err != nil {
return []ProfileInfo{}, err
}
dict, err := ios.ParsePlist(responseBytes)
if err != nil {
return []ProfileInfo{}, err
}
identifiersIntf, ok := dict["OrderedIdentifiers"]
if !ok {
return []ProfileInfo{}, fmt.Errorf("invalid plist response, missing key 'OrderedIdentifiers' dump: %x", responseBytes)
}
identifiers, ok := identifiersIntf.([]interface{})
if !ok {
return []ProfileInfo{}, fmt.Errorf("identifiers should be array, dump: %x", responseBytes)
}
profiles := make([]ProfileInfo, len(identifiers))
for i, id := range identifiers {
idString, ok := id.(string)
if !ok {
return []ProfileInfo{}, fmt.Errorf("identifiers should be array of strings, dump: %x", responseBytes)
}
profile, err := parseProfile(idString, dict)
if err != nil {
return []ProfileInfo{}, err
}
profiles[i] = profile
}
return profiles, nil
}
func parseProfile(idString string, dict map[string]interface{}) (ProfileInfo, error) {
result := ProfileInfo{}
result.Identifier = idString
manifestIntf, ok := dict["ProfileManifest"]
if !ok {
return result, fmt.Errorf("missing key ProfileManifest %+v", dict)
}
manifest, ok := manifestIntf.(map[string]interface{})
if !ok {
return result, fmt.Errorf("ProfileManifest should be a map %+v", dict)
}
manifestIntf, ok = manifest[idString]
if !ok {
return result, fmt.Errorf("missing key %s %+v", idString, dict)
}
manifest, ok = manifestIntf.(map[string]interface{})
if !ok {
return result, fmt.Errorf("%s should be a map %+v", idString, dict)
}
result.Manifest.IsActive, ok = manifest["IsActive"].(bool)
if !ok {
return result, fmt.Errorf("keyError %+v", dict)
}
result.Manifest.Description, ok = manifest["Description"].(string)
if !ok {
return result, fmt.Errorf("keyError %+v", dict)
}
result.Status, ok = dict["Status"].(string)
if !ok {
return result, fmt.Errorf("keyError %+v", dict)
}
metadataIntf, ok := dict["ProfileMetadata"]
if !ok {
return result, fmt.Errorf("missing key ProfileMetadata %+v", dict)
}
metadata, ok := metadataIntf.(map[string]interface{})
if !ok {
return result, fmt.Errorf("ProfileMetadata should be a map %+v", dict)
}
metadataIntf, ok = metadata[idString]
if !ok {
return result, fmt.Errorf("missing key %s %+v", idString, dict)
}
metadata, ok = metadataIntf.(map[string]interface{})
if !ok {
return result, fmt.Errorf("%s should be a map %+v", idString, dict)
}
result.Metadata.PayloadDescription, ok = metadata["PayloadDescription"].(string)
if !ok {
result.Metadata.PayloadDescription = ""
}
result.Metadata.PayloadDisplayName, ok = metadata["PayloadDisplayName"].(string)
if !ok {
return result, fmt.Errorf("keyError PayloadDisplayName %+v", dict)
}
result.Metadata.PayloadRemovalDisallowed, ok = metadata["PayloadRemovalDisallowed"].(bool)
if !ok {
return result, fmt.Errorf("keyError PayloadRemovalDisallowed %+v", dict)
}
result.Metadata.PayloadUUID, ok = metadata["PayloadUUID"].(string)
if !ok {
return result, fmt.Errorf("keyError PayloadUUID %+v", dict)
}
result.Metadata.PayloadVersion, ok = metadata["PayloadVersion"].(uint64)
if !ok {
return result, fmt.Errorf("keyError PayloadVersion %+v", dict)
}
return result, nil
}
func (mcInstallConn *Connection) Escalate(p12bytes []byte, p12Password string) error {
supervisedPrivateKey, supervisionCert, err := pkcs12.Decode(p12bytes, p12Password)
if err != nil {
return err
}
request := map[string]interface{}{"RequestType": "Escalate", "SupervisorCertificate": supervisionCert.Raw}
dict, err := mcInstallConn.sendAndReceive(request)
if err != nil {
return err
}
if !checkStatus(dict) {
return fmt.Errorf("escalate response had error %+v", dict)
}
challengeInt, ok := dict["Challenge"]
if !ok {
return fmt.Errorf("missing key Challenge %+v", dict)
}
challenge, ok := challengeInt.([]byte)
signedRequest, err := ios.Sign(challenge, supervisionCert, supervisedPrivateKey)
if err != nil {
return err
}
request = map[string]interface{}{"RequestType": "EscalateResponse", "SignedRequest": signedRequest}
dict, err = mcInstallConn.sendAndReceive(request)
if err != nil {
return err
}
if !checkStatus(dict) {
return fmt.Errorf("escalateresponse response had error %+v", dict)
}
request = map[string]interface{}{"RequestType": "ProceedWithKeybagMigration"}
dict, err = mcInstallConn.sendAndReceive(request)
if err != nil {
return err
}
if !checkStatus(dict) {
return fmt.Errorf("proceedWithKeybagMigration response had error %+v", dict)
}
return nil
}
func checkStatus(response map[string]interface{}) bool {
statusIntf, ok := response["Status"]
if !ok {
return false
}
status, ok := statusIntf.(string)
if !ok {
return false
}
if "Acknowledged" != status {
return false
}
return true
}
func (mcInstallConn *Connection) sendAndReceive(request map[string]interface{}) (map[string]interface{}, error) {
reader := mcInstallConn.deviceConn.Reader()
requestBytes, err := mcInstallConn.plistCodec.Encode(request)
if err != nil {
return map[string]interface{}{}, err
}
err = mcInstallConn.deviceConn.Send(requestBytes)
if err != nil {
return map[string]interface{}{}, err
}
responseBytes, err := mcInstallConn.plistCodec.Decode(reader)
if err != nil {
return map[string]interface{}{}, err
}
return ios.ParsePlist(responseBytes)
}
func (mcInstallConn *Connection) HandleList() ([]ProfileInfo, error) {
reader := mcInstallConn.deviceConn.Reader()
request := map[string]interface{}{"RequestType": "GetProfileList"}
requestBytes, err := mcInstallConn.plistCodec.Encode(request)
if err != nil {
return []ProfileInfo{}, err
}
err = mcInstallConn.deviceConn.Send(requestBytes)
if err != nil {
return []ProfileInfo{}, err
}
return mcInstallConn.readExchangeResponse(reader)
}
//Close closes the underlying DeviceConnection
func (mcInstallConn *Connection) Close() error {
return mcInstallConn.deviceConn.Close()
}
func (mcInstallConn *Connection) AddProfile(profilePlist []byte) error {
return mcInstallConn.addProfile(profilePlist, "InstallProfile")
}
func (mcInstallConn *Connection) addProfile(profilePlist []byte, installcmd string) error {
request := map[string]interface{}{"RequestType": installcmd, "Payload": profilePlist}
requestBytes, err := mcInstallConn.plistCodec.Encode(request)
if err != nil {
return err
}
err = mcInstallConn.deviceConn.Send(requestBytes)
if err != nil {
return err
}
respBytes, err := mcInstallConn.plistCodec.Decode(mcInstallConn.deviceConn.Reader())
if err != nil {
return err
}
plist, err := ios.ParsePlist(respBytes)
if checkStatus(plist) {
return nil
}
log.Errorf("received remove response %+v", plist)
return fmt.Errorf("remove failed")
}
func (mcInstallConn *Connection) RemoveProfile(identifier string) error {
request := map[string]interface{}{"RequestType": "RemoveProfile", "ProfileIdentifier": identifier}
requestBytes, err := mcInstallConn.plistCodec.Encode(request)
if err != nil {
return err
}
err = mcInstallConn.deviceConn.Send(requestBytes)
if err != nil {
return err
}
respBytes, err := mcInstallConn.plistCodec.Decode(mcInstallConn.deviceConn.Reader())
if err != nil {
return err
}
plist, err := ios.ParsePlist(respBytes)
if checkStatus(plist) {
return nil
}
log.Errorf("received remove response %+v", plist)
return fmt.Errorf("remove failed")
}
func (mcInstallConn *Connection) AddProfileSupervised(profileFileBytes []byte, p12fileBytes []byte, password string) error {
err := mcInstallConn.Escalate(p12fileBytes, password)
if err != nil {
return err
}
return mcInstallConn.addProfile(profileFileBytes, "InstallProfileSilent")
}