mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-12-06 05:53:25 +01:00
Names say what each thing is: no package stutter, one word per concept, spelled out, none that differ only in case. The browser table puts one field per line. Wire names (flags, output fields, keys.json) are unchanged.
254 lines
10 KiB
Go
254 lines
10 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/moond4rk/hackbrowserdata/internal/browser"
|
|
"github.com/moond4rk/hackbrowserdata/internal/category"
|
|
"github.com/moond4rk/hackbrowserdata/internal/masterkey"
|
|
"github.com/moond4rk/hackbrowserdata/internal/record"
|
|
"github.com/moond4rk/hackbrowserdata/internal/snapshot"
|
|
)
|
|
|
|
var history = browser.FileSpec{Paths: []string{"History"}}
|
|
|
|
type extractFunc func(ctx context.Context, profileSnapshot browser.ProfileSnapshot, out browser.Collector) error
|
|
|
|
// fakeBase reads History for every category, so a test needs one fixture file per profile.
|
|
func fakeBase(extractFuncs map[category.Category]extractFunc) extractorsFunc {
|
|
return func(browser.UserDataDir, browser.Profile) browser.Extractors {
|
|
extractors := make(browser.Extractors, len(extractFuncs))
|
|
for dataCategory, extract := range extractFuncs {
|
|
extractors[dataCategory] = browser.Extractor{FileSpecs: []browser.FileSpec{history}, Extract: extract}
|
|
}
|
|
return extractors
|
|
}
|
|
}
|
|
|
|
func noKeys(t *testing.T) dirKeysFunc {
|
|
return func(context.Context, browser.UserDataDir, snapshot.Snapshot) (masterkey.Set, error) {
|
|
t.Error("keys were retrieved")
|
|
return nil, nil
|
|
}
|
|
}
|
|
|
|
func chromeDir(t *testing.T, id browser.UserDataDirID, profiles ...string) browser.UserDataDir {
|
|
t.Helper()
|
|
dir := browser.UserDataDir{ID: id, Browser: lookup(t, "chrome"), Path: t.TempDir()}
|
|
for _, name := range profiles {
|
|
touch(t, dir.Path, name, "History")
|
|
dir.Profiles = append(dir.Profiles, browser.Profile{Name: name, RelativePath: name})
|
|
}
|
|
return dir
|
|
}
|
|
|
|
func run(t *testing.T, base extractorsFunc, keys dirKeysFunc, categories []category.Category, dirs ...browser.UserDataDir) (Summary, string) {
|
|
t.Helper()
|
|
options := Options{Categories: categories, OutputDir: t.TempDir()}
|
|
summary, err := execute(context.Background(), newPlan(Discovery{Dirs: dirs}, options, base), options, base, keys)
|
|
require.NoError(t, err)
|
|
return summary, options.OutputDir
|
|
}
|
|
|
|
func outcomes(summary Summary) map[category.Category]Result {
|
|
out := make(map[category.Category]Result)
|
|
for _, result := range summary.Results {
|
|
out[result.Category] = result
|
|
}
|
|
return out
|
|
}
|
|
|
|
func TestExecute_Outcomes(t *testing.T) {
|
|
cookie := func(name string) *record.Cookie { return &record.Cookie{Name: name} }
|
|
base := fakeBase(map[category.Category]extractFunc{
|
|
category.Cookie: func(_ context.Context, _ browser.ProfileSnapshot, out browser.Collector) error {
|
|
out.Add(cookie("a"))
|
|
out.Add(cookie("b"))
|
|
out.Fail(errors.New("value not decrypted"))
|
|
return nil
|
|
},
|
|
category.Password: func(_ context.Context, _ browser.ProfileSnapshot, out browser.Collector) error {
|
|
out.Fail(errors.New("password not decrypted"))
|
|
return nil
|
|
},
|
|
category.History: func(context.Context, browser.ProfileSnapshot, browser.Collector) error { return nil },
|
|
category.Bookmark: func(context.Context, browser.ProfileSnapshot, browser.Collector) error {
|
|
return errors.New("corrupt")
|
|
},
|
|
category.Download: func(context.Context, browser.ProfileSnapshot, browser.Collector) error {
|
|
return fmt.Errorf("History: %w", fs.ErrNotExist)
|
|
},
|
|
category.Extension: func(_ context.Context, _ browser.ProfileSnapshot, out browser.Collector) error {
|
|
out.Add(&record.Extension{Name: "kept"})
|
|
return errors.New("truncated")
|
|
},
|
|
})
|
|
categories := []category.Category{
|
|
category.Cookie, category.Password, category.History, category.Bookmark, category.Download, category.Extension,
|
|
}
|
|
summary, out := run(t, base, noKeys(t), categories, chromeDir(t, "chrome", "Default"))
|
|
|
|
got := outcomes(summary)
|
|
assert.Equal(t, OutcomePartial, got[category.Cookie].Outcome)
|
|
assert.Equal(t, 2, got[category.Cookie].Records)
|
|
assert.Equal(t, 1, got[category.Cookie].Failures)
|
|
assert.Equal(t, OutcomeError, got[category.Password].Outcome, "problems and no record")
|
|
require.EqualError(t, got[category.Password].Err, "password not decrypted")
|
|
assert.Equal(t, OutcomeOK, got[category.History].Outcome, "an empty file is ok, not absent")
|
|
assert.Equal(t, OutcomeError, got[category.Bookmark].Outcome)
|
|
assert.Equal(t, OutcomeAbsent, got[category.Download].Outcome, "a file that vanished after planning")
|
|
assert.Equal(t, OutcomeError, got[category.Extension].Outcome)
|
|
|
|
data, err := os.ReadFile(filepath.Join(out, "extension.json"))
|
|
require.NoError(t, err)
|
|
assert.Contains(t, string(data), `"kept"`, "records emitted before the error are exported")
|
|
data, err = os.ReadFile(filepath.Join(out, "cookie.json"))
|
|
require.NoError(t, err)
|
|
assert.Contains(t, string(data), `"browser": "Chrome"`, "the collector sets the browser's display name")
|
|
assert.Contains(t, string(data), `"profile": "Default"`)
|
|
}
|
|
|
|
func TestExecute_KeysAreRetrievedOncePerUserDataDir(t *testing.T) {
|
|
cookie := func(ctx context.Context, profileSnapshot browser.ProfileSnapshot, out browser.Collector) error {
|
|
out.Add(&record.Cookie{Value: string(profileSnapshot.MasterKeys(ctx)[masterkey.V10])})
|
|
return nil
|
|
}
|
|
login := func(ctx context.Context, profileSnapshot browser.ProfileSnapshot, out browser.Collector) error {
|
|
out.Add(&record.Login{Password: string(profileSnapshot.MasterKeys(ctx)[masterkey.V10])})
|
|
return nil
|
|
}
|
|
calls := 0
|
|
keys := func(_ context.Context, dir browser.UserDataDir, dirSnapshot snapshot.Snapshot) (masterkey.Set, error) {
|
|
calls++
|
|
_, err := dirSnapshot.Path("Default/History")
|
|
require.NoError(t, err, "keys are read from the dir's own snapshot")
|
|
return masterkey.Set{masterkey.V10: []byte(dir.ID)}, errors.New("v20: no ABE")
|
|
}
|
|
base := fakeBase(map[category.Category]extractFunc{category.Cookie: cookie, category.Password: login})
|
|
categories := []category.Category{category.Cookie, category.Password}
|
|
|
|
summary, _ := run(t, base, keys, categories, chromeDir(t, "chrome", "Default", "Profile 1"))
|
|
|
|
assert.Equal(t, 1, calls)
|
|
last := summary.Results[len(summary.Results)-1]
|
|
assert.Equal(t, Extraction{UserDataDirID: "chrome"}, last.Extraction, "a retrieval error is reported once, for the dir")
|
|
assert.Equal(t, OutcomeError, last.Outcome)
|
|
require.ErrorContains(t, last.Err, "v20: no ABE")
|
|
assert.Len(t, summary.Results, 5)
|
|
}
|
|
|
|
// TestExecute_HistoryNeverRetrievesKeys checks data without secrets never retrieves a key, so never prompts on macOS.
|
|
func TestExecute_HistoryNeverRetrievesKeys(t *testing.T) {
|
|
path := t.TempDir()
|
|
touch(t, path, "Local State")
|
|
touch(t, path, "Default", "History")
|
|
touch(t, path, "Default", "Network", "Cookies")
|
|
dir := browser.UserDataDir{ID: "chrome", Browser: lookup(t, "chrome"), Path: path, Profiles: []browser.Profile{{Name: "Default", RelativePath: "Default"}}}
|
|
|
|
run(t, extractorsWith(nil), noKeys(t), []category.Category{category.History, category.Download}, dir)
|
|
|
|
calls := 0
|
|
counting := func(context.Context, browser.UserDataDir, snapshot.Snapshot) (masterkey.Set, error) {
|
|
calls++
|
|
return nil, nil
|
|
}
|
|
run(t, extractorsWith(nil), counting, []category.Category{category.Cookie, category.History}, dir)
|
|
assert.Equal(t, 1, calls)
|
|
}
|
|
|
|
func TestExecute_RemovesEachSnapshotBeforeTheNextDir(t *testing.T) {
|
|
var captured []string
|
|
base := fakeBase(map[category.Category]extractFunc{
|
|
category.History: func(_ context.Context, profileSnapshot browser.ProfileSnapshot, _ browser.Collector) error {
|
|
for _, earlier := range captured {
|
|
_, err := os.Stat(earlier)
|
|
require.ErrorIs(t, err, fs.ErrNotExist, "an earlier dir's copy is still on disk")
|
|
}
|
|
path, err := profileSnapshot.Path(history)
|
|
require.NoError(t, err)
|
|
captured = append(captured, path)
|
|
return nil
|
|
},
|
|
})
|
|
run(t, base, noKeys(t), []category.Category{category.History}, chromeDir(t, "chrome", "Default"), chromeDir(t, "chrome-2", "Default"))
|
|
|
|
require.Len(t, captured, 2)
|
|
for _, path := range captured {
|
|
_, err := os.Stat(path)
|
|
assert.ErrorIs(t, err, fs.ErrNotExist)
|
|
}
|
|
}
|
|
|
|
func TestExecute_StopEndsTheRunBeforeTheNextExtractionAndRemovesTheCopy(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
var copied []string
|
|
base := fakeBase(map[category.Category]extractFunc{
|
|
category.History: func(_ context.Context, profileSnapshot browser.ProfileSnapshot, _ browser.Collector) error {
|
|
path, err := profileSnapshot.Path(history)
|
|
require.NoError(t, err)
|
|
copied = append(copied, path)
|
|
cancel()
|
|
return nil
|
|
},
|
|
category.Download: func(context.Context, browser.ProfileSnapshot, browser.Collector) error {
|
|
t.Error("an extraction ran after the stop")
|
|
return nil
|
|
},
|
|
})
|
|
options := Options{Categories: []category.Category{category.History, category.Download}, OutputDir: t.TempDir()}
|
|
discovery := Discovery{Dirs: []browser.UserDataDir{chromeDir(t, "chrome", "Default"), chromeDir(t, "chrome-2", "Default")}}
|
|
|
|
_, err := execute(ctx, newPlan(discovery, options, base), options, base, noKeys(t))
|
|
require.ErrorIs(t, err, context.Canceled)
|
|
require.Len(t, copied, 1, "the next user data dir is never captured")
|
|
_, statErr := os.Stat(copied[0])
|
|
assert.ErrorIs(t, statErr, fs.ErrNotExist)
|
|
}
|
|
|
|
func TestExecute_OutputFailureStopsTheRunAndLeavesNoCopies(t *testing.T) {
|
|
if runtime.GOOS == "windows" || os.Geteuid() == 0 {
|
|
t.Skip("directory permissions do not deny writes here")
|
|
}
|
|
var copied string
|
|
base := fakeBase(map[category.Category]extractFunc{
|
|
category.History: func(_ context.Context, profileSnapshot browser.ProfileSnapshot, out browser.Collector) error {
|
|
copied, _ = profileSnapshot.Path(history)
|
|
out.Add(&record.History{URL: "https://example.com"})
|
|
return nil
|
|
},
|
|
})
|
|
options := Options{Categories: []category.Category{category.History}, OutputDir: t.TempDir()}
|
|
require.NoError(t, os.Chmod(options.OutputDir, 0o500))
|
|
defer func() { require.NoError(t, os.Chmod(options.OutputDir, 0o700)) }()
|
|
|
|
plan := newPlan(Discovery{Dirs: []browser.UserDataDir{chromeDir(t, "chrome", "Default")}}, options, base)
|
|
_, err := execute(context.Background(), plan, options, base, noKeys(t))
|
|
require.Error(t, err)
|
|
require.NotEmpty(t, copied)
|
|
_, statErr := os.Stat(copied)
|
|
assert.ErrorIs(t, statErr, fs.ErrNotExist)
|
|
}
|
|
|
|
func TestExecute_ImportedKeys(t *testing.T) {
|
|
want := masterkey.Set{masterkey.V10: []byte("dumped")}
|
|
keys := keysFromFile(map[browser.UserDataDirID]masterkey.Set{"chrome": want})
|
|
got, err := keys(context.Background(), browser.UserDataDir{ID: "chrome"}, snapshot.Snapshot{})
|
|
require.NoError(t, err)
|
|
assert.Equal(t, want, got)
|
|
|
|
got, err = keys(context.Background(), browser.UserDataDir{ID: "edge"}, snapshot.Snapshot{})
|
|
require.NoError(t, err)
|
|
assert.Empty(t, got, "a dump without the dir's keys decrypts nothing")
|
|
}
|