之所以选择ubuntu
,是因为ubuntu
上的bcc
安装包是破坏的,只能用源码安装。源码安装又必须从github.com
上下载源码,而github.com
在国内是间断可访问。
按照https://github.com/iovisor/bcc/blob/master/INSTALL.md#ubuntu---source的指南,而我的ubuntu
是18.04 LTS。所以,先安装依赖
sudo apt-get -y install bison build-essential cmake flex git libedit-dev \
libllvm6.0 llvm-6.0-dev libclang-6.0-dev python zlib1g-dev libelf-dev libfl-dev python3-distutils netperf iperf
同时安装lua
sudo apt-get -y install luajit luajit-5.1-dev
git clone -v --branch v0.24.0 https://ghproxy.com/https://github.com/iovisor/bcc.git
可以看到上面是在
https://github.com/iovisor/bcc.git
前加上加速器https://ghproxy.com/
由于
18.04 LTS
编译v0.25.0
版本会出错,需要降到v0.24.0
版本。
修改.gitmodules
文件,加入加速器
[submodule "src/cc/libbpf"]
path = src/cc/libbpf
url = https://ghproxy.com/https://github.com/libbpf/libbpf.git
[submodule "libbpf-tools/bpftool"]
path = libbpf-tools/bpftool
url = https://ghproxy.com/https://github.com/libbpf/bpftool
[submodule "libbpf-tools/blazesym"]
path = libbpf-tools/blazesym
url = https://ghproxy.com/https://github.com/libbpf/blazesym
v0.25.0
的版本才增加后面两个子模块
mkdir bcc/build; cd bcc/build
cmake ..
make
sudo make install
cmake -DPYTHON_CMD=python3 .. # build python3 binding
pushd src/python/
make
sudo make install
popd
bcc
是安装在/usr/share/bcc
目录
cd /usr/share/bcc/tools
可能是ubuntu
版本的问题,运行execsnoop
会出现这样的错误
Traceback (most recent call last):
File "./execsnoop", line 21, in <module>
from bcc import BPF
ImportError: No module named bcc
运行是
sudo ./execsnoop
,eBPF
一定要用特权进程加载到内核。
所以,需要设置环境变量来运行。
sudo PYTHONPATH=$(dirname `find /usr/lib -name bcc`):$PYTHONPATH ./execsnoop
执行一下其它命令,看看结果:
[email protected]:~$ whoami
nainiu
[email protected]:~$ curl ifconfig.io
^C
[email protected]:~$ ps
PID TTY TIME CMD
48548 pts/2 00:00:00 bash
49439 pts/2 00:00:00 ps
[email protected]:~$ id
uid=1000(nainiu) gid=1000(nainiu) groups=1000(nainiu),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),116(lpadmin),126(sambashare)
[email protected]:~$ whomaiCommand 'whomai' not found, did you mean:
command 'whoami' from deb coreutils
Try: sudo apt install <deb name>
[email protected]:~$ whoami
nainiu
结果
PCOMM PID PPID RET ARGS
whoami 49414 48548 0 /usr/bin/whoami
curl 49416 48548 0 /usr/bin/curl ifconfig.io
nm-dhcp-helper 49418 29292 0 /usr/lib/NetworkManager/nm-dhcp-helper
nm-dispatcher 49422 1 0 /usr/lib/NetworkManager/nm-dispatcher
01-ifupdown 49437 49422 0 /etc/NetworkManager/dispatcher.d/01-ifupdown ens33 dhcp4-change
ps 49439 48548 0 /bin/ps
id 49453 48548 0 /usr/bin/id
command-not-fou 49455 49454 0 /usr/lib/command-not-found -- whomai
snap 49456 49455 0 /usr/bin/snap advise-snap --format=json --command whomai
snap 49456 49455 0 /snap/snapd/current/usr/bin/snap advise-snap --format=json --command whomai
whoami 49469 48548 0 /usr/bin/whoami
暗号:13605