Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/browser/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
type Platform struct {
UserDataDirs []string // slash-separated, relative to the home directory; globs allowed
SecretLabel string // macOS Keychain account / Linux Secret Service label; "" = none
PortalAppID string // Linux: the app id whose XDG portal secret decrypts v12 values; "" = none
Executable string // Windows: the program the ABE payload is injected into
ExecutableFallbacks []string // Windows: program paths tried when the registry has no App Paths entry
ABE masterkey.ABEKind
Expand Down
21 changes: 21 additions & 0 deletions internal/browser/chromium/chromium.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/fs"
"os"
"path/filepath"
"strings"

"github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/masterkey"
Expand Down Expand Up @@ -164,6 +165,7 @@ func KeyLocator(dir browser.UserDataDir, dirSnapshot snapshot.Snapshot) (masterk
platform := dir.Browser.Platform()
locator := masterkey.Locator{
SecretLabel: platform.SecretLabel, Executable: platform.Executable, ExecutableFallbacks: platform.ExecutableFallbacks, ABE: platform.ABE,
PortalAppID: portalAppID(dir.Path, platform.PortalAppID),
}
path, err := dirSnapshot.Path(localState.Path)
switch {
Expand All @@ -175,6 +177,25 @@ func KeyLocator(dir browser.UserDataDir, dirSnapshot snapshot.Snapshot) (masterk
return locator, nil
}

// portalAppID is the v12 app id for a dir: a Flatpak install's id is fixed by its sandbox, taken from the
// .var/app/<id>/ path, so it wins outright; otherwise the browser's registered id applies.
func portalAppID(dirPath, registeredID string) string {
if flatpak := flatpakAppID(dirPath); flatpak != "" {
return flatpak
}
return registeredID
}

// flatpakAppID returns the Flatpak application id in a path like ".../.var/app/<id>/config/...", or "" if absent.
func flatpakAppID(dirPath string) string {
_, after, found := strings.Cut(filepath.ToSlash(dirPath), ".var/app/")
if !found {
return ""
}
id, _, _ := strings.Cut(after, "/")
return id
}

// KeySources are the files KeyLocator reads from a snapshot.
func KeySources() []browser.Source {
return []browser.Source{localState}
Expand Down
24 changes: 24 additions & 0 deletions internal/browser/chromium/chromium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,27 @@ func TestKeyLocator(t *testing.T) {
require.NoError(t, err, "a user data dir without Local State still has keys on macOS and Linux")
assert.Empty(t, locator.LocalState)
}

func TestPortalAppID(t *testing.T) {
tests := []struct {
name string
dirPath string
registeredID string
want string
}{
{name: "deb install uses the registered id", dirPath: "/home/u/.config/google-chrome", registeredID: "com.google.Chrome", want: "com.google.Chrome"},
{
// a Flatpak sandbox fixes the app id, so the path id wins over the table id and never falls back to it.
name: "flatpak id overrides the registered id", registeredID: "org.chromium.Chromium",
dirPath: "/home/u/.var/app/io.github.ungoogled_software.ungoogled_chromium/config/chromium",
want: "io.github.ungoogled_software.ungoogled_chromium",
},
{name: "no registered id leaves only the flatpak id", dirPath: "/home/u/.var/app/com.brave.Browser/config/BraveSoftware/Brave-Browser", want: "com.brave.Browser"},
{name: "no portal id for a browser without one", dirPath: "/home/u/.config/microsoft-edge", want: ""},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
assert.Equal(t, testCase.want, portalAppID(testCase.dirPath, testCase.registeredID))
})
}
}
8 changes: 3 additions & 5 deletions internal/browser/chromium/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ func decryptValue(masterKeys masterkey.Set, ciphertext []byte) ([]byte, error) {
return decryptCBC(masterKeys[masterkey.V10], ciphertext)
case masterkey.V11:
return decryptCBC(masterKeys[masterkey.V11], ciphertext)
case masterkey.V12:
return decryptGCM(masterKeys[masterkey.V12], ciphertext)
case masterkey.V20:
return decryptGCM(masterKeys[masterkey.V20], ciphertext)
case versionV12:
return nil, fmt.Errorf("unsupported cipher version v12 (Chromium SecretPortal / Flatpak; not yet implemented)")
case unversioned:
return masterkey.DecryptDPAPI(ciphertext)
default:
Expand All @@ -40,8 +40,6 @@ func decryptValue(masterKeys masterkey.Set, ciphertext []byte) ([]byte, error) {
}

const (
// versionV12 is recognized only to report a known gap: v12 is not implemented yet.
versionV12 masterkey.Version = "v12"
// unversioned is a value without a version prefix, which Chromium on Windows sealed with DPAPI.
unversioned masterkey.Version = ""

Expand All @@ -53,7 +51,7 @@ func detectVersion(ciphertext []byte) masterkey.Version {
return unversioned
}
switch version := masterkey.Version(ciphertext[:versionPrefixLength]); version {
case masterkey.V10, masterkey.V11, masterkey.V20, versionV12:
case masterkey.V10, masterkey.V11, masterkey.V12, masterkey.V20:
return version
default:
return unversioned
Expand Down
29 changes: 28 additions & 1 deletion internal/browser/chromium/decrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,33 @@ func TestDecryptValue_V10_CrossHostGCM(t *testing.T) {
assert.Equal(t, plaintext, got)
}

// TestDecryptValue_V12 seals a value as Chromium's SecretPortalKeyProvider does and decrypts it via the V12 slot.
func TestDecryptValue_V12(t *testing.T) {
key := []byte("0123456789abcdef0123456789abcdef") // 32-byte HKDF-SHA256 output
plaintext := []byte("portal_cookie_value")
nonce := []byte("v12_nonce_12")
gcm, err := crypto.AESGCMEncrypt(key, nonce, plaintext)
require.NoError(t, err)
ciphertext := append([]byte("v12"), append(nonce, gcm...)...)

t.Run("decrypts via the V12 slot", func(t *testing.T) {
got, err := decryptValue(masterkey.Set{masterkey.V12: key}, ciphertext)
require.NoError(t, err)
assert.Equal(t, plaintext, got)
})

t.Run("v12 routes to its own key, not V20", func(t *testing.T) {
wrong := bytes.Repeat([]byte{0x99}, 32)
_, err := decryptValue(masterkey.Set{masterkey.V12: wrong, masterkey.V20: key}, ciphertext)
require.Error(t, err, "the GCM tag must fail when the V12 slot holds the wrong key")
})

t.Run("missing V12 key errors", func(t *testing.T) {
_, err := decryptValue(masterkey.Set{masterkey.V10: key}, ciphertext)
require.Error(t, err)
})
}

// TestDecryptValue_V10_V11_SlotSeparation is #578 on Linux: v10 and v11 values each route to their own key.
func TestDecryptValue_V10_V11_SlotSeparation(t *testing.T) {
v10Key := bytes.Repeat([]byte{0x10}, 16) // V10 slot key (peanuts-derived kV10Key)
Expand Down Expand Up @@ -160,7 +187,7 @@ func TestDetectVersion(t *testing.T) {
}{
{"v10 prefix", []byte("v10" + "encrypted_data"), masterkey.V10},
{"v11 prefix", []byte("v11" + "encrypted_data"), masterkey.V11},
{"v12 prefix", []byte("v12" + "encrypted_data"), versionV12},
{"v12 prefix", []byte("v12" + "encrypted_data"), masterkey.V12},
{"v20 prefix", []byte("v20" + "encrypted_data"), masterkey.V20},
{"no prefix (DPAPI)", []byte{0x01, 0x00, 0x00, 0x00}, unversioned},
{"short input", []byte{0x01, 0x02}, unversioned},
Expand Down
4 changes: 4 additions & 0 deletions internal/browser/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var browsers = []Browser{
Linux: Platform{
UserDataDirs: []string{".config/google-chrome"},
SecretLabel: "Chrome Safe Storage",
PortalAppID: "com.google.Chrome",
},
Windows: Platform{
UserDataDirs: []string{"AppData/Local/Google/Chrome/User Data"},
Expand Down Expand Up @@ -60,6 +61,7 @@ var browsers = []Browser{
Linux: Platform{
UserDataDirs: []string{".config/chromium"},
SecretLabel: "Chromium Safe Storage",
PortalAppID: "org.chromium.Chromium",
},
Windows: Platform{
UserDataDirs: []string{"AppData/Local/Chromium/User Data"},
Expand All @@ -76,6 +78,7 @@ var browsers = []Browser{
Linux: Platform{
UserDataDirs: []string{".config/google-chrome-beta"},
SecretLabel: "Chrome Safe Storage",
PortalAppID: "com.google.Chrome.beta",
},
Windows: Platform{
UserDataDirs: []string{"AppData/Local/Google/Chrome Beta/User Data"},
Expand Down Expand Up @@ -170,6 +173,7 @@ var browsers = []Browser{
Linux: Platform{
UserDataDirs: []string{".config/BraveSoftware/Brave-Browser"},
SecretLabel: "Brave Safe Storage",
PortalAppID: "com.brave.Browser",
},
Windows: Platform{
UserDataDirs: []string{"AppData/Local/BraveSoftware/Brave-Browser/User Data"},
Expand Down
31 changes: 31 additions & 0 deletions internal/crypto/hkdf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package crypto

import (
"crypto/hmac"
"hash"
)

// HKDFKey is the extract-and-expand HKDF, written out because Go 1.20 has no crypto/hkdf and the project adds no
// x/crypto dependency; an empty salt means a block of newHash-sized zeros, as the construction specifies.
// keyLength must be at most 255*HashLen (the single-block AES-256 key here is well within it).
func HKDFKey(secret, salt, info []byte, keyLength int, newHash func() hash.Hash) []byte {
if len(salt) == 0 {
salt = make([]byte, newHash().Size())
}
extractor := hmac.New(newHash, salt)
extractor.Write(secret)
pseudoRandomKey := extractor.Sum(nil)

expander := hmac.New(newHash, pseudoRandomKey)
key := make([]byte, 0, keyLength)
var block []byte
for counter := byte(1); len(key) < keyLength; counter++ {
expander.Reset()
expander.Write(block)
expander.Write(info)
expander.Write([]byte{counter})
block = expander.Sum(nil)
key = append(key, block...)
}
return key[:keyLength]
}
63 changes: 63 additions & 0 deletions internal/crypto/hkdf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package crypto

import (
"crypto/sha256"
"encoding/hex"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// TestHKDFKey_RFC5869 uses the SHA-256 test vectors from RFC 5869 appendix A.1-A.3.
func TestHKDFKey_RFC5869(t *testing.T) {
tests := []struct {
name string
ikm string
salt string
info string
length int
want string
}{
{
name: "A.1 basic",
ikm: "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
salt: "000102030405060708090a0b0c",
info: "f0f1f2f3f4f5f6f7f8f9",
length: 42,
want: "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865",
},
{
name: "A.2 longer inputs",
ikm: "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f",
salt: "606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeaf",
info: "b0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
length: 82,
want: "b11e398dc80327a1c8e7f78c596a49344f012eda2d4efad8a050cc4c19afa97c59045a99cac7827271cb41c65e590e09da3275600c2f09b8367793a9aca3db71cc30c58179ec3e87c14c01d5c1f3434f1d87",
},
{
name: "A.3 zero-length salt and info",
ikm: "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
salt: "",
info: "",
length: 42,
want: "8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d9d201395faa4b61a96c8",
},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
ikm := decodeHex(t, testCase.ikm)
salt := decodeHex(t, testCase.salt)
info := decodeHex(t, testCase.info)
got := HKDFKey(ikm, salt, info, testCase.length, sha256.New)
assert.Equal(t, testCase.want, hex.EncodeToString(got))
})
}
}

func decodeHex(t *testing.T, value string) []byte {
t.Helper()
decoded, err := hex.DecodeString(value)
require.NoError(t, err)
return decoded
}
2 changes: 2 additions & 0 deletions internal/masterkey/masterkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Version string
const (
V10 Version = "v10"
V11 Version = "v11"
V12 Version = "v12"
V20 Version = "v20"
)

Expand All @@ -36,6 +37,7 @@ var errStorageNotFound = errors.New("not found in credential store") //nolint:un
type Locator struct {
LocalState string // the copied Local State; "" when the user data dir has none
SecretLabel string // macOS Keychain account / Linux D-Bus Secret Service label
PortalAppID string // Linux: the XDG portal app id whose v12 secret decrypts this dir; "" = no v12
Executable string
ExecutableFallbacks []string
ABE ABEKind
Expand Down
Loading
Loading