2022-06-05 06:51:37 +00:00
|
|
|
//go:build darwin
|
|
|
|
|
2022-04-08 11:06:04 +00:00
|
|
|
package chromium
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/sha1"
|
|
|
|
"errors"
|
2023-03-11 12:09:10 +00:00
|
|
|
"fmt"
|
2022-04-11 11:57:40 +00:00
|
|
|
"os"
|
2022-04-08 11:06:04 +00:00
|
|
|
"os/exec"
|
2022-04-11 11:57:40 +00:00
|
|
|
"strings"
|
2022-04-08 11:06:04 +00:00
|
|
|
|
2024-04-15 08:11:25 +00:00
|
|
|
"github.com/moond4rk/hackbrowserdata/crypto"
|
2024-10-22 03:13:06 +00:00
|
|
|
"github.com/moond4rk/hackbrowserdata/log"
|
2024-04-12 11:10:41 +00:00
|
|
|
"github.com/moond4rk/hackbrowserdata/types"
|
2022-04-08 11:06:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-08-16 11:24:36 +00:00
|
|
|
errWrongSecurityCommand = errors.New("wrong security command")
|
|
|
|
errCouldNotFindInKeychain = errors.New("could not be find in keychain")
|
2022-04-08 11:06:04 +00:00
|
|
|
)
|
|
|
|
|
2023-03-10 06:52:26 +00:00
|
|
|
func (c *Chromium) GetMasterKey() ([]byte, error) {
|
2022-04-11 11:57:40 +00:00
|
|
|
// don't need chromium key file for macOS
|
2024-04-12 11:10:41 +00:00
|
|
|
defer os.Remove(types.ChromiumKey.TempFilename())
|
2022-04-11 11:57:40 +00:00
|
|
|
// Get the master key from the keychain
|
2022-04-08 11:06:04 +00:00
|
|
|
// $ security find-generic-password -wa 'Chrome'
|
2023-03-11 12:09:10 +00:00
|
|
|
var (
|
|
|
|
stdout, stderr bytes.Buffer
|
|
|
|
)
|
|
|
|
cmd := exec.Command("security", "find-generic-password", "-wa", strings.TrimSpace(c.storage)) //nolint:gosec
|
2022-04-08 11:06:04 +00:00
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
2023-03-11 12:09:10 +00:00
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
return nil, fmt.Errorf("run security command failed: %w, message %s", err, stderr.String())
|
2022-04-08 11:06:04 +00:00
|
|
|
}
|
2023-03-11 12:09:10 +00:00
|
|
|
|
2022-04-08 11:06:04 +00:00
|
|
|
if stderr.Len() > 0 {
|
2022-04-11 11:57:40 +00:00
|
|
|
if strings.Contains(stderr.String(), "could not be found") {
|
2022-08-16 11:24:36 +00:00
|
|
|
return nil, errCouldNotFindInKeychain
|
2022-04-11 11:57:40 +00:00
|
|
|
}
|
2022-04-08 11:06:04 +00:00
|
|
|
return nil, errors.New(stderr.String())
|
|
|
|
}
|
2023-03-11 12:09:10 +00:00
|
|
|
|
|
|
|
secret := bytes.TrimSpace(stdout.Bytes())
|
|
|
|
if len(secret) == 0 {
|
2022-08-16 11:24:36 +00:00
|
|
|
return nil, errWrongSecurityCommand
|
2022-04-08 11:06:04 +00:00
|
|
|
}
|
2023-03-11 12:09:10 +00:00
|
|
|
salt := []byte("saltysalt")
|
2022-04-08 11:06:04 +00:00
|
|
|
// @https://source.chromium.org/chromium/chromium/src/+/master:components/os_crypt/os_crypt_mac.mm;l=157
|
2024-04-15 08:11:25 +00:00
|
|
|
key := crypto.PBKDF2Key(secret, salt, 1003, 16, sha1.New)
|
2022-04-11 07:53:19 +00:00
|
|
|
if key == nil {
|
2022-08-16 11:24:36 +00:00
|
|
|
return nil, errWrongSecurityCommand
|
2022-04-08 11:06:04 +00:00
|
|
|
}
|
2022-04-11 07:53:19 +00:00
|
|
|
c.masterKey = key
|
2024-10-22 03:13:06 +00:00
|
|
|
log.Debugf("get master key success, browser %s", c.name)
|
2022-04-11 07:53:19 +00:00
|
|
|
return key, nil
|
2022-04-08 11:06:04 +00:00
|
|
|
}
|