forked from gwatts/pinfinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinfinder_test.go
257 lines (229 loc) · 6.34 KB
/
pinfinder_test.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
const pinData = `<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>RestrictionsPasswordKey</key>
<data>
ioN63+yl6OFZ4/C7xl9VejMLDi0=
</data>
<key>RestrictionsPasswordSalt</key>
<data>
iNciDA==
</data>
</dict>
</plist>
`
var (
// extracted values from above plist (pin is 1234)
dataKey = []byte{0x8a, 0x83, 0x7a, 0xdf, 0xec, 0xa5, 0xe8, 0xe1, 0x59, 0xe3, 0xf0, 0xbb, 0xc6, 0x5f, 0x55, 0x7a, 0x33, 0xb, 0xe, 0x2d}
dataSalt = []byte{0x88, 0xd7, 0x22, 0xc}
dataPIN = "1234"
)
func mkInfo(tm, devname string) []byte {
return []byte(fmt.Sprintf(`<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Last Backup Date</key>
<date>%s</date>
<key>Display Name</key>
<string>%s</string>
</dict>
</plist>
`, tm, devname))
}
func mkManifest(isEncrypted bool) []byte {
b := "<false />"
if isEncrypted {
b = "<true />"
}
return []byte(fmt.Sprintf(`<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IsEncrypted</key>
%s
</dict>
</plist>
`, b))
}
func setupDataDir() string {
tmp, err := ioutil.TempDir("", "pinfinder")
if err != nil {
panic("Could not create test directory: " + err.Error())
}
b1path := filepath.Join(tmp, "backup1")
b2path := filepath.Join(tmp, "backup2")
b3path := filepath.Join(tmp, "nobackup")
b4path := filepath.Join(tmp, "encbackup")
b5path := filepath.Join(tmp, "encnopcbackup")
b6path := filepath.Join(tmp, "ios10backup")
os.Mkdir(b1path, 0777)
os.Mkdir(b2path, 0777)
os.Mkdir(b3path, 0777)
os.Mkdir(b4path, 0777)
os.Mkdir(b5path, 0777)
os.Mkdir(b6path, 0777)
os.Mkdir(filepath.Join(b6path, "39"), 0777)
ioutil.WriteFile(
filepath.Join(b1path, "398bc9c2aeeab4cb0c12ada0f52eea12cf14f40b"),
[]byte(pinData),
0644)
ioutil.WriteFile(
filepath.Join(b1path, "398bc9c2aeeab4cb0c12ada0f52eea12cf14f40c"),
[]byte("not a plist"),
0644)
ioutil.WriteFile(
filepath.Join(b1path, "Info.plist"),
mkInfo("2014-11-25T21:39:29Z", "device one"),
0644)
ioutil.WriteFile(
filepath.Join(b1path, "Manifest.plist"),
mkManifest(false),
0644)
// no passcode for b2
ioutil.WriteFile(
filepath.Join(b2path, "398bc9c2aeeab4cb0c12ada0f52eea12cf14f40c"),
[]byte("not a plist"),
0644)
ioutil.WriteFile(
filepath.Join(b2path, "Info.plist"),
mkInfo("2015-11-25T21:39:29Z", "device two"),
0644)
ioutil.WriteFile(
filepath.Join(b2path, "Manifest.plist"),
mkManifest(false),
0644)
// b3 doesn't contain a backup at all
ioutil.WriteFile(
filepath.Join(b3path, "random file"),
[]byte("not a plist"),
0644)
// b4 is marked as encrypted
ioutil.WriteFile(
filepath.Join(b4path, "398bc9c2aeeab4cb0c12ada0f52eea12cf14f40b"),
[]byte("this would be an encrypted plist"),
0644)
ioutil.WriteFile(
filepath.Join(b4path, "Info.plist"),
mkInfo("2014-11-24T20:39:29Z", "device three"),
0644)
ioutil.WriteFile(
filepath.Join(b4path, "Manifest.plist"),
mkManifest(true),
0644)
// b5 is encrypted, but has no passcode file
ioutil.WriteFile(
filepath.Join(b5path, "Info.plist"),
mkInfo("2014-11-24T19:39:29Z", "device four"),
0644)
ioutil.WriteFile(
filepath.Join(b5path, "Manifest.plist"),
mkManifest(true),
0644)
// b6 contains a passcode with iOS 10 file layout
ioutil.WriteFile(
filepath.Join(b6path, "39", "398bc9c2aeeab4cb0c12ada0f52eea12cf14f40b"),
[]byte(pinData),
0644)
ioutil.WriteFile(
filepath.Join(b6path, "Info.plist"),
mkInfo("2016-09-23T21:39:29Z", "ios10 device"),
0644)
ioutil.WriteFile(
filepath.Join(b6path, "Manifest.plist"),
mkManifest(false),
0644)
return tmp
}
func TestLoadBackup(t *testing.T) {
tmpDir := setupDataDir()
defer os.RemoveAll(tmpDir)
path := filepath.Join(tmpDir, "backup1")
backup := loadBackup(path)
if backup == nil {
t.Fatal("loadBackup failed")
}
if backup.Path != path {
t.Errorf("Path incorrect expected=%q actual=%q", path, backup.Path)
}
if backup.Info.DisplayName != "device one" {
t.Errorf("Incorrect device name: %v", backup.Info)
}
}
func TestLoadBackups(t *testing.T) {
tmpDir := setupDataDir()
defer os.RemoveAll(tmpDir)
b := new(backups)
err := b.loadBackups(tmpDir)
if err != nil {
t.Fatal("loadBackups failed", err)
}
if len(b.backups) != 5 {
t.Fatal("Incorrect backup count", len(b.backups))
}
// Should of been sorted into reverse time order
if devname := b.backups[0].Info.DisplayName; devname != "ios10 device" {
t.Errorf("First entry is not ios10 device got %q", devname)
}
if devname := b.backups[1].Info.DisplayName; devname != "device two" {
t.Errorf("Second entry is not device two, got %q", devname)
}
if devname := b.backups[2].Info.DisplayName; devname != "device one" {
t.Errorf("Third entry is not device one, got %q", devname)
}
if devname := b.backups[3].Info.DisplayName; devname != "device three" {
t.Errorf("Fourth entry is not device wthree, got %q", devname)
}
if !b.backups[3].isEncrypted() {
t.Error("device three not marked as encrypted")
}
if status := b.backups[3].Status; status != msgIsEncrypted {
t.Error("device three does not have correct status: ", status)
}
if status := b.backups[4].Status; status != msgNoPasscode {
t.Error("device four does not have correct status", status)
}
}
func TestParseRestriction(t *testing.T) {
tmpDir := setupDataDir()
defer os.RemoveAll(tmpDir)
for _, base := range []string{"backup1", "ios10backup"} {
path := filepath.Join(tmpDir, base)
b := loadBackup(path)
if b == nil {
t.Fatal("Failed to load backup")
}
key := b.Restrictions.Key
salt := b.Restrictions.Salt
if !bytes.Equal(key, dataKey) {
t.Error("key doesn't match")
}
if !bytes.Equal(salt, dataSalt) {
t.Error("salt doesn't match")
}
fmt.Printf("key: %#v\nsalt: %#v\n", key, salt)
}
}
func TestFindPINOK(t *testing.T) {
pin, err := findPIN(dataKey, dataSalt)
if err != nil {
t.Error("Unexpected error", err)
}
if pin != dataPIN {
t.Error("Did not get expected PIN. Received", pin)
}
}
func TestFindPINFail(t *testing.T) {
_, err := findPIN(dataKey, []byte{0x88, 0xd7, 0x22, 0xc0}) // change last byte of salt
if err == nil {
t.Error("Did not receive expected error")
}
}