You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.5 KiB
60 lines
1.5 KiB
3 years ago
|
//go:build darwin
|
||
|
|
||
3 years ago
|
package chromium
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"crypto/sha1"
|
||
|
"errors"
|
||
3 years ago
|
"os"
|
||
3 years ago
|
"os/exec"
|
||
3 years ago
|
"strings"
|
||
3 years ago
|
|
||
|
"golang.org/x/crypto/pbkdf2"
|
||
3 years ago
|
|
||
|
"hack-browser-data/internal/item"
|
||
3 years ago
|
"hack-browser-data/internal/log"
|
||
3 years ago
|
)
|
||
|
|
||
|
var (
|
||
2 years ago
|
errWrongSecurityCommand = errors.New("wrong security command")
|
||
|
errCouldNotFindInKeychain = errors.New("could not be find in keychain")
|
||
3 years ago
|
)
|
||
|
|
||
|
func (c *chromium) GetMasterKey() ([]byte, error) {
|
||
|
var (
|
||
|
cmd *exec.Cmd
|
||
|
stdout, stderr bytes.Buffer
|
||
|
)
|
||
3 years ago
|
// don't need chromium key file for macOS
|
||
|
defer os.Remove(item.TempChromiumKey)
|
||
|
// Get the master key from the keychain
|
||
3 years ago
|
// $ security find-generic-password -wa 'Chrome'
|
||
2 years ago
|
cmd = exec.Command("security", "find-generic-password", "-wa", strings.TrimSpace(c.storage)) //nolint:gosec
|
||
3 years ago
|
cmd.Stdout = &stdout
|
||
|
cmd.Stderr = &stderr
|
||
|
err := cmd.Run()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if stderr.Len() > 0 {
|
||
3 years ago
|
if strings.Contains(stderr.String(), "could not be found") {
|
||
2 years ago
|
return nil, errCouldNotFindInKeychain
|
||
3 years ago
|
}
|
||
3 years ago
|
return nil, errors.New(stderr.String())
|
||
|
}
|
||
3 years ago
|
chromeSecret := bytes.TrimSpace(stdout.Bytes())
|
||
3 years ago
|
if chromeSecret == nil {
|
||
2 years ago
|
return nil, errWrongSecurityCommand
|
||
3 years ago
|
}
|
||
3 years ago
|
chromeSalt := []byte("saltysalt")
|
||
3 years ago
|
// @https://source.chromium.org/chromium/chromium/src/+/master:components/os_crypt/os_crypt_mac.mm;l=157
|
||
|
key := pbkdf2.Key(chromeSecret, chromeSalt, 1003, 16, sha1.New)
|
||
3 years ago
|
if key == nil {
|
||
2 years ago
|
return nil, errWrongSecurityCommand
|
||
3 years ago
|
}
|
||
3 years ago
|
c.masterKey = key
|
||
3 years ago
|
log.Infof("%s initialized master key success", c.name)
|
||
3 years ago
|
return key, nil
|
||
3 years ago
|
}
|