mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-05-19 18:58:03 +02:00
1a3aea553e
* feat: add Firefox Browser implementation with new v2 architecture Add Firefox NewBrowsers + Extract pipeline following the Chromium v2 pattern. Firefox-specific differences handled: - Profile discovery: random directory names (e.g. abc123.default-release) - Master key: NSS/ASN1PBE from key4.db (platform-agnostic, no DPAPI/Keychain) - Key validation: reuse logins.json from acquireFiles tempPaths - Extract: only Password needs masterKey; Cookie is plaintext - No CreditCard or SessionStorage support Files: - firefox_new.go: Browser struct, NewBrowsers, Extract, getMasterKey, extractCategory, deriveKeys, validateKeyWithLogins, profile discovery - masterkey.go: extracted shared NSS logic (processMasterKey, queryMetaData, queryNssPrivateCandidates, parseLoginCipherPairs, canDecryptAnyLoginCipherPair) - firefox_new_test.go: table-driven tests with shared fixtures - source.go: remove dataSource wrapper, use []sourcePath directly - firefox.go: remove functions moved to masterkey.go * fix: address Copilot review feedback on Firefox v2 - Fix stale comment referencing removed readLoginCipherPairs - Rename finallyKey to derivedKey for clarity in processMasterKey - Add sqlite driver import to masterkey.go for self-containedness * refactor: rewrite Firefox masterkey and improve naming Masterkey rewrite: - Replace raw SQL functions with structured key4DB type (globalSalt, passwordCheck, privateKeys) for clear data modeling - Split processMasterKey into verifyPasswordCheck + decryptPrivateKey - Add nssKeyTypeTag constant for the magic bytes - Rename finallyKey to derivedKey - Add sqlite driver import for self-containedness - Return error (not fallback) when logins validation explicitly fails Naming cleanup: - loginPair → encryptedLogin (clarify these are encrypted blobs) - parseLoginPairs → sampleEncryptedLogins (clarify sampling purpose) - canDecryptLogin → tryDecryptLogins (accurate verb, plural alignment) - Expand abbreviated variables: p→login, uPBE→userPBE, pPBE→pwdPBE Password extraction: - Keep entries when decryptPBE fails (URL preserved, user/pwd empty) - Align with Chromium behavior where decrypt failure doesn't skip records Old code cleanup: - firefox.go GetMasterKey now delegates to retrieveMasterKey - Remove functions moved to masterkey.go * docs: add RFC-003 for crypto package naming cleanup Track accumulated naming and structural issues in crypto/asn1pbe.go and cross-browser shared code for a future dedicated refactoring pass. * refactor: move masterkey tests to masterkey_test.go - Rename firefox_test.go to masterkey_test.go since all tests in this file test masterkey.go functions (readKey4DB, sampleEncryptedLogins) - Fix TestReadKey4DB to check nssPrivate rows as a set instead of assuming SQLite insertion order - Future deletion of firefox.go won't accidentally remove masterkey tests
236 lines
7.1 KiB
Go
236 lines
7.1 KiB
Go
package firefox
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/moond4rk/hackbrowserdata/filemanager"
|
|
"github.com/moond4rk/hackbrowserdata/log"
|
|
"github.com/moond4rk/hackbrowserdata/types"
|
|
"github.com/moond4rk/hackbrowserdata/utils/fileutil"
|
|
)
|
|
|
|
// Browser represents a single Firefox profile ready for extraction.
|
|
type Browser struct {
|
|
cfg types.BrowserConfig
|
|
name string // display name: "Firefox-97nszz88.default-release"
|
|
profileDir string // absolute path to profile directory
|
|
sources map[types.Category][]sourcePath // Category → candidate paths (priority order)
|
|
sourcePaths map[types.Category]resolvedPath // Category → discovered absolute path
|
|
}
|
|
|
|
// NewBrowsers discovers Firefox profiles under cfg.UserDataDir and returns
|
|
// one Browser per profile. Firefox profile directories have random names
|
|
// (e.g. "97nszz88.default-release"); any subdirectory containing known
|
|
// data files is treated as a valid profile.
|
|
func NewBrowsers(cfg types.BrowserConfig) ([]*Browser, error) {
|
|
profileDirs := discoverProfiles(cfg.UserDataDir, firefoxSources)
|
|
if len(profileDirs) == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
var browsers []*Browser
|
|
for _, profileDir := range profileDirs {
|
|
sourcePaths := resolveSourcePaths(firefoxSources, profileDir)
|
|
if len(sourcePaths) == 0 {
|
|
continue
|
|
}
|
|
browsers = append(browsers, &Browser{
|
|
cfg: cfg,
|
|
name: cfg.Name + "-" + filepath.Base(profileDir),
|
|
profileDir: profileDir,
|
|
sources: firefoxSources,
|
|
sourcePaths: sourcePaths,
|
|
})
|
|
}
|
|
return browsers, nil
|
|
}
|
|
|
|
func (b *Browser) Name() string {
|
|
return b.name
|
|
}
|
|
|
|
// 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)
|
|
|
|
masterKey, err := b.getMasterKey(session, tempPaths)
|
|
if err != nil {
|
|
log.Debugf("get master key for %s: %v", b.name, err)
|
|
}
|
|
|
|
data := &types.BrowserData{}
|
|
for _, cat := range categories {
|
|
path, ok := tempPaths[cat]
|
|
if !ok {
|
|
continue
|
|
}
|
|
b.extractCategory(data, cat, masterKey, path)
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// getMasterKey retrieves the Firefox master encryption key from key4.db.
|
|
// The key is derived via NSS ASN1 PBE decryption (platform-agnostic).
|
|
// If logins.json was already acquired by acquireFiles, the derived key
|
|
// is validated by attempting to decrypt an actual login entry.
|
|
func (b *Browser) getMasterKey(session *filemanager.Session, tempPaths map[types.Category]string) ([]byte, error) {
|
|
key4Src := filepath.Join(b.profileDir, "key4.db")
|
|
if !fileutil.IsFileExists(key4Src) {
|
|
return nil, nil
|
|
}
|
|
key4Dst := filepath.Join(session.TempDir(), "key4.db")
|
|
if err := session.Acquire(key4Src, key4Dst, false); err != nil {
|
|
return nil, fmt.Errorf("acquire key4.db: %w", err)
|
|
}
|
|
|
|
// logins.json is already acquired by acquireFiles as the Password source;
|
|
// reuse it for master key validation if available.
|
|
loginsPath := tempPaths[types.Password]
|
|
return retrieveMasterKey(key4Dst, loginsPath)
|
|
}
|
|
|
|
// retrieveMasterKey reads key4.db and derives the master key using NSS.
|
|
// If loginsPath is non-empty, the derived key is validated against actual
|
|
// login data to ensure the correct candidate is selected.
|
|
func retrieveMasterKey(key4Path, loginsPath string) ([]byte, error) {
|
|
k4, err := readKey4DB(key4Path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
keys, err := k4.deriveKeys()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(keys) == 0 {
|
|
return nil, errors.New("no valid master key candidates in key4.db")
|
|
}
|
|
|
|
// No logins to validate against — return the first derived key.
|
|
if loginsPath == "" {
|
|
return keys[0], nil
|
|
}
|
|
|
|
// Validate against actual login data.
|
|
if key := validateKeyWithLogins(keys, loginsPath); key != nil {
|
|
return key, nil
|
|
}
|
|
|
|
return nil, fmt.Errorf("derived %d key(s) but none could decrypt logins", len(keys))
|
|
}
|
|
|
|
// extractCategory calls the appropriate extract function for a category.
|
|
func (b *Browser) extractCategory(data *types.BrowserData, cat types.Category, masterKey []byte, path string) {
|
|
var err error
|
|
switch cat {
|
|
case types.Password:
|
|
data.Passwords, err = extractPasswords(masterKey, path)
|
|
case types.Cookie:
|
|
data.Cookies, err = extractCookies(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.Extension:
|
|
data.Extensions, err = extractExtensions(path)
|
|
case types.LocalStorage:
|
|
data.LocalStorage, err = extractLocalStorage(path)
|
|
case types.CreditCard, types.SessionStorage:
|
|
// Firefox does not support CreditCard or SessionStorage extraction.
|
|
}
|
|
if err != nil {
|
|
log.Debugf("extract %s for %s: %v", cat, b.name, err)
|
|
}
|
|
}
|
|
|
|
// resolvedPath holds the absolute path and type for a discovered source.
|
|
type resolvedPath struct {
|
|
absPath string
|
|
isDir bool
|
|
}
|
|
|
|
// discoverProfiles lists subdirectories of userDataDir that contain at least
|
|
// one known data source. Each such directory is a Firefox profile.
|
|
func discoverProfiles(userDataDir string, sources map[types.Category][]sourcePath) []string {
|
|
entries, err := os.ReadDir(userDataDir)
|
|
if err != nil {
|
|
log.Debugf("read user data dir %s: %v", userDataDir, err)
|
|
return nil
|
|
}
|
|
|
|
var profiles []string
|
|
for _, e := range entries {
|
|
if !e.IsDir() {
|
|
continue
|
|
}
|
|
dir := filepath.Join(userDataDir, e.Name())
|
|
if hasAnySource(sources, dir) {
|
|
profiles = append(profiles, dir)
|
|
}
|
|
}
|
|
return profiles
|
|
}
|
|
|
|
// hasAnySource checks if dir contains at least one source file or directory.
|
|
func hasAnySource(sources map[types.Category][]sourcePath, dir string) bool {
|
|
for _, candidates := range sources {
|
|
for _, sp := range candidates {
|
|
abs := filepath.Join(dir, sp.rel)
|
|
if _, err := os.Stat(abs); err == nil {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// resolveSourcePaths checks which sources actually exist in profileDir.
|
|
// Candidates are tried in priority order; the first existing path wins.
|
|
func resolveSourcePaths(sources map[types.Category][]sourcePath, profileDir string) map[types.Category]resolvedPath {
|
|
resolved := make(map[types.Category]resolvedPath)
|
|
for cat, candidates := range sources {
|
|
for _, sp := range candidates {
|
|
abs := filepath.Join(profileDir, sp.rel)
|
|
info, err := os.Stat(abs)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if sp.isDir == info.IsDir() {
|
|
resolved[cat] = resolvedPath{abs, sp.isDir}
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return resolved
|
|
}
|