From 536f2082f986fa4277e88386c725e8147ec17e64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=8D=E1=B4=8F=E1=B4=8F=C9=B4D4=CA=80=E1=B4=8B?= Date: Fri, 12 Apr 2024 19:10:41 +0800 Subject: [PATCH] refactor: Optimize traversal browser data logic (#311) * refactor: Refactor package names and imports for better code organization. * refactor: Package imports and variable types for consistency * chore: Disable unused-parameter rule in revive. * refactor: Refactor and organize data extraction and browserdata parse. * fix: rename wrong error message info --- .gitignore | 6 +- .golangci.yml | 44 ++++--- browser/browser.go | 10 +- browser/browser_darwin.go | 30 ++--- browser/browser_linux.go | 22 ++-- browser/browser_windows.go | 36 +++--- browser/chromium/chromium.go | 60 ++++----- browser/chromium/chromium_darwin.go | 4 +- browser/chromium/chromium_linux.go | 6 +- browser/chromium/chromium_windows.go | 6 +- browser/firefox/firefox.go | 26 ++-- .../bookmark/bookmark.go | 24 ++-- browserdata/browserdata.go | 68 ++++++++++ .../cookie/cookie.go | 24 ++-- .../creditcard/creditcard.go | 24 ++-- .../download/download.go | 24 ++-- .../extension/extension.go | 24 ++-- .../history/history.go | 24 ++-- browserdata/imports.go | 20 +++ .../localstorage/localstorage.go | 24 ++-- .../localstorage/localstorage_test.go | 0 {browsingdata => browserdata}/outputter.go | 6 +- .../outputter_test.go | 2 +- .../password/password.go | 33 +++-- .../sessionstorage/sessionstorage.go | 24 ++-- browsingdata/browsingdata.go | 117 ------------------ crypto/asn1pbe.go | 2 +- crypto/crypto.go | 8 +- crypto/crypto_darwin.go | 2 +- crypto/crypto_linux.go | 2 +- extractor/extractor.go | 10 ++ extractor/registration.go | 20 +++ item/item.go => types/types.go | 38 +++--- item/item_test.go => types/types_test.go | 26 ++-- utils/fileutil/filetutil.go | 6 +- 35 files changed, 449 insertions(+), 353 deletions(-) rename {browsingdata => browserdata}/bookmark/bookmark.go (80%) create mode 100644 browserdata/browserdata.go rename {browsingdata => browserdata}/cookie/cookie.go (83%) rename {browsingdata => browserdata}/creditcard/creditcard.go (79%) rename {browsingdata => browserdata}/download/download.go (80%) rename {browsingdata => browserdata}/extension/extension.go (85%) rename {browsingdata => browserdata}/history/history.go (77%) create mode 100644 browserdata/imports.go rename {browsingdata => browserdata}/localstorage/localstorage.go (81%) rename {browsingdata => browserdata}/localstorage/localstorage_test.go (100%) rename {browsingdata => browserdata}/outputter.go (90%) rename {browsingdata => browserdata}/outputter_test.go (95%) rename {browsingdata => browserdata}/password/password.go (82%) rename {browsingdata => browserdata}/sessionstorage/sessionstorage.go (81%) delete mode 100644 browsingdata/browsingdata.go create mode 100644 extractor/extractor.go create mode 100644 extractor/registration.go rename item/item.go => types/types.go (82%) rename item/item_test.go => types/types_test.go (85%) diff --git a/.gitignore b/.gitignore index 2a3b2a7..92aa2ea 100644 --- a/.gitignore +++ b/.gitignore @@ -191,9 +191,9 @@ results/ hack-browser-data !/cmd/hack-browser-data -!/browsingdata/history -!/browsingdata/history/history.go -!/browsingdata/history/history_test.go +!/browserdata/history +!/browserdata/history/history.go +!/browserdata/history/history_test.go # github action !/.github/workflows/unittest.yml diff --git a/.golangci.yml b/.golangci.yml index 117450b..44018ad 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,7 +1,5 @@ run: timeout: '5m' - skip-dirs: - - 'assets' allow-parallel-runners: true modules-download-mode: 'readonly' @@ -96,6 +94,8 @@ issues: - path: browser/browser\.go linters: - 'unused' + exclude-dirs: + - 'vendor' max-issues-per-linter: 0 max-same-issues: 0 @@ -194,16 +194,16 @@ linters-settings: # To check which checks are enabled run `GL_DEBUG=gocritic golangci-lint run`. # By default, list of stable checks is used. enabled-checks: - - nestingReduce - - unnamedResult +# - nestingReduce +# - unnamedResult - ruleguard - - captLocal - - elseif - - ifElseChain +# - captLocal +# - elseif +# - ifElseChain - rangeExprCopy - - tooManyResultsChecker - - truncateCmp - - underef +# - tooManyResultsChecker +# - truncateCmp +# - underef # Which checks should be disabled; can't be combined with 'enabled-checks'. # Default: [] # Enable multiple checks by tags, run `GL_DEBUG=gocritic golangci-lint run` to see all tags and checks. @@ -211,16 +211,16 @@ linters-settings: # Default: [] enabled-tags: - diagnostic - - style - - performance - - experimental - - opinionated - disabled-tags: - - diagnostic - - style - - performance - - experimental +# - style +# - performance +# - experimental - opinionated +# disabled-tags: +# - diagnostic +# - style +# - performance +# - experimental +# - opinionated # Settings passed to gocritic. # The settings key is the name of a supported gocritic checker. # The list of supported checkers can be find in https://go-critic.github.io/overview. @@ -357,4 +357,8 @@ linters-settings: constant-kind: true # DEPRECATED Suggest the use of syslog.Priority. # Default: false - syslog-priority: true \ No newline at end of file + syslog-priority: true + revive: + rules: + - name: unused-parameter + disabled: true diff --git a/browser/browser.go b/browser/browser.go index 80e0133..c8bc7b7 100644 --- a/browser/browser.go +++ b/browser/browser.go @@ -8,7 +8,7 @@ import ( "github.com/moond4rk/hackbrowserdata/browser/chromium" "github.com/moond4rk/hackbrowserdata/browser/firefox" - "github.com/moond4rk/hackbrowserdata/browsingdata" + "github.com/moond4rk/hackbrowserdata/browserdata" "github.com/moond4rk/hackbrowserdata/utils/fileutil" "github.com/moond4rk/hackbrowserdata/utils/typeutil" ) @@ -17,7 +17,7 @@ type Browser interface { // Name is browser's name Name() string // BrowsingData returns all browsing data in the browser. - BrowsingData(isFullExport bool) (*browsingdata.Data, error) + BrowsingData(isFullExport bool) (*browserdata.BrowserData, error) } // PickBrowsers returns a list of browsers that match the name and profile. @@ -47,7 +47,7 @@ func pickChromium(name, profile string) []Browser { slog.Warn("find browser failed, profile folder does not exist", "browser", v.name) continue } - multiChromium, err := chromium.New(v.name, v.storage, v.profilePath, v.items) + multiChromium, err := chromium.New(v.name, v.storage, v.profilePath, v.dataTypes) if err != nil { slog.Error("new chromium error", "err", err) continue @@ -65,7 +65,7 @@ func pickChromium(name, profile string) []Browser { if !fileutil.IsDirExists(filepath.Clean(profile)) { slog.Error("find browser failed, profile folder does not exist", "browser", c.name) } - chromiumList, err := chromium.New(c.name, c.storage, profile, c.items) + chromiumList, err := chromium.New(c.name, c.storage, profile, c.dataTypes) if err != nil { slog.Error("new chromium error", "err", err) } @@ -93,7 +93,7 @@ func pickFirefox(name, profile string) []Browser { continue } - if multiFirefox, err := firefox.New(profile, v.items); err == nil { + if multiFirefox, err := firefox.New(profile, v.dataTypes); err == nil { for _, b := range multiFirefox { slog.Warn("find browser success", "browser", b.Name()) browsers = append(browsers, b) diff --git a/browser/browser_darwin.go b/browser/browser_darwin.go index 9e2a697..ac2adf2 100644 --- a/browser/browser_darwin.go +++ b/browser/browser_darwin.go @@ -3,7 +3,7 @@ package browser import ( - "github.com/moond4rk/hackbrowserdata/item" + "github.com/moond4rk/hackbrowserdata/types" ) var ( @@ -11,85 +11,85 @@ var ( name string storage string profilePath string - items []item.Item + dataTypes []types.DataType }{ "chrome": { name: chromeName, storage: chromeStorageName, profilePath: chromeProfilePath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "edge": { name: edgeName, storage: edgeStorageName, profilePath: edgeProfilePath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "chromium": { name: chromiumName, storage: chromiumStorageName, profilePath: chromiumProfilePath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "chrome-beta": { name: chromeBetaName, storage: chromeBetaStorageName, profilePath: chromeBetaProfilePath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "opera": { name: operaName, profilePath: operaProfilePath, storage: operaStorageName, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "opera-gx": { name: operaGXName, profilePath: operaGXProfilePath, storage: operaStorageName, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "vivaldi": { name: vivaldiName, storage: vivaldiStorageName, profilePath: vivaldiProfilePath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "coccoc": { name: coccocName, storage: coccocStorageName, profilePath: coccocProfilePath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "brave": { name: braveName, profilePath: braveProfilePath, storage: braveStorageName, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "yandex": { name: yandexName, storage: yandexStorageName, profilePath: yandexProfilePath, - items: item.DefaultYandex, + dataTypes: types.DefaultYandexTypes, }, "arc": { name: arcName, profilePath: arcProfilePath, storage: arcStorageName, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, } firefoxList = map[string]struct { name string storage string profilePath string - items []item.Item + dataTypes []types.DataType }{ "firefox": { name: firefoxName, profilePath: firefoxProfilePath, - items: item.DefaultFirefox, + dataTypes: types.DefaultFirefoxTypes, }, } ) diff --git a/browser/browser_linux.go b/browser/browser_linux.go index f6f56c7..0b8df05 100644 --- a/browser/browser_linux.go +++ b/browser/browser_linux.go @@ -3,7 +3,7 @@ package browser import ( - "github.com/moond4rk/hackbrowserdata/item" + "github.com/moond4rk/hackbrowserdata/types" ) var ( @@ -11,61 +11,61 @@ var ( name string storage string profilePath string - items []item.Item + dataTypes []types.DataType }{ "chrome": { name: chromeName, storage: chromeStorageName, profilePath: chromeProfilePath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "edge": { name: edgeName, storage: edgeStorageName, profilePath: edgeProfilePath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "chromium": { name: chromiumName, storage: chromiumStorageName, profilePath: chromiumProfilePath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "chrome-beta": { name: chromeBetaName, storage: chromeBetaStorageName, profilePath: chromeBetaProfilePath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "opera": { name: operaName, profilePath: operaProfilePath, storage: operaStorageName, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "vivaldi": { name: vivaldiName, storage: vivaldiStorageName, profilePath: vivaldiProfilePath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "brave": { name: braveName, profilePath: braveProfilePath, storage: braveStorageName, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, } firefoxList = map[string]struct { name string storage string profilePath string - items []item.Item + dataTypes []types.DataType }{ "firefox": { name: firefoxName, profilePath: firefoxProfilePath, - items: item.DefaultFirefox, + dataTypes: types.DefaultFirefoxTypes, }, } ) diff --git a/browser/browser_windows.go b/browser/browser_windows.go index fab8a63..6c0d0a4 100644 --- a/browser/browser_windows.go +++ b/browser/browser_windows.go @@ -3,7 +3,7 @@ package browser import ( - "github.com/moond4rk/hackbrowserdata/item" + "github.com/moond4rk/hackbrowserdata/types" ) var ( @@ -11,89 +11,89 @@ var ( name string profilePath string storage string - items []item.Item + dataTypes []types.DataType }{ "chrome": { name: chromeName, profilePath: chromeUserDataPath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "edge": { name: edgeName, profilePath: edgeProfilePath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "chromium": { name: chromiumName, profilePath: chromiumUserDataPath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "chrome-beta": { name: chromeBetaName, profilePath: chromeBetaUserDataPath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "opera": { name: operaName, profilePath: operaProfilePath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "opera-gx": { name: operaGXName, profilePath: operaGXProfilePath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "vivaldi": { name: vivaldiName, profilePath: vivaldiProfilePath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "coccoc": { name: coccocName, profilePath: coccocProfilePath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "brave": { name: braveName, profilePath: braveProfilePath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "yandex": { name: yandexName, profilePath: yandexProfilePath, - items: item.DefaultYandex, + dataTypes: types.DefaultYandexTypes, }, "360": { name: speed360Name, profilePath: speed360ProfilePath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "qq": { name: qqBrowserName, profilePath: qqBrowserProfilePath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "dc": { name: dcBrowserName, profilePath: dcBrowserProfilePath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, "sogou": { name: sogouName, profilePath: sogouProfilePath, - items: item.DefaultChromium, + dataTypes: types.DefaultChromiumTypes, }, } firefoxList = map[string]struct { name string storage string profilePath string - items []item.Item + dataTypes []types.DataType }{ "firefox": { name: firefoxName, profilePath: firefoxProfilePath, - items: item.DefaultFirefox, + dataTypes: types.DefaultFirefoxTypes, }, } ) diff --git a/browser/chromium/chromium.go b/browser/chromium/chromium.go index a83b6d1..748cd0b 100644 --- a/browser/chromium/chromium.go +++ b/browser/chromium/chromium.go @@ -6,8 +6,8 @@ import ( "path/filepath" "strings" - "github.com/moond4rk/hackbrowserdata/browsingdata" - "github.com/moond4rk/hackbrowserdata/item" + "github.com/moond4rk/hackbrowserdata/browserdata" + "github.com/moond4rk/hackbrowserdata/types" "github.com/moond4rk/hackbrowserdata/utils/fileutil" "github.com/moond4rk/hackbrowserdata/utils/typeutil" ) @@ -17,28 +17,28 @@ type Chromium struct { storage string profilePath string masterKey []byte - items []item.Item - itemPaths map[item.Item]string + dataTypes []types.DataType + Paths map[types.DataType]string } // New create instance of Chromium browser, fill item's path if item is existed. -func New(name, storage, profilePath string, items []item.Item) ([]*Chromium, error) { +func New(name, storage, profilePath string, dataTypes []types.DataType) ([]*Chromium, error) { c := &Chromium{ name: name, storage: storage, profilePath: profilePath, - items: items, + dataTypes: dataTypes, } - multiItemPaths, err := c.userItemPaths(c.profilePath, c.items) + multiDataTypePaths, err := c.userDataTypePaths(c.profilePath, c.dataTypes) if err != nil { return nil, err } - chromiumList := make([]*Chromium, 0, len(multiItemPaths)) - for user, itemPaths := range multiItemPaths { + chromiumList := make([]*Chromium, 0, len(multiDataTypePaths)) + for user, itemPaths := range multiDataTypePaths { chromiumList = append(chromiumList, &Chromium{ name: fileutil.BrowserName(name, user), - items: typeutil.Keys(itemPaths), - itemPaths: itemPaths, + dataTypes: typeutil.Keys(itemPaths), + Paths: itemPaths, storage: storage, }) } @@ -49,13 +49,13 @@ func (c *Chromium) Name() string { return c.name } -func (c *Chromium) BrowsingData(isFullExport bool) (*browsingdata.Data, error) { - items := c.items +func (c *Chromium) BrowsingData(isFullExport bool) (*browserdata.BrowserData, error) { + items := c.dataTypes if !isFullExport { - items = item.FilterSensitiveItems(c.items) + items = types.FilterSensitiveItems(c.dataTypes) } - data := browsingdata.New(items) + data := browserdata.New(items) if err := c.copyItemToLocal(); err != nil { return nil, err @@ -75,15 +75,15 @@ func (c *Chromium) BrowsingData(isFullExport bool) (*browsingdata.Data, error) { } func (c *Chromium) copyItemToLocal() error { - for i, path := range c.itemPaths { + for i, path := range c.Paths { filename := i.TempFilename() var err error switch { case fileutil.IsDirExists(path): - if i == item.ChromiumLocalStorage { + if i == types.ChromiumLocalStorage { err = fileutil.CopyDir(path, filename, "lock") } - if i == item.ChromiumSessionStorage { + if i == types.ChromiumSessionStorage { err = fileutil.CopyDir(path, filename, "lock") } default: @@ -97,9 +97,9 @@ func (c *Chromium) copyItemToLocal() error { return nil } -// userItemPaths return a map of user to item path, map[profile 1][item's name & path key pair] -func (c *Chromium) userItemPaths(profilePath string, items []item.Item) (map[string]map[item.Item]string, error) { - multiItemPaths := make(map[string]map[item.Item]string) +// userDataTypePaths return a map of user to item path, map[profile 1][item's name & path key pair] +func (c *Chromium) userDataTypePaths(profilePath string, items []types.DataType) (map[string]map[types.DataType]string, error) { + multiItemPaths := make(map[string]map[types.DataType]string) parentDir := fileutil.ParentDir(profilePath) err := filepath.Walk(parentDir, chromiumWalkFunc(items, multiItemPaths)) if err != nil { @@ -109,27 +109,27 @@ func (c *Chromium) userItemPaths(profilePath string, items []item.Item) (map[str var dir string for userDir, v := range multiItemPaths { for _, p := range v { - if strings.HasSuffix(p, item.ChromiumKey.Filename()) { + if strings.HasSuffix(p, types.ChromiumKey.Filename()) { keyPath = p dir = userDir break } } } - t := make(map[string]map[item.Item]string) + t := make(map[string]map[types.DataType]string) for userDir, v := range multiItemPaths { if userDir == dir { continue } t[userDir] = v - t[userDir][item.ChromiumKey] = keyPath - fillLocalStoragePath(t[userDir], item.ChromiumLocalStorage) + t[userDir][types.ChromiumKey] = keyPath + fillLocalStoragePath(t[userDir], types.ChromiumLocalStorage) } return t, nil } // chromiumWalkFunc return a filepath.WalkFunc to find item's path -func chromiumWalkFunc(items []item.Item, multiItemPaths map[string]map[item.Item]string) filepath.WalkFunc { +func chromiumWalkFunc(items []types.DataType, multiItemPaths map[string]map[types.DataType]string) filepath.WalkFunc { return func(path string, info fs.FileInfo, err error) error { for _, v := range items { if info.Name() != v.Filename() { @@ -145,18 +145,18 @@ func chromiumWalkFunc(items []item.Item, multiItemPaths map[string]map[item.Item if _, exist := multiItemPaths[profileFolder]; exist { multiItemPaths[profileFolder][v] = path } else { - multiItemPaths[profileFolder] = map[item.Item]string{v: path} + multiItemPaths[profileFolder] = map[types.DataType]string{v: path} } } return err } } -func fillLocalStoragePath(itemPaths map[item.Item]string, storage item.Item) { - if p, ok := itemPaths[item.ChromiumHistory]; ok { +func fillLocalStoragePath(itemPaths map[types.DataType]string, storage types.DataType) { + if p, ok := itemPaths[types.ChromiumHistory]; ok { lsp := filepath.Join(filepath.Dir(p), storage.Filename()) if fileutil.IsDirExists(lsp) { - itemPaths[item.ChromiumLocalStorage] = lsp + itemPaths[types.ChromiumLocalStorage] = lsp } } } diff --git a/browser/chromium/chromium_darwin.go b/browser/chromium/chromium_darwin.go index eef9b43..b156f2b 100644 --- a/browser/chromium/chromium_darwin.go +++ b/browser/chromium/chromium_darwin.go @@ -14,7 +14,7 @@ import ( "golang.org/x/crypto/pbkdf2" - "github.com/moond4rk/hackbrowserdata/item" + "github.com/moond4rk/hackbrowserdata/types" ) var ( @@ -24,7 +24,7 @@ var ( func (c *Chromium) GetMasterKey() ([]byte, error) { // don't need chromium key file for macOS - defer os.Remove(item.ChromiumKey.TempFilename()) + defer os.Remove(types.ChromiumKey.TempFilename()) // Get the master key from the keychain // $ security find-generic-password -wa 'Chrome' var ( diff --git a/browser/chromium/chromium_linux.go b/browser/chromium/chromium_linux.go index ae01d67..d9a8985 100644 --- a/browser/chromium/chromium_linux.go +++ b/browser/chromium/chromium_linux.go @@ -12,13 +12,13 @@ import ( keyring "github.com/ppacher/go-dbus-keyring" "golang.org/x/crypto/pbkdf2" - "github.com/moond4rk/hackbrowserdata/item" + "github.com/moond4rk/hackbrowserdata/types" ) func (c *Chromium) GetMasterKey() ([]byte, error) { // what is d-bus @https://dbus.freedesktop.org/ // don't need chromium key file for Linux - defer os.Remove(item.ChromiumKey.TempFilename()) + defer os.Remove(types.ChromiumKey.TempFilename()) conn, err := dbus.SessionBus() if err != nil { @@ -56,7 +56,7 @@ func (c *Chromium) GetMasterKey() ([]byte, error) { if label == c.storage { se, err := i.GetSecret(session.Path()) if err != nil { - return nil, fmt.Errorf("get storage from dbus error: %v" + err.Error()) + return nil, fmt.Errorf("get storage from dbus: %w", err) } secret = se.Value } diff --git a/browser/chromium/chromium_windows.go b/browser/chromium/chromium_windows.go index 7b45191..b97c992 100644 --- a/browser/chromium/chromium_windows.go +++ b/browser/chromium/chromium_windows.go @@ -11,18 +11,18 @@ import ( "github.com/tidwall/gjson" "github.com/moond4rk/hackbrowserdata/crypto" - "github.com/moond4rk/hackbrowserdata/item" + "github.com/moond4rk/hackbrowserdata/types" "github.com/moond4rk/hackbrowserdata/utils/fileutil" ) var errDecodeMasterKeyFailed = errors.New("decode master key failed") func (c *Chromium) GetMasterKey() ([]byte, error) { - b, err := fileutil.ReadFile(item.ChromiumKey.TempFilename()) + b, err := fileutil.ReadFile(types.ChromiumKey.TempFilename()) if err != nil { return nil, err } - defer os.Remove(item.ChromiumKey.TempFilename()) + defer os.Remove(types.ChromiumKey.TempFilename()) encryptedKey := gjson.Get(b, "os_crypt.encrypted_key") if !encryptedKey.Exists() { diff --git a/browser/firefox/firefox.go b/browser/firefox/firefox.go index f8240e1..974d39b 100644 --- a/browser/firefox/firefox.go +++ b/browser/firefox/firefox.go @@ -11,9 +11,9 @@ import ( _ "modernc.org/sqlite" // sqlite3 driver TODO: replace with chooseable driver - "github.com/moond4rk/hackbrowserdata/browsingdata" + "github.com/moond4rk/hackbrowserdata/browserdata" "github.com/moond4rk/hackbrowserdata/crypto" - "github.com/moond4rk/hackbrowserdata/item" + "github.com/moond4rk/hackbrowserdata/types" "github.com/moond4rk/hackbrowserdata/utils/fileutil" "github.com/moond4rk/hackbrowserdata/utils/typeutil" ) @@ -23,15 +23,15 @@ type Firefox struct { storage string profilePath string masterKey []byte - items []item.Item - itemPaths map[item.Item]string + items []types.DataType + itemPaths map[types.DataType]string } var ErrProfilePathNotFound = errors.New("profile path not found") // New returns new Firefox instances. -func New(profilePath string, items []item.Item) ([]*Firefox, error) { - multiItemPaths := make(map[string]map[item.Item]string) +func New(profilePath string, items []types.DataType) ([]*Firefox, error) { + multiItemPaths := make(map[string]map[types.DataType]string) // ignore walk dir error since it can be produced by a single entry _ = filepath.WalkDir(profilePath, firefoxWalkFunc(items, multiItemPaths)) @@ -57,7 +57,7 @@ func (f *Firefox) copyItemToLocal() error { return nil } -func firefoxWalkFunc(items []item.Item, multiItemPaths map[string]map[item.Item]string) fs.WalkDirFunc { +func firefoxWalkFunc(items []types.DataType, multiItemPaths map[string]map[types.DataType]string) fs.WalkDirFunc { return func(path string, info fs.DirEntry, err error) error { for _, v := range items { if info.Name() == v.Filename() { @@ -65,7 +65,7 @@ func firefoxWalkFunc(items []item.Item, multiItemPaths map[string]map[item.Item] if _, exist := multiItemPaths[parentBaseDir]; exist { multiItemPaths[parentBaseDir][v] = path } else { - multiItemPaths[parentBaseDir] = map[item.Item]string{v: path} + multiItemPaths[parentBaseDir] = map[types.DataType]string{v: path} } } } @@ -76,7 +76,7 @@ func firefoxWalkFunc(items []item.Item, multiItemPaths map[string]map[item.Item] // GetMasterKey returns master key of Firefox. from key4.db func (f *Firefox) GetMasterKey() ([]byte, error) { - tempFilename := item.FirefoxKey4.TempFilename() + tempFilename := types.FirefoxKey4.TempFilename() // Open and defer close of the database. keyDB, err := sql.Open("sqlite", tempFilename) @@ -135,7 +135,7 @@ func processMasterKey(metaItem1, metaItem2, nssA11, nssA102 []byte) ([]byte, err return nil, errors.New("flag verification failed: password-check not found") } - var keyLin = []byte{248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1} + keyLin := []byte{248, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1} if !bytes.Equal(nssA102, keyLin) { return nil, errors.New("master key verification failed: nssA102 not equal to expected value") } @@ -159,13 +159,13 @@ func (f *Firefox) Name() string { return f.name } -func (f *Firefox) BrowsingData(isFullExport bool) (*browsingdata.Data, error) { +func (f *Firefox) BrowsingData(isFullExport bool) (*browserdata.BrowserData, error) { items := f.items if !isFullExport { - items = item.FilterSensitiveItems(f.items) + items = types.FilterSensitiveItems(f.items) } - b := browsingdata.New(items) + b := browserdata.New(items) if err := f.copyItemToLocal(); err != nil { return nil, err diff --git a/browsingdata/bookmark/bookmark.go b/browserdata/bookmark/bookmark.go similarity index 80% rename from browsingdata/bookmark/bookmark.go rename to browserdata/bookmark/bookmark.go index fe70d8c..5a0e7c4 100644 --- a/browsingdata/bookmark/bookmark.go +++ b/browserdata/bookmark/bookmark.go @@ -10,11 +10,21 @@ import ( "github.com/tidwall/gjson" _ "modernc.org/sqlite" // import sqlite3 driver - "github.com/moond4rk/hackbrowserdata/item" + "github.com/moond4rk/hackbrowserdata/extractor" + "github.com/moond4rk/hackbrowserdata/types" "github.com/moond4rk/hackbrowserdata/utils/fileutil" "github.com/moond4rk/hackbrowserdata/utils/typeutil" ) +func init() { + extractor.RegisterExtractor(types.ChromiumBookmark, func() extractor.Extractor { + return new(ChromiumBookmark) + }) + extractor.RegisterExtractor(types.FirefoxBookmark, func() extractor.Extractor { + return new(FirefoxBookmark) + }) +} + type ChromiumBookmark []bookmark type bookmark struct { @@ -25,12 +35,12 @@ type bookmark struct { DateAdded time.Time } -func (c *ChromiumBookmark) Parse(_ []byte) error { - bookmarks, err := fileutil.ReadFile(item.ChromiumBookmark.TempFilename()) +func (c *ChromiumBookmark) Extract(_ []byte) error { + bookmarks, err := fileutil.ReadFile(types.ChromiumBookmark.TempFilename()) if err != nil { return err } - defer os.Remove(item.ChromiumBookmark.TempFilename()) + defer os.Remove(types.ChromiumBookmark.TempFilename()) r := gjson.Parse(bookmarks) if r.Exists() { roots := r.Get("roots") @@ -92,12 +102,12 @@ const ( closeJournalMode = `PRAGMA journal_mode=off` ) -func (f *FirefoxBookmark) Parse(_ []byte) error { - db, err := sql.Open("sqlite", item.FirefoxBookmark.TempFilename()) +func (f *FirefoxBookmark) Extract(_ []byte) error { + db, err := sql.Open("sqlite", types.FirefoxBookmark.TempFilename()) if err != nil { return err } - defer os.Remove(item.FirefoxBookmark.TempFilename()) + defer os.Remove(types.FirefoxBookmark.TempFilename()) defer db.Close() _, err = db.Exec(closeJournalMode) if err != nil { diff --git a/browserdata/browserdata.go b/browserdata/browserdata.go new file mode 100644 index 0000000..bed2f01 --- /dev/null +++ b/browserdata/browserdata.go @@ -0,0 +1,68 @@ +package browserdata + +import ( + "log/slog" + + "github.com/moond4rk/hackbrowserdata/extractor" + "github.com/moond4rk/hackbrowserdata/types" + "github.com/moond4rk/hackbrowserdata/utils/fileutil" +) + +type BrowserData struct { + extractors map[types.DataType]extractor.Extractor +} + +func New(items []types.DataType) *BrowserData { + bd := &BrowserData{ + extractors: make(map[types.DataType]extractor.Extractor), + } + bd.addExtractors(items) + return bd +} + +func (d *BrowserData) Recovery(masterKey []byte) error { + for _, source := range d.extractors { + if err := source.Extract(masterKey); err != nil { + slog.Error("parse error", "source_data", source.Name(), "err", err.Error()) + continue + } + } + return nil +} + +func (d *BrowserData) Output(dir, browserName, flag string) { + output := newOutPutter(flag) + + for _, source := range d.extractors { + if source.Len() == 0 { + // if the length of the export data is 0, then it is not necessary to output + continue + } + filename := fileutil.Filename(browserName, source.Name(), output.Ext()) + + f, err := output.CreateFile(dir, filename) + if err != nil { + slog.Error("create file error", "filename", filename, "err", err.Error()) + continue + } + if err := output.Write(source, f); err != nil { + slog.Error("write to file error", "filename", filename, "err", err.Error()) + continue + } + if err := f.Close(); err != nil { + slog.Error("close file error", "filename", filename, "err", err.Error()) + continue + } + slog.Warn("export success", "filename", filename) + } +} + +func (d *BrowserData) addExtractors(items []types.DataType) { + for _, itemType := range items { + if source := extractor.CreateExtractor(itemType); source != nil { + d.extractors[itemType] = source + } else { + slog.Debug("source not found", "source", itemType) + } + } +} diff --git a/browsingdata/cookie/cookie.go b/browserdata/cookie/cookie.go similarity index 83% rename from browsingdata/cookie/cookie.go rename to browserdata/cookie/cookie.go index d1ecc3d..638dbb1 100644 --- a/browsingdata/cookie/cookie.go +++ b/browserdata/cookie/cookie.go @@ -11,10 +11,20 @@ import ( _ "modernc.org/sqlite" "github.com/moond4rk/hackbrowserdata/crypto" - "github.com/moond4rk/hackbrowserdata/item" + "github.com/moond4rk/hackbrowserdata/extractor" + "github.com/moond4rk/hackbrowserdata/types" "github.com/moond4rk/hackbrowserdata/utils/typeutil" ) +func init() { + extractor.RegisterExtractor(types.ChromiumCookie, func() extractor.Extractor { + return new(ChromiumCookie) + }) + extractor.RegisterExtractor(types.FirefoxCookie, func() extractor.Extractor { + return new(FirefoxCookie) + }) +} + type ChromiumCookie []cookie type cookie struct { @@ -35,12 +45,12 @@ const ( queryChromiumCookie = `SELECT name, encrypted_value, host_key, path, creation_utc, expires_utc, is_secure, is_httponly, has_expires, is_persistent FROM cookies` ) -func (c *ChromiumCookie) Parse(masterKey []byte) error { - db, err := sql.Open("sqlite", item.ChromiumCookie.TempFilename()) +func (c *ChromiumCookie) Extract(masterKey []byte) error { + db, err := sql.Open("sqlite", types.ChromiumCookie.TempFilename()) if err != nil { return err } - defer os.Remove(item.ChromiumCookie.TempFilename()) + defer os.Remove(types.ChromiumCookie.TempFilename()) defer db.Close() rows, err := db.Query(queryChromiumCookie) if err != nil { @@ -103,12 +113,12 @@ const ( queryFirefoxCookie = `SELECT name, value, host, path, creationTime, expiry, isSecure, isHttpOnly FROM moz_cookies` ) -func (f *FirefoxCookie) Parse(_ []byte) error { - db, err := sql.Open("sqlite", item.FirefoxCookie.TempFilename()) +func (f *FirefoxCookie) Extract(_ []byte) error { + db, err := sql.Open("sqlite", types.FirefoxCookie.TempFilename()) if err != nil { return err } - defer os.Remove(item.FirefoxCookie.TempFilename()) + defer os.Remove(types.FirefoxCookie.TempFilename()) defer db.Close() rows, err := db.Query(queryFirefoxCookie) diff --git a/browsingdata/creditcard/creditcard.go b/browserdata/creditcard/creditcard.go similarity index 79% rename from browsingdata/creditcard/creditcard.go rename to browserdata/creditcard/creditcard.go index fe72f6e..dc0948c 100644 --- a/browsingdata/creditcard/creditcard.go +++ b/browserdata/creditcard/creditcard.go @@ -9,9 +9,19 @@ import ( _ "modernc.org/sqlite" "github.com/moond4rk/hackbrowserdata/crypto" - "github.com/moond4rk/hackbrowserdata/item" + "github.com/moond4rk/hackbrowserdata/extractor" + "github.com/moond4rk/hackbrowserdata/types" ) +func init() { + extractor.RegisterExtractor(types.ChromiumCreditCard, func() extractor.Extractor { + return new(ChromiumCreditCard) + }) + extractor.RegisterExtractor(types.YandexCreditCard, func() extractor.Extractor { + return new(YandexCreditCard) + }) +} + type ChromiumCreditCard []card type card struct { @@ -28,12 +38,12 @@ const ( queryChromiumCredit = `SELECT guid, name_on_card, expiration_month, expiration_year, card_number_encrypted, billing_address_id, nickname FROM credit_cards` ) -func (c *ChromiumCreditCard) Parse(masterKey []byte) error { - db, err := sql.Open("sqlite", item.ChromiumCreditCard.TempFilename()) +func (c *ChromiumCreditCard) Extract(masterKey []byte) error { + db, err := sql.Open("sqlite", types.ChromiumCreditCard.TempFilename()) if err != nil { return err } - defer os.Remove(item.ChromiumCreditCard.TempFilename()) + defer os.Remove(types.ChromiumCreditCard.TempFilename()) defer db.Close() rows, err := db.Query(queryChromiumCredit) @@ -84,12 +94,12 @@ func (c *ChromiumCreditCard) Len() int { type YandexCreditCard []card -func (c *YandexCreditCard) Parse(masterKey []byte) error { - db, err := sql.Open("sqlite", item.YandexCreditCard.TempFilename()) +func (c *YandexCreditCard) Extract(masterKey []byte) error { + db, err := sql.Open("sqlite", types.YandexCreditCard.TempFilename()) if err != nil { return err } - defer os.Remove(item.YandexCreditCard.TempFilename()) + defer os.Remove(types.YandexCreditCard.TempFilename()) defer db.Close() rows, err := db.Query(queryChromiumCredit) if err != nil { diff --git a/browsingdata/download/download.go b/browserdata/download/download.go similarity index 80% rename from browsingdata/download/download.go rename to browserdata/download/download.go index 02bf183..44a425d 100644 --- a/browsingdata/download/download.go +++ b/browserdata/download/download.go @@ -11,10 +11,20 @@ import ( "github.com/tidwall/gjson" _ "modernc.org/sqlite" // import sqlite3 driver - "github.com/moond4rk/hackbrowserdata/item" + "github.com/moond4rk/hackbrowserdata/extractor" + "github.com/moond4rk/hackbrowserdata/types" "github.com/moond4rk/hackbrowserdata/utils/typeutil" ) +func init() { + extractor.RegisterExtractor(types.ChromiumDownload, func() extractor.Extractor { + return new(ChromiumDownload) + }) + extractor.RegisterExtractor(types.FirefoxDownload, func() extractor.Extractor { + return new(FirefoxDownload) + }) +} + type ChromiumDownload []download type download struct { @@ -30,12 +40,12 @@ const ( queryChromiumDownload = `SELECT target_path, tab_url, total_bytes, start_time, end_time, mime_type FROM downloads` ) -func (c *ChromiumDownload) Parse(_ []byte) error { - db, err := sql.Open("sqlite", item.ChromiumDownload.TempFilename()) +func (c *ChromiumDownload) Extract(_ []byte) error { + db, err := sql.Open("sqlite", types.ChromiumDownload.TempFilename()) if err != nil { return err } - defer os.Remove(item.ChromiumDownload.TempFilename()) + defer os.Remove(types.ChromiumDownload.TempFilename()) defer db.Close() rows, err := db.Query(queryChromiumDownload) if err != nil { @@ -81,12 +91,12 @@ const ( closeJournalMode = `PRAGMA journal_mode=off` ) -func (f *FirefoxDownload) Parse(_ []byte) error { - db, err := sql.Open("sqlite", item.FirefoxDownload.TempFilename()) +func (f *FirefoxDownload) Extract(_ []byte) error { + db, err := sql.Open("sqlite", types.FirefoxDownload.TempFilename()) if err != nil { return err } - defer os.Remove(item.FirefoxDownload.TempFilename()) + defer os.Remove(types.FirefoxDownload.TempFilename()) defer db.Close() _, err = db.Exec(closeJournalMode) diff --git a/browsingdata/extension/extension.go b/browserdata/extension/extension.go similarity index 85% rename from browsingdata/extension/extension.go rename to browserdata/extension/extension.go index e45d3ae..7337900 100644 --- a/browsingdata/extension/extension.go +++ b/browserdata/extension/extension.go @@ -8,10 +8,20 @@ import ( "github.com/tidwall/gjson" "golang.org/x/text/language" - "github.com/moond4rk/hackbrowserdata/item" + "github.com/moond4rk/hackbrowserdata/extractor" + "github.com/moond4rk/hackbrowserdata/types" "github.com/moond4rk/hackbrowserdata/utils/fileutil" ) +func init() { + extractor.RegisterExtractor(types.ChromiumExtension, func() extractor.Extractor { + return new(ChromiumExtension) + }) + extractor.RegisterExtractor(types.FirefoxExtension, func() extractor.Extractor { + return new(FirefoxExtension) + }) +} + type ChromiumExtension []*extension type extension struct { @@ -24,12 +34,12 @@ type extension struct { HomepageURL string } -func (c *ChromiumExtension) Parse(_ []byte) error { - extensionFile, err := fileutil.ReadFile(item.ChromiumExtension.TempFilename()) +func (c *ChromiumExtension) Extract(_ []byte) error { + extensionFile, err := fileutil.ReadFile(types.ChromiumExtension.TempFilename()) if err != nil { return err } - defer os.Remove(item.ChromiumExtension.TempFilename()) + defer os.Remove(types.ChromiumExtension.TempFilename()) result, err := parseChromiumExtensions(extensionFile) if err != nil { @@ -113,12 +123,12 @@ type FirefoxExtension []*extension var lang = language.Und -func (f *FirefoxExtension) Parse(_ []byte) error { - s, err := fileutil.ReadFile(item.FirefoxExtension.TempFilename()) +func (f *FirefoxExtension) Extract(_ []byte) error { + s, err := fileutil.ReadFile(types.FirefoxExtension.TempFilename()) if err != nil { return err } - _ = os.Remove(item.FirefoxExtension.TempFilename()) + _ = os.Remove(types.FirefoxExtension.TempFilename()) j := gjson.Parse(s) for _, v := range j.Get("addons").Array() { // https://searchfox.org/mozilla-central/source/toolkit/mozapps/extensions/internal/XPIDatabase.jsm#157 diff --git a/browsingdata/history/history.go b/browserdata/history/history.go similarity index 77% rename from browsingdata/history/history.go rename to browserdata/history/history.go index fb2cbbf..7ff624e 100644 --- a/browsingdata/history/history.go +++ b/browserdata/history/history.go @@ -10,10 +10,20 @@ import ( // import sqlite3 driver _ "modernc.org/sqlite" - "github.com/moond4rk/hackbrowserdata/item" + "github.com/moond4rk/hackbrowserdata/extractor" + "github.com/moond4rk/hackbrowserdata/types" "github.com/moond4rk/hackbrowserdata/utils/typeutil" ) +func init() { + extractor.RegisterExtractor(types.ChromiumHistory, func() extractor.Extractor { + return new(ChromiumHistory) + }) + extractor.RegisterExtractor(types.FirefoxHistory, func() extractor.Extractor { + return new(FirefoxHistory) + }) +} + type ChromiumHistory []history type history struct { @@ -27,12 +37,12 @@ const ( queryChromiumHistory = `SELECT url, title, visit_count, last_visit_time FROM urls` ) -func (c *ChromiumHistory) Parse(_ []byte) error { - db, err := sql.Open("sqlite", item.ChromiumHistory.TempFilename()) +func (c *ChromiumHistory) Extract(_ []byte) error { + db, err := sql.Open("sqlite", types.ChromiumHistory.TempFilename()) if err != nil { return err } - defer os.Remove(item.ChromiumHistory.TempFilename()) + defer os.Remove(types.ChromiumHistory.TempFilename()) defer db.Close() rows, err := db.Query(queryChromiumHistory) @@ -78,12 +88,12 @@ const ( closeJournalMode = `PRAGMA journal_mode=off` ) -func (f *FirefoxHistory) Parse(_ []byte) error { - db, err := sql.Open("sqlite", item.FirefoxHistory.TempFilename()) +func (f *FirefoxHistory) Extract(_ []byte) error { + db, err := sql.Open("sqlite", types.FirefoxHistory.TempFilename()) if err != nil { return err } - defer os.Remove(item.FirefoxHistory.TempFilename()) + defer os.Remove(types.FirefoxHistory.TempFilename()) defer db.Close() _, err = db.Exec(closeJournalMode) diff --git a/browserdata/imports.go b/browserdata/imports.go new file mode 100644 index 0000000..91c4718 --- /dev/null +++ b/browserdata/imports.go @@ -0,0 +1,20 @@ +// Package browserdata is responsible for initializing all the necessary +// components that handle different types of browser data extraction. +// This file, imports.go, is specifically used to import various data +// handler packages to ensure their initialization logic is executed. +// These imports are crucial as they trigger the `init()` functions +// within each package, which typically handle registration of their +// specific data handlers to a central registry. +package browserdata + +import ( + _ "github.com/moond4rk/hackbrowserdata/browserdata/bookmark" + _ "github.com/moond4rk/hackbrowserdata/browserdata/cookie" + _ "github.com/moond4rk/hackbrowserdata/browserdata/creditcard" + _ "github.com/moond4rk/hackbrowserdata/browserdata/download" + _ "github.com/moond4rk/hackbrowserdata/browserdata/extension" + _ "github.com/moond4rk/hackbrowserdata/browserdata/history" + _ "github.com/moond4rk/hackbrowserdata/browserdata/localstorage" + _ "github.com/moond4rk/hackbrowserdata/browserdata/password" + _ "github.com/moond4rk/hackbrowserdata/browserdata/sessionstorage" +) diff --git a/browsingdata/localstorage/localstorage.go b/browserdata/localstorage/localstorage.go similarity index 81% rename from browsingdata/localstorage/localstorage.go rename to browserdata/localstorage/localstorage.go index f8a25d8..5d51476 100644 --- a/browsingdata/localstorage/localstorage.go +++ b/browserdata/localstorage/localstorage.go @@ -12,11 +12,21 @@ import ( "golang.org/x/text/encoding/unicode" "golang.org/x/text/transform" - "github.com/moond4rk/hackbrowserdata/item" + "github.com/moond4rk/hackbrowserdata/extractor" + "github.com/moond4rk/hackbrowserdata/types" "github.com/moond4rk/hackbrowserdata/utils/byteutil" "github.com/moond4rk/hackbrowserdata/utils/typeutil" ) +func init() { + extractor.RegisterExtractor(types.ChromiumLocalStorage, func() extractor.Extractor { + return new(ChromiumLocalStorage) + }) + extractor.RegisterExtractor(types.FirefoxLocalStorage, func() extractor.Extractor { + return new(FirefoxLocalStorage) + }) +} + type ChromiumLocalStorage []storage type storage struct { @@ -28,12 +38,12 @@ type storage struct { const maxLocalStorageValueLength = 1024 * 2 -func (c *ChromiumLocalStorage) Parse(_ []byte) error { - db, err := leveldb.OpenFile(item.ChromiumLocalStorage.TempFilename(), nil) +func (c *ChromiumLocalStorage) Extract(_ []byte) error { + db, err := leveldb.OpenFile(types.ChromiumLocalStorage.TempFilename(), nil) if err != nil { return err } - defer os.RemoveAll(item.ChromiumLocalStorage.TempFilename()) + defer os.RemoveAll(types.ChromiumLocalStorage.TempFilename()) defer db.Close() iter := db.NewIterator(nil, nil) @@ -105,12 +115,12 @@ const ( closeJournalMode = `PRAGMA journal_mode=off` ) -func (f *FirefoxLocalStorage) Parse(_ []byte) error { - db, err := sql.Open("sqlite", item.FirefoxLocalStorage.TempFilename()) +func (f *FirefoxLocalStorage) Extract(_ []byte) error { + db, err := sql.Open("sqlite", types.FirefoxLocalStorage.TempFilename()) if err != nil { return err } - defer os.Remove(item.FirefoxLocalStorage.TempFilename()) + defer os.Remove(types.FirefoxLocalStorage.TempFilename()) defer db.Close() _, err = db.Exec(closeJournalMode) diff --git a/browsingdata/localstorage/localstorage_test.go b/browserdata/localstorage/localstorage_test.go similarity index 100% rename from browsingdata/localstorage/localstorage_test.go rename to browserdata/localstorage/localstorage_test.go diff --git a/browsingdata/outputter.go b/browserdata/outputter.go similarity index 90% rename from browsingdata/outputter.go rename to browserdata/outputter.go index 107dca5..4e78001 100644 --- a/browsingdata/outputter.go +++ b/browserdata/outputter.go @@ -1,4 +1,4 @@ -package browsingdata +package browserdata import ( "encoding/csv" @@ -11,6 +11,8 @@ import ( "github.com/gocarina/gocsv" "golang.org/x/text/encoding/unicode" "golang.org/x/text/transform" + + "github.com/moond4rk/hackbrowserdata/extractor" ) type outPutter struct { @@ -28,7 +30,7 @@ func newOutPutter(flag string) *outPutter { return o } -func (o *outPutter) Write(data Source, writer io.Writer) error { +func (o *outPutter) Write(data extractor.Extractor, writer io.Writer) error { switch o.json { case true: encoder := json.NewEncoder(writer) diff --git a/browsingdata/outputter_test.go b/browserdata/outputter_test.go similarity index 95% rename from browsingdata/outputter_test.go rename to browserdata/outputter_test.go index 329e5fa..6767c48 100644 --- a/browsingdata/outputter_test.go +++ b/browserdata/outputter_test.go @@ -1,4 +1,4 @@ -package browsingdata +package browserdata import ( "os" diff --git a/browsingdata/password/password.go b/browserdata/password/password.go similarity index 82% rename from browsingdata/password/password.go rename to browserdata/password/password.go index 9606710..e8912dd 100644 --- a/browsingdata/password/password.go +++ b/browserdata/password/password.go @@ -12,10 +12,23 @@ import ( _ "modernc.org/sqlite" // import sqlite3 driver "github.com/moond4rk/hackbrowserdata/crypto" - "github.com/moond4rk/hackbrowserdata/item" + "github.com/moond4rk/hackbrowserdata/extractor" + "github.com/moond4rk/hackbrowserdata/types" "github.com/moond4rk/hackbrowserdata/utils/typeutil" ) +func init() { + extractor.RegisterExtractor(types.ChromiumPassword, func() extractor.Extractor { + return new(ChromiumPassword) + }) + extractor.RegisterExtractor(types.YandexPassword, func() extractor.Extractor { + return new(YandexPassword) + }) + extractor.RegisterExtractor(types.FirefoxPassword, func() extractor.Extractor { + return new(FirefoxPassword) + }) +} + type ChromiumPassword []loginData type loginData struct { @@ -31,12 +44,12 @@ const ( queryChromiumLogin = `SELECT origin_url, username_value, password_value, date_created FROM logins` ) -func (c *ChromiumPassword) Parse(masterKey []byte) error { - db, err := sql.Open("sqlite", item.ChromiumPassword.TempFilename()) +func (c *ChromiumPassword) Extract(masterKey []byte) error { + db, err := sql.Open("sqlite", types.ChromiumPassword.TempFilename()) if err != nil { return err } - defer os.Remove(item.ChromiumPassword.TempFilename()) + defer os.Remove(types.ChromiumPassword.TempFilename()) defer db.Close() rows, err := db.Query(queryChromiumLogin) @@ -98,12 +111,12 @@ const ( queryYandexLogin = `SELECT action_url, username_value, password_value, date_created FROM logins` ) -func (c *YandexPassword) Parse(masterKey []byte) error { - db, err := sql.Open("sqlite", item.YandexPassword.TempFilename()) +func (c *YandexPassword) Extract(masterKey []byte) error { + db, err := sql.Open("sqlite", types.YandexPassword.TempFilename()) if err != nil { return err } - defer os.Remove(item.YandexPassword.TempFilename()) + defer os.Remove(types.YandexPassword.TempFilename()) defer db.Close() rows, err := db.Query(queryYandexLogin) @@ -162,7 +175,7 @@ func (c *YandexPassword) Len() int { type FirefoxPassword []loginData -func (f *FirefoxPassword) Parse(globalSalt []byte) error { +func (f *FirefoxPassword) Extract(globalSalt []byte) error { logins, err := getFirefoxLoginData() if err != nil { return err @@ -200,11 +213,11 @@ func (f *FirefoxPassword) Parse(globalSalt []byte) error { } func getFirefoxLoginData() ([]loginData, error) { - s, err := os.ReadFile(item.FirefoxPassword.TempFilename()) + s, err := os.ReadFile(types.FirefoxPassword.TempFilename()) if err != nil { return nil, err } - defer os.Remove(item.FirefoxPassword.TempFilename()) + defer os.Remove(types.FirefoxPassword.TempFilename()) loginsJSON := gjson.GetBytes(s, "logins") var logins []loginData if loginsJSON.Exists() { diff --git a/browsingdata/sessionstorage/sessionstorage.go b/browserdata/sessionstorage/sessionstorage.go similarity index 81% rename from browsingdata/sessionstorage/sessionstorage.go rename to browserdata/sessionstorage/sessionstorage.go index a8cf31d..c12e73d 100644 --- a/browsingdata/sessionstorage/sessionstorage.go +++ b/browserdata/sessionstorage/sessionstorage.go @@ -12,11 +12,21 @@ import ( "golang.org/x/text/encoding/unicode" "golang.org/x/text/transform" - "github.com/moond4rk/hackbrowserdata/item" + "github.com/moond4rk/hackbrowserdata/extractor" + "github.com/moond4rk/hackbrowserdata/types" "github.com/moond4rk/hackbrowserdata/utils/byteutil" "github.com/moond4rk/hackbrowserdata/utils/typeutil" ) +func init() { + extractor.RegisterExtractor(types.ChromiumSessionStorage, func() extractor.Extractor { + return new(ChromiumSessionStorage) + }) + extractor.RegisterExtractor(types.FirefoxSessionStorage, func() extractor.Extractor { + return new(FirefoxSessionStorage) + }) +} + type ChromiumSessionStorage []session type session struct { @@ -28,12 +38,12 @@ type session struct { const maxLocalStorageValueLength = 1024 * 2 -func (c *ChromiumSessionStorage) Parse(_ []byte) error { - db, err := leveldb.OpenFile(item.ChromiumSessionStorage.TempFilename(), nil) +func (c *ChromiumSessionStorage) Extract(_ []byte) error { + db, err := leveldb.OpenFile(types.ChromiumSessionStorage.TempFilename(), nil) if err != nil { return err } - defer os.RemoveAll(item.ChromiumSessionStorage.TempFilename()) + defer os.RemoveAll(types.ChromiumSessionStorage.TempFilename()) defer db.Close() iter := db.NewIterator(nil, nil) @@ -113,12 +123,12 @@ const ( closeJournalMode = `PRAGMA journal_mode=off` ) -func (f *FirefoxSessionStorage) Parse(_ []byte) error { - db, err := sql.Open("sqlite", item.FirefoxSessionStorage.TempFilename()) +func (f *FirefoxSessionStorage) Extract(_ []byte) error { + db, err := sql.Open("sqlite", types.FirefoxSessionStorage.TempFilename()) if err != nil { return err } - defer os.Remove(item.FirefoxSessionStorage.TempFilename()) + defer os.Remove(types.FirefoxSessionStorage.TempFilename()) defer db.Close() _, err = db.Exec(closeJournalMode) diff --git a/browsingdata/browsingdata.go b/browsingdata/browsingdata.go deleted file mode 100644 index f38db59..0000000 --- a/browsingdata/browsingdata.go +++ /dev/null @@ -1,117 +0,0 @@ -package browsingdata - -import ( - "log/slog" - - "github.com/moond4rk/hackbrowserdata/browsingdata/bookmark" - "github.com/moond4rk/hackbrowserdata/browsingdata/cookie" - "github.com/moond4rk/hackbrowserdata/browsingdata/creditcard" - "github.com/moond4rk/hackbrowserdata/browsingdata/download" - "github.com/moond4rk/hackbrowserdata/browsingdata/extension" - "github.com/moond4rk/hackbrowserdata/browsingdata/history" - "github.com/moond4rk/hackbrowserdata/browsingdata/localstorage" - "github.com/moond4rk/hackbrowserdata/browsingdata/password" - "github.com/moond4rk/hackbrowserdata/browsingdata/sessionstorage" - "github.com/moond4rk/hackbrowserdata/item" - "github.com/moond4rk/hackbrowserdata/utils/fileutil" -) - -type Data struct { - sources map[item.Item]Source -} - -type Source interface { - Parse(masterKey []byte) error - - Name() string - - Len() int -} - -func New(items []item.Item) *Data { - bd := &Data{ - sources: make(map[item.Item]Source), - } - bd.addSources(items) - return bd -} - -func (d *Data) Recovery(masterKey []byte) error { - for _, source := range d.sources { - if err := source.Parse(masterKey); err != nil { - slog.Error("parse error", "source_data", source.Name(), "err", err.Error()) - continue - } - } - return nil -} - -func (d *Data) Output(dir, browserName, flag string) { - output := newOutPutter(flag) - - for _, source := range d.sources { - if source.Len() == 0 { - // if the length of the export data is 0, then it is not necessary to output - continue - } - filename := fileutil.ItemName(browserName, source.Name(), output.Ext()) - - f, err := output.CreateFile(dir, filename) - if err != nil { - slog.Error("create file error", "filename", filename, "err", err.Error()) - continue - } - if err := output.Write(source, f); err != nil { - slog.Error("write to file error", "filename", filename, "err", err.Error()) - continue - } - if err := f.Close(); err != nil { - slog.Error("close file error", "filename", filename, "err", err.Error()) - continue - } - slog.Warn("export success", "filename", filename) - } -} - -func (d *Data) addSources(items []item.Item) { - for _, source := range items { - switch source { - case item.ChromiumPassword: - d.sources[source] = &password.ChromiumPassword{} - case item.ChromiumCookie: - d.sources[source] = &cookie.ChromiumCookie{} - case item.ChromiumBookmark: - d.sources[source] = &bookmark.ChromiumBookmark{} - case item.ChromiumHistory: - d.sources[source] = &history.ChromiumHistory{} - case item.ChromiumDownload: - d.sources[source] = &download.ChromiumDownload{} - case item.ChromiumCreditCard: - d.sources[source] = &creditcard.ChromiumCreditCard{} - case item.ChromiumLocalStorage: - d.sources[source] = &localstorage.ChromiumLocalStorage{} - case item.ChromiumSessionStorage: - d.sources[source] = &sessionstorage.ChromiumSessionStorage{} - case item.ChromiumExtension: - d.sources[source] = &extension.ChromiumExtension{} - case item.YandexPassword: - d.sources[source] = &password.YandexPassword{} - case item.YandexCreditCard: - d.sources[source] = &creditcard.YandexCreditCard{} - case item.FirefoxPassword: - d.sources[source] = &password.FirefoxPassword{} - case item.FirefoxCookie: - d.sources[source] = &cookie.FirefoxCookie{} - case item.FirefoxBookmark: - d.sources[source] = &bookmark.FirefoxBookmark{} - case item.FirefoxHistory: - d.sources[source] = &history.FirefoxHistory{} - case item.FirefoxDownload: - d.sources[source] = &download.FirefoxDownload{} - case item.FirefoxLocalStorage: - d.sources[source] = &localstorage.FirefoxLocalStorage{} - case item.FirefoxExtension: - d.sources[source] = &extension.FirefoxExtension{} - } - } -} diff --git a/crypto/asn1pbe.go b/crypto/asn1pbe.go index beb9eb2..397c81a 100644 --- a/crypto/asn1pbe.go +++ b/crypto/asn1pbe.go @@ -62,7 +62,7 @@ func (n nssPBE) Decrypt(globalSalt []byte) ([]byte, error) { return DES3Decrypt(key, iv, n.Encrypted) } -func (n nssPBE) Encrypt(globalSalt []byte, plaintext []byte) ([]byte, error) { +func (n nssPBE) Encrypt(globalSalt, plaintext []byte) ([]byte, error) { key, iv := n.deriveKeyAndIV(globalSalt) return DES3Encrypt(key, iv, plaintext) diff --git a/crypto/crypto.go b/crypto/crypto.go index 9638345..dedd919 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -9,9 +9,7 @@ import ( "fmt" ) -var ( - ErrCiphertextLengthIsInvalid = errors.New("ciphertext length is invalid") -) +var ErrCiphertextLengthIsInvalid = errors.New("ciphertext length is invalid") func AES128CBCDecrypt(key, iv, ciphertext []byte) ([]byte, error) { block, err := aes.NewCipher(key) @@ -57,7 +55,7 @@ func AES128CBCEncrypt(key, iv, plaintext []byte) ([]byte, error) { return encryptedData, nil } -func DES3Decrypt(key, iv []byte, ciphertext []byte) ([]byte, error) { +func DES3Decrypt(key, iv, ciphertext []byte) ([]byte, error) { block, err := des.NewTripleDESCipher(key) if err != nil { return nil, err @@ -76,7 +74,7 @@ func DES3Decrypt(key, iv []byte, ciphertext []byte) ([]byte, error) { return pkcs5UnPadding(sq) } -func DES3Encrypt(key, iv []byte, plaintext []byte) ([]byte, error) { +func DES3Encrypt(key, iv, plaintext []byte) ([]byte, error) { block, err := des.NewTripleDESCipher(key) if err != nil { return nil, err diff --git a/crypto/crypto_darwin.go b/crypto/crypto_darwin.go index 691a065..3d2f10d 100644 --- a/crypto/crypto_darwin.go +++ b/crypto/crypto_darwin.go @@ -6,7 +6,7 @@ func DecryptWithChromium(key, password []byte) ([]byte, error) { if len(password) <= 3 { return nil, ErrCiphertextLengthIsInvalid } - var iv = []byte{32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32} + iv := []byte{32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32} return AES128CBCDecrypt(key, iv, password[3:]) } diff --git a/crypto/crypto_linux.go b/crypto/crypto_linux.go index 81e8587..d9eaf0e 100644 --- a/crypto/crypto_linux.go +++ b/crypto/crypto_linux.go @@ -6,7 +6,7 @@ func DecryptWithChromium(key, encryptPass []byte) ([]byte, error) { if len(encryptPass) < 3 { return nil, ErrCiphertextLengthIsInvalid } - var iv = []byte{32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32} + iv := []byte{32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32} return AES128CBCDecrypt(key, iv, encryptPass[3:]) } diff --git a/extractor/extractor.go b/extractor/extractor.go new file mode 100644 index 0000000..1cc6f69 --- /dev/null +++ b/extractor/extractor.go @@ -0,0 +1,10 @@ +package extractor + +// Extractor is an interface for extracting data from browser data files +type Extractor interface { + Extract(masterKey []byte) error + + Name() string + + Len() int +} diff --git a/extractor/registration.go b/extractor/registration.go new file mode 100644 index 0000000..747a328 --- /dev/null +++ b/extractor/registration.go @@ -0,0 +1,20 @@ +package extractor + +import ( + "github.com/moond4rk/hackbrowserdata/types" +) + +var extractorRegistry = make(map[types.DataType]func() Extractor) + +// RegisterExtractor is used to register the data source +func RegisterExtractor(dataType types.DataType, factoryFunc func() Extractor) { + extractorRegistry[dataType] = factoryFunc +} + +// CreateExtractor is used to create the data source +func CreateExtractor(dataType types.DataType) Extractor { + if factoryFunc, ok := extractorRegistry[dataType]; ok { + return factoryFunc() + } + return nil +} diff --git a/item/item.go b/types/types.go similarity index 82% rename from item/item.go rename to types/types.go index 577e2b6..457614a 100644 --- a/item/item.go +++ b/types/types.go @@ -1,4 +1,4 @@ -package item +package types import ( "fmt" @@ -6,10 +6,10 @@ import ( "path/filepath" ) -type Item int +type DataType int const ( - ChromiumKey Item = iota + ChromiumKey DataType = iota ChromiumPassword ChromiumCookie ChromiumBookmark @@ -35,7 +35,7 @@ const ( FirefoxExtension ) -var itemFileNames = map[Item]string{ +var itemFileNames = map[DataType]string{ ChromiumKey: fileChromiumKey, ChromiumPassword: fileChromiumPassword, ChromiumCookie: fileChromiumCookie, @@ -60,25 +60,26 @@ var itemFileNames = map[Item]string{ FirefoxCreditCard: UnsupportedItem, } -func (i Item) Filename() string { +// Filename returns the filename for the item, defined by browser +// chromium local storage is a folder, so it returns the file name of the folder +func (i DataType) Filename() string { if fileName, ok := itemFileNames[i]; ok { return fileName } return UnsupportedItem } -const tempSuffix = "temp" - // TempFilename returns the temp filename for the item with suffix // eg: chromiumKey_0.temp -func (i Item) TempFilename() string { +func (i DataType) TempFilename() string { + const tempSuffix = "temp" tempFile := fmt.Sprintf("%s_%d.%s", i.Filename(), i, tempSuffix) return filepath.Join(os.TempDir(), tempFile) } // IsSensitive returns whether the item is sensitive data // password, cookie, credit card, master key is unlimited -func (i Item) IsSensitive() bool { +func (i DataType) IsSensitive() bool { switch i { case ChromiumKey, ChromiumCookie, ChromiumPassword, ChromiumCreditCard, FirefoxKey4, FirefoxPassword, FirefoxCookie, FirefoxCreditCard, @@ -90,8 +91,8 @@ func (i Item) IsSensitive() bool { } // FilterSensitiveItems returns the sensitive items -func FilterSensitiveItems(items []Item) []Item { - var filtered []Item +func FilterSensitiveItems(items []DataType) []DataType { + var filtered []DataType for _, item := range items { if item.IsSensitive() { filtered = append(filtered, item) @@ -100,8 +101,8 @@ func FilterSensitiveItems(items []Item) []Item { return filtered } -// DefaultFirefox returns the default items for the firefox browser -var DefaultFirefox = []Item{ +// DefaultFirefoxTypes returns the default items for the firefox browser +var DefaultFirefoxTypes = []DataType{ FirefoxKey4, FirefoxPassword, FirefoxCookie, @@ -114,8 +115,8 @@ var DefaultFirefox = []Item{ FirefoxExtension, } -// DefaultYandex returns the default items for the yandex browser -var DefaultYandex = []Item{ +// DefaultYandexTypes returns the default items for the yandex browser +var DefaultYandexTypes = []DataType{ ChromiumKey, ChromiumCookie, ChromiumBookmark, @@ -128,8 +129,8 @@ var DefaultYandex = []Item{ YandexCreditCard, } -// DefaultChromium returns the default items for the chromium browser -var DefaultChromium = []Item{ +// DefaultChromiumTypes returns the default items for the chromium browser +var DefaultChromiumTypes = []DataType{ ChromiumKey, ChromiumPassword, ChromiumCookie, @@ -164,9 +165,6 @@ const ( fileFirefoxData = "places.sqlite" fileFirefoxLocalStorage = "webappsstore.sqlite" fileFirefoxExtension = "extensions.json" -) -const ( - UnknownItem = "unknown item" UnsupportedItem = "unsupported item" ) diff --git a/item/item_test.go b/types/types_test.go similarity index 85% rename from item/item_test.go rename to types/types_test.go index 87f1d1d..9a68c97 100644 --- a/item/item_test.go +++ b/types/types_test.go @@ -1,4 +1,4 @@ -package item +package types import ( "fmt" @@ -9,23 +9,23 @@ import ( "github.com/stretchr/testify/assert" ) -func TestItem_FileName(t *testing.T) { - for _, item := range DefaultChromium { +func TestDataType_FileName(t *testing.T) { + for _, item := range DefaultChromiumTypes { assert.Equal(t, item.Filename(), item.filename()) } - for _, item := range DefaultFirefox { + for _, item := range DefaultFirefoxTypes { assert.Equal(t, item.Filename(), item.filename()) } - for _, item := range DefaultYandex { + for _, item := range DefaultYandexTypes { assert.Equal(t, item.Filename(), item.filename()) } } -func TestItem_TempFilename(t *testing.T) { +func TestDataType_TempFilename(t *testing.T) { asserts := assert.New(t) testCases := []struct { - item Item + item DataType expected string }{ {ChromiumKey, "Local State"}, @@ -45,10 +45,10 @@ func TestItem_TempFilename(t *testing.T) { } } -func TestItem_IsSensitive(t *testing.T) { +func TestDataType_IsSensitive(t *testing.T) { asserts := assert.New(t) testCases := []struct { - item Item + item DataType expected bool }{ {ChromiumKey, true}, @@ -63,11 +63,11 @@ func TestItem_IsSensitive(t *testing.T) { func TestFilterSensitiveItems(t *testing.T) { asserts := assert.New(t) testCases := []struct { - items []Item + items []DataType expected int }{ - {[]Item{ChromiumKey, ChromiumBookmark, ChromiumPassword}, 2}, - {[]Item{ChromiumBookmark, ChromiumHistory}, 0}, + {[]DataType{ChromiumKey, ChromiumBookmark, ChromiumPassword}, 2}, + {[]DataType{ChromiumBookmark, ChromiumHistory}, 0}, } for _, tc := range testCases { @@ -79,7 +79,7 @@ func TestFilterSensitiveItems(t *testing.T) { } } -func (i Item) filename() string { +func (i DataType) filename() string { switch i { case ChromiumKey: return fileChromiumKey diff --git a/utils/fileutil/filetutil.go b/utils/fileutil/filetutil.go index 54bb99b..e1e5d95 100644 --- a/utils/fileutil/filetutil.go +++ b/utils/fileutil/filetutil.go @@ -64,10 +64,10 @@ func CopyFile(src, dst string) error { return nil } -// ItemName returns the filename from the provided path -func ItemName(browser, item, ext string) string { +// Filename returns the filename from the provided path +func Filename(browser, dataType, ext string) string { replace := strings.NewReplacer(" ", "_", ".", "_", "-", "_") - return strings.ToLower(fmt.Sprintf("%s_%s.%s", replace.Replace(browser), item, ext)) + return strings.ToLower(fmt.Sprintf("%s_%s.%s", replace.Replace(browser), dataType, ext)) } func BrowserName(browser, user string) string {