深入淺出 eBPF 安全項目 Tracee

https://www.ebpf.top/post/tracee_intro/

1. Tracee 介紹

1.1 Tracee 介紹

Tracee 是一個用 於 Linux 的運行時安全和取證工具。它使用 Linux eBPF 技術在運行時跟蹤系統和應用程序,並分析收集的事件以檢測可疑的行爲模式。Tracee 以 Docker 鏡像的形式交付,監控操作系統並根據預定義的行爲模式集檢測可疑行爲。官網文檔參見這裏

Tracee 由以下子項目組成:

  • Trace-eBPF - 使用 eBPF 進行 Linux 追蹤和取證,BPF 底層代碼實現參見 tracee.bpf.c

  • Trace-Rules - 運行時安全檢測引擎,在真實的使用場景中通過管道的方式從 tracee-ebpf 中接受數據,具體運行命令如下

    1
    $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 個函數,格式如下:

1
2
3
4
5
6
7
8
9
$  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,可選項啓用了 BTFBTF 啓用可以通過 /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-headerskernel-devel` 兩個包;

如果內核啓用了 BTF 功能,可以直接使用官方提供的鏡像進行測試:

1
$ sudo docker run --name tracee --rm --privileged -it aquasec/tracee:latest trace

如果系統未啓用 BTF 功能,則需要加載內核 /lib/modules/ 和 /usr/src 目錄,並運行以下命令:

1
2
$ 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 選項進行查看,結果如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$ 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 --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 進行測試:

1
2
3
4
5
6
7
8
# 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 進行執行, 在上述命令運行後,輸出以下信息:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$ 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]

*** 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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$ 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 庫依賴的結果。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
$ 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 可以使用以下命令:

1
$ 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 路徑加載報錯如下:

1
2
3
4
5
6
7
8
9
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

修改方式

1
2
+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

1
2
$ 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

1
2
$ 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 等問題

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ 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` 即可:

1
2
-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 中,但是退出的時候未能夠正常清理。

1
2
3
4
5
# 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 文件進行查看:

1
2
3
4
5
6
7
$ 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 即可。

1
2
3
4
5
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 golang 拉取包超時

1
2
3
4
5
6
 

添加代理執行 GOPROXY=https://goproxy.cn 即可。

1
$ 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=/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

詳細修改參見這裏:

1
2
3
4
5
-go_env := GOOS=linux GOARCH=$(ARCH:x86_64=amd64) CC=$(CMD_CLANG) CGO_CFLAGS="-I $(abspath $(LIBBPF_HEADERS))" CGO_LDFLAGS="$(abspath $(LIBBPF_OBJ))"
+go_env := GOOS=linux GOARCH=$(ARCH:x86_64=amd64) CC=$(CMD_CLANG) CGO_CFLAGS="-I $(abspath $(LIBBPF_HEADERS))" CGO_LDFLAGS="$(abspath $(LIBBPF_OBJ))" GOPROXY=https://goproxy.cn

-	--entrypoint make $(DOCKER_BUILDER) KERN_BLD_PATH=$(DOCKER_BUILDER_KERN_BLD) KERN_SRC_PATH=$(DOCKER_BUILDER_KERN_SRC) $(1)
+	--entrypoint make $(DOCKER_BUILDER) DOCKER_BUILDER_KERN_SRC=$(DOCKER_BUILDER_KERN_SRC) KERN_SRC_PATH=$(KERN_SRC_PATH) KERN_BLD_PATH=$(DOCKER_BUILDER_KERN_BLD) GOPROXY=https://goproxy.cn KERN_SRC_PATH=$(DOCKER_BUILDER_KERN_SRC) $(1)

5. 參考

  1. Tracee: Tracing Containers with eBPF
  2. Tracee:如何使用 eBPF 來追蹤容器和系統事件
// ... 
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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章