HackBrowserData/internal/browser/chromium/chromium.go

113 lines
2.5 KiB
Go
Raw Normal View History

2022-04-01 07:00:56 +00:00
package chromium
import (
2022-04-19 11:35:30 +00:00
"errors"
2022-04-01 07:00:56 +00:00
"io/ioutil"
"os"
"path/filepath"
"strings"
"hack-browser-data/internal/browingdata"
2022-04-02 06:47:12 +00:00
"hack-browser-data/internal/item"
"hack-browser-data/internal/utils/fileutil"
"hack-browser-data/internal/utils/typeutil"
2022-04-01 07:00:56 +00:00
)
type chromium struct {
name string
storage string
profilePath string
masterKey []byte
items []item.Item
itemPaths map[item.Item]string
}
2022-04-19 11:35:30 +00:00
var (
ErrProfilePathNotFound = errors.New("profile path not found")
)
2022-04-11 07:53:19 +00:00
// New create instance of chromium browser, fill item's path if item is existed.
2022-04-02 06:47:12 +00:00
func New(name, storage, profilePath string, items []item.Item) (*chromium, error) {
2022-04-11 07:53:19 +00:00
c := &chromium{
name: name,
storage: storage,
}
2022-04-01 07:00:56 +00:00
// TODO: Handle file path is not exist
if !fileutil.FolderExists(profilePath) {
2022-04-19 11:35:30 +00:00
return nil, ErrProfilePathNotFound
2022-04-01 07:00:56 +00:00
}
2022-04-11 07:53:19 +00:00
itemsPaths, err := c.getItemPath(profilePath, items)
if err != nil {
return nil, err
}
2022-04-11 07:53:19 +00:00
c.profilePath = profilePath
c.itemPaths = itemsPaths
c.items = typeutil.Keys(itemsPaths)
2022-04-01 07:00:56 +00:00
return c, err
}
2022-04-11 11:57:40 +00:00
func (c *chromium) Name() string {
2022-04-01 07:00:56 +00:00
return c.name
}
func (c *chromium) BrowsingData() (*browingdata.Data, error) {
2022-04-11 07:53:19 +00:00
b := browingdata.New(c.items)
if err := c.copyItemToLocal(); err != nil {
return nil, err
}
2022-04-11 11:57:40 +00:00
masterKey, err := c.GetMasterKey()
if err != nil {
return nil, err
}
c.masterKey = masterKey
2022-04-11 07:53:19 +00:00
if err := b.Recovery(c.masterKey); err != nil {
return nil, err
2022-04-01 07:00:56 +00:00
}
2022-04-11 07:53:19 +00:00
return b, nil
2022-04-01 07:00:56 +00:00
}
2022-04-11 07:53:19 +00:00
func (c *chromium) copyItemToLocal() error {
for i, path := range c.itemPaths {
// var dstFilename = item.TempName()
var filename = i.String()
2022-04-01 07:00:56 +00:00
// TODO: Handle read file error
2022-04-11 07:53:19 +00:00
d, err := ioutil.ReadFile(path)
2022-04-01 07:00:56 +00:00
if err != nil {
return err
2022-04-01 07:00:56 +00:00
}
2022-04-11 07:53:19 +00:00
err = ioutil.WriteFile(filename, d, 0777)
2022-04-01 07:00:56 +00:00
if err != nil {
return err
}
}
return nil
}
2022-04-11 07:53:19 +00:00
func (c *chromium) getItemPath(profilePath string, items []item.Item) (map[item.Item]string, error) {
2022-04-01 07:00:56 +00:00
var itemPaths = make(map[item.Item]string)
2022-04-14 15:36:11 +00:00
parentDir := fileutil.ParentDir(profilePath)
baseDir := fileutil.BaseDir(profilePath)
err := filepath.Walk(parentDir, chromiumWalkFunc(items, itemPaths, baseDir))
2022-04-01 07:00:56 +00:00
return itemPaths, err
}
2022-04-14 15:36:11 +00:00
func chromiumWalkFunc(items []item.Item, itemPaths map[item.Item]string, baseDir string) filepath.WalkFunc {
2022-04-01 07:00:56 +00:00
return func(path string, info os.FileInfo, err error) error {
for _, it := range items {
switch {
2022-04-02 06:47:12 +00:00
case it.FileName() == info.Name():
if it == item.ChromiumKey {
2022-04-01 07:00:56 +00:00
itemPaths[it] = path
}
2022-04-14 15:36:11 +00:00
if strings.Contains(path, baseDir) {
2022-04-01 07:00:56 +00:00
itemPaths[it] = path
}
}
}
return err
}
}