怎麼開始
-
從 bpftrace 開始:
-
分析 incoming 封包:L7 Tracing with eBPF: HTTP and Beyond via Socket Filters and Syscall Tracepoints - eunomia
-
所有可以用的 API 都在這裡:eBPF Docs
graph TD; subgraph user[User Space] bpf_prog[BPF Program] prog_bpf[prog.bpf Bytecode] app[Application] bpf_prog --> |"LLVM/clang"| prog_bpf end subgraph kernel[Kernel] subgraph eBPF bpf_byte[BPF Bytecode] jit[verifier + JIT] native[Native Code] prog_bpf --> |"BPF()"| bpf_byte bpf_byte --> jit jit --> native end bpf_maps[BPF Maps] kernel_funcs[Kernel Functions] native --> |Hooks| kernel_funcs eBPF <--> bpf_maps app --> |"BPF()"| bpf_maps end
一些短語
from eBPF
- probes: 探針
- kprobes: kernel space probes
- uprobes: user space probes
- kertprobes: kernel return probes
- uretprobes: userspace return probes
- perf event
- socket filter
- XDP
- tracepoint
- CO-RE: Compile Once Run Everywhere
- hook point: 啟動點
from C code
__bpf_ntohs:Network To Host Short,將網路位元組順序 (Network Byte Order,為 Big-Endian) 轉換為主機位元組順序 (Host Byte Order,x86 通常為 Little-Endian)htons:Host TO Network Short integerhtonl、ntohl依此類推
ntop:Network to Presentation,將數字格式的 IP 位址轉換為字串格式bpf_skb_load_bytes:從網路封包的 socket buffer (sk_buff) 中讀取資料ETH_P_IP:frame type field in the Ethernet frame, where 0x0800 indicates IPv4.IP_MF:“More Fragments” flag. If this flag is set to1, it indicates that the packet is part of a fragmented sequence and more fragments are expected. 可參考:network-packet-structure
參考資料
- eBPF Documentary
- Modernizing BPF for the next 10 years [LWN.net]
- 教練我想玩eBPF :: 2022 iThome 鐵人賽
- 2022 IThome 鐵人賽 - 學習 EBPF 系列 · Louis Li’s Blog
- (一):一個很有趣的案例是我們可以使用 eBPF 來做 ssl 加密連線的監看。SSL/TSL 的連線加密通常是在 user space 應用程式內完成加密的,因此即便我們監看應用程式送入 kernal socket 的內容,內容也已經是被加密的了。但是要拆解應用程式來查看又相對比較複雜困難,使用 eBPF 就可以用一個相對簡單的方法來監看加密訊息。
- 在 Linux 上,應用程式的加密經常會使用 libssl 這個 library 來完成,並使用 libssl 提供的 SSL_read 和 SSL_write 取代 socket 的 read 和 write,透過 eBPF 的功能,我們可以比較簡單的直接監聽應用程式對這兩個函數的呼叫,並直接提取出未加密的連線內容。
- GitHub - gojue/ecapture: Capturing SSL/TLS plaintext without a CA certificate using eBPF. Supported on Linux/Android kernels for amd64/arm64.
- 透過 eBPF 攔 TLS 連線的明文 – Gea-Suan Lin’s BLOG
- eBPF 隨筆(三): eBPF CO-RE. 在前一篇文章 eBPF 隨筆(二):eBPF verifier… | by Ian Chen | Medium
- BPF and XDP Reference Guide — Cilium 1.16.4 documentation
- Hello XDP! - Aya
- Chapter 2 - HackMD
- 初探 Linux Kernel 中的 BPF 與 XDP 技術:以 Tiny Load Balancer 為例 | by Ian Chen | Starbugs Weekly 星巴哥技術專欄 | Medium
- 绿色记忆:eBPF学习笔记
- 什麼是 eBPF ? An Introduction and Deep Dive into the eBPF Technology
- Linux 核心設計: eBPF - HackMD
- 安裝 - HackMD