From 2f5482736b18aa6f117767ec6c588c0c93ebccec Mon Sep 17 00:00:00 2001 From: polunzh Date: Sat, 12 Dec 2020 11:12:52 +0800 Subject: [PATCH] add file type and filepath param --- cmd/cmd.go | 62 ++++++++------------- core/data/parse.go | 135 +++++++++++++++++++++++---------------------- 2 files changed, 90 insertions(+), 107 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index f2b86ad..3d4e682 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -3,7 +3,6 @@ package cmd import ( "hack-browser-data/core" "hack-browser-data/log" - "hack-browser-data/utils" "os" "strings" @@ -14,6 +13,8 @@ var ( browser string exportDir string outputFormat string + fileType string + filepath string verbose bool compress bool ) @@ -30,6 +31,8 @@ func Execute() { &cli.StringFlag{Name: "browser", Aliases: []string{"b"}, Destination: &browser, Value: "all", Usage: "Available browsers: all|" + strings.Join(core.ListBrowser(), "|")}, &cli.StringFlag{Name: "results-dir", Aliases: []string{"dir"}, Destination: &exportDir, Value: "results", Usage: "Export dir"}, &cli.StringFlag{Name: "format", Aliases: []string{"f"}, Destination: &outputFormat, Value: "csv", Usage: "Format, csv|json|console"}, + &cli.StringFlag{Name: "file-type", Aliases: []string{"ft"}, Destination: &fileType, Value: "csv", Usage: "File Type, bookmark|cookie|history|password|creditcard"}, + &cli.StringFlag{Name: "filepath", Destination: &filepath, Value: "csv", Usage: "Filepath of the file type"}, }, HideHelpCommand: true, Action: func(c *cli.Context) error { @@ -43,56 +46,35 @@ func Execute() { if err != nil { log.Error(err) } - err = utils.MakeDir(exportDir) + + browser := browsers[0] + err = browser.InitSecretKey() if err != nil { log.Error(err) } - for _, browser := range browsers { - err := browser.InitSecretKey() + item, err := browser.GetItem(fileType) + if err != nil { + log.Error(err) + } + name := browser.GetName() + key := browser.GetSecretKey() + switch browser.(type) { + case *core.Chromium: + err := item.ChromeParse(key, filepath) if err != nil { log.Error(err) } - // default select all items - // you can get single item with browser.GetItem(itemName) - items, err := browser.GetAllItems() + case *core.Firefox: + err := item.FirefoxParse(filepath) if err != nil { log.Error(err) } - name := browser.GetName() - key := browser.GetSecretKey() - for _, item := range items { - err := item.CopyDB() - if err != nil { - log.Error(err) - } - switch browser.(type) { - case *core.Chromium: - err := item.ChromeParse(key) - if err != nil { - log.Error(err) - } - case *core.Firefox: - err := item.FirefoxParse() - if err != nil { - log.Error(err) - } - } - err = item.Release() - if err != nil { - log.Error(err) - } - err = item.OutPut(outputFormat, name, exportDir) - if err != nil { - log.Error(err) - } - } } - if compress { - err = utils.Compress(exportDir) - if err != nil { - log.Error(err) - } + err = item.OutPut(outputFormat, name, exportDir) + if err != nil { + log.Error(err) } + return nil }, } diff --git a/core/data/parse.go b/core/data/parse.go index 520aea5..2b8e535 100644 --- a/core/data/parse.go +++ b/core/data/parse.go @@ -20,19 +20,19 @@ import ( type Item interface { // ChromeParse parse chrome items, Password and Cookie need secret key - ChromeParse(key []byte) error + ChromeParse(key []byte, filepath string) error // FirefoxParse parse firefox items - FirefoxParse() error + FirefoxParse(filepath string) error // OutPut file name and format type OutPut(format, browser, dir string) error // CopyDB is copy item db file to current dir - CopyDB() error + // CopyDB() error // Release is delete item db file - Release() error + // Release() error } const ( @@ -78,8 +78,8 @@ func NewBookmarks(main, sub string) Item { return &bookmarks{mainPath: main} } -func (b *bookmarks) ChromeParse(key []byte) error { - bookmarks, err := utils.ReadFile(ChromeBookmarkFile) +func (b *bookmarks) ChromeParse(key []byte, filepath string) error { + bookmarks, err := utils.ReadFile(filepath) if err != nil { return err } @@ -115,7 +115,7 @@ func getBookmarkChildren(value gjson.Result, b *bookmarks) (children gjson.Resul return children } -func (b *bookmarks) FirefoxParse() error { +func (b *bookmarks) FirefoxParse(filepath string) error { var ( err error keyDB *sql.DB @@ -123,7 +123,7 @@ func (b *bookmarks) FirefoxParse() error { tempMap map[int64]string bookmarkUrl string ) - keyDB, err = sql.Open("sqlite3", FirefoxDataFile) + keyDB, err = sql.Open("sqlite3", filepath) if err != nil { return err } @@ -163,13 +163,13 @@ func (b *bookmarks) FirefoxParse() error { return nil } -func (b *bookmarks) CopyDB() error { - return copyToLocalPath(b.mainPath, filepath.Base(b.mainPath)) -} +// func (b *bookmarks) CopyDB() error { +// return copyToLocalPath(b.mainPath, filepath.Base(b.mainPath)) +// } -func (b *bookmarks) Release() error { - return os.Remove(filepath.Base(b.mainPath)) -} +// func (b *bookmarks) Release() error { +// return os.Remove(filepath.Base(b.mainPath)) +// } func (b *bookmarks) OutPut(format, browser, dir string) error { sort.Slice(b.bookmarks, func(i, j int) bool { @@ -197,9 +197,10 @@ func NewCookies(main, sub string) Item { return &cookies{mainPath: main} } -func (c *cookies) ChromeParse(secretKey []byte) error { +// 解析 chrome cookie +func (c *cookies) ChromeParse(secretKey []byte, filepath string) error { c.cookies = make(map[string][]cookie) - cookieDB, err := sql.Open("sqlite3", ChromeCookieFile) + cookieDB, err := sql.Open("sqlite3", filepath) if err != nil { return err } @@ -255,9 +256,9 @@ func (c *cookies) ChromeParse(secretKey []byte) error { return nil } -func (c *cookies) FirefoxParse() error { +func (c *cookies) FirefoxParse(filepath string) error { c.cookies = make(map[string][]cookie) - cookieDB, err := sql.Open("sqlite3", FirefoxCookieFile) + cookieDB, err := sql.Open("sqlite3", filepath) if err != nil { return err } @@ -299,13 +300,13 @@ func (c *cookies) FirefoxParse() error { return nil } -func (c *cookies) CopyDB() error { - return copyToLocalPath(c.mainPath, filepath.Base(c.mainPath)) -} +// func (c *cookies) CopyDB() error { +// return copyToLocalPath(c.mainPath, filepath.Base(c.mainPath)) +// } -func (c *cookies) Release() error { - return os.Remove(filepath.Base(c.mainPath)) -} +// func (c *cookies) Release() error { +// return os.Remove(filepath.Base(c.mainPath)) +// } func (c *cookies) OutPut(format, browser, dir string) error { switch format { @@ -330,8 +331,8 @@ func NewHistoryData(main, sub string) Item { return &historyData{mainPath: main} } -func (h *historyData) ChromeParse(key []byte) error { - historyDB, err := sql.Open("sqlite3", ChromeHistoryFile) +func (h *historyData) ChromeParse(key []byte, filepath string) error { + historyDB, err := sql.Open("sqlite3", filepath) if err != nil { return err } @@ -370,7 +371,7 @@ func (h *historyData) ChromeParse(key []byte) error { return nil } -func (h *historyData) FirefoxParse() error { +func (h *historyData) FirefoxParse(filepath string) error { var ( err error keyDB *sql.DB @@ -378,7 +379,7 @@ func (h *historyData) FirefoxParse() error { tempMap map[int64]string ) tempMap = make(map[int64]string) - keyDB, err = sql.Open("sqlite3", FirefoxDataFile) + keyDB, err = sql.Open("sqlite3", filepath) if err != nil { return err } @@ -422,13 +423,13 @@ func (h *historyData) FirefoxParse() error { return nil } -func (h *historyData) CopyDB() error { - return copyToLocalPath(h.mainPath, filepath.Base(h.mainPath)) -} +// func (h *historyData) CopyDB() error { +// return copyToLocalPath(h.mainPath, filepath.Base(h.mainPath)) +// } -func (h *historyData) Release() error { - return os.Remove(filepath.Base(h.mainPath)) -} +// func (h *historyData) Release() error { +// return os.Remove(filepath.Base(h.mainPath)) +// } func (h *historyData) OutPut(format, browser, dir string) error { sort.Slice(h.history, func(i, j int) bool { @@ -461,8 +462,8 @@ func NewCPasswords(main, sub string) Item { return &passwords{mainPath: main} } -func (p *passwords) ChromeParse(key []byte) error { - loginDB, err := sql.Open("sqlite3", ChromePasswordFile) +func (p *passwords) ChromeParse(key []byte, filepath string) error { + loginDB, err := sql.Open("sqlite3", filepath) if err != nil { return err } @@ -514,7 +515,7 @@ func (p *passwords) ChromeParse(key []byte) error { return nil } -func (p *passwords) FirefoxParse() error { +func (p *passwords) FirefoxParse(filepath string) error { globalSalt, metaBytes, nssA11, nssA102, err := getFirefoxDecryptKey() if err != nil { return err @@ -574,27 +575,27 @@ func (p *passwords) FirefoxParse() error { return nil } -func (p *passwords) CopyDB() error { - err := copyToLocalPath(p.mainPath, filepath.Base(p.mainPath)) - if err != nil { - log.Error(err) - } - if p.subPath != "" { - err = copyToLocalPath(p.subPath, filepath.Base(p.subPath)) - } - return err -} - -func (p *passwords) Release() error { - err := os.Remove(filepath.Base(p.mainPath)) - if err != nil { - log.Error(err) - } - if p.subPath != "" { - err = os.Remove(filepath.Base(p.subPath)) - } - return err -} +// func (p *passwords) CopyDB() error { +// err := copyToLocalPath(p.mainPath, filepath.Base(p.mainPath)) +// if err != nil { +// log.Error(err) +// } +// if p.subPath != "" { +// err = copyToLocalPath(p.subPath, filepath.Base(p.subPath)) +// } +// return err +// } + +// func (p *passwords) Release() error { +// err := os.Remove(filepath.Base(p.mainPath)) +// if err != nil { +// log.Error(err) +// } +// if p.subPath != "" { +// err = os.Remove(filepath.Base(p.subPath)) +// } +// return err +// } func (p *passwords) OutPut(format, browser, dir string) error { sort.Sort(p) @@ -620,13 +621,13 @@ func NewCCards(main string, sub string) Item { return &creditCards{mainPath: main} } -func (c *creditCards) FirefoxParse() error { +func (c *creditCards) FirefoxParse(filepath string) error { return nil // FireFox does not have a credit card saving feature } -func (c *creditCards) ChromeParse(secretKey []byte) error { +func (c *creditCards) ChromeParse(secretKey []byte, filepath string) error { c.cards = make(map[string][]card) - creditDB, err := sql.Open("sqlite3", ChromeCreditFile) + creditDB, err := sql.Open("sqlite3", filepath) if err != nil { return err } @@ -673,13 +674,13 @@ func (c *creditCards) ChromeParse(secretKey []byte) error { return nil } -func (c *creditCards) CopyDB() error { - return copyToLocalPath(c.mainPath, filepath.Base(c.mainPath)) -} +// func (c *creditCards) CopyDB() error { +// return copyToLocalPath(c.mainPath, filepath.Base(c.mainPath)) +// } -func (c *creditCards) Release() error { - return os.Remove(filepath.Base(c.mainPath)) -} +// func (c *creditCards) Release() error { +// return os.Remove(filepath.Base(c.mainPath)) +// } func (c *creditCards) OutPut(format, browser, dir string) error { switch format {