-
Notifications
You must be signed in to change notification settings - Fork 3
/
cookies.go
103 lines (88 loc) · 2.61 KB
/
cookies.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
package chromedb
import (
"crypto/aes"
"crypto/cipher"
"crypto/sha1"
"database/sql"
"fmt"
"os"
"strings"
_ "github.com/mattn/go-sqlite3"
"golang.org/x/crypto/pbkdf2"
)
type Cookie struct {
Domain string `json:"domain"`
Name string `json:"name"`
EncryptedValue []byte `json:"encrypted_value"`
Value string `json:"value"`
}
func GetCookies(cookiesPath string) ([]Cookie, error) {
db, err := sql.Open("sqlite3", cookiesPath)
if err != nil {
return nil, err
}
defer db.Close()
// query := "SELECT name, value, host_key, encrypted_value FROM cookies WHERE host_key like ?"
// rows, err := db.Query(query, fmt.Sprintf("%%%s%%", domain))
query := "SELECT name, value, host_key, encrypted_value FROM cookies"
rows, err := db.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
var cookies []Cookie
for rows.Next() {
var cookie Cookie
err := rows.Scan(&cookie.Name, &cookie.Value, &cookie.Domain, &cookie.EncryptedValue)
if err != nil {
return nil, err
}
cookies = append(cookies, cookie)
}
return cookies, nil
}
func GetKey() ([]byte, error) {
browserPassword := os.Getenv("BROWSER_PASSWORD")
if browserPassword == "" {
return []byte{}, fmt.Errorf("BROWSER_PASSWORD environment variable not set")
}
password := strings.TrimSpace(string(browserPassword))
return pbkdf2.Key([]byte(password), []byte("saltysalt"), 1003, 16, sha1.New), nil
}
func DecryptValue(encryptedValue, key []byte) (string, error) {
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
// The EncryptedValue is prefixed with "v10", remove it
// TODO check if prefix is v10
if len(encryptedValue) < 3 {
return "", fmt.Errorf("encrypted length less than 3")
}
version := string(encryptedValue[0:3])
if version != "v10" {
return "", fmt.Errorf("unsported encrypted value version: %s", version)
}
encryptedValue = encryptedValue[3:]
decrypted := make([]byte, len(encryptedValue))
const (
aescbcSalt = `saltysalt`
aescbcIV = ` `
aescbcIterationsLinux = 1
aescbcIterationsMacOS = 1003
aescbcLength = 16
)
cbc := cipher.NewCBCDecrypter(block, []byte(aescbcIV))
cbc.CryptBlocks(decrypted, encryptedValue)
if len(decrypted) == 0 {
return "", fmt.Errorf("not enough bits")
}
if len(decrypted)%aescbcLength != 0 {
return "", fmt.Errorf("decrypted data block length is not a multiple of %d", aescbcLength)
}
paddingLen := int(decrypted[len(decrypted)-1])
if paddingLen > 16 {
return "", fmt.Errorf("invalid last block padding length: %d", paddingLen)
}
return string(decrypted[:len(decrypted)-paddingLen]), nil
}