基于Linux bpf跟踪文件读写

基于Linux bpf跟踪文件读写

一、Ubuntu 20.04环境搭建

[eBPF/Ubuntu 18.04 LTS源码构建bcc.md at master · linuxkerneltravel/eBPF · GitHub](https://github.com/linuxkerneltravel/eBPF/blob/master/Install-BCC/Ubuntu 18.04 LTS源码构建bcc.md)

二、文件打开监控

这里我们监控内核通用块层的vfs_read()函数:

打印打开文件的Pid,inode号,文件名信息。

#!/usr/bin/python
from bcc import BPF

prog = """
    #include <asm/ptrace.h>
    #include <linux/fs.h>

    int trace_vfs_open(struct pt_regs *ctx, const struct path *path, struct file *file)
    {
        char *file_name = NULL;
        u32 pid = bpf_get_current_pid_tgid() >> 32;
        u32 uid = bpf_get_current_uid_gid();

        bpf_trace_printk("%d %d %s\\n", pid, path->dentry->d_inode->i_ino, path->dentry->d_iname);
        return 0;
    }
"""

b = BPF(text = prog)
b.attach_kprobe(event = "vfs_open", fn_name = "trace_vfs_open")

print("%-18s %-16s %-6s %s"%("TIME(s)", "COMM", "PID", "MESSAGE"))

#output
while 1:
    try:
        (task, pid, cpu, flags, ts, msg) = b.trace_fields()
    except ValueError:
        continue
    print("%-18.9f %-16s %-6d %s"%(ts, task, pid, msg))

抓取信息

root@curtis-Aspire-E5-471G:/home/curtis/code/bpf/eBPF-master/write_code# ./trace_open.py
TIME(s)            COMM             PID    MESSAGE
19160.428362000    trace_open.py    13025  13025 3015816 trace_open.py
19160.428453000    trace_open.py    13025  13025 1974484 __init__.py
19160.429079000    trace_open.py    13025  13025 1974484 __init__.py

三、文件读监控

这里我们监控内核通用块层的vfs_open()函数:

#!/usr/bin/python
from bcc import BPF

prog = """
    #include <asm/ptrace.h>
    #include <linux/fs.h>

    int trace_vfs_open(struct pt_regs *ctx, struct file *file, char __user *buf, size_t count, loff_t *pos)
    {
        u32 pid = bpf_get_current_pid_tgid() >> 32;
        u32 uid = bpf_get_current_uid_gid();

        bpf_trace_printk("%d %d %s\\n", pid, file->f_inode->i_ino, file->f_path.dentry->d_iname);
        return 0;
    }
"""

b = BPF(text = prog)
b.attach_kprobe(event = "vfs_read", fn_name = "trace_vfs_open")

print("%-18s %-16s %-6s %s"%("TIME(s)", "COMM", "PID", "MESSAGE"))

#output
while 1:
    try:
        (task, pid, cpu, flags, ts, msg) = b.trace_fields()
    except ValueError:
        continue
    print("%-18.9f %-16s %-6d %s"%(ts, task, pid, msg))

抓取信息

19768.367784000    polkitd          668    668 2254 1000
19768.367893000    polkitd          668    668 11799035 passwd
19768.367991000    polkitd          668    668 11796553 group
19768.368283000    polkitd          668    668 285 userdb

三、文件写监控

#!/usr/bin/python
from bcc import BPF

prog = """
    #include <asm/ptrace.h>
    #include <linux/fs.h>

    int trace_vfs_write(struct pt_regs *ctx, struct file *file, const char __user *buf, size_t count, loff_t *pos)
    {
        u32 pid = bpf_get_current_pid_tgid() >> 32;
        u32 uid = bpf_get_current_uid_gid();

        bpf_trace_printk("%d %d %s\\n", pid, file->f_inode->i_ino, file->f_path.dentry->d_iname);
        return 0;
    }
"""

b = BPF(text = prog)
b.attach_kprobe(event = "vfs_write", fn_name = "trace_vfs_write")

print("%-18s %-16s %-6s %s"%("TIME(s)", "COMM", "PID", "MESSAGE"))

#output
while 1:
    try:
        (task, pid, cpu, flags, ts, msg) = b.trace_fields()
    except ValueError:
        continue
    print("%-18.9f %-16s %-6d %s"%(ts, task, pid, msg))

抓取信息

20069.360673000    trace_write.py   13078  13078 4 1
20069.360719000    trace_write.py   13078  13078 4 1
20069.360722000    sshd             4546   4546 178514 TCP
20069.360761000    sshd             4546   4546 178514 TCP
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值