HackBrowserData/browser/chromium/chromium.go

153 lines
3.9 KiB
Go
Raw Normal View History

2022-04-01 07:00:56 +00:00
package chromium
import (
2022-06-05 06:04:39 +00:00
"io/fs"
2022-04-01 07:00:56 +00:00
"path/filepath"
"strings"
2023-03-10 06:52:26 +00:00
"github.com/moond4rk/HackBrowserData/browingdata"
"github.com/moond4rk/HackBrowserData/item"
"github.com/moond4rk/HackBrowserData/utils/fileutil"
"github.com/moond4rk/HackBrowserData/utils/typeutil"
2022-04-01 07:00:56 +00:00
)
2023-03-10 06:52:26 +00:00
type Chromium struct {
2022-04-01 07:00:56 +00:00
name string
storage string
profilePath string
masterKey []byte
items []item.Item
itemPaths map[item.Item]string
}
2023-03-10 06:52:26 +00:00
// 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{
2022-06-05 06:04:39 +00:00
name: name,
storage: storage,
profilePath: profilePath,
items: items,
2022-04-11 07:53:19 +00:00
}
2022-06-05 06:04:39 +00:00
multiItemPaths, err := c.getMultiItemPath(c.profilePath, c.items)
2022-04-11 07:53:19 +00:00
if err != nil {
return nil, err
}
2023-03-10 06:52:26 +00:00
chromiumList := make([]*Chromium, 0, len(multiItemPaths))
2022-06-05 06:04:39 +00:00
for user, itemPaths := range multiItemPaths {
2023-03-10 06:52:26 +00:00
chromiumList = append(chromiumList, &Chromium{
2022-06-05 06:04:39 +00:00
name: fileutil.BrowserName(name, user),
items: typeutil.Keys(itemPaths),
itemPaths: itemPaths,
storage: storage,
2022-06-05 06:04:39 +00:00
})
}
return chromiumList, nil
2022-04-01 07:00:56 +00:00
}
2023-03-10 06:52:26 +00:00
func (c *Chromium) Name() string {
2022-04-01 07:00:56 +00:00
return c.name
}
2023-03-10 06:52:26 +00:00
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
}
2023-03-10 06:52:26 +00:00
func (c *Chromium) copyItemToLocal() error {
2022-04-11 07:53:19 +00:00
for i, path := range c.itemPaths {
2022-04-29 15:59:37 +00:00
filename := i.String()
var err error
switch {
case fileutil.FolderExists(path):
if i == item.ChromiumLocalStorage {
err = fileutil.CopyDir(path, filename, "lock")
}
2022-04-29 15:59:37 +00:00
if i == item.ChromiumExtension {
err = fileutil.CopyDirHasSuffix(path, filename, "manifest.json")
}
2022-04-29 15:59:37 +00:00
default:
err = fileutil.CopyFile(path, filename)
}
if err != nil {
return err
2022-04-01 07:00:56 +00:00
}
}
return nil
}
2023-03-10 06:52:26 +00:00
func (c *Chromium) getMultiItemPath(profilePath string, items []item.Item) (map[string]map[item.Item]string, error) {
// multiItemPaths is a map of user to item path, map[profile 1][item's name & path key pair]
multiItemPaths := make(map[string]map[item.Item]string)
2022-06-05 06:04:39 +00:00
parentDir := fileutil.ParentDir(profilePath)
err := filepath.Walk(parentDir, chromiumWalkFunc(items, multiItemPaths))
2022-06-05 06:04:39 +00:00
if err != nil {
return nil, err
}
var keyPath string
var dir string
for userDir, v := range multiItemPaths {
for _, p := range v {
if strings.HasSuffix(p, item.ChromiumKey.FileName()) {
keyPath = p
dir = userDir
break
}
}
}
t := make(map[string]map[item.Item]string)
for userDir, v := range multiItemPaths {
if userDir == dir {
continue
}
t[userDir] = v
t[userDir][item.ChromiumKey] = keyPath
fillLocalStoragePath(t[userDir], item.ChromiumLocalStorage)
2022-06-05 06:04:39 +00:00
}
return t, nil
}
func chromiumWalkFunc(items []item.Item, multiItemPaths map[string]map[item.Item]string) filepath.WalkFunc {
2022-06-05 06:04:39 +00:00
return func(path string, info fs.FileInfo, err error) error {
for _, v := range items {
if info.Name() == v.FileName() {
2022-08-16 11:30:53 +00:00
if strings.Contains(path, "System Profile") {
2022-06-05 06:04:39 +00:00
continue
}
2022-08-16 11:30:53 +00:00
profileFolder := fileutil.ParentBaseDir(path)
if strings.Contains(filepath.ToSlash(path), "/Network/Cookies") {
profileFolder = fileutil.BaseDir(strings.ReplaceAll(filepath.ToSlash(path), "/Network/Cookies", ""))
}
if _, exist := multiItemPaths[profileFolder]; exist {
multiItemPaths[profileFolder][v] = path
2022-06-05 06:04:39 +00:00
} else {
2022-08-16 11:30:53 +00:00
multiItemPaths[profileFolder] = map[item.Item]string{v: path}
2022-06-05 06:04:39 +00:00
}
}
}
return err
}
}
func fillLocalStoragePath(itemPaths map[item.Item]string, storage item.Item) {
if p, ok := itemPaths[item.ChromiumHistory]; ok {
lsp := filepath.Join(filepath.Dir(p), storage.FileName())
if fileutil.FolderExists(lsp) {
itemPaths[item.ChromiumLocalStorage] = lsp
}
}
}