From d288a6e0089f208946388be20a7b48ffecbbdcc0 Mon Sep 17 00:00:00 2001 From: robertmin1 <104002271+robertmin1@users.noreply.github.com> Date: Wed, 31 May 2023 01:25:26 +0300 Subject: [PATCH 1/2] Dump Stack Trace Dump Stack Trace --- main.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/main.go b/main.go index 8f7d4f3..fa156ad 100644 --- a/main.go +++ b/main.go @@ -33,14 +33,17 @@ import ( "encoding/hex" "errors" "fmt" + go_log "log" "math" "net" "os" "os/exec" + "runtime" "strconv" "strings" "sync" "syscall" + "time" "unsafe" "github.com/hlandau/dexlogconfig" @@ -192,6 +195,11 @@ func HandleConnect(task strace.Task, record *strace.TraceRecord, program *exec.C if IsAddressAllowed(address, cfg) { //nolint log.Infof("Connecting to %v", IPPort) } else { + // Dump Stack Trace and Process Information + if err := DumpStackTrace(record.PID); err != nil { + return err + } + if cfg.LogLeaks { log.Warnf("Proxy Leak detected, but allowed : %v", IPPort) @@ -530,6 +538,44 @@ func Socksify(args strace.SyscallArguments, record *strace.TraceRecord, t strace return nil // Support more proxies } +func DumpStackTrace(pid int) error { + // Create or open the log file in append mode + logFile, err := os.OpenFile("stack_trace.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) //nolint + if err != nil { + return err + } + + defer logFile.Close() + // Set the log output to the log file + go_log.SetOutput(logFile) + + commPath := fmt.Sprintf("/proc/%d/cmdline", pid) + + commBytes, err := os.ReadFile(commPath) + if err != nil { + return err + } + // Split the contents by null byte to separate command and arguments + cmdline := strings.Split(string(commBytes), "\x00") + + // Add a separator with date and time for the new instance of the program + separator := fmt.Sprintf("-------------------- New Instance: %s --------------------", time.Now().Format("2006-01-02 15:04:05")) + go_log.Println(separator) + + // Add the PID and Process Name and Args. + go_log.Printf("PID :%v", pid) + go_log.Printf("Program and Arguments:%v\n", cmdline) + + // Get the stack trace + stack := make([]byte, 8192) + length := runtime.Stack(stack, true) + + // Write the stack trace to the log file + go_log.Println(string(stack[:length])) + + return nil +} + func (i FullAddress) String() string { switch { case i.IP == nil: From ec07b0dc979f6fd101ef471916275c59d2a3974f Mon Sep 17 00:00:00 2001 From: robertmin1 <104002271+robertmin1@users.noreply.github.com> Date: Wed, 31 May 2023 01:52:26 +0300 Subject: [PATCH 2/2] dump stack trace --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index fa156ad..de986ca 100644 --- a/main.go +++ b/main.go @@ -559,7 +559,7 @@ func DumpStackTrace(pid int) error { cmdline := strings.Split(string(commBytes), "\x00") // Add a separator with date and time for the new instance of the program - separator := fmt.Sprintf("-------------------- New Instance: %s --------------------", time.Now().Format("2006-01-02 15:04:05")) + separator := fmt.Sprintf("----------- New Instance: %s ------------", time.Now().Format("2006-01-02 15:04:05")) go_log.Println(separator) // Add the PID and Process Name and Args.