forked from XIAZY/wcctl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkey_test.go
More file actions
215 lines (202 loc) · 6.44 KB
/
Copy pathkey_test.go
File metadata and controls
215 lines (202 loc) · 6.44 KB
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
package main
import (
"bytes"
"encoding/hex"
"encoding/json"
"io"
"os"
"path/filepath"
"strings"
"testing"
)
func TestRunKeyWithoutSubcommandPrintsUsage(t *testing.T) {
var output bytes.Buffer
if err := runKey(nil, strings.NewReader(""), &output, io.Discard); err != nil {
t.Fatal(err)
}
for _, expected := range []string{"wcctl key <subcommand>", "acquire", "extract"} {
if !strings.Contains(output.String(), expected) {
t.Fatalf("usage is missing %q:\n%s", expected, output.String())
}
}
}
func TestRunKeyExtractFromCapture(t *testing.T) {
root := t.TempDir()
t.Setenv("HOME", root)
accountPath, capturePath, keyPath := createExtractionFixture(t, root)
var output, errors bytes.Buffer
err := runKey([]string{
"extract",
"-capture", capturePath,
"-data-dir", accountPath,
"-keys", keyPath,
}, strings.NewReader(""), &output, &errors)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(errors.String(), "saved 1 verified database key") {
t.Fatalf("unexpected status output:\n%s", errors.String())
}
assertFixtureKeySaved(t, keyPath)
}
func TestRunKeyAcquireOrchestratesCaptureAndCleanup(t *testing.T) {
if os.Geteuid() == 0 {
t.Skip("key acquire intentionally rejects direct root execution")
}
root := t.TempDir()
t.Setenv("HOME", root)
accountPath, sourceCapture, keyPath := createExtractionFixture(t, root)
originalSIP := readSIPStatus
originalProcesses := findRunningWeChat
originalDump := runPrivilegedDump
t.Cleanup(func() {
readSIPStatus = originalSIP
findRunningWeChat = originalProcesses
runPrivilegedDump = originalDump
})
readSIPStatus = func() ([]byte, error) {
return []byte("System Integrity Protection status: disabled."), nil
}
findRunningWeChat = func() []processInfo {
return []processInfo{{PID: 1234, Name: "WeChat", Path: "/Applications/WeChat.app/Contents/MacOS/WeChat"}}
}
var generatedCapture string
runPrivilegedDump = func(_ string, capturePath string, _, _, _, _ int, _ io.Reader, _ io.Writer) error {
generatedCapture = capturePath
if err := os.MkdirAll(filepath.Join(capturePath, "segments"), 0o700); err != nil {
return err
}
data, err := os.ReadFile(filepath.Join(sourceCapture, "segments", "sample.bin"))
if err != nil {
return err
}
return os.WriteFile(filepath.Join(capturePath, "segments", "sample.bin"), data, 0o600)
}
var output, errors bytes.Buffer
err := runKey([]string{
"acquire", "-yes",
"-data-dir", accountPath,
"-keys", keyPath,
}, strings.NewReader(""), &output, &errors)
if err != nil {
t.Fatal(err)
}
assertFixtureKeySaved(t, keyPath)
if generatedCapture == "" {
t.Fatal("privileged capture was not invoked")
}
if _, err := os.Stat(generatedCapture); !os.IsNotExist(err) {
t.Fatalf("temporary capture was not removed: %v", err)
}
for _, expected := range []string{
"Re-enable System Integrity Protection now",
"csrutil enable",
"Normal wcctl queries work with SIP enabled",
} {
if !strings.Contains(errors.String(), expected) {
t.Fatalf("successful acquisition is missing SIP reminder %q:\n%s", expected, errors.String())
}
}
}
func TestKeyAcquireRequiresDisabledSIPBeforeProcessDiscovery(t *testing.T) {
if os.Geteuid() == 0 {
t.Skip("key acquire intentionally rejects direct root execution")
}
t.Setenv("HOME", t.TempDir())
originalSIP := readSIPStatus
originalProcesses := findRunningWeChat
t.Cleanup(func() {
readSIPStatus = originalSIP
findRunningWeChat = originalProcesses
})
readSIPStatus = func() ([]byte, error) {
return []byte("System Integrity Protection status: enabled."), nil
}
findRunningWeChat = func() []processInfo {
t.Fatal("process discovery should not run while SIP is enabled")
return nil
}
err := runKey([]string{"acquire", "-yes"}, strings.NewReader(""), io.Discard, io.Discard)
if err == nil || !strings.Contains(err.Error(), "System Integrity Protection is enabled") {
t.Fatalf("unexpected error: %v", err)
}
}
func TestKeyAcquireRequiresRunningWeChat(t *testing.T) {
if os.Geteuid() == 0 {
t.Skip("key acquire intentionally rejects direct root execution")
}
t.Setenv("HOME", t.TempDir())
originalSIP := readSIPStatus
originalProcesses := findRunningWeChat
t.Cleanup(func() {
readSIPStatus = originalSIP
findRunningWeChat = originalProcesses
})
readSIPStatus = func() ([]byte, error) {
return []byte("System Integrity Protection status: disabled."), nil
}
findRunningWeChat = func() []processInfo { return nil }
err := runKey([]string{"acquire", "-yes"}, strings.NewReader(""), io.Discard, io.Discard)
if err == nil || !strings.Contains(err.Error(), "WeChat is not running") {
t.Fatalf("unexpected error: %v", err)
}
}
func TestPrepareCaptureDirectoryIsPrivate(t *testing.T) {
path, automatic, err := prepareCaptureDirectory(filepath.Join(t.TempDir(), "capture"))
if err != nil {
t.Fatal(err)
}
if automatic {
t.Fatal("explicit capture path marked automatic")
}
info, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
if info.Mode().Perm() != 0o700 {
t.Fatalf("capture permissions = %o, want 700", info.Mode().Perm())
}
}
func createExtractionFixture(t *testing.T, root string) (accountPath, capturePath, keyPath string) {
t.Helper()
accountPath = filepath.Join(root, "wxid_test")
databasePath := filepath.Join(accountPath, "db_storage", "message", "message_0.db")
if err := os.MkdirAll(filepath.Dir(databasePath), 0o700); err != nil {
t.Fatal(err)
}
key := bytes.Repeat([]byte{0x42}, 32)
page := authenticatedTestPage(key)
if err := os.WriteFile(databasePath, page, 0o600); err != nil {
t.Fatal(err)
}
capturePath = filepath.Join(root, "capture")
if err := os.MkdirAll(filepath.Join(capturePath, "segments"), 0o700); err != nil {
t.Fatal(err)
}
payload := "noise x'" + hex.EncodeToString(key) + hex.EncodeToString(page[:16]) + "' trailing"
if err := os.WriteFile(filepath.Join(capturePath, "segments", "sample.bin"), []byte(payload), 0o600); err != nil {
t.Fatal(err)
}
keyPath = filepath.Join(root, ".wcctl", "keys.json")
return accountPath, capturePath, keyPath
}
func assertFixtureKeySaved(t *testing.T, keyPath string) {
t.Helper()
data, err := os.ReadFile(keyPath)
if err != nil {
t.Fatal(err)
}
var store keyStore
if err := json.Unmarshal(data, &store); err != nil {
t.Fatal(err)
}
user, ok := store.Users["wxid_test"]
if !ok || len(user.Databases) != 1 {
t.Fatalf("unexpected key store: %#v", store)
}
for _, database := range user.Databases {
if database.AESKey != strings.Repeat("42", 32) {
t.Fatalf("unexpected AES key: %s", database.AESKey)
}
}
}