feat: rename browser layout and add generics util function

This commit is contained in:
ᴍᴏᴏɴD4ʀᴋ
2022-04-08 19:06:04 +08:00
parent 6e05315ac6
commit 4551931c89
24 changed files with 1717 additions and 244 deletions
+22 -17
View File
@@ -4,12 +4,13 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"hack-browser-data/internal/data"
"hack-browser-data/internal/browingdata"
"hack-browser-data/internal/item"
"hack-browser-data/internal/utils/fileutil"
"hack-browser-data/internal/utils/typeutil"
)
type chromium struct {
@@ -21,24 +22,26 @@ type chromium struct {
itemPaths map[item.Item]string
}
// New 根据浏览器信息生成 Browser Interface
// New creates a new instance of chromium browser, fill item's path if item is exist.
func New(name, storage, profilePath string, items []item.Item) (*chromium, error) {
// 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)
if err != nil {
return nil, err
}
c := &chromium{
name: name,
storage: storage,
profilePath: profilePath,
items: items,
items: typeutil.Keys(itemsPaths),
itemPaths: itemsPaths,
}
absProfilePath := path.Join(homeDir, filepath.Clean(c.ProfilePath))
// TODO: Handle file path is not exist
if !isFileExist(absProfilePath) {
return nil, fmt.Errorf("%s profile path is not exist", absProfilePath)
}
itemsPaths, err := getChromiumItemPath(absProfilePath, c.items)
if err != nil {
return nil, err
}
c.itemPaths = itemsPaths
// new browsing data
return c, err
}
@@ -46,8 +49,9 @@ func (c *chromium) GetName() string {
return c.name
}
func (c *chromium) GetBrowsingData() []data.BrowsingData {
var browsingData []data.BrowsingData
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 {
@@ -95,9 +99,10 @@ func chromiumWalkFunc(items []item.Item, itemPaths map[item.Item]string) filepat
for _, it := range items {
switch {
case it.FileName() == info.Name():
if it == it.chromiumKey {
if it == item.ChromiumKey {
itemPaths[it] = path
}
// TODO: Handle file path is not in Default folder
if strings.Contains(path, "Default") {
itemPaths[it] = path
}
@@ -0,0 +1,45 @@
package chromium
import (
"bytes"
"crypto/sha1"
"errors"
"os/exec"
"golang.org/x/crypto/pbkdf2"
)
var (
ErrWrongSecurityCommand = errors.New("macOS wrong security command")
)
func (c *chromium) GetMasterKey() ([]byte, error) {
var (
cmd *exec.Cmd
stdout, stderr bytes.Buffer
)
// $ security find-generic-password -wa 'Chrome'
cmd = exec.Command("security", "find-generic-password", "-wa", c.storage)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return nil, err
}
if stderr.Len() > 0 {
return nil, errors.New(stderr.String())
}
temp := stdout.Bytes()
chromeSecret := temp[:len(temp)-1]
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
}
return nil, errors.New("macOS wrong security command")
}
@@ -0,0 +1 @@
package chromium
@@ -0,0 +1,30 @@
package chromium
import (
"encoding/base64"
"errors"
"github.com/smallstep/cli/utils"
"github.com/tidwall/gjson"
)
var (
errDecodeMasterKeyFailed = errors.New("decode master key failed")
)
func (c *chromium) GetMasterKey() ([]byte, error) {
keyFile, err := utils.ReadFile(item.TempChromiumKey)
if err != nil {
return nil, err
}
encryptedKey := gjson.Get(keyFile, "os_crypt.encrypted_key")
if encryptedKey.Exists() {
pureKey, err := base64.StdEncoding.DecodeString(encryptedKey.String())
if err != nil {
return nil, errDecodeMasterKeyFailed
}
c.browserInfo.masterKey, err = decrypter.DPApi(pureKey[5:])
return c.browserInfo.masterKey, err
}
return nil, nil
}