原文地址: https://www.ebpf.top/post/tracee_intro/
1. Tracee 介紹
1.1 Tracee 介紹
Tracee 是一個用 于 Linux 的運行時安全和取證工具,它使用 Linux eBPF 技術在運行時跟蹤系統和應用程式,并分析收集的事件以檢測可疑的行為模式,Tracee 以 Docker 鏡像的形式交付,監控作業系統并根據預定義的行為模式集檢測可疑行為,完整檔案參見:https://aquasecurity.github.io/tracee/dev/,
Tracee 由以下子專案組成:
-
Trace-eBPF - 使用 eBPF 進行 Linux 追蹤和取證,BPF 底層代碼實作參見 tracee.bpf.c;
-
Trace-Rules - 運行時安全檢測引擎,在真實的使用場景中通過管道的方式從 Trace-eBPF 中接受資料,具體運行命令 如下:
$TRACEE_EBPF_EXE --output=format:gob --security-alerts | $TRACEE_RULES_EXE --input-tracee=file:stdin --input-tracee=format:gob $@ -
libbpfgo 基于 Linux libbpf 的 Go 的 eBPF 庫,Tracee 程式通過 cgo 訪問 libbpf C 語言庫;
Tracee 運行系統最低內核版本要求 >= 4.18,可以根據是否開啟 CO-RE 進行 BPF 底層代碼編譯,運行 Tracee 需要足夠的權限才能運行,測驗可以直接使用 root 用戶運行或者在 Docker 模型下使用 --privileged 模式運行,
Tracee 在最近的版本中增加了一個非常有意思的功能抓取 --capture,可以將讀寫檔案、記憶體檔案、網路資料包等進行抓取并保存,該功能主要是用于取證相關功能,
1.2 Tracee 與 Falco 的區別
看到 Tracee 這款基于 eBPF 技術的安全產品,很自然想到的對應產品是 Falco,如果你對 Falco 不了解,那么可以參見 這篇文章, Tracee 與 Falco 還是有諸多類似的功能,只是從實作和架構上看, Tracee 更加直接和簡單,也沒有特別復雜的規則引擎,作者給出的與 Falco 定位不同如下,更加詳細的可參見 這里:
Falco 是一個規則引擎,基于 sysdig 的開放源代碼,它從 sysdig 獲取原始事件,并與 yaml 檔案中 falco 語言定義的規則相匹配,相比之下,Tracee 從 eBPF 中追蹤事件,但不執行基于這些事件的規則,
我們撰寫 Tracee 時考慮到了以下幾點:
- Tracee 從一開始就被設計成一個基于 eBPF 的輕量級事件追蹤器,
- Tracee 建立在 bcc 的基礎上,并沒有重寫低級別的 BPF 介面,
- Tracee 被設計成易于擴展,例如,在 tracee 中添加對新的系統呼叫的支持就像添加兩行代碼一樣簡單,在這里你可以描述系統呼叫的名稱和引數型別,
- 其他事件也被支持,比如內部內核函式,我們現在已經支持 cap_capable,我們正在增加對 security_bprm_check lsm 鉤子的支持,由于 lsm 安全鉤子是安全的戰略要點,我們計劃在不久的將來增加更多這樣的鉤子,
其實,從使用的場景上來說 Tracee 與 Falco 不是非 A 即 B 的功能,在 Tracee 也可以與 FalcoSideKick 進行集成,作為一個事件輸入源使用,
從下面兩者架構圖的對比,我們也可以略微熟悉一二, Tracee 更加直接和簡潔,規則引擎的維護也不是重點,而且規則引擎恰恰是 Falco 的重點,
Falco 的架構圖如下:

而 Tracee 的架構圖如下:

