This repository has been archived by the owner on Nov 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
chrome_darwin.go
130 lines (112 loc) · 3.13 KB
/
chrome_darwin.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
// build: darwin
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"golang.org/x/crypto/pbkdf2"
)
const openSSLcmdv10 = "openssl enc -base64 -d -aes-128-cbc -iv '%s' -K %s <<< %s 2>%s"
type decryptFunc func(ct []byte, iv []byte, key []byte) ([]byte, error)
func (Chrome) dbPathGlob() string {
return filepath.Join(os.Getenv("HOME"), "Library", "Application Support", "Google", "Chrome", "*", "Login Data")
}
func chromeDecryptionKey() (string, error) {
const prefix = "password: \""
cmd := exec.Command("security", "find-generic-password", "-w", "-ga", "Chrome")
log.Print("trying to get the Chrome key from keychain, please follow the prompts")
out, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("error getting credentials from keychain: %s [%s]", err.Error(), string(out))
}
pwd := strings.Trim(string(out), "\n")
if len(pwd) == 0 {
return "", errors.New("key not found in keychain")
}
return pwd, nil
}
func chromeInit(useOpenSSL bool) (*Chrome, error) {
key, err := chromeDecryptionKey()
if err != nil {
return nil, err
}
derivedKey := pbkdf2.Key([]byte(key), []byte("saltysalt"), 1003, 16, sha1.New)
c := &Chrome{
key: derivedKey,
iv: bytes.Repeat([]byte(" "), 16),
openSSL: useOpenSSL,
}
return c, nil
}
func (c *Chrome) decryptField(ct []byte) ([]byte, error) {
const (
// https://cs.chromium.org/chromium/src/components/os_crypt/os_crypt_mac.mm
// Prefix for cypher text returned by current encryption version. We
// prefix the cypher text with this string so that future data
// migration can detect this and migrate to different encryption
// without data loss.
v10 = "v10"
v11 = "v11"
)
if len(ct) == 0 {
return []byte{}, nil
}
ver := string(ct[:len(v10)])
var decryptor decryptFunc
switch ver {
default: // no encryption
return ct, nil
case v10:
if c.openSSL {
decryptor = c.openSSLdecryptv10
break
}
decryptor = c.decryptv10
case v11:
return nil, errors.New("unsupported password version")
}
return decryptor(ct[len(v10):], c.iv, c.key)
}
func (c *Chrome) decryptv10(ct []byte, iv []byte, key []byte) ([]byte, error) {
if len(ct) == 0 {
return []byte{}, nil
}
if len(ct)%aes.BlockSize != 0 {
return nil, errors.New("ciphertext is not a multiple of the block size")
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
mode := cipher.NewCBCDecrypter(block, iv)
mode.CryptBlocks(ct, ct)
return pkcs5trim(ct), nil
}
func (c *Chrome) openSSLdecryptv10(ct []byte, iv []byte, key []byte) ([]byte, error) {
if len(ct) == 0 {
return []byte{}, nil
}
hexKey := hex.EncodeToString(key)
hexEncPassword := base64.StdEncoding.EncodeToString(ct)
ivHex := hex.EncodeToString(iv)
cmd := fmt.Sprintf(openSSLcmdv10, ivHex, hexKey, hexEncPassword, os.DevNull)
output, err := exec.Command("sh", "-c", cmd).CombinedOutput()
if err != nil {
return nil, errors.New("unable to decipher password")
}
return output, nil
}
func pkcs5trim(pt []byte) []byte {
padSz := pt[len(pt)-1]
return pt[:len(pt)-int(padSz)]
}