-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchromium.go
104 lines (90 loc) · 2.35 KB
/
chromium.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
package gocookie
import (
"crypto/sha1"
"database/sql"
"errors"
"fmt"
"net/http"
"golang.org/x/crypto/pbkdf2"
_ "modernc.org/sqlite"
)
const queryChromiumCookie = `SELECT name, encrypted_value, host_key, path, expires_utc, is_secure, is_httponly FROM cookies`
type chromium struct {
name string
cookiePath string
secretKey []byte
}
func newChromium(name, cookiePath string, secretKey []byte) (Browser, error) {
if cookiePath == "" || len(secretKey) == 0 {
return nil, errors.New("empty cookie path or secret key")
}
return &chromium{
name: name,
cookiePath: cookiePath,
secretKey: secretKey,
}, nil
}
func (c *chromium) GetName() string {
return c.name
}
func (c *chromium) GetCookies(domainFilter domainFilter) ([]*http.Cookie, error) {
cookiesDB, err := sql.Open("sqlite", "file:"+c.cookiePath+"?mode=ro")
if err != nil {
return nil, err
}
defer cookiesDB.Close()
rows, err := cookiesDB.Query(queryChromiumCookie)
if err != nil {
return nil, err
}
var (
cookies []*http.Cookie
aesKey = pbkdf2.Key(c.secretKey, []byte("saltysalt"), 1003, 16, sha1.New)
iv = []byte{32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32}
)
for rows.Next() {
var (
name string
encryptValue []byte
hostKey string
path string
expiresUTC int64
isSecure int
isHTTPOnly int
)
if err = rows.Scan(&name, &encryptValue, &hostKey, &path, &expiresUTC, &isSecure, &isHTTPOnly); err != nil {
return nil, err
}
if domainFilter != nil && !domainFilter(hostKey) {
continue
}
value, err := c.decrypt(aesKey, iv, encryptValue)
if err != nil {
return nil, err
}
// fmt.Println(name, string(value), hostKey, path, expiresUTC, isSecure, isHTTPOnly)
cookies = append(cookies, &http.Cookie{
Name: name,
Value: string(value),
Domain: hostKey,
Path: path,
Expires: ChromiumTimeToUnix(expiresUTC),
Secure: isSecure > 0,
HttpOnly: isHTTPOnly > 0,
})
}
return cookies, nil
}
func (c *chromium) decrypt(key, iv, encryptValue []byte) ([]byte, error) {
if len(encryptValue) <= 3 {
return nil, errors.New("encryptValue is too short")
}
value, err := DecryptAESCBC(key, iv, encryptValue[3:])
if err != nil {
return nil, err
}
if len(value) < 32 {
return nil, fmt.Errorf("value is too short: %d", len(value))
}
return value[32:], nil
}