From c59483239c5aba752cf6c5bb89f83c0a63af1337 Mon Sep 17 00:00:00 2001 From: moonD4rk Date: Wed, 18 Nov 2026 15:26:59 +0800 Subject: [PATCH 1/4] feat(firefox): decrypt profiles with a primary password Firefox key derivation assumed an empty primary password. Add --firefox-pw / --firefox-pw-stdin; each profile is tried with the empty password first, then the given one, so one run can mix protected and unprotected profiles. --- cmd/hack-browser-data/dump.go | 8 ++- cmd/hack-browser-data/password.go | 29 +++++++++ cmd/hack-browser-data/testdata/flags.golden | 4 ++ internal/app/execute.go | 8 ++- internal/browser/extractor.go | 23 +++++-- internal/browser/firefox/firefox.go | 12 ++-- internal/browser/firefox/masterkey.go | 63 ++++++++++++++----- .../browser/firefox/primarypassword_test.go | 34 ++++++++++ internal/crypto/asn1pbe.go | 26 ++++---- 9 files changed, 165 insertions(+), 42 deletions(-) create mode 100644 internal/browser/firefox/primarypassword_test.go diff --git a/cmd/hack-browser-data/dump.go b/cmd/hack-browser-data/dump.go index c0db00f..1c3963d 100644 --- a/cmd/hack-browser-data/dump.go +++ b/cmd/hack-browser-data/dump.go @@ -14,6 +14,7 @@ func newDumpCommand() *cobra.Command { var ( flags app.DumpFlags keychain keychainFlags + firefox firefoxFlags ) command := &cobra.Command{ @@ -25,7 +26,7 @@ func newDumpCommand() *cobra.Command { hack-browser-data dump -f cookie-editor hack-browser-data dump --zip`, RunE: func(command *cobra.Command, args []string) error { - return runDump(command.Context(), command.ErrOrStderr(), flags, keychain) + return runDump(command.Context(), command.ErrOrStderr(), flags, keychain, firefox) }, } @@ -35,13 +36,14 @@ func newDumpCommand() *cobra.Command { command.Flags().StringVarP(&flags.OutputDir, "dir", "d", "results", "output directory") command.Flags().StringVarP(&flags.ProfilePath, "profile-path", "p", "", "custom profile dir path, get with chrome://version") addKeychainPasswordFlags(command, &keychain) + addFirefoxPasswordFlags(command, &firefox) command.Flags().BoolVar(&flags.Zip, "zip", false, "compress output to zip") command.Flags().IntVar(&flags.MaxStorageValue, "max-storage-value", app.DefaultMaxStorageValue, "max storage value chars, 0=unlimited") return command } -func runDump(ctx context.Context, stderr io.Writer, flags app.DumpFlags, keychain keychainFlags) error { +func runDump(ctx context.Context, stderr io.Writer, flags app.DumpFlags, keychain keychainFlags, firefox firefoxFlags) error { options, err := app.ValidateDump(flags, keychainPasswordFunc(keychain, os.Stdin)) if err != nil { return err @@ -50,7 +52,7 @@ func runDump(ctx context.Context, stderr io.Writer, flags app.DumpFlags, keychai if warnIfNothingFound(stderr, discovery, "no browsers found") { return nil } - summary, err := app.Execute(ctx, discovery, options.Categories, options.Output, options.Keychain) + summary, err := app.Execute(ctx, discovery, options.Categories, options.Output, options.Keychain, firefoxPasswordFunc(firefox, os.Stdin)) announceExport(stderr, summary.Exported) if ctx.Err() == nil { report(stderr, summary) diff --git a/cmd/hack-browser-data/password.go b/cmd/hack-browser-data/password.go index 66ea51f..4e96e9d 100644 --- a/cmd/hack-browser-data/password.go +++ b/cmd/hack-browser-data/password.go @@ -26,6 +26,35 @@ func addKeychainPasswordFlags(command *cobra.Command, flags *keychainFlags) { command.MarkFlagsMutuallyExclusive("keychain-pw", "keychain-pw-stdin") } +// firefoxFlags say where a Firefox primary password comes from; both empty means the empty password, the common case. +type firefoxFlags struct { + password string + passwordStdin bool +} + +func addFirefoxPasswordFlags(command *cobra.Command, flags *firefoxFlags) { + command.Flags().StringVar(&flags.password, "firefox-pw", "", "Firefox primary password (visible in ps and shell history)") + command.Flags().BoolVar(&flags.passwordStdin, "firefox-pw-stdin", false, + "read the Firefox primary password from the first line of stdin") + command.MarkFlagsMutuallyExclusive("firefox-pw", "firefox-pw-stdin") +} + +// firefoxPasswordFunc is read lazily when a profile has a primary password; no flag means the empty password, no prompt. +func firefoxPasswordFunc(flags firefoxFlags, stdin *os.File) func(context.Context) ([]byte, error) { + switch { + case flags.password != "": + password := []byte(flags.password) + return func(context.Context) ([]byte, error) { return password, nil } + case flags.passwordStdin: + return func(ctx context.Context) ([]byte, error) { + line, err := untilDone(ctx, func() (string, error) { return firstLine(stdin) }) + return []byte(line), err + } + default: + return func(context.Context) ([]byte, error) { return nil, nil } + } +} + var errNoPassword = errors.New("macOS login password not given and stdin is not a terminal") // keychainPasswordFunc is read lazily: a flag, else a no-echo prompt on a terminal, else none (metadata only). diff --git a/cmd/hack-browser-data/testdata/flags.golden b/cmd/hack-browser-data/testdata/flags.golden index 7a9d021..c381b7d 100644 --- a/cmd/hack-browser-data/testdata/flags.golden +++ b/cmd/hack-browser-data/testdata/flags.golden @@ -1,6 +1,8 @@ hack-browser-data --browser -b string "all" hack-browser-data --category -c string "all" hack-browser-data --dir -d string "results" +hack-browser-data --firefox-pw string "" +hack-browser-data --firefox-pw-stdin bool "false" hack-browser-data --format -f string "json" hack-browser-data --help -h bool "false" hack-browser-data --keychain-pw string "" @@ -17,6 +19,8 @@ hack-browser-data archive --verbose -v bool "false" inherited hack-browser-data dump --browser -b string "all" hack-browser-data dump --category -c string "all" hack-browser-data dump --dir -d string "results" +hack-browser-data dump --firefox-pw string "" +hack-browser-data dump --firefox-pw-stdin bool "false" hack-browser-data dump --format -f string "json" hack-browser-data dump --help -h bool "false" hack-browser-data dump --keychain-pw string "" diff --git a/internal/app/execute.go b/internal/app/execute.go index 7082958..125ce30 100644 --- a/internal/app/execute.go +++ b/internal/app/execute.go @@ -19,9 +19,12 @@ import ( // ran before it, with the files written, so the caller can list them. func Execute( ctx context.Context, discovery Discovery, categories []category.Category, output Output, keychainConfig KeychainConfig, + firefoxPassword func(context.Context) ([]byte, error), ) (Summary, error) { keychain := newLoginKeychain(keychainConfig) - fromMachine := runner{extractors: extractorsFor, retrieveKeys: keysFromMachine(keychain), loginKeychain: keychain} + fromMachine := runner{ + extractors: extractorsFor, retrieveKeys: keysFromMachine(keychain), loginKeychain: keychain, firefoxPassword: firefoxPassword, + } return execute(ctx, discovery, categories, output, fromMachine) } @@ -56,6 +59,7 @@ type runner struct { extractors extractorsFunc retrieveKeys dirKeysFunc loginKeychain browser.LoginKeychain + firefoxPassword func(context.Context) ([]byte, error) logProgress bool maxStorageValue int } @@ -109,7 +113,7 @@ func (r runner) runDir(ctx context.Context, dir browser.UserDataDir, categories if err := ctx.Err(); err != nil { return summary, fmt.Errorf("run stopped: %w", err) } - input := browser.NewInput(dirSnapshot, extraction.profile, keys.get, r.loginKeychain) + input := browser.NewInput(dirSnapshot, extraction.profile, keys.get, r.loginKeychain).WithFirefoxPassword(r.firefoxPassword) result, err := r.runExtraction(ctx, extraction, input, dir.Browser.DisplayName) if err != nil { return summary, err diff --git a/internal/browser/extractor.go b/internal/browser/extractor.go index 5794a94..5c40788 100644 --- a/internal/browser/extractor.go +++ b/internal/browser/extractor.go @@ -60,10 +60,11 @@ type LoginKeychain interface { // Input is what an extractor reads for one profile: its captured files, the dir's master keys, and the run's keychain. 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) ([]byte, 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) ([]byte, error)) Input { + in.firefoxPassword = password + return in +} + +// FirefoxPrimaryPassword is the user-supplied primary password, or empty when none was given; it may block on first use. +func (in Input) FirefoxPrimaryPassword(ctx context.Context) ([]byte, error) { + if in.firefoxPassword == nil { + return nil, nil + } + return in.firefoxPassword(ctx) +} diff --git a/internal/browser/firefox/firefox.go b/internal/browser/firefox/firefox.go index c6f42f7..3ca6585 100644 --- a/internal/browser/firefox/firefox.go +++ b/internal/browser/firefox/firefox.go @@ -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, err := input.FirefoxPrimaryPassword(ctx) + if err != nil { + return nil, err + } + return retrieveMasterKey(key4Path, loginsPath, primaryPassword) } diff --git a/internal/browser/firefox/masterkey.go b/internal/browser/firefox/masterkey.go index 5d40710..db5a18f 100644 --- a/internal/browser/firefox/masterkey.go +++ b/internal/browser/firefox/masterkey.go @@ -15,13 +15,14 @@ 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 empty when the user gave none; a profile protected by a primary password needs the right one. +func retrieveMasterKey(key4Path, loginsPath string, primaryPassword []byte) ([]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 +90,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 []byte) ([][]byte, error) { + password, err := k.selectPassword(primaryPassword) + if err != nil { return nil, err } @@ -99,7 +106,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 +116,37 @@ func (k *key4DB) deriveKeys() ([][]byte, error) { return keys, nil } -func (k *key4DB) verifyPasswordCheck() error { - pbe, err := crypto.NewASN1PBE(k.passwordCheck) - if err != nil { - return fmt.Errorf("parse password check: %w", err) +// selectPassword returns the empty password for a profile with no primary password, else the user's once it checks out, +// so one run can mix profiles that have a primary password with the many that do not. +func (k *key4DB) selectPassword(primaryPassword []byte) ([]byte, error) { + if k.passwordChecks(nil) { + return nil, nil } - plain, err := pbe.Decrypt(k.globalSalt) - if err != nil { - return fmt.Errorf("decrypt password check: %w", err) + if len(primaryPassword) > 0 && k.passwordChecks(primaryPassword) { + return primaryPassword, nil } - if !bytes.Contains(plain, []byte("password-check")) { - return errors.New("password check verification failed") + if len(primaryPassword) == 0 { + return nil, errPrimaryPasswordRequired } - return nil + return nil, errPrimaryPasswordWrong } -func (k *key4DB) decryptPrivateKey(entry privateKey) ([]byte, error) { +// passwordChecks reports whether the password decrypts the "password-check" marker metaData holds for exactly this test. +func (k *key4DB) passwordChecks(password []byte) bool { + pbe, err := crypto.NewASN1PBE(k.passwordCheck) + if err != nil { + return false + } + plain, err := pbe.Decrypt(saltWithPassword(k.globalSalt, password)) + 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(saltWithPassword(k.globalSalt, password)) if err != nil { return nil, fmt.Errorf("decrypt private key: %w", err) } @@ -140,6 +157,18 @@ func (k *key4DB) decryptPrivateKey(entry privateKey) ([]byte, error) { return derivedKey, nil } +// saltWithPassword is the PBE input NSS hashes: the global salt, or the global salt followed by the primary password. +// It never mutates globalSalt, which the caller reuses for the password check and every key entry. +func saltWithPassword(globalSalt, password []byte) []byte { + if len(password) == 0 { + return globalSalt + } + combined := make([]byte, 0, len(globalSalt)+len(password)) + combined = append(combined, globalSalt...) + combined = append(combined, password...) + return combined +} + type encryptedLogin struct { username []byte password []byte diff --git a/internal/browser/firefox/primarypassword_test.go b/internal/browser/firefox/primarypassword_test.go new file mode 100644 index 0000000..5107b95 --- /dev/null +++ b/internal/browser/firefox/primarypassword_test.go @@ -0,0 +1,34 @@ +package firefox + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestSaltWithPassword(t *testing.T) { + salt := []byte("saltsalt") + + // An empty password derives from the global salt alone. + assert.Equal(t, salt, saltWithPassword(salt, nil)) + + // A password is appended after the salt. + assert.Equal(t, []byte("saltsaltpw"), saltWithPassword(salt, []byte("pw"))) + + // The result must not share globalSalt's backing array, which the password check and every key entry reuse. + got := saltWithPassword(salt, []byte("pw")) + got[0] = 'X' + assert.Equal(t, []byte("saltsalt"), salt, "globalSalt must not be mutated") +} + +func TestSelectPassword_NoValidCheck(t *testing.T) { + // A password check that decrypts for no password looks like a profile whose password is simply wrong or missing. + key4 := &key4DB{globalSalt: []byte("salt"), passwordCheck: []byte("not a valid pbe blob")} + + _, err := key4.selectPassword(nil) + require.ErrorIs(t, err, errPrimaryPasswordRequired) + + _, err = key4.selectPassword([]byte("hunter2")) + require.ErrorIs(t, err, errPrimaryPasswordWrong) +} diff --git a/internal/crypto/asn1pbe.go b/internal/crypto/asn1pbe.go index 85b586e..394da52 100644 --- a/internal/crypto/asn1pbe.go +++ b/internal/crypto/asn1pbe.go @@ -46,20 +46,21 @@ type privateKeyPBE struct { Encrypted []byte } -func (pbe privateKeyPBE) Decrypt(globalSalt []byte) ([]byte, error) { - key, iv := pbe.deriveKeyAndIV(globalSalt) +func (pbe privateKeyPBE) Decrypt(secret []byte) ([]byte, error) { + key, iv := pbe.deriveKeyAndIV(secret) return des3Decrypt(key, iv, pbe.Encrypted) } -func (pbe privateKeyPBE) Encrypt(globalSalt, plaintext []byte) ([]byte, error) { - key, iv := pbe.deriveKeyAndIV(globalSalt) +func (pbe privateKeyPBE) Encrypt(secret, plaintext []byte) ([]byte, error) { + key, iv := pbe.deriveKeyAndIV(secret) return des3Encrypt(key, iv, plaintext) } // deriveKeyAndIV follows https://searchfox.org/mozilla-central/source/security/nss/lib/softoken/lowpbe.c -func (pbe privateKeyPBE) deriveKeyAndIV(globalSalt []byte) ([]byte, []byte) { +// secret is the global salt, or the global salt followed by the primary password when the profile has one. +func (pbe privateKeyPBE) deriveKeyAndIV(secret []byte) ([]byte, []byte) { entrySalt := pbe.Algorithm.Salt.EntrySalt - passwordHash := sha1.Sum(globalSalt) + passwordHash := sha1.Sum(secret) combinedHash := sha1.Sum(append(passwordHash[:], entrySalt...)) paddedSalt := zeroPad(entrySalt, 20) @@ -106,18 +107,19 @@ type saltAttributes struct { } } -func (pbe passwordCheckPBE) Decrypt(globalSalt []byte) ([]byte, error) { - key, iv := pbe.deriveKeyAndIV(globalSalt) +func (pbe passwordCheckPBE) Decrypt(secret []byte) ([]byte, error) { + key, iv := pbe.deriveKeyAndIV(secret) return AESCBCDecrypt(key, iv, pbe.Encrypted) } -func (pbe passwordCheckPBE) Encrypt(globalSalt, plaintext []byte) ([]byte, error) { - key, iv := pbe.deriveKeyAndIV(globalSalt) +func (pbe passwordCheckPBE) Encrypt(secret, plaintext []byte) ([]byte, error) { + key, iv := pbe.deriveKeyAndIV(secret) return AESCBCEncrypt(key, iv, plaintext) } -func (pbe passwordCheckPBE) deriveKeyAndIV(globalSalt []byte) ([]byte, []byte) { - password := sha1.Sum(globalSalt) +// secret is the global salt, or the global salt followed by the primary password when the profile has one. +func (pbe passwordCheckPBE) deriveKeyAndIV(secret []byte) ([]byte, []byte) { + password := sha1.Sum(secret) params := pbe.Algorithm.KDFParams.PBKDF2.Salt key := PBKDF2Key(password[:], params.EntrySalt, params.IterationCount, params.KeySize, sha256.New) From b0c01fff18fdbdca8485856f3556d5e4d30f4816 Mon Sep 17 00:00:00 2001 From: moonD4rk Date: Wed, 18 Nov 2026 17:17:04 +0800 Subject: [PATCH 2/4] fix(firefox): stdin reuse, corrupt-DB error, primary-pw tests --firefox-pw-stdin read a fresh line per profile, so only the first got the password; read it once and lazily, and make it exclusive with --keychain-pw-stdin. A key4.db that will not parse is a corrupt DB, not a wrong password. Add positive and end-to-end primary-password tests. --- cmd/hack-browser-data/dump.go | 1 + cmd/hack-browser-data/password.go | 11 ++- internal/browser/browsertest/firefox.go | 18 +++- internal/browser/firefox/firefox.go | 5 +- internal/browser/firefox/masterkey.go | 40 +++++---- .../browser/firefox/primarypassword_test.go | 89 +++++++++++++++++-- internal/crypto/asn1pbe.go | 3 +- 7 files changed, 135 insertions(+), 32 deletions(-) diff --git a/cmd/hack-browser-data/dump.go b/cmd/hack-browser-data/dump.go index 1c3963d..6050512 100644 --- a/cmd/hack-browser-data/dump.go +++ b/cmd/hack-browser-data/dump.go @@ -39,6 +39,7 @@ func newDumpCommand() *cobra.Command { addFirefoxPasswordFlags(command, &firefox) command.Flags().BoolVar(&flags.Zip, "zip", false, "compress output to zip") command.Flags().IntVar(&flags.MaxStorageValue, "max-storage-value", app.DefaultMaxStorageValue, "max storage value chars, 0=unlimited") + command.MarkFlagsMutuallyExclusive("keychain-pw-stdin", "firefox-pw-stdin") return command } diff --git a/cmd/hack-browser-data/password.go b/cmd/hack-browser-data/password.go index 4e96e9d..093f0fd 100644 --- a/cmd/hack-browser-data/password.go +++ b/cmd/hack-browser-data/password.go @@ -8,6 +8,7 @@ import ( "io" "os" "strings" + "sync" "github.com/spf13/cobra" "golang.org/x/term" @@ -46,9 +47,15 @@ func firefoxPasswordFunc(flags firefoxFlags, stdin *os.File) func(context.Contex password := []byte(flags.password) return func(context.Context) ([]byte, error) { return password, nil } case flags.passwordStdin: + var once sync.Once + var password []byte + var readErr error return func(ctx context.Context) ([]byte, error) { - line, err := untilDone(ctx, func() (string, error) { return firstLine(stdin) }) - return []byte(line), err + once.Do(func() { + line, err := untilDone(ctx, func() (string, error) { return firstLine(stdin) }) + password, readErr = []byte(line), err + }) + return password, readErr } default: return func(context.Context) ([]byte, error) { return nil, nil } diff --git a/internal/browser/browsertest/firefox.go b/internal/browser/browsertest/firefox.go index 8abbb5b..5fa1824 100644 --- a/internal/browser/browsertest/firefox.go +++ b/internal/browser/browsertest/firefox.go @@ -53,7 +53,21 @@ func WriteFirefoxUserDataDir(t *testing.T, profilesDir string) { } } +// WriteFirefoxPrimaryPasswordProfile writes one profile at dir whose key4.db is sealed with globalSalt+password, so its +// master key needs that password; logins stay sealed with the last key, the master key. +func WriteFirefoxPrimaryPasswordProfile(t *testing.T, dir string, globalSalt, password []byte, keys [][]byte) { + t.Helper() + seal := append(append([]byte(nil), globalSalt...), password...) + writeFirefoxProfileSealed(t, dir, "pw.firefox.test", time.Unix(1704067200, 0).UTC(), globalSalt, seal, keys) +} + func writeFirefoxProfile(t *testing.T, dir, site string, start time.Time, globalSalt []byte, keys [][]byte) { + writeFirefoxProfileSealed(t, dir, site, start, globalSalt, globalSalt, keys) +} + +// writeFirefoxProfileSealed seals key4.db with key4Seal, which a primary-password profile sets to globalSalt+password +// while metaData still stores the bare globalSalt, mirroring NSS. +func writeFirefoxProfileSealed(t *testing.T, dir, site string, start time.Time, globalSalt, key4Seal []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 +79,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, key4Seal, 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, key4Seal, fmt.Sprintf("%s/key-%d", site, i), key), firefoxKeyID)) } writeSQLite(t, filepath.Join(dir, "key4.db"), key4Statements...) diff --git a/internal/browser/firefox/firefox.go b/internal/browser/firefox/firefox.go index 3ca6585..ac03d0e 100644 --- a/internal/browser/firefox/firefox.go +++ b/internal/browser/firefox/firefox.go @@ -150,9 +150,6 @@ func profileKey(ctx context.Context, input browser.Input, loginsPath string) ([] if err != nil { return nil, err } - primaryPassword, err := input.FirefoxPrimaryPassword(ctx) - if err != nil { - return nil, err - } + primaryPassword := func() ([]byte, error) { return input.FirefoxPrimaryPassword(ctx) } return retrieveMasterKey(key4Path, loginsPath, primaryPassword) } diff --git a/internal/browser/firefox/masterkey.go b/internal/browser/firefox/masterkey.go index db5a18f..ac36366 100644 --- a/internal/browser/firefox/masterkey.go +++ b/internal/browser/firefox/masterkey.go @@ -15,8 +15,9 @@ import ( ) // retrieveMasterKey derives the key candidates in key4.db and returns the one that opens the logins in loginsPath. -// primaryPassword is empty when the user gave none; a profile protected by a primary password needs the right one. -func retrieveMasterKey(key4Path, loginsPath string, primaryPassword []byte) ([]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 @@ -95,7 +96,7 @@ var ( errPrimaryPasswordWrong = errors.New("primary password is incorrect") ) -func (k *key4DB) deriveKeys(primaryPassword []byte) ([][]byte, error) { +func (k *key4DB) deriveKeys(primaryPassword func() ([]byte, error)) ([][]byte, error) { password, err := k.selectPassword(primaryPassword) if err != nil { return nil, err @@ -116,28 +117,33 @@ func (k *key4DB) deriveKeys(primaryPassword []byte) ([][]byte, error) { return keys, nil } -// selectPassword returns the empty password for a profile with no primary password, else the user's once it checks out, -// so one run can mix profiles that have a primary password with the many that do not. -func (k *key4DB) selectPassword(primaryPassword []byte) ([]byte, error) { - if k.passwordChecks(nil) { +// 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 nil, fmt.Errorf("parse key4.db password check: %w", err) + } + if passwordChecks(pbe, saltWithPassword(k.globalSalt, nil)) { return nil, nil } - if len(primaryPassword) > 0 && k.passwordChecks(primaryPassword) { - return primaryPassword, nil + password, err := primaryPassword() + if err != nil { + return nil, err + } + if len(password) > 0 && passwordChecks(pbe, saltWithPassword(k.globalSalt, password)) { + return password, nil } - if len(primaryPassword) == 0 { + if len(password) == 0 { return nil, errPrimaryPasswordRequired } return nil, errPrimaryPasswordWrong } -// passwordChecks reports whether the password decrypts the "password-check" marker metaData holds for exactly this test. -func (k *key4DB) passwordChecks(password []byte) bool { - pbe, err := crypto.NewASN1PBE(k.passwordCheck) - if err != nil { - return false - } - plain, err := pbe.Decrypt(saltWithPassword(k.globalSalt, password)) +// passwordChecks reports whether secret decrypts the "password-check" marker the profile stored in metaData.item2. +func passwordChecks(pbe crypto.ASN1PBE, secret []byte) bool { + plain, err := pbe.Decrypt(secret) return err == nil && bytes.Contains(plain, []byte("password-check")) } diff --git a/internal/browser/firefox/primarypassword_test.go b/internal/browser/firefox/primarypassword_test.go index 5107b95..367162f 100644 --- a/internal/browser/firefox/primarypassword_test.go +++ b/internal/browser/firefox/primarypassword_test.go @@ -1,10 +1,16 @@ 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" ) func TestSaltWithPassword(t *testing.T) { @@ -22,13 +28,84 @@ func TestSaltWithPassword(t *testing.T) { assert.Equal(t, []byte("saltsalt"), salt, "globalSalt must not be mutated") } -func TestSelectPassword_NoValidCheck(t *testing.T) { - // A password check that decrypts for no password looks like a profile whose password is simply wrong or missing. +// 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(saltWithPassword(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 := []byte("home-global-salt-20b") + 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 := key4.selectPassword(nil) - require.ErrorIs(t, err, errPrimaryPasswordRequired) + _, err = retrieveMasterKey(key4, logins, constPassword("")) + require.ErrorIs(t, err, errPrimaryPasswordRequired, "a protected profile with no password asks for one") - _, err = key4.selectPassword([]byte("hunter2")) - require.ErrorIs(t, err, errPrimaryPasswordWrong) + _, err = retrieveMasterKey(key4, logins, constPassword("wrong")) + require.ErrorIs(t, err, errPrimaryPasswordWrong, "a wrong password is reported as wrong") } diff --git a/internal/crypto/asn1pbe.go b/internal/crypto/asn1pbe.go index 394da52..f54325b 100644 --- a/internal/crypto/asn1pbe.go +++ b/internal/crypto/asn1pbe.go @@ -11,7 +11,8 @@ import ( const des3KeySize = 24 // 3DES uses 24-byte (192-bit) keys -// ASN1PBE keys privateKeyPBE and passwordCheckPBE with the global salt, and credentialPBE with the master key. +// ASN1PBE keys privateKeyPBE and passwordCheckPBE from the global salt (plus the primary password when one is set), and +// credentialPBE from the master key. type ASN1PBE interface { Decrypt(key []byte) ([]byte, error) Encrypt(key, plaintext []byte) ([]byte, error) From 92c29a3ad0a4dd81138efef45266f77e8876a819 Mon Sep 17 00:00:00 2001 From: moonD4rk Date: Wed, 18 Nov 2026 21:49:59 +0800 Subject: [PATCH 3/4] fix(firefox): hash 48-byte NSS salts with SHA-384 NSS gives a newly set primary password a 48-byte salt and SHA-384, so real Firefox 159 profiles failed the password check even with the right password. --- internal/browser/browsertest/firefox.go | 21 ++++----- internal/browser/firefox/masterkey.go | 24 +++------- .../browser/firefox/primarypassword_test.go | 19 +------- internal/crypto/asn1pbe.go | 45 +++++++++++-------- internal/crypto/asn1pbe_test.go | 22 +++++++-- 5 files changed, 61 insertions(+), 70 deletions(-) diff --git a/internal/browser/browsertest/firefox.go b/internal/browser/browsertest/firefox.go index 5fa1824..d1e06bf 100644 --- a/internal/browser/browsertest/firefox.go +++ b/internal/browser/browsertest/firefox.go @@ -53,21 +53,18 @@ func WriteFirefoxUserDataDir(t *testing.T, profilesDir string) { } } -// WriteFirefoxPrimaryPasswordProfile writes one profile at dir whose key4.db is sealed with globalSalt+password, so its -// master key needs that password; logins stay sealed with the last key, the master key. +// 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() - seal := append(append([]byte(nil), globalSalt...), password...) - writeFirefoxProfileSealed(t, dir, "pw.firefox.test", time.Unix(1704067200, 0).UTC(), globalSalt, seal, keys) + 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, globalSalt, keys) + writeFirefoxProfileSealed(t, dir, site, start, globalSalt, crypto.NSSPasswordKey(globalSalt, nil), keys) } -// writeFirefoxProfileSealed seals key4.db with key4Seal, which a primary-password profile sets to globalSalt+password -// while metaData still stores the bare globalSalt, mirroring NSS. -func writeFirefoxProfileSealed(t *testing.T, dir, site string, start time.Time, globalSalt, key4Seal []byte, keys [][]byte) { +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() } @@ -79,11 +76,11 @@ func writeFirefoxProfileSealed(t *testing.T, dir, site string, start time.Time, firefoxMetaDataSchema, firefoxNSSPrivateSchema, fmt.Sprintf(`INSERT INTO metaData (id, item1, item2) VALUES ('password', x'%x', x'%x')`, - globalSalt, sealFirefoxPBES2(t, key4Seal, 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, key4Seal, 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...) @@ -185,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 @@ -197,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 { diff --git a/internal/browser/firefox/masterkey.go b/internal/browser/firefox/masterkey.go index ac36366..d5284ed 100644 --- a/internal/browser/firefox/masterkey.go +++ b/internal/browser/firefox/masterkey.go @@ -125,14 +125,14 @@ func (k *key4DB) selectPassword(primaryPassword func() ([]byte, error)) ([]byte, if err != nil { return nil, fmt.Errorf("parse key4.db password check: %w", err) } - if passwordChecks(pbe, saltWithPassword(k.globalSalt, nil)) { + if passwordChecks(pbe, crypto.NSSPasswordKey(k.globalSalt, nil)) { return nil, nil } password, err := primaryPassword() if err != nil { return nil, err } - if len(password) > 0 && passwordChecks(pbe, saltWithPassword(k.globalSalt, password)) { + if len(password) > 0 && passwordChecks(pbe, crypto.NSSPasswordKey(k.globalSalt, password)) { return password, nil } if len(password) == 0 { @@ -141,9 +141,9 @@ func (k *key4DB) selectPassword(primaryPassword func() ([]byte, error)) ([]byte, return nil, errPrimaryPasswordWrong } -// passwordChecks reports whether secret decrypts the "password-check" marker the profile stored in metaData.item2. -func passwordChecks(pbe crypto.ASN1PBE, secret []byte) bool { - plain, err := pbe.Decrypt(secret) +// 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")) } @@ -152,7 +152,7 @@ func (k *key4DB) decryptPrivateKey(entry privateKey, password []byte) ([]byte, e if err != nil { return nil, fmt.Errorf("parse private key: %w", err) } - derivedKey, err := pbe.Decrypt(saltWithPassword(k.globalSalt, password)) + derivedKey, err := pbe.Decrypt(crypto.NSSPasswordKey(k.globalSalt, password)) if err != nil { return nil, fmt.Errorf("decrypt private key: %w", err) } @@ -163,18 +163,6 @@ func (k *key4DB) decryptPrivateKey(entry privateKey, password []byte) ([]byte, e return derivedKey, nil } -// saltWithPassword is the PBE input NSS hashes: the global salt, or the global salt followed by the primary password. -// It never mutates globalSalt, which the caller reuses for the password check and every key entry. -func saltWithPassword(globalSalt, password []byte) []byte { - if len(password) == 0 { - return globalSalt - } - combined := make([]byte, 0, len(globalSalt)+len(password)) - combined = append(combined, globalSalt...) - combined = append(combined, password...) - return combined -} - type encryptedLogin struct { username []byte password []byte diff --git a/internal/browser/firefox/primarypassword_test.go b/internal/browser/firefox/primarypassword_test.go index 367162f..3a41e27 100644 --- a/internal/browser/firefox/primarypassword_test.go +++ b/internal/browser/firefox/primarypassword_test.go @@ -13,21 +13,6 @@ import ( "github.com/moond4rk/hackbrowserdata/internal/crypto" ) -func TestSaltWithPassword(t *testing.T) { - salt := []byte("saltsalt") - - // An empty password derives from the global salt alone. - assert.Equal(t, salt, saltWithPassword(salt, nil)) - - // A password is appended after the salt. - assert.Equal(t, []byte("saltsaltpw"), saltWithPassword(salt, []byte("pw"))) - - // The result must not share globalSalt's backing array, which the password check and every key entry reuse. - got := saltWithPassword(salt, []byte("pw")) - got[0] = 'X' - assert.Equal(t, []byte("saltsalt"), salt, "globalSalt must not be mutated") -} - // 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" @@ -39,7 +24,7 @@ func passwordCheckFor(t *testing.T, globalSalt, password []byte) []byte { require.NoError(t, err) pbe, err := crypto.NewASN1PBE(blob) require.NoError(t, err) - cipher, err := pbe.Encrypt(saltWithPassword(globalSalt, password), []byte("password-check")) + 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...) @@ -93,7 +78,7 @@ func TestSelectPassword_CorruptCheck(t *testing.T) { func TestRetrieveMasterKey_PrimaryPassword(t *testing.T) { dir := t.TempDir() - globalSalt := []byte("home-global-salt-20b") + 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}) diff --git a/internal/crypto/asn1pbe.go b/internal/crypto/asn1pbe.go index f54325b..d9a2adb 100644 --- a/internal/crypto/asn1pbe.go +++ b/internal/crypto/asn1pbe.go @@ -6,18 +6,30 @@ import ( "crypto/hmac" "crypto/sha1" "crypto/sha256" + "crypto/sha512" "encoding/asn1" ) const des3KeySize = 24 // 3DES uses 24-byte (192-bit) keys -// ASN1PBE keys privateKeyPBE and passwordCheckPBE from the global salt (plus the primary password when one is set), and -// credentialPBE from the master key. +// ASN1PBE keys privateKeyPBE and passwordCheckPBE with an NSSPasswordKey, and credentialPBE with the master key. type ASN1PBE interface { Decrypt(key []byte) ([]byte, error) Encrypt(key, plaintext []byte) ([]byte, error) } +// NSSPasswordKey hashes globalSalt+password as NSS does: a newly set primary password gets a 48-byte salt and SHA-384. +func NSSPasswordKey(globalSalt, password []byte) []byte { + input := make([]byte, 0, len(globalSalt)+len(password)) + input = append(append(input, globalSalt...), password...) + if len(globalSalt) == sha512.Size384 { + sum := sha512.Sum384(input) + return sum[:] + } + sum := sha1.Sum(input) + return sum[:] +} + func NewASN1PBE(encoded []byte) (pbe ASN1PBE, err error) { var ( nss privateKeyPBE @@ -47,22 +59,20 @@ type privateKeyPBE struct { Encrypted []byte } -func (pbe privateKeyPBE) Decrypt(secret []byte) ([]byte, error) { - key, iv := pbe.deriveKeyAndIV(secret) +func (pbe privateKeyPBE) Decrypt(passwordKey []byte) ([]byte, error) { + key, iv := pbe.deriveKeyAndIV(passwordKey) return des3Decrypt(key, iv, pbe.Encrypted) } -func (pbe privateKeyPBE) Encrypt(secret, plaintext []byte) ([]byte, error) { - key, iv := pbe.deriveKeyAndIV(secret) +func (pbe privateKeyPBE) Encrypt(passwordKey, plaintext []byte) ([]byte, error) { + key, iv := pbe.deriveKeyAndIV(passwordKey) return des3Encrypt(key, iv, plaintext) } // deriveKeyAndIV follows https://searchfox.org/mozilla-central/source/security/nss/lib/softoken/lowpbe.c -// secret is the global salt, or the global salt followed by the primary password when the profile has one. -func (pbe privateKeyPBE) deriveKeyAndIV(secret []byte) ([]byte, []byte) { +func (pbe privateKeyPBE) deriveKeyAndIV(passwordKey []byte) ([]byte, []byte) { entrySalt := pbe.Algorithm.Salt.EntrySalt - passwordHash := sha1.Sum(secret) - combinedHash := sha1.Sum(append(passwordHash[:], entrySalt...)) + combinedHash := sha1.Sum(append(append([]byte(nil), passwordKey...), entrySalt...)) paddedSalt := zeroPad(entrySalt, 20) saltHMAC := hmac.New(sha1.New, combinedHash[:]) @@ -108,22 +118,19 @@ type saltAttributes struct { } } -func (pbe passwordCheckPBE) Decrypt(secret []byte) ([]byte, error) { - key, iv := pbe.deriveKeyAndIV(secret) +func (pbe passwordCheckPBE) Decrypt(passwordKey []byte) ([]byte, error) { + key, iv := pbe.deriveKeyAndIV(passwordKey) return AESCBCDecrypt(key, iv, pbe.Encrypted) } -func (pbe passwordCheckPBE) Encrypt(secret, plaintext []byte) ([]byte, error) { - key, iv := pbe.deriveKeyAndIV(secret) +func (pbe passwordCheckPBE) Encrypt(passwordKey, plaintext []byte) ([]byte, error) { + key, iv := pbe.deriveKeyAndIV(passwordKey) return AESCBCEncrypt(key, iv, plaintext) } -// secret is the global salt, or the global salt followed by the primary password when the profile has one. -func (pbe passwordCheckPBE) deriveKeyAndIV(secret []byte) ([]byte, []byte) { - password := sha1.Sum(secret) - +func (pbe passwordCheckPBE) deriveKeyAndIV(passwordKey []byte) ([]byte, []byte) { params := pbe.Algorithm.KDFParams.PBKDF2.Salt - key := PBKDF2Key(password[:], params.EntrySalt, params.IterationCount, params.KeySize, sha256.New) + key := PBKDF2Key(passwordKey, params.EntrySalt, params.IterationCount, params.KeySize, sha256.New) // Firefox stores the IV with its ASN.1 OCTET STRING header (tag=0x04, length=0x0E). // The full 16-byte IV = [0x04, 0x0E] + 14-byte IV value from the parsed structure. diff --git a/internal/crypto/asn1pbe_test.go b/internal/crypto/asn1pbe_test.go index 11751fb..669fc96 100644 --- a/internal/crypto/asn1pbe_test.go +++ b/internal/crypto/asn1pbe_test.go @@ -104,7 +104,7 @@ func TestPrivateKeyPBE_Encrypt(t *testing.T) { }, }, } - encrypted, err := privateKeyPBETC.Encrypt(testCase.GlobalSalt, testCase.Plaintext) + encrypted, err := privateKeyPBETC.Encrypt(NSSPasswordKey(testCase.GlobalSalt, nil), testCase.Plaintext) require.NoError(t, err) assert.NotEmpty(t, encrypted) assert.Equal(t, privateKeyPBETC.Encrypted, encrypted) @@ -132,7 +132,7 @@ func TestPrivateKeyPBE_Decrypt(t *testing.T) { }, }, } - decrypted, err := privateKeyPBETC.Decrypt(testCase.GlobalSalt) + decrypted, err := privateKeyPBETC.Decrypt(NSSPasswordKey(testCase.GlobalSalt, nil)) require.NoError(t, err) assert.NotEmpty(t, decrypted) assert.Equal(t, pbePlaintext, decrypted) @@ -189,7 +189,7 @@ func TestPasswordCheckPBE_Encrypt(t *testing.T) { }, Encrypted: testCase.Encrypted, } - encrypted, err := passwordCheckPBETC.Encrypt(testCase.GlobalSalt, testCase.Plaintext) + encrypted, err := passwordCheckPBETC.Encrypt(NSSPasswordKey(testCase.GlobalSalt, nil), testCase.Plaintext) require.NoError(t, err) assert.NotEmpty(t, encrypted) assert.Equal(t, passwordCheckPBETC.Encrypted, encrypted) @@ -232,7 +232,7 @@ func TestPasswordCheckPBE_Decrypt(t *testing.T) { }, Encrypted: testCase.Encrypted, } - decrypted, err := passwordCheckPBETC.Decrypt(testCase.GlobalSalt) + decrypted, err := passwordCheckPBETC.Decrypt(NSSPasswordKey(testCase.GlobalSalt, nil)) require.NoError(t, err) assert.NotEmpty(t, decrypted) assert.Equal(t, pbePlaintext, decrypted) @@ -357,3 +357,17 @@ func TestCredentialPBE_ShortKey(t *testing.T) { require.ErrorIs(t, err, errShortKey) } } + +// TestNSSPasswordKey_Firefox159 replays metaData from a real Firefox 159 profile whose primary password is "hunter2". +func TestNSSPasswordKey_Firefox159(t *testing.T) { + globalSalt, err := hex.DecodeString("546f4e882d701365bd17ec777bf0a0522097698de795e2f4d15d09143f14c1ea0be7617e2b4789c06acb1972b4915e9d") + require.NoError(t, err) + passwordCheck, err := hex.DecodeString("308182306e06092a864886f70d01050d3061304206092a864886f70d01050c30350420b0b57ba501bea653854e421326e74e1180dfd92b529603d95e1156918b5ffae302022710020120300a06082a864886f70d0209301b060960864801650304012a040eb9c12e8fdb6ccde8a9a117776dfc041062cd23dfb1de6cee18cfc11a817f3f61") + require.NoError(t, err) + pbe, err := NewASN1PBE(passwordCheck) + require.NoError(t, err) + + plain, err := pbe.Decrypt(NSSPasswordKey(globalSalt, []byte("hunter2"))) + require.NoError(t, err) + assert.Contains(t, string(plain), "password-check") +} From cad6b78b54bbae9874269bd43802c75c0cde72f7 Mon Sep 17 00:00:00 2001 From: moonD4rk Date: Wed, 18 Nov 2026 22:06:37 +0800 Subject: [PATCH 4/4] refactor(app): route the Firefox password like the keychain's It bypassed DumpOptions, read once inside cmd, and used []byte where the keychain password uses string; Execute now owns the once-per-run read. --- cmd/hack-browser-data/dump.go | 4 ++-- cmd/hack-browser-data/password.go | 22 +++++++--------------- internal/app/dump.go | 20 +++++++++++--------- internal/app/dump_test.go | 17 +++++++++++------ internal/app/execute.go | 22 +++++++++++++++++++--- internal/app/execute_test.go | 28 ++++++++++++++++++++++++++++ internal/app/extract_golden_test.go | 2 +- internal/browser/extractor.go | 12 ++++++------ internal/browser/firefox/firefox.go | 5 ++++- 9 files changed, 89 insertions(+), 43 deletions(-) diff --git a/cmd/hack-browser-data/dump.go b/cmd/hack-browser-data/dump.go index 6050512..1a82421 100644 --- a/cmd/hack-browser-data/dump.go +++ b/cmd/hack-browser-data/dump.go @@ -45,7 +45,7 @@ func newDumpCommand() *cobra.Command { } func runDump(ctx context.Context, stderr io.Writer, flags app.DumpFlags, keychain keychainFlags, firefox firefoxFlags) error { - options, err := app.ValidateDump(flags, keychainPasswordFunc(keychain, os.Stdin)) + options, err := app.ValidateDump(flags, keychainPasswordFunc(keychain, os.Stdin), firefoxPasswordFunc(firefox, os.Stdin)) if err != nil { return err } @@ -53,7 +53,7 @@ func runDump(ctx context.Context, stderr io.Writer, flags app.DumpFlags, keychai if warnIfNothingFound(stderr, discovery, "no browsers found") { return nil } - summary, err := app.Execute(ctx, discovery, options.Categories, options.Output, options.Keychain, firefoxPasswordFunc(firefox, os.Stdin)) + summary, err := app.Execute(ctx, discovery, options.Categories, options.Output, options.Keychain, options.FirefoxPassword) announceExport(stderr, summary.Exported) if ctx.Err() == nil { report(stderr, summary) diff --git a/cmd/hack-browser-data/password.go b/cmd/hack-browser-data/password.go index 093f0fd..6ab3c57 100644 --- a/cmd/hack-browser-data/password.go +++ b/cmd/hack-browser-data/password.go @@ -8,7 +8,6 @@ import ( "io" "os" "strings" - "sync" "github.com/spf13/cobra" "golang.org/x/term" @@ -40,25 +39,18 @@ func addFirefoxPasswordFlags(command *cobra.Command, flags *firefoxFlags) { command.MarkFlagsMutuallyExclusive("firefox-pw", "firefox-pw-stdin") } -// firefoxPasswordFunc is read lazily when a profile has a primary password; no flag means the empty password, no prompt. -func firefoxPasswordFunc(flags firefoxFlags, stdin *os.File) func(context.Context) ([]byte, error) { +// firefoxPasswordFunc reads a flag, else the first line of stdin; nil means the empty password, and it never prompts. +func firefoxPasswordFunc(flags firefoxFlags, stdin *os.File) func(context.Context) (string, error) { switch { case flags.password != "": - password := []byte(flags.password) - return func(context.Context) ([]byte, error) { return password, nil } + password := flags.password + return func(context.Context) (string, error) { return password, nil } case flags.passwordStdin: - var once sync.Once - var password []byte - var readErr error - return func(ctx context.Context) ([]byte, error) { - once.Do(func() { - line, err := untilDone(ctx, func() (string, error) { return firstLine(stdin) }) - password, readErr = []byte(line), err - }) - return password, readErr + return func(ctx context.Context) (string, error) { + return untilDone(ctx, func() (string, error) { return firstLine(stdin) }) } default: - return func(context.Context) ([]byte, error) { return nil, nil } + return nil } } diff --git a/internal/app/dump.go b/internal/app/dump.go index 5d497ba..113a462 100644 --- a/internal/app/dump.go +++ b/internal/app/dump.go @@ -23,14 +23,15 @@ type DumpFlags struct { } type DumpOptions struct { - Target Target - Categories []category.Category - Output Output - Keychain KeychainConfig + Target Target + Categories []category.Category + Output Output + Keychain KeychainConfig + FirefoxPassword func(ctx context.Context) (string, error) // nil is the empty password; Execute reads it at most once } // ValidateDump checks every flag before any browser or secret is touched, so a bad flag fails fast. -func ValidateDump(flags DumpFlags, keychainPassword func(context.Context) (string, error)) (DumpOptions, error) { +func ValidateDump(flags DumpFlags, keychainPassword, firefoxPassword func(context.Context) (string, error)) (DumpOptions, error) { browsers, err := parseBrowsers(flags.Browser) if err != nil { return DumpOptions{}, err @@ -58,10 +59,11 @@ func ValidateDump(flags DumpFlags, keychainPassword func(context.Context) (strin return DumpOptions{}, err } return DumpOptions{ - Target: Target{Browsers: browsers, ProfilePath: profilePath, HomeDir: home}, - Categories: categories, - Output: Output{Dir: flags.OutputDir, Format: format, Zip: flags.Zip, MaxStorageValue: flags.MaxStorageValue}, - Keychain: KeychainConfig{Path: loginKeychainPath(home), Password: keychainPassword}, + Target: Target{Browsers: browsers, ProfilePath: profilePath, HomeDir: home}, + Categories: categories, + Output: Output{Dir: flags.OutputDir, Format: format, Zip: flags.Zip, MaxStorageValue: flags.MaxStorageValue}, + Keychain: KeychainConfig{Path: loginKeychainPath(home), Password: keychainPassword}, + FirefoxPassword: firefoxPassword, }, nil } diff --git a/internal/app/dump_test.go b/internal/app/dump_test.go index 5b2966f..a386c7b 100644 --- a/internal/app/dump_test.go +++ b/internal/app/dump_test.go @@ -18,7 +18,9 @@ import ( func TestValidateDump(t *testing.T) { keychainPassword := func(context.Context) (string, error) { return "pw", nil } - options, err := ValidateDump(DumpFlags{Browser: " Chrome ", Categories: "cookie, password,cookie", Format: "csv", OutputDir: "out"}, keychainPassword) + firefoxPassword := func(context.Context) (string, error) { return "ff", nil } + options, err := ValidateDump(DumpFlags{Browser: " Chrome ", Categories: "cookie, password,cookie", Format: "csv", OutputDir: "out"}, + keychainPassword, firefoxPassword) require.NoError(t, err) require.Len(t, options.Target.Browsers, 1) assert.Equal(t, "chrome", options.Target.Browsers[0].Name) @@ -30,8 +32,11 @@ func TestValidateDump(t *testing.T) { password, err := options.Keychain.Password(context.Background()) require.NoError(t, err) assert.Equal(t, "pw", password) + password, err = options.FirefoxPassword(context.Background()) + require.NoError(t, err) + assert.Equal(t, "ff", password) - options, err = ValidateDump(DumpFlags{Categories: "all", Format: "json"}, nil) + options, err = ValidateDump(DumpFlags{Categories: "all", Format: "json"}, nil, nil) require.NoError(t, err) assert.Equal(t, browser.Supported(), options.Target.Browsers) assert.Equal(t, category.All(), options.Categories) @@ -52,7 +57,7 @@ func TestValidateDump_RejectsBadInput(t *testing.T) { "missing -p": {Browser: "chrome", Categories: "all", Format: "json", ProfilePath: missing}, "-p is a file": {Browser: "chrome", Categories: "all", Format: "json", ProfilePath: file}, } { - _, err := ValidateDump(flags, nil) + _, err := ValidateDump(flags, nil, nil) assert.Error(t, err, name) } } @@ -72,7 +77,7 @@ func otherOSBrowser(t *testing.T) string { func TestValidateDump_CreatesNoOutputDir(t *testing.T) { root := t.TempDir() - options, err := ValidateDump(DumpFlags{Categories: "all", Format: "json", OutputDir: filepath.Join(root, "a", "b")}, nil) + options, err := ValidateDump(DumpFlags{Categories: "all", Format: "json", OutputDir: filepath.Join(root, "a", "b")}, nil, nil) require.NoError(t, err) assert.Equal(t, filepath.Join(root, "a", "b"), options.Output.Dir) _, err = os.Stat(filepath.Join(root, "a")) @@ -87,13 +92,13 @@ func TestValidateDump_RejectsAnOutputDirThatCannotBeCreated(t *testing.T) { require.NoError(t, os.Chmod(root, 0o500)) defer func() { require.NoError(t, os.Chmod(root, 0o700)) }() - _, err := ValidateDump(DumpFlags{Categories: "all", Format: "json", OutputDir: filepath.Join(root, "results")}, nil) + _, err := ValidateDump(DumpFlags{Categories: "all", Format: "json", OutputDir: filepath.Join(root, "results")}, nil, nil) assert.Error(t, err) } // TestValidateDump_IgnoresProfilePathForAllBrowsers keeps -p without a named browser ignored, as before; it is reported. func TestValidateDump_IgnoresProfilePathForAllBrowsers(t *testing.T) { - options, err := ValidateDump(DumpFlags{Browser: "all", Categories: "all", Format: "json", ProfilePath: filepath.Join(t.TempDir(), "missing")}, nil) + options, err := ValidateDump(DumpFlags{Browser: "all", Categories: "all", Format: "json", ProfilePath: filepath.Join(t.TempDir(), "missing")}, nil, nil) require.NoError(t, err) assert.Empty(t, options.Target.ProfilePath) } diff --git a/internal/app/execute.go b/internal/app/execute.go index 125ce30..726df5d 100644 --- a/internal/app/execute.go +++ b/internal/app/execute.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "sync" "github.com/moond4rk/hackbrowserdata/internal/browser" "github.com/moond4rk/hackbrowserdata/internal/category" @@ -19,15 +20,30 @@ import ( // ran before it, with the files written, so the caller can list them. func Execute( ctx context.Context, discovery Discovery, categories []category.Category, output Output, keychainConfig KeychainConfig, - firefoxPassword func(context.Context) ([]byte, error), + firefoxPassword func(context.Context) (string, error), ) (Summary, error) { keychain := newLoginKeychain(keychainConfig) fromMachine := runner{ - extractors: extractorsFor, retrieveKeys: keysFromMachine(keychain), loginKeychain: keychain, firefoxPassword: firefoxPassword, + extractors: extractorsFor, retrieveKeys: keysFromMachine(keychain), loginKeychain: keychain, + firefoxPassword: readOnce(firefoxPassword), } return execute(ctx, discovery, categories, output, fromMachine) } +// readOnce lets every protected Firefox profile of a run share one read, since stdin yields the password only once. +func readOnce(read func(context.Context) (string, error)) func(context.Context) (string, error) { + if read == nil { + return nil + } + var once sync.Once + var password string + var err error + return func(ctx context.Context) (string, error) { + once.Do(func() { password, err = read(ctx) }) + return password, err + } +} + // ExecuteWithKeys decrypts with a key file's keys, so it never reads this machine's secret stores. func ExecuteWithKeys( ctx context.Context, discovery Discovery, categories []category.Category, output Output, keys map[browser.UserDataDirID]masterkey.Set, @@ -59,7 +75,7 @@ type runner struct { extractors extractorsFunc retrieveKeys dirKeysFunc loginKeychain browser.LoginKeychain - firefoxPassword func(context.Context) ([]byte, error) + firefoxPassword func(context.Context) (string, error) logProgress bool maxStorageValue int } diff --git a/internal/app/execute_test.go b/internal/app/execute_test.go index 733bf50..0e2a103 100644 --- a/internal/app/execute_test.go +++ b/internal/app/execute_test.go @@ -1,6 +1,7 @@ package app import ( + "bytes" "context" "errors" "fmt" @@ -14,6 +15,7 @@ import ( "github.com/stretchr/testify/require" "github.com/moond4rk/hackbrowserdata/internal/browser" + "github.com/moond4rk/hackbrowserdata/internal/browser/browsertest" "github.com/moond4rk/hackbrowserdata/internal/category" "github.com/moond4rk/hackbrowserdata/internal/masterkey" "github.com/moond4rk/hackbrowserdata/internal/record" @@ -267,3 +269,29 @@ func TestExecute_OutputFailureStopsTheRunAndLeavesNoCopies(t *testing.T) { _, statErr := os.Stat(copied) assert.ErrorIs(t, statErr, fs.ErrNotExist) } + +// TestExecute_FirefoxPasswordReadOnce: stdin yields the password once, yet every protected profile of the run needs it. +func TestExecute_FirefoxPasswordReadOnce(t *testing.T) { + home := t.TempDir() + firefoxBrowser, ok := browser.Lookup("firefox") + require.True(t, ok) + profilesDir := filepath.Join(home, filepath.FromSlash(firefoxBrowser.Platform().UserDataDirs[0])) + globalSalt, masterKey := bytes.Repeat([]byte("16-byte-salt-abc"), 3), []byte("firefox-pw-master-key-24") + for _, profile := range []string{"a.default", "b.work"} { + browsertest.WriteFirefoxPrimaryPasswordProfile(t, filepath.Join(profilesDir, profile), globalSalt, []byte("hunter2"), [][]byte{masterKey}) + } + reads := 0 + password := func(context.Context) (string, error) { + reads++ + return "hunter2", nil + } + + discovery := Discover(context.Background(), Target{Browsers: []browser.Browser{firefoxBrowser}, HomeDir: home}) + summary, err := Execute(context.Background(), discovery, []category.Category{category.Password}, Output{Dir: t.TempDir()}, KeychainConfig{}, password) + require.NoError(t, err) + require.Len(t, summary.Results, 2) + for _, result := range summary.Results { + assert.Equal(t, OutcomeOK, result.Outcome, result.ProfileName) + } + assert.Equal(t, 1, reads, "one read serves every protected profile of the run") +} diff --git a/internal/app/extract_golden_test.go b/internal/app/extract_golden_test.go index d953c63..c0f44a9 100644 --- a/internal/app/extract_golden_test.go +++ b/internal/app/extract_golden_test.go @@ -59,7 +59,7 @@ func TestExtractGoldenSafari(t *testing.T) { func extractGoldenRun(t *testing.T, home string, keys map[browser.UserDataDirID]masterkey.Set, format string) string { t.Helper() out := t.TempDir() - options, err := app.ValidateDump(app.DumpFlags{Browser: "all", Categories: "all", Format: format, OutputDir: out, MaxStorageValue: app.DefaultMaxStorageValue}, nil) + options, err := app.ValidateDump(app.DumpFlags{Browser: "all", Categories: "all", Format: format, OutputDir: out, MaxStorageValue: app.DefaultMaxStorageValue}, nil, nil) require.NoError(t, err) options.Target.HomeDir = home diff --git a/internal/browser/extractor.go b/internal/browser/extractor.go index 5c40788..447adf4 100644 --- a/internal/browser/extractor.go +++ b/internal/browser/extractor.go @@ -58,13 +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 - firefoxPassword func(ctx context.Context) ([]byte, error) + 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 @@ -100,15 +100,15 @@ func (in Input) LoginKeychain() LoginKeychain { } // WithFirefoxPassword sets the lazy Firefox primary-password source; only the dump path supplies one. -func (in Input) WithFirefoxPassword(password func(context.Context) ([]byte, error)) Input { +func (in Input) WithFirefoxPassword(password func(context.Context) (string, error)) Input { in.firefoxPassword = password return in } -// FirefoxPrimaryPassword is the user-supplied primary password, or empty when none was given; it may block on first use. -func (in Input) FirefoxPrimaryPassword(ctx context.Context) ([]byte, error) { +// 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, nil + return "", nil } return in.firefoxPassword(ctx) } diff --git a/internal/browser/firefox/firefox.go b/internal/browser/firefox/firefox.go index ac03d0e..e544f3c 100644 --- a/internal/browser/firefox/firefox.go +++ b/internal/browser/firefox/firefox.go @@ -150,6 +150,9 @@ func profileKey(ctx context.Context, input browser.Input, loginsPath string) ([] if err != nil { return nil, err } - primaryPassword := func() ([]byte, error) { return input.FirefoxPrimaryPassword(ctx) } + primaryPassword := func() ([]byte, error) { + password, err := input.FirefoxPrimaryPassword(ctx) + return []byte(password), err + } return retrieveMasterKey(key4Path, loginsPath, primaryPassword) }