eCapture 一个神奇的开源工具
eCapture 一个神奇的开源工具 - 简书 (jianshu.com)
一 前言
前段时间,一直对eBPF感兴趣,也写了一些eBPF的入门介绍文章,朋友就发来个连接说这是一个好东西,我看,是利用eBPF来实现的,刚好最近在研究,所以就有了这篇的介绍文章。
二 eCapture 是什么
官方介绍如下:
eBPF HOOK uprobe实现的各种用户态进程的数据捕获,无需改动原程序。
SSL/HTTPS数据导出功能,针对HTTPS的数据包抓取,不需要导入CA证书。
bash的命令捕获,HIDS的bash命令监控解决方案。
mysql query等数据库的数据库审计解决方案。
地址:ecapture/README_CN.md at master · ehids/ecapture (github.com)
eBPF程序可以对内核的静态跟踪点,动态跟踪点,设置钩子,然后当内核执行到这些点的时候,会自动调用我们设置好的钩子,这样我们就可以对一些数据进行监控,网络流量也可以进行转发,甚至对数据包进行修改,简直是黑魔法;不光可以对内核的进行动态追踪,对用户态进程也可以,我记得我上篇文章就介绍对简单的c程序的代码的hook。
eCapture就是这个原理,对用户态程序的具体追踪,具体原理如下:
eCapture系统用户态程序使用Golang语言开发,具有良好的系统兼容性,无依赖快速部署,更适合云原生场景。 内核态代码使用C编写,使用clang/llvm编译,生产bpf字节码后,采用go-bindata转化为golang语法文件,之后采用ehids/ebpfmanager类库,调用bpf syscall进行加载、HOOK、map读取。 golang编译后,无其他任何依赖即可运行,兼容linux kernel 4.18以上所有版本。
架构图如下:

