mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-06-04 19:48:01 +02:00
refactor(browser): split installation and profile abstractions (#603)
* refactor(browser): split installation and profile abstractions A Chromium installation shares one master key across its profiles, but modeling each profile as its own Browser re-derived the key per profile. Browser now represents one installation holding its profiles and derives the key once; new types.Profile/ExtractResult/CountResult carry per-profile results. * style: gofumpt safari_test.go * test(chromium): rename shadowed loop var to path
This commit is contained in:
+83
-209
@@ -13,66 +13,89 @@ import (
|
||||
"github.com/moond4rk/hackbrowserdata/utils/fileutil"
|
||||
)
|
||||
|
||||
// Browser represents a single Chromium profile ready for extraction.
|
||||
// Browser is one Chromium installation: a single UserDataDir holding profiles
|
||||
// that share a master key. The key is derived once and reused across profiles.
|
||||
type Browser struct {
|
||||
cfg types.BrowserConfig
|
||||
profileDir string // absolute path to profile directory
|
||||
retrievers keyretriever.Retrievers // per-tier key sources (V10 / V11 / V20; unused tiers nil)
|
||||
sources map[types.Category][]sourcePath // Category → candidate paths (priority order)
|
||||
extractors map[types.Category]categoryExtractor // Category → custom extract function override
|
||||
sourcePaths map[types.Category]resolvedPath // Category → discovered absolute path
|
||||
cfg types.BrowserConfig
|
||||
retrievers keyretriever.Retrievers
|
||||
profiles []*profile
|
||||
|
||||
keysOnce sync.Once
|
||||
keys keyretriever.MasterKeys
|
||||
}
|
||||
|
||||
// NewBrowsers discovers Chromium profiles under cfg.UserDataDir and returns
|
||||
// one Browser per profile. Call SetKeyRetrievers on each returned browser before
|
||||
// Extract to enable decryption of sensitive data (passwords, cookies, etc.).
|
||||
func NewBrowsers(cfg types.BrowserConfig) ([]*Browser, error) {
|
||||
// NewBrowser discovers the Chromium profiles under cfg.UserDataDir and returns
|
||||
// the installation, or nil if no profile with resolvable sources exists. Call
|
||||
// SetKeyRetrievers before Extract to enable decryption of sensitive data.
|
||||
func NewBrowser(cfg types.BrowserConfig) (*Browser, error) {
|
||||
sources := sourcesForKind(cfg.Kind)
|
||||
extractors := extractorsForKind(cfg.Kind)
|
||||
|
||||
profileDirs := discoverProfiles(cfg.UserDataDir, sources)
|
||||
if len(profileDirs) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var browsers []*Browser
|
||||
for _, profileDir := range profileDirs {
|
||||
var profiles []*profile
|
||||
for _, profileDir := range discoverProfiles(cfg.UserDataDir, sources) {
|
||||
sourcePaths := resolveSourcePaths(sources, profileDir)
|
||||
if len(sourcePaths) == 0 {
|
||||
continue
|
||||
}
|
||||
browsers = append(browsers, &Browser{
|
||||
cfg: cfg,
|
||||
profiles = append(profiles, &profile{
|
||||
profileDir: profileDir,
|
||||
sources: sources,
|
||||
browserName: cfg.Name,
|
||||
kind: cfg.Kind,
|
||||
extractors: extractors,
|
||||
sourcePaths: sourcePaths,
|
||||
})
|
||||
}
|
||||
return browsers, nil
|
||||
if len(profiles) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return &Browser{cfg: cfg, profiles: profiles}, nil
|
||||
}
|
||||
|
||||
// SetKeyRetrievers wires the per-tier master-key retrievers (V10/V11/V20) used by Extract; unused tiers stay nil.
|
||||
func (b *Browser) SetKeyRetrievers(r keyretriever.Retrievers) {
|
||||
b.retrievers = r
|
||||
}
|
||||
// SetKeyRetrievers wires the per-tier master-key retrievers (V10/V11/V20) used by
|
||||
// Extract; unused tiers stay nil.
|
||||
func (b *Browser) SetKeyRetrievers(r keyretriever.Retrievers) { b.retrievers = r }
|
||||
|
||||
func (b *Browser) BrowserName() string { return b.cfg.Name }
|
||||
func (b *Browser) ProfileDir() string { return b.profileDir }
|
||||
func (b *Browser) UserDataDir() string { return b.cfg.UserDataDir }
|
||||
func (b *Browser) ProfileName() string {
|
||||
if b.profileDir == "" {
|
||||
return ""
|
||||
|
||||
// Profiles returns the identity of every profile in this installation.
|
||||
func (b *Browser) Profiles() []types.Profile {
|
||||
out := make([]types.Profile, 0, len(b.profiles))
|
||||
for _, p := range b.profiles {
|
||||
out = append(out, types.Profile{Name: p.name(), Dir: p.profileDir})
|
||||
}
|
||||
return filepath.Base(b.profileDir)
|
||||
return out
|
||||
}
|
||||
|
||||
// ExportKeys derives this profile's master keys without performing extraction.
|
||||
// Returns whatever tiers succeeded plus a joined error describing any failed
|
||||
// tiers; callers preserve partial results because a Chrome 127+ profile mixes
|
||||
// Extract derives the installation's master key once, then extracts every profile.
|
||||
func (b *Browser) Extract(categories []types.Category) ([]types.ExtractResult, error) {
|
||||
keys := b.masterKeys()
|
||||
results := make([]types.ExtractResult, 0, len(b.profiles))
|
||||
for _, p := range b.profiles {
|
||||
results = append(results, types.ExtractResult{
|
||||
Profile: types.Profile{Name: p.name(), Dir: p.profileDir},
|
||||
Data: p.extract(keys, categories),
|
||||
})
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// CountEntries counts entries per category for every profile without decryption.
|
||||
func (b *Browser) CountEntries(categories []types.Category) ([]types.CountResult, error) {
|
||||
results := make([]types.CountResult, 0, len(b.profiles))
|
||||
for _, p := range b.profiles {
|
||||
results = append(results, types.CountResult{
|
||||
Profile: types.Profile{Name: p.name(), Dir: p.profileDir},
|
||||
Counts: p.count(categories),
|
||||
})
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// ExportKeys derives the installation's master keys without extraction. Returns
|
||||
// whatever tiers succeeded plus a joined error describing any failed tiers;
|
||||
// callers preserve partial results because a Chrome 127+ installation mixes
|
||||
// v10 + v20 ciphertexts and a v20-only failure must not erase a usable v10 key.
|
||||
// Used by cross-host workflows where keys are produced on one host and consumed
|
||||
// on another.
|
||||
func (b *Browser) ExportKeys() (keyretriever.MasterKeys, error) {
|
||||
session, err := filemanager.NewSession()
|
||||
if err != nil {
|
||||
@@ -83,25 +106,34 @@ func (b *Browser) ExportKeys() (keyretriever.MasterKeys, error) {
|
||||
return keyretriever.NewMasterKeys(b.retrievers, b.buildHints(session))
|
||||
}
|
||||
|
||||
// buildHints discovers Local State (acquiring it into session.TempDir so Windows DPAPI/ABE retrievers can
|
||||
// read it from a path the process owns) and assembles per-tier retriever hints. Shared by Extract and
|
||||
// ExportKeys so the two stay in lockstep. Multi-profile layout: Local State lives in the parent of
|
||||
// profileDir. Flat layout (Opera): Local State sits alongside data files inside profileDir.
|
||||
func (b *Browser) buildHints(session *filemanager.Session) keyretriever.Hints {
|
||||
label := b.BrowserName() + "/" + b.ProfileName()
|
||||
var localStateDst string
|
||||
for _, dir := range []string{filepath.Dir(b.profileDir), b.profileDir} {
|
||||
candidate := filepath.Join(dir, "Local State")
|
||||
if !fileutil.FileExists(candidate) {
|
||||
continue
|
||||
// masterKeys derives the installation's keys exactly once and caches them.
|
||||
// Because derivation happens a single time per installation, a failure is warned
|
||||
// exactly once — no cross-profile dedup state is needed.
|
||||
func (b *Browser) masterKeys() keyretriever.MasterKeys {
|
||||
b.keysOnce.Do(func() {
|
||||
keys, err := b.ExportKeys()
|
||||
if err != nil {
|
||||
log.Warnf("%s: master key retrieval: %v", b.BrowserName(), err)
|
||||
}
|
||||
b.keys = keys
|
||||
})
|
||||
return b.keys
|
||||
}
|
||||
|
||||
// buildHints acquires Local State (into session.TempDir so Windows DPAPI/ABE
|
||||
// retrievers can read it from a path the process owns) and assembles per-tier
|
||||
// retriever hints. Local State lives at the installation root (cfg.UserDataDir)
|
||||
// in both the multi-profile and flat (Opera) layouts.
|
||||
func (b *Browser) buildHints(session *filemanager.Session) keyretriever.Hints {
|
||||
var localStateDst string
|
||||
candidate := filepath.Join(b.cfg.UserDataDir, "Local State")
|
||||
if fileutil.FileExists(candidate) {
|
||||
dst := filepath.Join(session.TempDir(), "Local State")
|
||||
if err := session.Acquire(candidate, dst, false); err != nil {
|
||||
log.Debugf("acquire Local State for %s: %v", label, err)
|
||||
break
|
||||
log.Debugf("acquire Local State for %s: %v", b.BrowserName(), err)
|
||||
} else {
|
||||
localStateDst = dst
|
||||
}
|
||||
localStateDst = dst
|
||||
break
|
||||
}
|
||||
|
||||
abeKey := ""
|
||||
@@ -115,164 +147,6 @@ func (b *Browser) buildHints(session *filemanager.Session) keyretriever.Hints {
|
||||
}
|
||||
}
|
||||
|
||||
// Extract copies browser files to a temp directory, retrieves the master key,
|
||||
// and extracts data for the requested categories.
|
||||
func (b *Browser) Extract(categories []types.Category) (*types.BrowserData, error) {
|
||||
session, err := filemanager.NewSession()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer session.Cleanup()
|
||||
|
||||
tempPaths := b.acquireFiles(session, categories)
|
||||
|
||||
keys := b.getMasterKeys(session)
|
||||
|
||||
data := &types.BrowserData{}
|
||||
for _, cat := range categories {
|
||||
path, ok := tempPaths[cat]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
b.extractCategory(data, cat, keys, path)
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// CountEntries copies browser files to a temp directory and counts entries
|
||||
// per category without decryption. Much faster than Extract for display-only
|
||||
// use cases like "list --detail".
|
||||
func (b *Browser) CountEntries(categories []types.Category) (map[types.Category]int, error) {
|
||||
session, err := filemanager.NewSession()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer session.Cleanup()
|
||||
|
||||
tempPaths := b.acquireFiles(session, categories)
|
||||
|
||||
counts := make(map[types.Category]int)
|
||||
for _, cat := range categories {
|
||||
path, ok := tempPaths[cat]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
counts[cat] = b.countCategory(cat, path)
|
||||
}
|
||||
return counts, nil
|
||||
}
|
||||
|
||||
// countCategory calls the appropriate count function for a category.
|
||||
func (b *Browser) countCategory(cat types.Category, path string) int {
|
||||
var count int
|
||||
var err error
|
||||
switch cat {
|
||||
case types.Password:
|
||||
count, err = countPasswords(path)
|
||||
case types.Cookie:
|
||||
count, err = countCookies(path)
|
||||
case types.History:
|
||||
count, err = countHistories(path)
|
||||
case types.Download:
|
||||
count, err = countDownloads(path)
|
||||
case types.Bookmark:
|
||||
count, err = countBookmarks(path)
|
||||
case types.CreditCard:
|
||||
if b.cfg.Kind == types.ChromiumYandex {
|
||||
count, err = countYandexCreditCards(path)
|
||||
} else {
|
||||
count, err = countCreditCards(path)
|
||||
}
|
||||
case types.Extension:
|
||||
if b.cfg.Kind == types.ChromiumOpera {
|
||||
count, err = countOperaExtensions(path)
|
||||
} else {
|
||||
count, err = countExtensions(path)
|
||||
}
|
||||
case types.LocalStorage:
|
||||
count, err = countLocalStorage(path)
|
||||
case types.SessionStorage:
|
||||
count, err = countSessionStorage(path)
|
||||
}
|
||||
if err != nil {
|
||||
log.Debugf("count %s for %s: %v", cat, b.BrowserName()+"/"+b.ProfileName(), err)
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// acquireFiles copies source files to the session temp directory.
|
||||
func (b *Browser) acquireFiles(session *filemanager.Session, categories []types.Category) map[types.Category]string {
|
||||
tempPaths := make(map[types.Category]string)
|
||||
for _, cat := range categories {
|
||||
rp, ok := b.sourcePaths[cat]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
dst := filepath.Join(session.TempDir(), cat.String())
|
||||
if err := session.Acquire(rp.absPath, dst, rp.isDir); err != nil {
|
||||
log.Debugf("acquire %s: %v", cat, err)
|
||||
continue
|
||||
}
|
||||
tempPaths[cat] = dst
|
||||
}
|
||||
return tempPaths
|
||||
}
|
||||
|
||||
// warnedMasterKeyFailure dedupes "master key retrieval" WARN per installation (BrowserName + UserDataDir);
|
||||
// profiles share one Safe Storage entry, but glob-expanded configs may yield multiple installations of the same browser.
|
||||
var warnedMasterKeyFailure sync.Map
|
||||
|
||||
// getMasterKeys retrieves master keys for all configured cipher tiers.
|
||||
func (b *Browser) getMasterKeys(session *filemanager.Session) keyretriever.MasterKeys {
|
||||
keys, err := keyretriever.NewMasterKeys(b.retrievers, b.buildHints(session))
|
||||
if err != nil {
|
||||
installKey := b.BrowserName() + "|" + b.cfg.UserDataDir
|
||||
if _, already := warnedMasterKeyFailure.LoadOrStore(installKey, struct{}{}); !already {
|
||||
log.Warnf("%s: master key retrieval: %v", b.BrowserName(), err)
|
||||
} else {
|
||||
log.Debugf("%s/%s: master key retrieval: %v", b.BrowserName(), b.ProfileName(), err)
|
||||
}
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
||||
// extractCategory calls the appropriate extract function for a category.
|
||||
// If a custom extractor is registered for this category (via extractorsForKind),
|
||||
// it is used instead of the default switch logic.
|
||||
func (b *Browser) extractCategory(data *types.BrowserData, cat types.Category, keys keyretriever.MasterKeys, path string) {
|
||||
if ext, ok := b.extractors[cat]; ok {
|
||||
if err := ext.extract(keys, path, data); err != nil {
|
||||
log.Debugf("extract %s for %s: %v", cat, b.BrowserName()+"/"+b.ProfileName(), err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
var err error
|
||||
switch cat {
|
||||
case types.Password:
|
||||
data.Passwords, err = extractPasswords(keys, path)
|
||||
case types.Cookie:
|
||||
data.Cookies, err = extractCookies(keys, path)
|
||||
case types.History:
|
||||
data.Histories, err = extractHistories(path)
|
||||
case types.Download:
|
||||
data.Downloads, err = extractDownloads(path)
|
||||
case types.Bookmark:
|
||||
data.Bookmarks, err = extractBookmarks(path)
|
||||
case types.CreditCard:
|
||||
data.CreditCards, err = extractCreditCards(keys, path)
|
||||
case types.Extension:
|
||||
data.Extensions, err = extractExtensions(path)
|
||||
case types.LocalStorage:
|
||||
data.LocalStorage, err = extractLocalStorage(path)
|
||||
case types.SessionStorage:
|
||||
data.SessionStorage, err = extractSessionStorage(path)
|
||||
}
|
||||
if err != nil {
|
||||
log.Debugf("extract %s for %s: %v", cat, b.BrowserName()+"/"+b.ProfileName(), err)
|
||||
}
|
||||
}
|
||||
|
||||
// discoverProfiles lists subdirectories of userDataDir that are valid
|
||||
// Chromium profile directories. A directory is considered a profile if it
|
||||
// contains a "Preferences" file, which Chromium creates for every profile.
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/moond4rk/hackbrowserdata/crypto/keyretriever"
|
||||
"github.com/moond4rk/hackbrowserdata/filemanager"
|
||||
"github.com/moond4rk/hackbrowserdata/types"
|
||||
)
|
||||
|
||||
@@ -220,19 +219,20 @@ func TestNewBrowsers(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cfg := types.BrowserConfig{Name: "Test", Kind: tt.kind, UserDataDir: tt.dir}
|
||||
browsers, err := NewBrowsers(cfg)
|
||||
b, err := NewBrowser(cfg)
|
||||
require.NoError(t, err)
|
||||
|
||||
if len(tt.wantProfiles) == 0 {
|
||||
assert.Empty(t, browsers)
|
||||
assert.Nil(t, b)
|
||||
return
|
||||
}
|
||||
require.Len(t, browsers, len(tt.wantProfiles))
|
||||
require.NotNil(t, b)
|
||||
require.Len(t, b.profiles, len(tt.wantProfiles))
|
||||
|
||||
nameMap := browsersByProfile(browsers)
|
||||
nameMap := profilesByName(b)
|
||||
assertProfiles(t, nameMap, tt.wantProfiles, tt.skipProfiles)
|
||||
assertCategories(t, nameMap, tt.wantCats)
|
||||
assertDirCategories(t, browsers, tt.wantDirs)
|
||||
assertDirCategories(t, b.profiles, tt.wantDirs)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -241,15 +241,15 @@ func TestNewBrowsers(t *testing.T) {
|
||||
// Test helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func browsersByProfile(browsers []*Browser) map[string]*Browser {
|
||||
m := make(map[string]*Browser, len(browsers))
|
||||
for _, b := range browsers {
|
||||
m[filepath.Base(b.profileDir)] = b
|
||||
func profilesByName(b *Browser) map[string]*profile {
|
||||
m := make(map[string]*profile, len(b.profiles))
|
||||
for _, p := range b.profiles {
|
||||
m[filepath.Base(p.profileDir)] = p
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func assertProfiles(t *testing.T, nameMap map[string]*Browser, want, skip []string) {
|
||||
func assertProfiles(t *testing.T, nameMap map[string]*profile, want, skip []string) {
|
||||
t.Helper()
|
||||
for _, w := range want {
|
||||
assert.Contains(t, nameMap, w, "should find profile %s", w)
|
||||
@@ -259,17 +259,17 @@ func assertProfiles(t *testing.T, nameMap map[string]*Browser, want, skip []stri
|
||||
}
|
||||
}
|
||||
|
||||
func assertCategories(t *testing.T, nameMap map[string]*Browser, wantCats map[string][]string) {
|
||||
func assertCategories(t *testing.T, nameMap map[string]*profile, wantCats map[string][]string) {
|
||||
t.Helper()
|
||||
for profileName, wantFiles := range wantCats {
|
||||
b, ok := nameMap[profileName]
|
||||
p, ok := nameMap[profileName]
|
||||
if !ok {
|
||||
t.Errorf("profile %s not found", profileName)
|
||||
continue
|
||||
}
|
||||
for _, wantFile := range wantFiles {
|
||||
found := false
|
||||
for _, rp := range b.sourcePaths {
|
||||
for _, rp := range p.sourcePaths {
|
||||
if filepath.Base(rp.absPath) == wantFile {
|
||||
found = true
|
||||
break
|
||||
@@ -280,11 +280,11 @@ func assertCategories(t *testing.T, nameMap map[string]*Browser, wantCats map[st
|
||||
}
|
||||
}
|
||||
|
||||
func assertDirCategories(t *testing.T, browsers []*Browser, cats []types.Category) {
|
||||
func assertDirCategories(t *testing.T, profiles []*profile, cats []types.Category) {
|
||||
t.Helper()
|
||||
for _, cat := range cats {
|
||||
for _, b := range browsers {
|
||||
if rp, ok := b.sourcePaths[cat]; ok {
|
||||
for _, p := range profiles {
|
||||
if rp, ok := p.sourcePaths[cat]; ok {
|
||||
assert.True(t, rp.isDir, "%s should be isDir=true", cat)
|
||||
}
|
||||
}
|
||||
@@ -345,74 +345,6 @@ func TestExtractorsForKind(t *testing.T) {
|
||||
assert.Contains(t, operaExt, types.Extension)
|
||||
}
|
||||
|
||||
// TestExtractCategory_CustomExtractor verifies that extractCategory dispatches
|
||||
// through a registered extractor instead of the default switch logic.
|
||||
func TestExtractCategory_CustomExtractor(t *testing.T) {
|
||||
// Create a Browser with a custom extractor that records it was called
|
||||
called := false
|
||||
testExtractor := extensionExtractor{
|
||||
fn: func(path string) ([]types.ExtensionEntry, error) {
|
||||
called = true
|
||||
return []types.ExtensionEntry{{Name: "custom", ID: "test-id"}}, nil
|
||||
},
|
||||
}
|
||||
|
||||
b := &Browser{
|
||||
extractors: map[types.Category]categoryExtractor{
|
||||
types.Extension: testExtractor,
|
||||
},
|
||||
}
|
||||
|
||||
data := &types.BrowserData{}
|
||||
b.extractCategory(data, types.Extension, keyretriever.MasterKeys{}, "unused-path")
|
||||
|
||||
assert.True(t, called, "custom extractor should be called")
|
||||
require.Len(t, data.Extensions, 1)
|
||||
assert.Equal(t, "custom", data.Extensions[0].Name)
|
||||
}
|
||||
|
||||
// TestExtractCategory_DefaultFallback verifies that extractCategory uses
|
||||
// the default switch when no extractor is registered.
|
||||
func TestExtractCategory_DefaultFallback(t *testing.T) {
|
||||
path := createTestDB(t, "History", urlsSchema,
|
||||
insertURL("https://example.com", "Example", 3, 13350000000000000),
|
||||
)
|
||||
|
||||
b := &Browser{
|
||||
extractors: nil, // no custom extractors
|
||||
}
|
||||
|
||||
data := &types.BrowserData{}
|
||||
b.extractCategory(data, types.History, keyretriever.MasterKeys{}, path)
|
||||
|
||||
require.Len(t, data.Histories, 1)
|
||||
assert.Equal(t, "Example", data.Histories[0].Title)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// acquireFiles
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestAcquireFiles(t *testing.T) {
|
||||
profileDir := filepath.Join(fixture.chrome, "Default")
|
||||
resolved := resolveSourcePaths(chromiumSources, profileDir)
|
||||
|
||||
b := &Browser{profileDir: profileDir, sources: chromiumSources, sourcePaths: resolved}
|
||||
|
||||
session, err := filemanager.NewSession()
|
||||
require.NoError(t, err)
|
||||
defer session.Cleanup()
|
||||
|
||||
cats := []types.Category{types.History, types.Cookie, types.Bookmark}
|
||||
paths := b.acquireFiles(session, cats)
|
||||
|
||||
assert.Len(t, paths, len(cats))
|
||||
for _, p := range paths {
|
||||
_, err := os.Stat(p)
|
||||
require.NoError(t, err, "acquired file should exist")
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Local State path validation
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -428,12 +360,12 @@ func TestLocalStatePath(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
browsers, err := NewBrowsers(types.BrowserConfig{Name: "Test", Kind: types.Chromium, UserDataDir: tt.dir})
|
||||
b, err := NewBrowser(types.BrowserConfig{Name: "Test", Kind: types.Chromium, UserDataDir: tt.dir})
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, browsers)
|
||||
require.NotNil(t, b)
|
||||
|
||||
for _, b := range browsers {
|
||||
localState := filepath.Join(filepath.Dir(b.profileDir), "Local State")
|
||||
for _, p := range b.profiles {
|
||||
localState := filepath.Join(filepath.Dir(p.profileDir), "Local State")
|
||||
if tt.want {
|
||||
assert.FileExists(t, localState)
|
||||
}
|
||||
@@ -503,22 +435,17 @@ func TestGetMasterKeys(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
browsers, err := NewBrowsers(types.BrowserConfig{
|
||||
b, err := NewBrowser(types.BrowserConfig{
|
||||
Name: "Test", Kind: types.Chromium, UserDataDir: tt.dir, KeychainLabel: tt.keychainLabel,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, browsers)
|
||||
require.NotNil(t, b)
|
||||
|
||||
b := browsers[0]
|
||||
if tt.retriever != nil {
|
||||
b.SetKeyRetrievers(keyretriever.Retrievers{V10: tt.retriever})
|
||||
}
|
||||
|
||||
session, err := filemanager.NewSession()
|
||||
require.NoError(t, err)
|
||||
defer session.Cleanup()
|
||||
|
||||
keys := b.getMasterKeys(session)
|
||||
keys := b.masterKeys()
|
||||
assert.Equal(t, tt.wantV10, keys.V10)
|
||||
assert.Nil(t, keys.V11, "V11 stays nil when no v11 retriever is wired")
|
||||
assert.Nil(t, keys.V20, "V20 stays nil when no v20 retriever is wired")
|
||||
@@ -550,20 +477,15 @@ func TestGetMasterKeys_AllTiersInvoked(t *testing.T) {
|
||||
v11mock := &mockRetriever{key: []byte("fake-v11-key")}
|
||||
v20mock := &mockRetriever{key: []byte("fake-v20-key")}
|
||||
|
||||
browsers, err := NewBrowsers(types.BrowserConfig{
|
||||
b, err := NewBrowser(types.BrowserConfig{
|
||||
Name: "Test", Kind: types.Chromium, UserDataDir: fixture.chrome, KeychainLabel: "Chrome",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, browsers)
|
||||
require.NotNil(t, b)
|
||||
|
||||
b := browsers[0]
|
||||
b.SetKeyRetrievers(keyretriever.Retrievers{V10: v10mock, V11: v11mock, V20: v20mock})
|
||||
|
||||
session, err := filemanager.NewSession()
|
||||
require.NoError(t, err)
|
||||
defer session.Cleanup()
|
||||
|
||||
keys := b.getMasterKeys(session)
|
||||
keys := b.masterKeys()
|
||||
assert.Equal(t, []byte("fake-v10-key"), keys.V10, "V10 slot must be populated")
|
||||
assert.Equal(t, []byte("fake-v11-key"), keys.V11, "V11 slot must be populated")
|
||||
assert.Equal(t, []byte("fake-v20-key"), keys.V20, "V20 slot must be populated")
|
||||
@@ -592,21 +514,16 @@ func TestGetMasterKeys_WindowsABEThreading(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
mock := &mockRetriever{key: []byte("k")}
|
||||
browsers, err := NewBrowsers(types.BrowserConfig{
|
||||
b, err := NewBrowser(types.BrowserConfig{
|
||||
Key: tt.key, Name: "Test", Kind: types.Chromium,
|
||||
UserDataDir: fixture.chrome, WindowsABE: tt.windowsABE,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, browsers)
|
||||
require.NotNil(t, b)
|
||||
|
||||
b := browsers[0]
|
||||
b.SetKeyRetrievers(keyretriever.Retrievers{V20: mock})
|
||||
|
||||
session, err := filemanager.NewSession()
|
||||
require.NoError(t, err)
|
||||
defer session.Cleanup()
|
||||
|
||||
b.getMasterKeys(session)
|
||||
b.masterKeys()
|
||||
assert.Equal(t, tt.wantABEKey, mock.hints.WindowsABEKey)
|
||||
})
|
||||
}
|
||||
@@ -638,22 +555,23 @@ func TestExtract(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
browsers, err := NewBrowsers(types.BrowserConfig{
|
||||
b, err := NewBrowser(types.BrowserConfig{
|
||||
Name: "Test", Kind: types.Chromium, UserDataDir: dir, KeychainLabel: "Chrome",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, browsers, 1)
|
||||
require.NotNil(t, b)
|
||||
|
||||
if tt.retriever != nil {
|
||||
browsers[0].SetKeyRetrievers(keyretriever.Retrievers{V10: tt.retriever})
|
||||
b.SetKeyRetrievers(keyretriever.Retrievers{V10: tt.retriever})
|
||||
}
|
||||
|
||||
result, err := browsers[0].Extract([]types.Category{types.History})
|
||||
results, err := b.Extract([]types.Category{types.History})
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
require.Len(t, result.Histories, 3)
|
||||
require.Len(t, results, 1)
|
||||
require.NotNil(t, results[0].Data)
|
||||
require.Len(t, results[0].Data.Histories, 3)
|
||||
// setupHistoryDB: Example(200) > GitHub(100) > Go Dev(50)
|
||||
assert.Equal(t, "Example", result.Histories[0].Title)
|
||||
assert.Equal(t, "Example", results[0].Data.Histories[0].Title)
|
||||
|
||||
if tt.wantRetriever {
|
||||
mock, ok := tt.retriever.(*mockRetriever)
|
||||
@@ -673,21 +591,22 @@ func TestCountEntries(t *testing.T) {
|
||||
mkFile(dir, "Default", "Preferences")
|
||||
installFile(t, filepath.Join(dir, "Default"), setupHistoryDB(t), "History")
|
||||
|
||||
browsers, err := NewBrowsers(types.BrowserConfig{
|
||||
b, err := NewBrowser(types.BrowserConfig{
|
||||
Name: "Test", Kind: types.Chromium, UserDataDir: dir,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, browsers, 1)
|
||||
require.NotNil(t, b)
|
||||
|
||||
// No retriever set — CountEntries should still work (no decryption needed).
|
||||
counts, err := browsers[0].CountEntries([]types.Category{types.History, types.Download})
|
||||
results, err := b.CountEntries([]types.Category{types.History, types.Download})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, results, 1)
|
||||
|
||||
assert.Equal(t, 3, counts[types.History])
|
||||
assert.Equal(t, 3, results[0].Counts[types.History])
|
||||
// Download uses a different table in the same file; since we only
|
||||
// created the urls table (not downloads), the count query will fail
|
||||
// gracefully and return 0.
|
||||
assert.Equal(t, 0, counts[types.Download])
|
||||
assert.Equal(t, 0, results[0].Counts[types.Download])
|
||||
}
|
||||
|
||||
func TestCountEntries_NoRetrieverNeeded(t *testing.T) {
|
||||
@@ -696,53 +615,17 @@ func TestCountEntries_NoRetrieverNeeded(t *testing.T) {
|
||||
// Login Data normally needs master key to extract, but CountEntries skips decryption.
|
||||
installFile(t, filepath.Join(dir, "Default"), setupLoginDB(t), "Login Data")
|
||||
|
||||
browsers, err := NewBrowsers(types.BrowserConfig{
|
||||
b, err := NewBrowser(types.BrowserConfig{
|
||||
Name: "Test", Kind: types.Chromium, UserDataDir: dir,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, browsers, 1)
|
||||
require.NotNil(t, b)
|
||||
|
||||
// No retriever set — CountEntries succeeds without master key.
|
||||
counts, err := browsers[0].CountEntries([]types.Category{types.Password})
|
||||
results, err := b.CountEntries([]types.Category{types.Password})
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 2, counts[types.Password])
|
||||
}
|
||||
|
||||
func TestCountCategory(t *testing.T) {
|
||||
t.Run("History", func(t *testing.T) {
|
||||
path := setupHistoryDB(t)
|
||||
b := &Browser{cfg: types.BrowserConfig{Kind: types.Chromium}}
|
||||
assert.Equal(t, 3, b.countCategory(types.History, path))
|
||||
})
|
||||
|
||||
t.Run("Cookie", func(t *testing.T) {
|
||||
path := setupCookieDB(t)
|
||||
b := &Browser{cfg: types.BrowserConfig{Kind: types.Chromium}}
|
||||
assert.Equal(t, 2, b.countCategory(types.Cookie, path))
|
||||
})
|
||||
|
||||
t.Run("Bookmark", func(t *testing.T) {
|
||||
path := setupBookmarkJSON(t)
|
||||
b := &Browser{cfg: types.BrowserConfig{Kind: types.Chromium}}
|
||||
assert.Equal(t, 3, b.countCategory(types.Bookmark, path))
|
||||
})
|
||||
|
||||
t.Run("Extension_Opera", func(t *testing.T) {
|
||||
path := createTestJSON(t, "Secure Preferences", `{
|
||||
"extensions": {
|
||||
"opsettings": {
|
||||
"ext1": {"location": 1, "manifest": {"name": "Ext", "version": "1.0"}}
|
||||
}
|
||||
}
|
||||
}`)
|
||||
b := &Browser{cfg: types.BrowserConfig{Kind: types.ChromiumOpera}}
|
||||
assert.Equal(t, 1, b.countCategory(types.Extension, path))
|
||||
})
|
||||
|
||||
t.Run("FileNotFound", func(t *testing.T) {
|
||||
b := &Browser{cfg: types.BrowserConfig{Kind: types.Chromium}}
|
||||
assert.Equal(t, 0, b.countCategory(types.History, "/nonexistent/path"))
|
||||
})
|
||||
require.Len(t, results, 1)
|
||||
assert.Equal(t, 2, results[0].Counts[types.Password])
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
package chromium
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/moond4rk/hackbrowserdata/crypto/keyretriever"
|
||||
"github.com/moond4rk/hackbrowserdata/filemanager"
|
||||
"github.com/moond4rk/hackbrowserdata/log"
|
||||
"github.com/moond4rk/hackbrowserdata/types"
|
||||
)
|
||||
|
||||
// profile is one Chromium profile under an installation — the leaf extraction
|
||||
// unit. It reads its own source files but reuses the installation's master keys.
|
||||
type profile struct {
|
||||
profileDir string
|
||||
browserName string
|
||||
kind types.BrowserKind
|
||||
extractors map[types.Category]categoryExtractor
|
||||
sourcePaths map[types.Category]resolvedPath
|
||||
}
|
||||
|
||||
func (p *profile) name() string {
|
||||
if p.profileDir == "" {
|
||||
return ""
|
||||
}
|
||||
return filepath.Base(p.profileDir)
|
||||
}
|
||||
|
||||
func (p *profile) label() string { return p.browserName + "/" + p.name() }
|
||||
|
||||
// extract copies the profile's source files to a temp directory and extracts the
|
||||
// requested categories, decrypting with the installation's master keys.
|
||||
func (p *profile) extract(keys keyretriever.MasterKeys, categories []types.Category) *types.BrowserData {
|
||||
session, err := filemanager.NewSession()
|
||||
if err != nil {
|
||||
log.Debugf("new session for %s: %v", p.label(), err)
|
||||
return &types.BrowserData{}
|
||||
}
|
||||
defer session.Cleanup()
|
||||
|
||||
tempPaths := p.acquireFiles(session, categories)
|
||||
data := &types.BrowserData{}
|
||||
for _, cat := range categories {
|
||||
path, ok := tempPaths[cat]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
p.extractCategory(data, cat, keys, path)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
// count counts entries per category without decryption.
|
||||
func (p *profile) count(categories []types.Category) map[types.Category]int {
|
||||
session, err := filemanager.NewSession()
|
||||
if err != nil {
|
||||
log.Debugf("new session for %s: %v", p.label(), err)
|
||||
return nil
|
||||
}
|
||||
defer session.Cleanup()
|
||||
|
||||
tempPaths := p.acquireFiles(session, categories)
|
||||
counts := make(map[types.Category]int)
|
||||
for _, cat := range categories {
|
||||
path, ok := tempPaths[cat]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
counts[cat] = p.countCategory(cat, path)
|
||||
}
|
||||
return counts
|
||||
}
|
||||
|
||||
// acquireFiles copies source files to the session temp directory.
|
||||
func (p *profile) acquireFiles(session *filemanager.Session, categories []types.Category) map[types.Category]string {
|
||||
tempPaths := make(map[types.Category]string)
|
||||
for _, cat := range categories {
|
||||
rp, ok := p.sourcePaths[cat]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
dst := filepath.Join(session.TempDir(), cat.String())
|
||||
if err := session.Acquire(rp.absPath, dst, rp.isDir); err != nil {
|
||||
log.Debugf("acquire %s: %v", cat, err)
|
||||
continue
|
||||
}
|
||||
tempPaths[cat] = dst
|
||||
}
|
||||
return tempPaths
|
||||
}
|
||||
|
||||
// extractCategory calls the appropriate extract function for a category. A custom
|
||||
// extractor (registered via extractorsForKind) takes precedence over the switch.
|
||||
func (p *profile) extractCategory(data *types.BrowserData, cat types.Category, keys keyretriever.MasterKeys, path string) {
|
||||
if ext, ok := p.extractors[cat]; ok {
|
||||
if err := ext.extract(keys, path, data); err != nil {
|
||||
log.Debugf("extract %s for %s: %v", cat, p.label(), err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
var err error
|
||||
switch cat {
|
||||
case types.Password:
|
||||
data.Passwords, err = extractPasswords(keys, path)
|
||||
case types.Cookie:
|
||||
data.Cookies, err = extractCookies(keys, path)
|
||||
case types.History:
|
||||
data.Histories, err = extractHistories(path)
|
||||
case types.Download:
|
||||
data.Downloads, err = extractDownloads(path)
|
||||
case types.Bookmark:
|
||||
data.Bookmarks, err = extractBookmarks(path)
|
||||
case types.CreditCard:
|
||||
data.CreditCards, err = extractCreditCards(keys, path)
|
||||
case types.Extension:
|
||||
data.Extensions, err = extractExtensions(path)
|
||||
case types.LocalStorage:
|
||||
data.LocalStorage, err = extractLocalStorage(path)
|
||||
case types.SessionStorage:
|
||||
data.SessionStorage, err = extractSessionStorage(path)
|
||||
}
|
||||
if err != nil {
|
||||
log.Debugf("extract %s for %s: %v", cat, p.label(), err)
|
||||
}
|
||||
}
|
||||
|
||||
// countCategory calls the appropriate count function for a category.
|
||||
func (p *profile) countCategory(cat types.Category, path string) int {
|
||||
var count int
|
||||
var err error
|
||||
switch cat {
|
||||
case types.Password:
|
||||
count, err = countPasswords(path)
|
||||
case types.Cookie:
|
||||
count, err = countCookies(path)
|
||||
case types.History:
|
||||
count, err = countHistories(path)
|
||||
case types.Download:
|
||||
count, err = countDownloads(path)
|
||||
case types.Bookmark:
|
||||
count, err = countBookmarks(path)
|
||||
case types.CreditCard:
|
||||
if p.kind == types.ChromiumYandex {
|
||||
count, err = countYandexCreditCards(path)
|
||||
} else {
|
||||
count, err = countCreditCards(path)
|
||||
}
|
||||
case types.Extension:
|
||||
if p.kind == types.ChromiumOpera {
|
||||
count, err = countOperaExtensions(path)
|
||||
} else {
|
||||
count, err = countExtensions(path)
|
||||
}
|
||||
case types.LocalStorage:
|
||||
count, err = countLocalStorage(path)
|
||||
case types.SessionStorage:
|
||||
count, err = countSessionStorage(path)
|
||||
}
|
||||
if err != nil {
|
||||
log.Debugf("count %s for %s: %v", cat, p.label(), err)
|
||||
}
|
||||
return count
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package chromium
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/moond4rk/hackbrowserdata/crypto/keyretriever"
|
||||
"github.com/moond4rk/hackbrowserdata/filemanager"
|
||||
"github.com/moond4rk/hackbrowserdata/types"
|
||||
)
|
||||
|
||||
// TestExtractCategory_CustomExtractor verifies that extractCategory dispatches
|
||||
// through a registered extractor instead of the default switch logic.
|
||||
func TestExtractCategory_CustomExtractor(t *testing.T) {
|
||||
// Create a profile with a custom extractor that records it was called
|
||||
called := false
|
||||
testExtractor := extensionExtractor{
|
||||
fn: func(path string) ([]types.ExtensionEntry, error) {
|
||||
called = true
|
||||
return []types.ExtensionEntry{{Name: "custom", ID: "test-id"}}, nil
|
||||
},
|
||||
}
|
||||
|
||||
p := &profile{
|
||||
extractors: map[types.Category]categoryExtractor{
|
||||
types.Extension: testExtractor,
|
||||
},
|
||||
}
|
||||
|
||||
data := &types.BrowserData{}
|
||||
p.extractCategory(data, types.Extension, keyretriever.MasterKeys{}, "unused-path")
|
||||
|
||||
assert.True(t, called, "custom extractor should be called")
|
||||
require.Len(t, data.Extensions, 1)
|
||||
assert.Equal(t, "custom", data.Extensions[0].Name)
|
||||
}
|
||||
|
||||
// TestExtractCategory_DefaultFallback verifies that extractCategory uses
|
||||
// the default switch when no extractor is registered.
|
||||
func TestExtractCategory_DefaultFallback(t *testing.T) {
|
||||
path := createTestDB(t, "History", urlsSchema,
|
||||
insertURL("https://example.com", "Example", 3, 13350000000000000),
|
||||
)
|
||||
|
||||
p := &profile{
|
||||
extractors: nil, // no custom extractors
|
||||
}
|
||||
|
||||
data := &types.BrowserData{}
|
||||
p.extractCategory(data, types.History, keyretriever.MasterKeys{}, path)
|
||||
|
||||
require.Len(t, data.Histories, 1)
|
||||
assert.Equal(t, "Example", data.Histories[0].Title)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// acquireFiles
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestAcquireFiles(t *testing.T) {
|
||||
profileDir := filepath.Join(fixture.chrome, "Default")
|
||||
resolved := resolveSourcePaths(chromiumSources, profileDir)
|
||||
|
||||
p := &profile{profileDir: profileDir, sourcePaths: resolved}
|
||||
|
||||
session, err := filemanager.NewSession()
|
||||
require.NoError(t, err)
|
||||
defer session.Cleanup()
|
||||
|
||||
cats := []types.Category{types.History, types.Cookie, types.Bookmark}
|
||||
paths := p.acquireFiles(session, cats)
|
||||
|
||||
assert.Len(t, paths, len(cats))
|
||||
for _, path := range paths {
|
||||
_, err := os.Stat(path)
|
||||
require.NoError(t, err, "acquired file should exist")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCountCategory(t *testing.T) {
|
||||
t.Run("History", func(t *testing.T) {
|
||||
path := setupHistoryDB(t)
|
||||
p := &profile{kind: types.Chromium}
|
||||
assert.Equal(t, 3, p.countCategory(types.History, path))
|
||||
})
|
||||
|
||||
t.Run("Cookie", func(t *testing.T) {
|
||||
path := setupCookieDB(t)
|
||||
p := &profile{kind: types.Chromium}
|
||||
assert.Equal(t, 2, p.countCategory(types.Cookie, path))
|
||||
})
|
||||
|
||||
t.Run("Bookmark", func(t *testing.T) {
|
||||
path := setupBookmarkJSON(t)
|
||||
p := &profile{kind: types.Chromium}
|
||||
assert.Equal(t, 3, p.countCategory(types.Bookmark, path))
|
||||
})
|
||||
|
||||
t.Run("Extension_Opera", func(t *testing.T) {
|
||||
path := createTestJSON(t, "Secure Preferences", `{
|
||||
"extensions": {
|
||||
"opsettings": {
|
||||
"ext1": {"location": 1, "manifest": {"name": "Ext", "version": "1.0"}}
|
||||
}
|
||||
}
|
||||
}`)
|
||||
p := &profile{kind: types.ChromiumOpera}
|
||||
assert.Equal(t, 1, p.countCategory(types.Extension, path))
|
||||
})
|
||||
|
||||
t.Run("FileNotFound", func(t *testing.T) {
|
||||
p := &profile{kind: types.Chromium}
|
||||
assert.Equal(t, 0, p.countCategory(types.History, "/nonexistent/path"))
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user