参考主线:https://zhuanlan.zhihu.com/p/464285672
1、在虚拟机中安装golang和libbpfgo,我使用虚拟机是fedora35版本,没有用最新的36beta的原因是它安装过程中运行一会儿就死机。
libbpfgo的源码路径(好像很容易被墙):
https://github.com/aquasecurity/libbpfgo
采用
make libbpfgo-static
安装。
安装遇到错误:
o: golang.org/x/[email protected]: unrecognized import path "golang.org/x/sys": https fetch: Get "https://golang.org/x/sys?go-get=1": dial tcp 172.217.163.49:443: connect: connection refused
找到解决办法:
mkdir -p $GOPATH/src/golang.org/x
cd $GOPATH/src/golang.org/x
git clone https://github.com/golang/sync.git
git clone https://github.com/golang/crypto.git
git clone https://github.com/golang/sys.git
git clone又遇到如下错误:
fatal: unable to access 'https://github.com/golang/sys.git/': OpenSSL SSL_read: Connection reset by peer, errno 104
解决办法如下:
[root@fedora x]# git config --global --unset http.proxy
[root@fedora x]# git config --global --unset https.proxy
[root@fedora x]# git clone https://github.com/golang/sys.git
之后返回运行make libbpfgo-static,仍报错:
unrecognized import path "golang.org/x/sys"
解决办法:
export GOPROXY=https://goproxy.io
source /etc/profile
按照例子编写内核部分代码:
hello.bpf.c
#include
#include
#include
SEC("kprobe/sys_execve")
int hello(void *ctx)
{
bpf_printk("I'm alive!");
return 0;
}
char _license[] SEC("license") = "GPL";
编译:
clang -O2 -c -target bpf hello.bpf.c
按照例子编写用户态部门代码:hellobpf.go
package main
import (
"C"
bpf "github.com/aquasecurity/tracee/libbpfgo"
)
import (
"fmt"
)
const sys_execve="__arm64_sys_execve"
func main() {
bpfModule, err := bpf.NewModuleFromFile("hello.bpf.o")
must(err)
defer bpfModule.Close()
err = bpfModule.BPFLoadObject()
must(err)
prog, err := bpfModule.GetProgram("hello")
must(err)
_, err = prog.AttachKprobe(sys_execve)
must(err)
bpf.TracePrint()
fmt.Println("Cleaning up")
}
func must(err error) {
if err != nil {
panic(err)
}
}
编译:
CC=clang CGO_CFLAGS="-I /usr/include/bpf" CGO_LDFLAGS="/usr/src/kernels/5.15.17-200.fc35.aarch64/tools/bpf/resolve_btfids/libbpf/libbpf.a" go build
编译失败:
[root@fedora study]# CC=clang CGO_CFLAGS="-I /usr/include/bpf" CGO_LDFLAGS="/home/s30/libbpfgo/output/libbpf.a" go build
hellobpf.go:6:9: no required module provides package github.com/aquasecurity/tracee/libbpfgo; to add it:
go get github.com/aquasecurity/tracee/libbpfgo
按照提示执行
go get github.com/aquasecurity/tracee/libbpfgo
不能成功。
无意中在上述链接中搜索“libbpfgo”,在源码中发现了libbpfgo的路径应该是
go get github.com/aquasecurity/libbpfgo
修改后进行尝试,
[root@fedora study]# CC=clang CGO_CFLAGS="-I /usr/include/bpf" CGO_LDFLAGS="/home/s30/libbpfgo/output/libbpf.a" go build
hellobpf.go:7:2: no required module provides package github.com/aquasecurity/libbpfgo; to add it:
go get github.com/aquasecurity/libbpfgo
[root@fedora study]# go get github.com/aquasecurity/libbpfgo
go: downloading github.com/aquasecurity/libbpfgo v0.1.1
go: downloading golang.org/x/sys v0.0.0-20210514084401-e8d321eab015
github.com/aquasecurity/libbpfgo imports
github.com/aquasecurity/libbpfgo/helpers imports
golang.org/x/sys/unix: unrecognized import path "golang.org/x/sys": https fetch: Get "https://golang.org/x/sys?go-get=1": dial tcp 142.251.42.241:443: connect: connection refused
查了下,据说是众所周知的原因造成的。
想了下,libbpfgo前面不是已经下载到本地了么?为什么还要在线导入呢?
跟着学习到这里,卡住了,需要补很多golang的课程。后来觉得这样跑偏了。找了本