mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-12-04 05:43:27 +01:00
feat(firefox): decrypt profiles with a primary password (#704)
Profiles protected by a Firefox primary password could not be decrypted; --firefox-pw and --firefox-pw-stdin supply it, read once per run, and new 48-byte NSS salts hash with SHA-384. Closes #109
This commit is contained in:
1 parent
c733ac9d6d
commit
b14b2c021a
15 files changed
+332
-69
No files matched your search
@@ -53,7 +53,18 @@ func WriteFirefoxUserDataDir(t *testing.T, profilesDir string) {
|
||||
}
|
||||
}
|
||||
|
||||
// WriteFirefoxPrimaryPasswordProfile writes one profile at dir whose key4.db opens only with password.
|
||||
func WriteFirefoxPrimaryPasswordProfile(t *testing.T, dir string, globalSalt, password []byte, keys [][]byte) {
|
||||
t.Helper()
|
||||
passwordKey := crypto.NSSPasswordKey(globalSalt, password)
|
||||
writeFirefoxProfileSealed(t, dir, "pw.firefox.test", time.Unix(1704067200, 0).UTC(), globalSalt, passwordKey, keys)
|
||||
}
|
||||
|
||||
func writeFirefoxProfile(t *testing.T, dir, site string, start time.Time, globalSalt []byte, keys [][]byte) {
|
||||
writeFirefoxProfileSealed(t, dir, site, start, globalSalt, crypto.NSSPasswordKey(globalSalt, nil), keys)
|
||||
}
|
||||
|
||||
func writeFirefoxProfileSealed(t *testing.T, dir, site string, start time.Time, globalSalt, passwordKey []byte, keys [][]byte) {
|
||||
t.Helper()
|
||||
micros := func(offset time.Duration) int64 { return start.Add(offset).UnixMicro() }
|
||||
millis := func(offset time.Duration) int64 { return start.Add(offset).UnixMilli() }
|
||||
@@ -65,11 +76,11 @@ func writeFirefoxProfile(t *testing.T, dir, site string, start time.Time, global
|
||||
firefoxMetaDataSchema,
|
||||
firefoxNSSPrivateSchema,
|
||||
fmt.Sprintf(`INSERT INTO metaData (id, item1, item2) VALUES ('password', x'%x', x'%x')`,
|
||||
globalSalt, sealFirefoxPBES2(t, globalSalt, site+"/password-check", []byte("password-check"))),
|
||||
globalSalt, sealFirefoxPBES2(t, passwordKey, site+"/password-check", []byte("password-check"))),
|
||||
}
|
||||
for i, key := range keys {
|
||||
key4Statements = append(key4Statements, fmt.Sprintf(`INSERT INTO nssPrivate (id, a11, a102) VALUES (%d, x'%x', x'%x')`,
|
||||
i+1, sealFirefoxPBES2(t, globalSalt, fmt.Sprintf("%s/key-%d", site, i), key), firefoxKeyID))
|
||||
i+1, sealFirefoxPBES2(t, passwordKey, fmt.Sprintf("%s/key-%d", site, i), key), firefoxKeyID))
|
||||
}
|
||||
writeSQLite(t, filepath.Join(dir, "key4.db"), key4Statements...)
|
||||
|
||||
@@ -171,7 +182,7 @@ type firefoxPBES2 struct {
|
||||
Ciphertext []byte
|
||||
}
|
||||
|
||||
func sealFirefoxPBES2(t *testing.T, globalSalt []byte, seed string, plaintext []byte) []byte {
|
||||
func sealFirefoxPBES2(t *testing.T, passwordKey []byte, seed string, plaintext []byte) []byte {
|
||||
t.Helper()
|
||||
var pbe firefoxPBES2
|
||||
pbe.Algorithm.OID = oidPBES2
|
||||
@@ -183,7 +194,7 @@ func sealFirefoxPBES2(t *testing.T, globalSalt []byte, seed string, plaintext []
|
||||
kdf.Params.PRF.OID = oidHMACWithSHA256
|
||||
pbe.Algorithm.Params.Cipher.OID = oidAES256CBC
|
||||
pbe.Algorithm.Params.Cipher.IV = firefoxBytes(seed+"/iv", 14)
|
||||
return sealFirefoxValue(t, func() ([]byte, error) { return asn1.Marshal(pbe) }, &pbe.Ciphertext, globalSalt, plaintext)
|
||||
return sealFirefoxValue(t, func() ([]byte, error) { return asn1.Marshal(pbe) }, &pbe.Ciphertext, passwordKey, plaintext)
|
||||
}
|
||||
|
||||
type firefoxLoginPBE struct {
|
||||
|
||||
@@ -58,12 +58,13 @@ type LoginKeychain interface {
|
||||
InternetPasswords(ctx context.Context) ([]keychain.InternetPassword, error)
|
||||
}
|
||||
|
||||
// Input is what an extractor reads for one profile: its captured files, the dir's master keys, and the run's keychain.
|
||||
// Input is what an extractor reads for one profile: its captured files, the dir's master keys, and the run's secrets.
|
||||
type Input struct {
|
||||
Profile Profile
|
||||
snapshot snapshot.Snapshot
|
||||
masterKeys func(ctx context.Context) masterkey.Set
|
||||
loginKeychain LoginKeychain
|
||||
Profile Profile
|
||||
snapshot snapshot.Snapshot
|
||||
masterKeys func(ctx context.Context) masterkey.Set
|
||||
loginKeychain LoginKeychain
|
||||
firefoxPassword func(ctx context.Context) (string, error)
|
||||
}
|
||||
|
||||
// NewInput takes masterKeys, the dir's call-once retrieval, nil for bases that keep no Chromium keys, and the run's
|
||||
@@ -97,3 +98,17 @@ func (in Input) MasterKeys(ctx context.Context) masterkey.Set {
|
||||
func (in Input) LoginKeychain() LoginKeychain {
|
||||
return in.loginKeychain
|
||||
}
|
||||
|
||||
// WithFirefoxPassword sets the lazy Firefox primary-password source; only the dump path supplies one.
|
||||
func (in Input) WithFirefoxPassword(password func(context.Context) (string, error)) Input {
|
||||
in.firefoxPassword = password
|
||||
return in
|
||||
}
|
||||
|
||||
// FirefoxPrimaryPassword is the user's primary password, "" when none was given; it may block on first use.
|
||||
func (in Input) FirefoxPrimaryPassword(ctx context.Context) (string, error) {
|
||||
if in.firefoxPassword == nil {
|
||||
return "", nil
|
||||
}
|
||||
return in.firefoxPassword(ctx)
|
||||
}
|
||||
@@ -134,21 +134,25 @@ func newPlainExtractor(parse func(string, browser.Collector) error, source brows
|
||||
func newDecryptingExtractor(parse func(func() ([]byte, error), string, browser.Collector) error, source browser.Source) browser.Extractor {
|
||||
return browser.Extractor{
|
||||
Sources: []browser.Source{source, keyStore},
|
||||
Extract: func(_ context.Context, input browser.Input, out browser.Collector) error {
|
||||
Extract: func(ctx context.Context, input browser.Input, out browser.Collector) error {
|
||||
path, err := input.Path(source)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
masterKey := func() ([]byte, error) { return profileKey(input, path) }
|
||||
masterKey := func() ([]byte, error) { return profileKey(ctx, input, path) }
|
||||
return parse(masterKey, path, out)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func profileKey(input browser.Input, loginsPath string) ([]byte, error) {
|
||||
func profileKey(ctx context.Context, input browser.Input, loginsPath string) ([]byte, error) {
|
||||
key4Path, err := input.Path(keyStore)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return retrieveMasterKey(key4Path, loginsPath)
|
||||
primaryPassword := func() ([]byte, error) {
|
||||
password, err := input.FirefoxPrimaryPassword(ctx)
|
||||
return []byte(password), err
|
||||
}
|
||||
return retrieveMasterKey(key4Path, loginsPath, primaryPassword)
|
||||
}
|
||||
@@ -15,13 +15,15 @@ import (
|
||||
)
|
||||
|
||||
// retrieveMasterKey derives the key candidates in key4.db and returns the one that opens the logins in loginsPath.
|
||||
func retrieveMasterKey(key4Path, loginsPath string) ([]byte, error) {
|
||||
// primaryPassword is read only when a profile turns out to have one, so a run of mostly-unprotected profiles never
|
||||
// blocks on stdin or a prompt it does not need.
|
||||
func retrieveMasterKey(key4Path, loginsPath string, primaryPassword func() ([]byte, error)) ([]byte, error) {
|
||||
key4, err := readKey4DB(key4Path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
keys, err := key4.deriveKeys()
|
||||
keys, err := key4.deriveKeys(primaryPassword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -89,8 +91,14 @@ func readKey4DB(path string) (*key4DB, error) {
|
||||
return &record, nil
|
||||
}
|
||||
|
||||
func (k *key4DB) deriveKeys() ([][]byte, error) {
|
||||
if err := k.verifyPasswordCheck(); err != nil {
|
||||
var (
|
||||
errPrimaryPasswordRequired = errors.New("profile has a primary password; pass it with --firefox-pw")
|
||||
errPrimaryPasswordWrong = errors.New("primary password is incorrect")
|
||||
)
|
||||
|
||||
func (k *key4DB) deriveKeys(primaryPassword func() ([]byte, error)) ([][]byte, error) {
|
||||
password, err := k.selectPassword(primaryPassword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -99,7 +107,7 @@ func (k *key4DB) deriveKeys() ([][]byte, error) {
|
||||
if !bytes.Equal(entry.typeTag, nssKeyTypeTag) {
|
||||
continue
|
||||
}
|
||||
key, err := k.decryptPrivateKey(entry)
|
||||
key, err := k.decryptPrivateKey(entry, password)
|
||||
if err != nil {
|
||||
log.Debugf("decrypt nss private key: %v", err)
|
||||
continue
|
||||
@@ -109,27 +117,42 @@ func (k *key4DB) deriveKeys() ([][]byte, error) {
|
||||
return keys, nil
|
||||
}
|
||||
|
||||
func (k *key4DB) verifyPasswordCheck() error {
|
||||
// selectPassword uses the empty password for a profile with no primary password, else the user's once it checks out,
|
||||
// reading primaryPassword (which may block) only when the empty one does not fit. A passwordCheck that will not parse
|
||||
// is a corrupt DB, reported as such rather than as a wrong password.
|
||||
func (k *key4DB) selectPassword(primaryPassword func() ([]byte, error)) ([]byte, error) {
|
||||
pbe, err := crypto.NewASN1PBE(k.passwordCheck)
|
||||
if err != nil {
|
||||
return fmt.Errorf("parse password check: %w", err)
|
||||
return nil, fmt.Errorf("parse key4.db password check: %w", err)
|
||||
}
|
||||
plain, err := pbe.Decrypt(k.globalSalt)
|
||||
if passwordChecks(pbe, crypto.NSSPasswordKey(k.globalSalt, nil)) {
|
||||
return nil, nil
|
||||
}
|
||||
password, err := primaryPassword()
|
||||
if err != nil {
|
||||
return fmt.Errorf("decrypt password check: %w", err)
|
||||
return nil, err
|
||||
}
|
||||
if !bytes.Contains(plain, []byte("password-check")) {
|
||||
return errors.New("password check verification failed")
|
||||
if len(password) > 0 && passwordChecks(pbe, crypto.NSSPasswordKey(k.globalSalt, password)) {
|
||||
return password, nil
|
||||
}
|
||||
return nil
|
||||
if len(password) == 0 {
|
||||
return nil, errPrimaryPasswordRequired
|
||||
}
|
||||
return nil, errPrimaryPasswordWrong
|
||||
}
|
||||
|
||||
func (k *key4DB) decryptPrivateKey(entry privateKey) ([]byte, error) {
|
||||
// passwordChecks reports whether passwordKey decrypts the "password-check" marker the profile stored in metaData.item2.
|
||||
func passwordChecks(pbe crypto.ASN1PBE, passwordKey []byte) bool {
|
||||
plain, err := pbe.Decrypt(passwordKey)
|
||||
return err == nil && bytes.Contains(plain, []byte("password-check"))
|
||||
}
|
||||
|
||||
func (k *key4DB) decryptPrivateKey(entry privateKey, password []byte) ([]byte, error) {
|
||||
pbe, err := crypto.NewASN1PBE(entry.encrypted)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse private key: %w", err)
|
||||
}
|
||||
derivedKey, err := pbe.Decrypt(k.globalSalt)
|
||||
derivedKey, err := pbe.Decrypt(crypto.NSSPasswordKey(k.globalSalt, password))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decrypt private key: %w", err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package firefox
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/moond4rk/hackbrowserdata/internal/browser/browsertest"
|
||||
"github.com/moond4rk/hackbrowserdata/internal/crypto"
|
||||
)
|
||||
|
||||
// passwordCheckPBEHex is a real metaData.item2 PBE blob from crypto's vectors (global salt "moond4rk" x3); its single
|
||||
// 16-byte ciphertext block is swapped for one over "password-check" to make a primary-password fixture.
|
||||
const passwordCheckPBEHex = "307a3066060960864801650304012e3059303a060960864801650304012e302d04186d6f6f6e6434726b6d6f6f6e6434726b6d6f6f6e6434726b020101020120300b060960864801650304012e301b060960864801650304012e040e303132333435363730313233343504100474679f2e6256518b7adb877beaa154"
|
||||
|
||||
// passwordCheckFor builds a metaData.item2 that decrypts to "password-check" only under globalSalt + password.
|
||||
func passwordCheckFor(t *testing.T, globalSalt, password []byte) []byte {
|
||||
t.Helper()
|
||||
blob, err := hex.DecodeString(passwordCheckPBEHex)
|
||||
require.NoError(t, err)
|
||||
pbe, err := crypto.NewASN1PBE(blob)
|
||||
require.NoError(t, err)
|
||||
cipher, err := pbe.Encrypt(crypto.NSSPasswordKey(globalSalt, password), []byte("password-check"))
|
||||
require.NoError(t, err)
|
||||
require.Len(t, cipher, 16, "a one-block ciphertext replaces the vector's own")
|
||||
return append(append([]byte(nil), blob[:len(blob)-len(cipher)]...), cipher...)
|
||||
}
|
||||
|
||||
func constPassword(password string) func() ([]byte, error) {
|
||||
return func() ([]byte, error) {
|
||||
if password == "" {
|
||||
return nil, nil
|
||||
}
|
||||
return []byte(password), nil
|
||||
}
|
||||
}
|
||||
|
||||
func TestSelectPassword_PrimaryPassword(t *testing.T) {
|
||||
globalSalt := bytes.Repeat([]byte("moond4rk"), 3)
|
||||
const password = "hunter2"
|
||||
key4 := &key4DB{globalSalt: globalSalt, passwordCheck: passwordCheckFor(t, globalSalt, []byte(password))}
|
||||
|
||||
selected, err := key4.selectPassword(constPassword(password))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, []byte(password), selected, "the right primary password is selected")
|
||||
|
||||
_, err = key4.selectPassword(constPassword(""))
|
||||
require.ErrorIs(t, err, errPrimaryPasswordRequired, "a protected profile with no password asks for one")
|
||||
|
||||
_, err = key4.selectPassword(constPassword("wrong"))
|
||||
require.ErrorIs(t, err, errPrimaryPasswordWrong, "a wrong password is reported as wrong")
|
||||
}
|
||||
|
||||
func TestSelectPassword_NoPrimaryPassword(t *testing.T) {
|
||||
globalSalt := bytes.Repeat([]byte("moond4rk"), 3)
|
||||
key4 := &key4DB{globalSalt: globalSalt, passwordCheck: passwordCheckFor(t, globalSalt, nil)}
|
||||
|
||||
selected, err := key4.selectPassword(func() ([]byte, error) {
|
||||
t.Fatal("must not read the user's password when the empty one fits")
|
||||
return nil, nil
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.Nil(t, selected, "the empty password is used")
|
||||
}
|
||||
|
||||
func TestSelectPassword_CorruptCheck(t *testing.T) {
|
||||
// A password check that will not parse is a corrupt DB, not a wrong or missing password.
|
||||
key4 := &key4DB{globalSalt: []byte("salt"), passwordCheck: []byte("not a valid pbe blob")}
|
||||
_, err := key4.selectPassword(constPassword("whatever"))
|
||||
require.Error(t, err)
|
||||
require.NotErrorIs(t, err, errPrimaryPasswordRequired)
|
||||
require.NotErrorIs(t, err, errPrimaryPasswordWrong)
|
||||
}
|
||||
|
||||
func TestRetrieveMasterKey_PrimaryPassword(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
globalSalt := bytes.Repeat([]byte("16-byte-salt-abc"), 3) // NSS's 48-byte salt for a newly set primary password
|
||||
password := []byte("hunter2")
|
||||
masterKey := []byte("firefox-pw-master-key-24")
|
||||
browsertest.WriteFirefoxPrimaryPasswordProfile(t, dir, globalSalt, password, [][]byte{masterKey})
|
||||
key4, logins := filepath.Join(dir, "key4.db"), filepath.Join(dir, "logins.json")
|
||||
|
||||
got, err := retrieveMasterKey(key4, logins, constPassword(string(password)))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, masterKey, got, "the right password recovers the master key")
|
||||
|
||||
_, err = retrieveMasterKey(key4, logins, constPassword(""))
|
||||
require.ErrorIs(t, err, errPrimaryPasswordRequired, "a protected profile with no password asks for one")
|
||||
|
||||
_, err = retrieveMasterKey(key4, logins, constPassword("wrong"))
|
||||
require.ErrorIs(t, err, errPrimaryPasswordWrong, "a wrong password is reported as wrong")
|
||||
}
|
||||
Reference in new issue
Block a user