nullswan/bpfsnitch
GitHub: nullswan/bpfsnitch
基于 eBPF 技术的 Linux 和 Kubernetes 实时系统调用与网络监控工具,通过 Prometheus 指标提供内核级可观测性。
Stars: 95 | Forks: 4
# bpfsnitch
[](LICENSE) [](https://goreportcard.com/report/github.com/nullswan/bpfsnitch)
bpfsnitch 是一款针对 Linux 系统和 Kubernetes 集群的开源实时监控工具。受 GlassWire 启发,bpfsnitch 利用 eBPF (extended Berkeley Packet Filter) 技术,通过追踪系统调用和网络活动,在尽可能底层提供可观测性。它能够实时监控每一个系统调用和网络事件,为您深入了解系统和容器内部发生的活动提供宝贵洞见。
## 目录
- [bpfsnitch](#bpfsnitch)
- [目录](#table-of-contents)
- [功能特性](#features)
- [前置条件](#prerequisites)
- [安装说明](#installation)
- [在 Kubernetes 集群上](#on-kubernetes-clusters)
- [指标](#metrics)
- [关键指标](#key-metrics)
- [指标输出示例](#example-metrics-output)
- [性能表现](#performance)
- [配置(即将推出)](#configuration-soon)
- [自定义系统调用监控](#customizing-syscall-monitoring)
- [预定义系统调用](#predefined-syscalls)
- [教育价值](#educational-value)
- [许可证](#license)
- [未来计划](#future-plans)
- [联系方式](#contact)
- [致谢](#acknowledgments)
- [加入社区](#join-the-community)
## 功能特性
- **实时监控**:追踪发生的每一个系统调用和网络事件。
- **eBPF 驱动**:利用 eBPF 在内核层面实现高效、低开销的监控。
- **Kubernetes 支持**:可作为 DaemonSet 部署,监控整个 Kubernetes 集群。
- **可自定义系统调用监控**:预定义了关键系统调用列表,并计划提供可配置的系统调用白名单。
- **Prometheus 集成**:通过 Prometheus 抓取端点暴露指标,便于集成到现有的监控体系中。
- **Pod 感知**:为指标添加 Pod 名称标签,提供细粒度的可见性。
- **开源**:基于 GPL 许可证发布,鼓励社区协作。
## 前置条件
- **支持 eBPF 的 Linux 内核**:bpfsnitch 需要支持 eBPF 的 Linux 内核版本(推荐 5.8 或更高版本)。
- **eBPF 库**:确保已安装 `libbpf` 及相关依赖项。
- **Prometheus**:用于指标抓取和监控。
- **容器运行时**:支持 Docker 和 Containerd Kubernetes 环境。
## 安装说明
### 在 Kubernetes 集群上
将 bpfsnitch 部署为 DaemonSet 以监控集群中的所有节点。
1. 应用 DaemonSet 清单文件
```
curl -s https://raw.githubusercontent.com/nullswan/bpfsnitch/main/deployments/daemonset.yaml | kubectl apply -f -
```
## 指标
bpfsnitch 暴露了各种 Prometheus 指标,深入展示系统调用和网络活动。
### 关键指标
- 系统调用计数器:进程或 Pod 发起的特定系统调用的计数。
- 网络字节计数器:发送和接收的总字节数,按 Pod 和远程子网标记。
- 网络数据包计数器:发送和接收的总数据包数。
- DNS 查询计数器:Pod 发起的 DNS 查询数量。
### 指标输出示例
```
# HELP bpfsnitch_dns_query_counter DNS 查询数量
# TYPE bpfsnitch_dns_query_counter counter
bpfsnitch_dns_query_counter{container="kube-proxy-cwn8r"} 23
# HELP bpfsnitch_network_received_bytes_counter 接收的字节数
# TYPE bpfsnitch_network_received_bytes_counter counter
bpfsnitch_network_received_bytes_counter{pod="nginx-7b9f54988c-2tpbd",remote_subnet="0.0.0.0/24"} 1334512
# HELP bpfsnitch_network_received_packets_counter 接收的数据包数量
# TYPE bpfsnitch_network_received_packets_counter counter
bpfsnitch_network_received_packets_counter{pod="nginx-7b9f54988c-2tpbd",remote_subnet="0.0.0.0/24"} 623
# HELP bpfsnitch_network_sent_bytes_counter 发送的字节数
# TYPE bpfsnitch_network_sent_bytes_counter counter
bpfsnitch_network_sent_packets_counter{pod="kube-proxy-cwn8r",remote_subnet="1.2.3.0/24"} 1293500
bpfsnitch_network_sent_packets_counter{pod="kube-proxy-cwn8r",remote_subnet="1.2.97.0/24"} 80
bpfsnitch_network_sent_packets_counter{pod="kube-proxy-cwn8r",remote_subnet="1.2.111.0/24"} 310
bpfsnitch_network_sent_packets_counter{pod="kube-proxy-cwn8r",remote_subnet="1.2.108.0/24"} 65
# HELP bpfsnitch_network_sent_packets_counter 发送的数据包数量
# TYPE bpfsnitch_network_sent_packets_counter counter
bpfsnitch_network_sent_packets_counter{pod="kube-proxy-cwn8r",remote_subnet="1.2.3.0/24"} 1529
bpfsnitch_network_sent_packets_counter{pod="kube-proxy-cwn8r",remote_subnet="1.2.97.0/24"} 1
bpfsnitch_network_sent_packets_counter{pod="kube-proxy-cwn8r",remote_subnet="1.2.111.0/24"} 3
bpfsnitch_network_sent_packets_counter{pod="kube-proxy-cwn8r",remote_subnet="1.2.108.0/24"} 1
# HELP bpfsnitch_syscall_counter syscall 数量
# TYPE bpfsnitch_syscall_counter counter
bpfsnitch_syscall_counter{pod="kube-proxy-cwn8r",syscall="arch_prctl"} 520
bpfsnitch_syscall_counter{pod="kube-proxy-cwn8r",syscall="bind"} 2713
bpfsnitch_syscall_counter{pod="kube-proxy-cwn8r",syscall="clone"} 818
bpfsnitch_syscall_counter{pod="kube-proxy-cwn8r",syscall="connect"} 264
bpfsnitch_syscall_counter{pod="kube-proxy-cwn8r",syscall="getrandom"} 578
bpfsnitch_syscall_counter{pod="kube-proxy-cwn8r",syscall="getsockname"} 2845
bpfsnitch_syscall_counter{pod="kube-proxy-cwn8r",syscall="read"} 16424
bpfsnitch_syscall_counter{pod="kube-proxy-cwn8r",syscall="recvmsg"} 56939
bpfsnitch_syscall_counter{pod="kube-proxy-cwn8r",syscall="sendmsg"} 443
bpfsnitch_syscall_counter{pod="kube-proxy-cwn8r",syscall="sendto"} 32007
bpfsnitch_syscall_counter{pod="kube-proxy-cwn8r",syscall="setsockopt"} 819
bpfsnitch_syscall_counter{pod="kube-proxy-cwn8r",syscall="socket"} 2845
bpfsnitch_syscall_counter{pod="kube-proxy-cwn8r",syscall="wait4"} 818
```
## 性能表现
从 v0.1.0 版本开始,bpfsnitch 利用 eBPF 技术构建,旨在实现轻量级和高效。它在内核层面监控系统调用和网络事件,以最小的系统影响提供实时洞察。
在生产环境中,bpfsnitch 通常在每 60 秒的抓取周期内平均使用 5ms 的 CPU,并维持最高 `250MB` 的内存占用。其 CPU 使用率被静态限制在 `100ms` 以内,确保无论系统配置或工作负载如何,性能都保持一致。请参阅 [DaemonSet resources](https://github.com/search?q=repo%3Anullswan/bpfsnitch%20resources&type=code)
要实时监控 bpfsnitch 的性能,请使用 -pprof 标志启动它以暴露 pprof 服务器。访问 /debug/pprof 路由获取实时性能分析数据,以分析 CPU 和内存使用情况并根据需要优化性能。
我们致力于在未来的版本中提供详细的性能基准测试和优化技巧,帮助您最大化利用 bpfsnitch 的优势。
## 配置(即将推出)
### 自定义系统调用监控
bpfsnitch 附带了一个预定义的系统调用监控列表,重点关注可能影响系统安全性或稳定性的关键操作。我们计划在未来的版本中提供可配置的系统调用白名单,允许您根据特定需求定制监控。
#### 预定义系统调用
```
var WhitelistedSyscalls = []int{
SyscallToId["clone"],
SyscallToId["execve"],
SyscallToId["mknodat"],
SyscallToId["chroot"],
SyscallToId["mount"],
SyscallToId["umount2"],
SyscallToId["pivot_root"],
SyscallToId["setuid"],
SyscallToId["setgid"],
...
```
## 教育价值
bpfsnitch 不仅是一个强大的监控工具,也是一个绝佳的教育资源。它提供了以下方面的见解:
- eBPF 编程:了解 eBPF 程序是如何编写并附加到内核函数的。
- 系统调用机制:理解系统调用的工作原理及其对系统行为的影响。
- 内核级监控:获取关于 Linux 底层监控技术的知识。
- 可观测性实践:探索如何为 Prometheus 等现代监控系统收集和暴露指标。
## 许可证
bpfsnitch 基于 GNU General Public License (GPL) 发布。
## 未来计划
- 可配置系统调用白名单:允许用户定义要监控的系统调用。
- 用户界面:开发基于 Web 的 UI,以便于可视化和管理。
- 性能优化:提高数据收集和处理的效率。
- 更多指标:包含更细粒度的指标,例如延迟测量和错误计数。
- 扩展容器支持:增强与各种容器运行时和编排平台的兼容性。
## 联系方式
- 贡献者:[@nullswan](https://github.com/nullswan)
- 审阅者:[@gmarcha](https://github.com/gmarcha), [@naofel1](https://github.com/naofel1)
- GitHub Issues: [https://github.com/nullswan/bpfsnitch/issues](https://github.com/nullswan/bpfsnitch/issues)
## 致谢
- eBPF 社区:为 eBPF 开发提供了丰富的资源和支持。
- Prometheus:感谢其强大的监控和告警工具包。
- 开源社区:感谢所有为开源生态系统做出贡献的人,正是你们让 bpfsnitch 这样的项目成为可能。
## 加入社区
如果您觉得 bpfsnitch 有价值,请在 GitHub 上给我们一个 ⭐ star,并与可能感兴趣的其他人分享。您的支持帮助我们改进并发展这个项目!
如果您有任何问题或在开始使用 bpfsnitch 时需要帮助,请随时联系。我们期待您的反馈!
标签:API集成, CISA项目, DaemonSet, Docker镜像, EVTX分析, EVTX分析, Go, Hpfeeds, IP 地址批量处理, JSONLines, Ruby工具, SQLite数据库, Web截图, 二进制发布, 内核监控, 可观测性, 子域名突变, 容器安全, 开源工具, 性能分析, 日志审计, 系统调用, 网络安全, 自定义请求头, 请求拦截, 隐私保护, 零信任