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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions cmd/hack-browser-data/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func newDumpCommand() *cobra.Command {
var (
flags app.DumpFlags
keychain keychainFlags
firefox firefoxFlags
)

command := &cobra.Command{
Expand All @@ -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)
},
}

Expand All @@ -35,22 +36,24 @@ 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
}
discovery := app.Discover(ctx, options.Target)
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)
Expand Down
28 changes: 28 additions & 0 deletions cmd/hack-browser-data/password.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
4 changes: 4 additions & 0 deletions cmd/hack-browser-data/testdata/flags.golden
Original file line number Diff line number Diff line change
@@ -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 ""
Expand All @@ -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 ""
Expand Down
20 changes: 11 additions & 9 deletions internal/app/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
17 changes: 11 additions & 6 deletions internal/app/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}
}
Expand All @@ -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"))
Expand All @@ -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)
}
24 changes: 22 additions & 2 deletions internal/app/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"sync"

"github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/category"
Expand All @@ -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,
Expand Down Expand Up @@ -56,6 +75,7 @@ type runner struct {
extractors extractorsFunc
retrieveKeys dirKeysFunc
loginKeychain browser.LoginKeychain
firefoxPassword func(context.Context) (string, error)
logProgress bool
maxStorageValue int
}
Expand Down Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions internal/app/execute_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"bytes"
"context"
"errors"
"fmt"
Expand All @@ -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"
Expand Down Expand Up @@ -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")
}
2 changes: 1 addition & 1 deletion internal/app/extract_golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 15 additions & 4 deletions internal/browser/browsertest/firefox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand All @@ -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...)

Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
25 changes: 20 additions & 5 deletions internal/browser/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Loading
Loading