2023-07-17 05:02:29 +02:00
|
|
|
package browserbiometrics
|
|
|
|
|
|
|
|
import (
|
2024-03-15 15:38:41 +01:00
|
|
|
_ "embed"
|
2023-07-17 05:02:29 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2023-12-22 15:29:58 +01:00
|
|
|
|
2023-12-30 18:53:01 +01:00
|
|
|
"github.com/quexten/goldwarden/agent/config"
|
2023-12-22 15:29:58 +01:00
|
|
|
"github.com/quexten/goldwarden/browserbiometrics/logging"
|
2023-07-17 05:02:29 +02:00
|
|
|
)
|
|
|
|
|
2024-03-15 15:38:41 +01:00
|
|
|
//go:embed mozilla-com.8bit.bitwarden.json
|
|
|
|
var templateMozilla string
|
|
|
|
|
|
|
|
//go:embed chrome-com.8bit.bitwarden.json
|
|
|
|
var templateChrome string
|
|
|
|
|
|
|
|
//go:embed goldwarden-proxy.sh
|
|
|
|
var proxyScript string
|
|
|
|
|
2023-12-30 21:00:36 +01:00
|
|
|
var chromiumPaths = []string{
|
|
|
|
"~/.config/google-chrome/",
|
|
|
|
"~/.config/google-chrome-beta/",
|
|
|
|
"~/.config/google-chrome-unstable/",
|
|
|
|
"~/.config/chromium/",
|
|
|
|
"~/.config/BraveSoftware/Brave-Browser/",
|
|
|
|
"~/.config/thorium/",
|
|
|
|
"~/.config/microsoft-edge-beta/",
|
|
|
|
"~/.config/microsoft-edge-dev/",
|
|
|
|
}
|
|
|
|
var mozillaPaths = []string{"~/.mozilla/", "~/.librewolf/", "~/.waterfox/"}
|
|
|
|
|
2023-07-17 05:02:29 +02:00
|
|
|
const appID = "com.quexten.bw-bio-handler"
|
|
|
|
|
|
|
|
var transportKey []byte
|
|
|
|
|
2024-03-15 15:59:35 +01:00
|
|
|
func Main(rtCfg *config.RuntimeConfig) error {
|
2023-12-22 15:29:58 +01:00
|
|
|
logging.Debugf("Starting browserbiometrics")
|
2024-03-15 15:59:35 +01:00
|
|
|
var err error
|
|
|
|
transportKey, err = generateTransportKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-12-22 15:29:58 +01:00
|
|
|
logging.Debugf("Generated transport key")
|
2023-07-17 05:02:29 +02:00
|
|
|
|
|
|
|
setupCommunication()
|
2024-03-15 15:59:35 +01:00
|
|
|
return readLoop(rtCfg)
|
2023-07-17 05:02:29 +02:00
|
|
|
}
|
|
|
|
|
2023-08-03 00:42:31 +02:00
|
|
|
func DetectAndInstallBrowsers() error {
|
|
|
|
var err error
|
2023-12-30 21:00:36 +01:00
|
|
|
|
|
|
|
// first, ensure the native messaging hosts dirs exist
|
|
|
|
for _, path := range chromiumPaths {
|
|
|
|
path = strings.ReplaceAll(path, "~", os.Getenv("HOME"))
|
|
|
|
_, err = os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = os.Stat(path + "NativeMessagingHosts/")
|
|
|
|
if err == nil {
|
|
|
|
fmt.Println("Native messaging host directory already exists: " + path + "NativeMessagingHosts/")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = os.MkdirAll(path+"NativeMessagingHosts/", 0755)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error creating native messaging host directory: " + err.Error())
|
|
|
|
} else {
|
|
|
|
fmt.Println("Created native messaging host directory: " + path + "NativeMessagingHosts/")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, path := range mozillaPaths {
|
|
|
|
path = strings.ReplaceAll(path, "~", os.Getenv("HOME"))
|
|
|
|
_, err = os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = os.Stat(path + "native-messaging-hosts/")
|
|
|
|
if err == nil {
|
|
|
|
fmt.Println("Native messaging host directory already exists: " + path + "native-messaging-hosts/")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = os.MkdirAll(path+"native-messaging-hosts/", 0755)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error creating native messaging host directory: " + err.Error())
|
|
|
|
} else {
|
|
|
|
fmt.Println("Created native messaging host directory: " + path + "native-messaging-hosts/")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-03 00:42:31 +02:00
|
|
|
err = detectAndInstallBrowsers(".config")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-12-30 21:00:36 +01:00
|
|
|
for _, path := range mozillaPaths {
|
|
|
|
path = strings.ReplaceAll(path, "~/", "")
|
|
|
|
err = detectAndInstallBrowsers(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-03 00:42:31 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-17 05:02:29 +02:00
|
|
|
func detectAndInstallBrowsers(startPath string) error {
|
|
|
|
home := os.Getenv("HOME")
|
|
|
|
err := filepath.Walk(home+"/"+startPath, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var tempPath string
|
|
|
|
if !strings.HasPrefix(path, home) {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
tempPath = strings.TrimPrefix(path, home)
|
|
|
|
}
|
2024-03-14 20:09:41 +01:00
|
|
|
if strings.Count(tempPath, "/") > 5 {
|
2023-07-17 05:02:29 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.IsDir() && info.Name() == "native-messaging-hosts" {
|
|
|
|
fmt.Printf("Found mozilla-like browser: %s\n", path)
|
2024-02-17 12:28:04 +01:00
|
|
|
|
2024-02-18 06:36:01 +01:00
|
|
|
fmt.Println("Removing old manifest and proxy script")
|
2024-03-15 15:29:44 +01:00
|
|
|
err = os.Chown(path+"/com.8bit.bitwarden.json", 7, 7)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.Remove(path + "/com.8bit.bitwarden.json")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.Chown(path+"/goldwarden-proxy.sh", 7, 7)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.Remove(path + "/goldwarden-proxy.sh")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-17 12:28:04 +01:00
|
|
|
|
2024-02-18 06:36:01 +01:00
|
|
|
fmt.Println("Writing new manifest")
|
2024-03-15 15:29:55 +01:00
|
|
|
manifest := strings.Replace(templateMozilla, "@PATH@", path+"/goldwarden-proxy.sh", 1)
|
2024-02-17 12:28:04 +01:00
|
|
|
err = os.WriteFile(path+"/com.8bit.bitwarden.json", []byte(manifest), 0444)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-18 06:36:01 +01:00
|
|
|
|
|
|
|
fmt.Println("Writing new proxy script")
|
2024-02-17 12:28:04 +01:00
|
|
|
err = os.WriteFile(path+"/goldwarden-proxy.sh", []byte(proxyScript), 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-17 05:02:29 +02:00
|
|
|
} else if info.IsDir() && info.Name() == "NativeMessagingHosts" {
|
|
|
|
fmt.Printf("Found chrome-like browser: %s\n", path)
|
2024-02-17 12:28:04 +01:00
|
|
|
|
2024-02-18 06:36:01 +01:00
|
|
|
fmt.Println("Removing old manifest and proxy script")
|
2024-03-15 15:29:44 +01:00
|
|
|
err = os.Chown(path+"/com.8bit.bitwarden.json", 7, 7)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.Remove(path + "/com.8bit.bitwarden.json")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.Chown(path+"/goldwarden-proxy.sh", 7, 7)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.Remove(path + "/goldwarden-proxy.sh")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-17 12:28:04 +01:00
|
|
|
|
2024-02-18 06:36:01 +01:00
|
|
|
fmt.Println("Writing new manifest")
|
2024-03-15 15:29:55 +01:00
|
|
|
manifest := strings.Replace(templateChrome, "@PATH@", path+"/goldwarden-proxy.sh", 1)
|
2024-02-17 12:28:04 +01:00
|
|
|
err = os.WriteFile(path+"/com.8bit.bitwarden.json", []byte(manifest), 0444)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-02-18 06:36:01 +01:00
|
|
|
|
|
|
|
fmt.Println("Writing new proxy script")
|
2024-02-17 12:28:04 +01:00
|
|
|
err = os.WriteFile(path+"/goldwarden-proxy.sh", []byte(proxyScript), 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-17 05:02:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|