三 安装和使用
我曾经想在centos 8.x 上尝试,发现充满了挫折,真不建议用这个系统作为ebpf的实验环境,又发现不少朋友采用 ubuntu服务器,遂下载 ubuntu-21.10-live-server-amd64.iso安装体验了一把,可以说是非常顺利,几乎使用到的安装命令都是一把过,强烈推荐,而且没有的命令,会直接提供安装命令,非常的人性化;
当然如果你就喜欢挑战困难,可以试试centos 8.x上编译内核,打开一些选项后,编译起来常常报错,安装相关依赖又困难重重,我遇到错误如下,如果有大神对这方便比较精通可以指点下:
vim .config
# 添加: CONFIG_DEBUG_INFO_BTF=y
# 编译
make -j4
LD [M] kernel/bpf/preload/bpf_preload.o
ld: kernel/bpf/preload/bpf_preload_kern.o: unable to initialize decompress status for section .debug_addr
ld: kernel/bpf/preload/bpf_preload_kern.o: unable to initialize decompress status for section .debug_addr
ld: kernel/bpf/preload/bpf_preload_kern.o: unable to initialize decompress status for section .debug_addr
ld: kernel/bpf/preload/bpf_preload_kern.o: unable to initialize decompress status for section .debug_addr
kernel/bpf/preload/bpf_preload_kern.o: file not recognized: 不可识别的文件格式
make[3]: *** [scripts/Makefile.build:483:kernel/bpf/preload/bpf_preload.o] 错误 1
make[2]: *** [scripts/Makefile.build:549:kernel/bpf/preload] 错误 2
make[1]: *** [scripts/Makefile.build:549:kernel/bpf] 错误 2
make: *** [Makefile:1846:kernel] 错误 2
make: *** 正在等待未完成的任务....
3.1 ubuntu下环境准备
3.1.1 安装编译和依赖头文件工具等
sudo apt-get install -y make clang llvm libelf-dev libbpf-dev bpfcc-tools libbpfcc-dev linux-tools-$(uname -r) linux-headers-$(uname -r)
3.1.2 安装git和golang
sudo apt-get git
sudo apt-get golang
指定golang使用代理,不然下载不了github上的库:
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.io,direct
3.1.3 编译eCapture
非常顺利的一气呵成的编译:
git clone https://github.com/ehids/ecapture.git
cd ecapture
make
3.2 运行使用
root@ubuntu-lab:/home/miao/ecapture-master# ./bin/ecapture
NAME:
ecapture - capture text SSL content without CA cert by ebpf hook.
USAGE:
ecapture [flags]
VERSION:
--
COMMANDS:
bash capture bash command
help Help about any command
mysqld capture sql queries from mysqld 5.6/5.7/8.0 .
tls alias name:openssl , use to capture tls/ssl text content without CA cert.
DESCRIPTION:
eCapture is a tool that can capture plaintext packets
such as HTTPS and TLS without installing a CA certificate.
It can also capture bash commands, which is suitable for
security auditing scenarios, such as database auditing of mysqld, etc.
Repository: https://github.com/ehids/ecapture
OPTIONS:
-d, --debug[=false] enable debug logging
-h, --help[=false] help for ecapture
--hex[=false] print byte strings as hex encoded strings
-p, --pid=0 if target_pid is 0 then we target all pids
通过命令来看,核心的三个功能,一个是bash的输入内容的记录,一个是对mysql的sql命令的记录,一个是对tls 明文数据捕获。
- bash记录,可以用在入侵检测,或者我们打开功能后可以监看黑客的入侵过程;
- mysql的sql命令记录,可以用在数据库审计,数据防泄露等场景;
- tls明文数据捕获,可以用在比较机器被入侵了,通信是加密的,我们可以通过这些命令查看传递的是什么内容,这点真的很酷。
好了,下面进入演示环节:
3.2.1 bash命令记录
root@ubuntu-lab:/home/miao/ecapture-master# ./bin/ecapture bash
2022/04/09 04:45:48 start to run EBPFProbeBash module
2022/04/09 04:45:48 pid info :8130
2022/04/09 04:45:48 HOOK binrayPath:/bin/bash, FunctionName:readline
2022/04/09 04:45:48 target all process.
2022/04/09 04:45:54 PID:3687, Comm:bash, Line: pwd
2022/04/09 04:46:03 PID:3687, Comm:bash, Line: cat /etc/passwd
如上,我们可以在记录中查看到命令执行的时间,命令名为bash,命令id为3687,具体命令行为pwd,第二个命令为cat /etc/passwd.
3.2.2 mysql命令记录
# 安装server 注意这个程序不支持mysql,所以只能安装mariadb
miao@ubuntu-lab:~/ehids-agent-master$ sudo apt-get install mariadb-server
# mysql命令执行:
sudo mysql -u root
[sudo] password for miao:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 30
Server version: 10.5.15-MariaDB-0ubuntu0.21.10.1 Ubuntu 21.10
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.001 sec)
MariaDB [(none)]> create database test;
Query OK, 1 row affected (0.000 sec)
MariaDB [(none)]> use test;
Database changed
MariaDB [test]> CREATE TABLE user(
-> id int(10) unsigned not null auto_increment,
-> name varchar(25),
-> sex varchar(5),
-> age int(10),
-> password varchar(25),
-> primary key(id))engine=Innodb;
Query OK, 0 rows affected (0.013 sec)
MariaDB [test]> create table test(id int(10),name varchar(64),aget int(10));
Query OK, 0 rows affected (0.010 sec)
MariaDB [test]> insert test values(1,'abc',14);
Query OK, 1 row affected (0.003 sec)
MariaDB [test]> insert test values(2,'edf',14);
Query OK, 1 row affected (0.001 sec)
MariaDB [test]> insert test values(3,'dfg',18);
Query OK, 1 row affected (0.001 sec)
MariaDB [test]> select * from test;
+------+------+------+
| id | name | aget |
+------+------+------+
| 1 | abc | 14 |
| 2 | edf | 14 |
| 3 | dfg | 18 |
+------+------+------+
3 rows in set (0.000 sec)
MariaDB [test]> delete from test where id=3;
Query OK, 1 row affected (0.001 sec)
MariaDB [test]> commit;
Query OK, 0 rows affected (0.000 sec)
MariaDB [test]> drop table user;
Query OK, 0 rows affected (0.003 sec)
MariaDB [test]> quit
Bye
演示了增删改查和创建删除表等命令,我们来看看审计的情况:
root@ubuntu-lab:/home/miao/ecapture-master# ./bin/ecapture mysqld
2022/04/09 04:57:32 start to run EBPFProbeMysqld module
2022/04/09 04:57:32 pid info :11307
2022/04/09 04:57:32 Mysql Version:mysqld-5.6, binrayPath:/usr/sbin/mariadbd, FunctionName:_Z16dispatch_command19enum_server_commandP3THDPcjbb ,UprobeOffset:0
2022/04/09 04:57:48 PID:10185, Comm:mariadbd, Time:3827419930458, length:(32/32), Line:select @@version_comment limit 1
2022/04/09 04:58:06 PID:10185, Comm:mariadbd, Time:3845780980800, length:(14/14), Line:show databases
2022/04/09 04:58:16 PID:10185, Comm:mariadbd, Time:3855625666790, length:(20/20), Line:create database test
2022/04/09 04:58:20 PID:10185, Comm:mariadbd, Time:3859549915643, length:(17/17), Line:SELECT DATABASE()
2022/04/09 04:58:20 PID:10185, Comm:mariadbd, Time:3859551387926, length:(14/14), Line:show databases
2022/04/09 04:58:20 PID:10185, Comm:mariadbd, Time:3859551716349, length:(11/11), Line:show tables
2022/04/09 05:00:39 PID:10185, Comm:mariadbd, Time:3998880500344, length:(228/228)