2. Tracee 的作業原理
Tracee 中的 tracee-ebpf 模塊的核心能力包括: 事件跟蹤(trace)、抓取(capture)和輸出(output)三個能力,
tracee-ebpf 的核心能力在于底層 eBPF 程式抓取事件的能力,tracee-ebpf 默認實作了諸多的事件抓取功能,可以通過 trace -l 參看到底層支持的函式全集( 0.6.1 版本大概 390 個函式,格式如下:
$ sudo docker run --name tracee-only --rm --privileged --pid=host -v /lib/modules/:/lib/modules/:ro -v /usr/src:/usr/src:ro -v /tmp/tracee:/tmp/tracee -v /boot/:/boot tracee -l
System Calls: Sets: Arguments:
____________ ____ _________
read [syscalls fs fs_read_write] (int fd, void* buf, size_t count)
write [syscalls fs fs_read_write] (int fd, void* buf, size_t count)
open [default syscalls fs fs_file_ops] (const char* pathname, int flags, mode_t mode)
openat [default syscalls fs fs_file_ops] (int dirfd, const char* pathname, int flags, mode_t mode)
...
- 第一列為系統呼叫函式名字;
- 第二列為該函式歸屬為的子類(注可歸屬多個,比如 read 函式,歸屬于 syscalls/fs/fs_read_write 3 個子類,除了 fs 外,net 集合中也包含了許多的跟蹤函式);
- 第三列為該函式的原型,可以使用引數中的欄位進行過濾,支持特定的運算,比如
==!=等常見的邏輯運算子,對于字串也支持通配符操作;
這里簡單介紹兩個樣例,更加詳細的可以使用 tracee --trace help 命令查看,
--trace s=fs --trace e!=open,openat跟蹤 fs 集合中的所有事件,但是不包括open,openat兩個函式;--trace openat.pathname!=/tmp/1,/bin/ls這里表示不跟蹤 openat 事件中,pathname 為/tmp/1,/bin/ls的事件,注意這里的openat.pathname為跟蹤函式名與函式引數的組合;
以上跟蹤事件的過濾條件通過介面設定進內核中對應的 map 結構中,在完成過濾和事件跟蹤以后,通過 perf_event 的方式上報到用戶空間程式中,可以保存到奧檔案后續進行處理,或者直接通過管道發送至 tracee-rule 進行決議和進行更高級別的上報,詳細參見上一章節的架構圖,
3. Tracee 功能測驗
3.1 功能測驗
測驗前需要保證內核版本及相關條件滿足最小要求:
- 內核 >= 4.18,可選項啟用了 BTF,BTF 啟用可以通過 /boot/config* 檔案檢查 CONFIG_DEBUG_INFO_BTF 是否啟用;(grep CONFIG_DEBUG_INFO_BTF /boot/config-xx-yyy)
- Linux 內核頭檔案已經安裝,Ubuntu/Debian/Arch/Manjaro 中為 linux-headers 包,CentOS/Fedora 中為 kernel-headers 和 kernel-devel 兩個包;
如果內核啟用了 BTF 功能,可以直接使用官方提供的鏡像進行測驗:
$ sudo docker run --name tracee --rm --privileged -it aquasec/tracee:latest trace
如果系統未啟用 BTF 功能,則需要加載內核 /lib/modules/ 和 /usr/src 目錄,并運行以下命令:
$ sudo docker run --name tracee --rm --privileged --pid=host -v /boot:/boot:ro -v /lib/modules/:/lib/modules/:ro -v /usr/src:/usr/src:ro -v /tmp/tracee:/tmp/tracee -e TINI_SUBREAPER=true aquasec/tracee
Loaded signature(s): [TRC-1 TRC-2 TRC-3 TRC-4 TRC-5 TRC-6 TRC-7]
掛載 /boot 目錄方便讀取 /boot/config* 等相關檔案,-e TINI_SUBREAPER=true 是為了讓 tini 作為父行程進行子行程回收的能力,
在運行以后我們可以發現最后有一系列簽名輸出 TRC-1 TRC-2 TRC-3 TRC-4 TRC-5 TRC-6 TRC-7,這些簽名代表了對應檢測的選項,我們可以使用 --list 選項進行查看,結果如下:
# docker run --name tracee --rm --privileged --pid=host -v /boot:/boot:ro -v /lib/modules/:/lib/modules/:ro -v /usr/src:/usr/src:ro -v /tmp/tracee:/tmp/tracee -e TINI_SUBREAPER=true aquasec/tracee --list
Loaded signature(s): [TRC-1 TRC-2 TRC-3 TRC-4 TRC-5 TRC-6 TRC-7]
ID NAME VERSION DESCRIPTION
TRC-1 Standard Input/Output Over Socket 0.1.0 Redirection of process's standard input/output to socket
TRC-2 Anti-Debugging 0.1.0 Process uses anti-debugging technique to block debugger
TRC-3 Code injection 0.1.0 Possible code injection into another process
TRC-4 Dynamic Code Loading 0.1.0 Writing to executable allocated memory region
TRC-5 Fileless Execution 0.1.0 Executing a process from memory, without a file in the disk
TRC-6 kernel module loading 0.1.0 Attempt to load a kernel module detection
TRC-7 LD_PRELOAD 0.1.0 Usage of LD_PRELOAD to allow hooks on process
使用 elfexec 進行測驗:
# From https://github.com/abbat/elfexec/releases
$ wget https://github.com/abbat/elfexec/releases/download/v0.3/elfexec.x64.glibc.xz
$ chmod u+x elfexec.x64.glib && mv ./elfexec.x64.glibc ./elfexec
$ echo 'IyEvYmluL3NoCmVjaG8gIkhlbGxvISIK' | base64 -d|./elfexec
Hello!
$ echo 'IyEvYmluL3NoCmVjaG8gIkhlbGxvISIK' | base64 -d
#!/bin/sh
echo "Hello!"
上述命令就是將一個輸出 echo hello 的腳本重定向到 elfexec 進行執行, 在上述命令運行后,輸出以下資訊:
# docker run --name tracee --rm --privileged --pid=host -v /boot:/boot:ro -v /lib/modules/:/lib/modules/:ro -v /usr/src:/usr/src:ro -v /tmp/tracee:/tmp/tracee -e TINI_SUBREAPER=true aquasec/tracee
Loaded signature(s): [TRC-1 TRC-2 TRC-3 TRC-4 TRC-5 TRC-6 TRC-7]
*** Detection ***
Time: 2021-09-10T09:10:25Z
Signature ID: TRC-5
Signature: Fileless Execution
Data: map[]
Command: elfexec
Hostname: VM-0-14-ubuntu
這里我們看到測驗觸發的簽名為 TRC-5, 詳細情況為 ”Fileless Execution“,命令為 ”elfexec“,
4. 原始碼編譯 eBPF 程式
4.1 鏡像方式編譯
Tracee 支持我們自己基于系統編譯 eBPF 程式,然后將編譯后的 eBPF 位元組碼傳遞至 Docker 鏡像進行運行,推薦 eBPF 程式編譯通過 Docker 鏡像進行,如果使用本機環境編譯需要安裝并保證 GNU Make >= 4.3 - clang >= 11,
推薦編譯和運行基于 Ubuntu 系列的系統(Ubuntu 20.04),猜測 Tracee 的主要測驗環境應該是在 Ubuntu 系列中,CentOS 系列測驗偏少,
推薦 Ubuntu 20.04 版本,在 CentOS 5.4 內核中編譯遇到不少問題,主要是環境差異導致,包括內核編譯目錄軟連接,dockerfile 等問題,參見我提交的 pr
$ git clone --recursive https://github.com/aquasecurity/tracee.git
$ make bpf DOCKER=1 # --just-print 只是列印,編譯完成后可以在 dist 目錄中看到編譯好的位元組碼程式
$ ls -hl dist/
total 7.8M
-rw-r--r-- 1 root root 3.1M Sep 10 17:22 tracee.bpf.5_4_132-1_el7_elrepo_x86_64.v0_6_1-1-gce65764.o
-rw-r--r-- 1 root root 4.7M Sep 10 17:22 tracee.bpf.core.o
# 可以通過 TRACEE_BPF_FILE 環境變數指定我們需要加載的 eBPF 程式,這里使用目錄 /tmp/tracee
$ sudo docker run --name tracee --rm --privileged --pid=host -v /lib/modules/:/lib/modules/:ro -v /usr/src:/usr/src:ro -v /tmp/tracee:/tmp/tracee -e TRACEE_BPF_FILE=/tmp/tracee/tracee.bpf.core.o aquasec/tracee
如果編譯 tracee-ebpf 我們也會發現,底層還是會依賴動態庫,只是因為 tracee-ebpf 底層使用 cgo 機制使用 libbpf 庫依賴的結果,
$ file tracee-ebpf/dist/tracee-ebpf| tr , '\n' tracee-ebpf/dist/tracee-ebpf: ELF 64-bit LSB executable x86-64 version 1 (SYSV) dynamically linked (uses shared libs) for GNU/Linux 3.2.0 BuildID[sha1]=5bd7dbfd0475f015e268e321476dfc928d06d950 not stripped $ # ldd tracee-ebpf/dist/tracee-ebpf tracee-ebpf/dist/tracee-ebpf: /lib64/libc.so.6: version `GLIBC_2.22' not found (required by tracee-ebpf/dist/tracee-ebpf) linux-vdso.so.1 => (0x00007ffe559d7000) libelf.so.1 => /lib64/libelf.so.1 (0x00007f5d0c884000) libz.so.1 => /lib64/libz.so.1 (0x00007f5d0c66e000) libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f5d0c452000) libc.so.6 => /lib64/libc.so.6 (0x00007f5d0c084000) /lib64/ld-linux-x86-64.so.2 (0x00007f5d0ca9c000)
單獨驗證 tracee-ebpf 可以使用以下命令
$ sudo docker run --name tracee-only --rm --privileged --pid=host -v /lib/modules/:/lib/modules/:ro -v /usr/src:/usr/src:ro -v /tmp/tracee:/tmp/tracee -v /boot/:/boot tracee --trace event=execve --output table-verbose --debug|more
4.2 編譯錯誤處理
4.2.1 "bind": invalid mount path: './' mount path must be absolute
tracee-ebpf 使用 Docker-builder 路徑加載報錯如下:
Step 3/3 : WORKDIR /tracee
---> Using cache
---> 5e8a792b8117
Successfully built 5e8a792b8117
Successfully tagged tracee-builder:latest
docker run --rm -v /root/tracee/tracee-ebpf:./ -v /root/tracee/tracee-ebpf:/tracee/tracee-ebpf -w /tracee/tracee-ebpf --entrypoint make tracee-builder KERN_BLD_PATH=/usr/src/kernels/5.4.132-1.el7.elrepo.x86_64 KERN_SRC_PATH=build dist/tracee-ebpf VERSION=v0.6.1-1-gce65764
docker: Error response from daemon: invalid volume specification: '/root/tracee/tracee-ebpf:./': invalid mount config for type "bind": invalid mount path: './' mount path must be absolute.
See 'docker run --help'.
make: *** [dist/tracee-ebpf] Error 125
修改方式
+DOCKER_BUILDER_KERN_BLD ?= ``(if ``(shell readlink -f ``(KERN_BLD_PATH)),``(shell readlink -f ``(KERN_BLD_PATH)),``(KERN_BLD_PATH))
+DOCKER_BUILDER_KERN_SRC ?= ``(if ``(shell readlink -f ``(KERN_SRC_PATH)),``(shell readlink -f ``(KERN_SRC_PATH)),``(KERN_SRC_PATH))
主要差異點為系統不同,軟鏈接的方式不同導致
CentOS
# ls -hl /lib/modules/5.4.132-1.el7.elrepo.x86_64/source
lrwxrwxrwx 1 root root 5 Jul 22 15:12 /lib/modules/5.4.132-1.el7.elrepo.x86_64/source -> build
Ubuntu
# ls -hl /lib/modules/5.4.0-42-generic/build
lrwxrwxrwx 1 root root 39 Jul 10 2020 /lib/modules/5.4.0-42-generic/build -> /usr/src/linux-headers-5.4.0-42-generic
4.2.2 單獨生成 tracee-ebpf 鏡像時,make: uname: Operation not permitted 等問題
$ make docker
docker build --build-arg VERSION=v0.6.1-1-gce65764 -t tracee:latest .
Sending build context to Docker daemon 2.116GB
Step 1/16 : ARG BASE=fat
Step 2/16 : FROM golang:1.16-alpine as builder
ARG BASE=fat
FROM golang:1.16-alpine as builder
RUN apk --no-cache update && apk --no-cache add git clang llvm make gcc libc6-compat coreutils linux-headers musl-dev elfutils-dev libelf-static zlib-static
WORKDIR /tracee
// ...
make: uname: Operation not permitted
make: find: Operation not permitted
make: uname: Operation not permitted
make: /bin/sh: Operation not permitted
mkdir -p dist
make: mkdir: Operation not permitted
make: *** [Makefile:51: dist] Error 127
The command '/bin/sh -c make build VERSION=$VERSION' returned a non-zero code: 2
make: *** [docker] Error 2
該問題是 builder 基礎鏡像 golang:1.16-alpine 版本升級版本(alpine3.14 以后)導致的,明確指定為 golang:1.16-alpine3.13 即可:
-FROM golang:1.16-alpine as builder
+FROM golang:1.16-alpine3.13 as builder
4.2.3 failed to add kprobe 'p:kprobes/psecurity_file_open security_file_open': -17
例外退出后,再次運行可能會導致 ailed to create kprobe event: -17 的錯誤,錯誤的原因是使用了傳統的 kprobe 方式,寫入到 /sys/kernel/debug/tracing/kprobe_events 中,但是退出的時候未能夠正常清理,
# docker run --name tracee --rm --privileged -v /lib/modules/:/lib/modules/:ro -v /usr/src:/usr/src:ro -v /tmp/tracee:/tmp/tracee -it aquasec/tracee:latest
2021/09/07 02:46:41 [INFO] : Enabled Outputs :
2021/09/07 02:46:41 [INFO] : Falco Sidekick is up and listening on port 2801
failed to add kprobe 'p:kprobes/psecurity_file_open security_file_open': -17
failed to create kprobe event: -17
如果出現錯誤可以通過 /sys/kernel/debug/tracing/kprobe_events 檔案進行查看:
$ cat /sys/kernel/debug/tracing/kprobe_events
p:kprobes/psecurity_mmap_addr security_mmap_addr
p:kprobes/psecurity_file_mprotect security_file_mprotect
p:kprobes/psecurity_bprm_check security_bprm_check
p:kprobes/pcap_capable cap_capable
p:kprobes/psecurity_inode_unlink security_inode_unlink
p:kprobes/psecurity_file_open security_file_open
修復 sudo bash -c "echo""> /sys/kernel/debug/tracing/kprobe_events",參見 issue 447 和 639,
4.2.4 ‘err’ may be used uninitialized in this function
編譯 libbpf 的時候可能報錯,需要修改 Makefile 檔案中的 CFLAGS ?= -g -O2 -Werror -Wall,洗掉 -Werror 即可,
btf_dump.c: In function ‘btf_dump_dump_type_data.isra.24’:
btf_dump.c:2266:5: error: ‘err’ may be used uninitialized in this function [-Werror=maybe-uninitialized]
if (err < 0)
^
cc1: all warnings being treated as errors
4.2.5 Go 拉取包超時
// ...
GOOS=linux GOARCH=amd64 CC=clang CGO_CFLAGS="-I /tracee/tracee-ebpf/dist/libbpf/usr/include" CGO_LDFLAGS="/tracee/tracee-ebpf/dist/libbpf/libbpf.a" go build -tags netgo -v -o dist/tracee-ebpf \
-ldflags "-w -extldflags \"\"-X main.version=v0.6.1-1-gce65764"
go: github.com/aquasecurity/[email protected]: Get "https://proxy.golang.org/github.com/aquasecurity/libbpfgo/@v/v0.2.1-libbpf-0.4.0.mod": dial tcp 142.251.42.241:443: i/o timeout
make: *** [Makefile:59: dist/tracee-ebpf] Error 1
make: *** [dist/tracee-ebpf] Error 2
添加代理執行 GOPROXY=https://goproxy.cn 即可,
$ docker run --rm -v /usr/src/kernels:/usr/src/kernels/ -v /root/tracee/tracee-ebpf:/tracee/tracee-ebpf -w /tracee/tracee-ebpf --entrypoint make tracee-builder DOCKER_BUILDER_KERN_SRC=https://www.cnblogs.com/usr/src/kernels/5.4.132-1.el7.elrepo.x86_64 KERN_SRC_PATH=/lib/modules/5.4.132-1.el7.elrepo.x86_64/source KERN_BLD_PATH=/usr/src/kernels/5.4.132-1.el7.elrepo.x86_64 KERN_SRC_PATH=/usr/src/kernels/5.4.132-1.el7.elrepo.x86_64 dist/tracee-ebpf VERSION=v0.6.1-1-gce65764 GOPROXY=https://goproxy.cn
5. 參考
- Tracee: Tracing Containers with eBPF
- Tracee:如何使用 eBPF 來追蹤容器和系統事件
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/299092.html
標籤:其他
