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
@@ -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,14 +36,16 @@ 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")
|
||||
command.MarkFlagsMutuallyExclusive("keychain-pw-stdin", "firefox-pw-stdin")
|
||||
|
||||
return command
|
||||
}
|
||||
|
||||
func runDump(ctx context.Context, stderr io.Writer, flags app.DumpFlags, keychain keychainFlags) error {
|
||||
options, err := app.ValidateDump(flags, keychainPasswordFunc(keychain, os.Stdin))
|
||||
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), firefoxPasswordFunc(firefox, os.Stdin))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -50,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)
|
||||
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)
|
||||
|
||||
@@ -26,6 +26,34 @@ 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 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 := flags.password
|
||||
return func(context.Context) (string, error) { return password, nil }
|
||||
case flags.passwordStdin:
|
||||
return func(ctx context.Context) (string, error) {
|
||||
return untilDone(ctx, func() (string, error) { return firstLine(stdin) })
|
||||
}
|
||||
default:
|
||||
return 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).
|
||||
|
||||
+4
@@ -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 ""
|
||||
|
||||
+11
-9
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
+22
-2
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/moond4rk/hackbrowserdata/internal/browser"
|
||||
"github.com/moond4rk/hackbrowserdata/internal/category"
|
||||
@@ -19,12 +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) (string, 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: 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,
|
||||
@@ -56,6 +75,7 @@ type runner struct {
|
||||
extractors extractorsFunc
|
||||
retrieveKeys dirKeysFunc
|
||||
loginKeychain browser.LoginKeychain
|
||||
firefoxPassword func(context.Context) (string, error)
|
||||
logProgress bool
|
||||
maxStorageValue int
|
||||
}
|
||||
@@ -109,7 +129,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
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
+26
-16
@@ -6,17 +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 with the global salt, and credentialPBE with 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
|
||||
@@ -46,21 +59,20 @@ type privateKeyPBE struct {
|
||||
Encrypted []byte
|
||||
}
|
||||
|
||||
func (pbe privateKeyPBE) Decrypt(globalSalt []byte) ([]byte, error) {
|
||||
key, iv := pbe.deriveKeyAndIV(globalSalt)
|
||||
func (pbe privateKeyPBE) Decrypt(passwordKey []byte) ([]byte, error) {
|
||||
key, iv := pbe.deriveKeyAndIV(passwordKey)
|
||||
return des3Decrypt(key, iv, pbe.Encrypted)
|
||||
}
|
||||
|
||||
func (pbe privateKeyPBE) Encrypt(globalSalt, plaintext []byte) ([]byte, error) {
|
||||
key, iv := pbe.deriveKeyAndIV(globalSalt)
|
||||
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
|
||||
func (pbe privateKeyPBE) deriveKeyAndIV(globalSalt []byte) ([]byte, []byte) {
|
||||
func (pbe privateKeyPBE) deriveKeyAndIV(passwordKey []byte) ([]byte, []byte) {
|
||||
entrySalt := pbe.Algorithm.Salt.EntrySalt
|
||||
passwordHash := sha1.Sum(globalSalt)
|
||||
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[:])
|
||||
@@ -106,21 +118,19 @@ type saltAttributes struct {
|
||||
}
|
||||
}
|
||||
|
||||
func (pbe passwordCheckPBE) Decrypt(globalSalt []byte) ([]byte, error) {
|
||||
key, iv := pbe.deriveKeyAndIV(globalSalt)
|
||||
func (pbe passwordCheckPBE) Decrypt(passwordKey []byte) ([]byte, error) {
|
||||
key, iv := pbe.deriveKeyAndIV(passwordKey)
|
||||
return AESCBCDecrypt(key, iv, pbe.Encrypted)
|
||||
}
|
||||
|
||||
func (pbe passwordCheckPBE) Encrypt(globalSalt, plaintext []byte) ([]byte, error) {
|
||||
key, iv := pbe.deriveKeyAndIV(globalSalt)
|
||||
func (pbe passwordCheckPBE) Encrypt(passwordKey, plaintext []byte) ([]byte, error) {
|
||||
key, iv := pbe.deriveKeyAndIV(passwordKey)
|
||||
return AESCBCEncrypt(key, iv, plaintext)
|
||||
}
|
||||
|
||||
func (pbe passwordCheckPBE) deriveKeyAndIV(globalSalt []byte) ([]byte, []byte) {
|
||||
password := sha1.Sum(globalSalt)
|
||||
|
||||
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.
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
Reference in new issue
Block a user