-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
house_arrest.go
176 lines (155 loc) · 5.45 KB
/
house_arrest.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
package house_arrest
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"strings"
"github.com/danielpaulus/go-ios/ios"
"howett.net/plist"
)
const serviceName = "com.apple.mobile.house_arrest"
type Connection struct {
deviceConn ios.DeviceConnectionInterface
packageNumber uint64
}
func New(device ios.DeviceEntry, bundleID string) (*Connection, error) {
deviceConn, err := ios.ConnectToService(device, serviceName)
if err != nil {
return &Connection{}, err
}
err = vendContainer(deviceConn, bundleID)
if err != nil {
return &Connection{}, err
}
return &Connection{deviceConn: deviceConn}, nil
}
func vendContainer(deviceConn ios.DeviceConnectionInterface, bundleID string) error {
plistCodec := ios.NewPlistCodec()
vendContainer := map[string]interface{}{"Command": "VendContainer", "Identifier": bundleID}
msg, err := plistCodec.Encode(vendContainer)
if err != nil {
return fmt.Errorf("VendContainer Encoding cannot fail unless the encoder is broken: %v", err)
}
err = deviceConn.Send(msg)
if err != nil {
return err
}
reader := deviceConn.Reader()
response, err := plistCodec.Decode(reader)
if err != nil {
return err
}
return checkResponse(response)
}
func checkResponse(vendContainerResponseBytes []byte) error {
response, err := plistFromBytes(vendContainerResponseBytes)
if err != nil {
return err
}
if "Complete" == response.Status {
return nil
}
if response.Error != "" {
return errors.New(response.Error)
}
return errors.New("unknown error during vendcontainer")
}
func plistFromBytes(plistBytes []byte) (vendContainerResponse, error) {
var vendResponse vendContainerResponse
decoder := plist.NewDecoder(bytes.NewReader(plistBytes))
err := decoder.Decode(&vendResponse)
if err != nil {
return vendResponse, err
}
return vendResponse, nil
}
type vendContainerResponse struct {
Status string
Error string
}
func (c Connection) Close() {
c.deviceConn.Close()
}
func (conn *Connection) SendFile(fileContents []byte, filePath string) error {
handle, err := conn.openFileForWriting(filePath)
if err != nil {
return err
}
err = conn.sendFileContents(fileContents, handle)
if err != nil {
return err
}
return conn.closeHandle(handle)
}
func (conn *Connection) ListFiles(filePath string) ([]string, error) {
headerPayload := []byte(filePath)
headerLength := uint64(len(headerPayload))
this_length := afc_header_size + headerLength
header := AfcPacketHeader{Magic: afc_magic, Packet_num: conn.packageNumber, Operation: afc_operation_read_dir, This_length: this_length, Entire_length: this_length}
conn.packageNumber++
packet := AfcPacket{header: header, headerPayload: headerPayload, payload: make([]byte, 0)}
response, err := conn.sendAfcPacketAndAwaitResponse(packet)
if err != nil {
return []string{}, err
}
fileList := string(response.payload)
return strings.Split(fileList, string([]byte{0})), nil
}
func (conn *Connection) openFileForWriting(filePath string) (byte, error) {
pathBytes := []byte(filePath)
headerLength := 8 + uint64(len(pathBytes))
headerPayload := make([]byte, headerLength)
binary.LittleEndian.PutUint64(headerPayload, afc_fopen_wronly)
copy(headerPayload[8:], pathBytes)
this_length := afc_header_size + headerLength
header := AfcPacketHeader{Magic: afc_magic, Packet_num: conn.packageNumber, Operation: afc_operation_file_open, This_length: this_length, Entire_length: this_length}
conn.packageNumber++
packet := AfcPacket{header: header, headerPayload: headerPayload, payload: make([]byte, 0)}
response, err := conn.sendAfcPacketAndAwaitResponse(packet)
if err != nil {
return 0, err
}
if response.header.Operation != afc_operation_file_open_result {
return 0, fmt.Errorf("Unexpected afc response, expected %x received %x", afc_operation_status, response.header.Operation)
}
return response.headerPayload[0], nil
}
func (conn *Connection) sendAfcPacketAndAwaitResponse(packet AfcPacket) (AfcPacket, error) {
err := Encode(packet, conn.deviceConn.Writer())
if err != nil {
return AfcPacket{}, err
}
return Decode(conn.deviceConn.Reader())
}
func (conn *Connection) sendFileContents(fileContents []byte, handle byte) error {
headerPayload := make([]byte, 8)
headerPayload[0] = handle
header := AfcPacketHeader{Magic: afc_magic, Packet_num: conn.packageNumber, Operation: afc_operation_file_write, This_length: 8 + afc_header_size, Entire_length: 8 + afc_header_size + uint64(len(fileContents))}
conn.packageNumber++
packet := AfcPacket{header: header, headerPayload: headerPayload, payload: fileContents}
response, err := conn.sendAfcPacketAndAwaitResponse(packet)
if err != nil {
return err
}
if response.header.Operation != afc_operation_status {
return fmt.Errorf("Unexpected afc response, expected %x received %x", afc_operation_status, response.header.Operation)
}
return nil
}
func (conn *Connection) closeHandle(handle byte) error {
headerPayload := make([]byte, 8)
headerPayload[0] = handle
this_length := 8 + afc_header_size
header := AfcPacketHeader{Magic: afc_magic, Packet_num: conn.packageNumber, Operation: afc_operation_file_close, This_length: this_length, Entire_length: this_length}
conn.packageNumber++
packet := AfcPacket{header: header, headerPayload: headerPayload, payload: make([]byte, 0)}
response, err := conn.sendAfcPacketAndAwaitResponse(packet)
if err != nil {
return err
}
if response.header.Operation != afc_operation_status {
return fmt.Errorf("Unexpected afc response, expected %x received %x", afc_operation_status, response.header.Operation)
}
return nil
}