Skip to content

Update probe_bash.go #479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions user/module/probe_bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ package module
import (
"bytes"
"context"
"debug/elf"
"ecapture/assets"
"ecapture/user/config"
"ecapture/user/event"
"errors"
"fmt"
"log"
"math"

"github.com/cilium/ebpf"
manager "github.com/gojue/ebpfmanager"
"golang.org/x/sys/unix"
"log"
"math"
)

type MBashProbe struct {
Expand Down Expand Up @@ -138,16 +140,47 @@ func (b *MBashProbe) setupManagers() {
binaryPath = "/bin/bash"
}

b.logger.Printf("%s\tHOOK binrayPath:%s, FunctionName:readline\n", b.Name(), binaryPath)
var readlineFuncName string // 将默认hook函数改为readline_internal_teardown说明:https://github.com/gojue/ecapture/pull/479

getReadlineFuncName := func(binaryPath string) string {
//打开二进制文件,在符号表中查找是否有readline_internal_teardown。
file, err := elf.Open(binaryPath)
if err != nil {
log.Fatal(err)
}
defer file.Close()

symbols, err := file.DynamicSymbols()
if err != nil {
log.Fatal(err)
}

targetSymbol := "readline_internal_teardown"
found := false
for _, sym := range symbols {
if sym.Name == targetSymbol {
found = true
break
}
}
if found {
return "readline_internal_teardown"
} else {
return "readline"
}
}
readlineFuncName = getReadlineFuncName(binaryPath)

b.logger.Printf("%s\tHOOK binrayPath:%s, FunctionName:%s\n", b.Name(), binaryPath, readlineFuncName)
b.logger.Printf("%s\tHOOK binrayPath:%s, FunctionName:execute_command\n", b.Name(), binaryPath)

b.bpfManager = &manager.Manager{
Probes: []*manager.Probe{
{
Section: "uretprobe/bash_readline",
EbpfFuncName: "uretprobe_bash_readline",
AttachToFuncName: "readline",
//UprobeOffset: 0x8232, //若找不到 readline 函数,则使用offset便宜地址方式
AttachToFuncName: readlineFuncName,
//UprobeOffset: 0x8232, //若找不到 readline 函数,则使用offset偏移地址方式
BinaryPath: binaryPath, // 可能是 /bin/bash 也可能是 readline.so的真实地址
},
{
Expand Down