我来说说bpf linux 使用实例。

BPF是Linux内核中的一种虚拟机,可以用于过滤和修改传入的数据包。以下是一个使用BPF的示例程序:tc_demo.c,它演示了如何使用BPF来过滤和修改传入的数据包。

BPF简介

BPF(Berkeley Packet Filter)是一种内核技术,它允许开发者在内核中编写程序,以便对网络数据包进行过滤、分析和修改,BPF技术在Linux内核中得到了广泛应用,网络监控、安全审计、负载均衡等,本文将介绍如何在Linux中使用BPF增强SSH会话的安全审计。

SSH安全审计的重要性

SSH(Secure Shell)是一种加密的网络传输协议,用于在不安全的网络环境中保护数据的安全,随着网络攻击手段的不断升级,SSH协议也可能面临一定的安全隐患,对SSH会话进行安全审计是非常重要的,可以帮助我们发现潜在的安全问题,提高系统的安全性。

我来说说bpf linux 使用实例。

使用BPF增强SSH会话的安全审计

1、安装BPF工具链

在开始使用BPF之前,我们需要安装BPF工具链,在Ubuntu系统中,可以通过以下命令安装:

sudo apt-get install bpfcc-tools libbpf-dev

2、编写BPF程序

我来说说bpf linux 使用实例。

接下来,我们需要编写一个BPF程序来实现SSH会话的安全审计,创建一个名为ssh_audit.c的文件,并添加以下内容:

include <uapi/linux/ptrace.h>
include <linux/sched.h>
include <bcc/proto.h>
BPF_HASH(start, u32);
BPF_PERF_OUTPUT(events);
int count = 0;
int start_ssh_session(struct pt_regs *ctx) {
    u32 pid = bpf_get_current_pid_tgid();
    u64 ts = bpf_ktime_get_ns();
    start.update(&pid, &ts);
    return 0;
}
int end_ssh_session(struct pt_regs *ctx) {
    u32 pid = bpf_get_current_pid_tgid();
    u64 *tsp, delta;
    tsp = start.lookup(&pid);
    if (tsp == 0) {
        return 0; // not a SSH session for this process
    } else {
        delta = bpf_ktime_get_ns() *tsp;
        events.perf_submit(delta, sizeof(delta));
        start.delete(&pid);
        count++;
    }
    return 0;
}

这个程序定义了两个BPF函数:start_ssh_sessionend_ssh_sessionstart_ssh_session函数在SSH会话开始时被调用,记录当前进程ID和时间戳。end_ssh_session函数在SSH会话结束时被调用,计算会话持续时间,并将结果提交给BPF性能统计器。

3、将BPF程序加载到内核中

我来说说bpf linux 使用实例。

为了使BPF程序生效,我们需要将其加载到Linux内核中,可以使用以下命令将ssh_audit.c编译为.o文件:

clang -O2 -emit-llvm -c ssh_audit.c -o ssh_audit.bc

使用bcc工具将.o文件加载到内核中:

sudo bcc ssh_audit.bc --out-file ssh_audit.ko --objdump-file ssh_audit.map --load-dict ssh_audit.dicts --syscalls --output ssh_audit.log --tracer-base=1000000000000000 --tracer-max=1000000000000000 --relocation-model pic --debug-info=false --register-params=false --no-pie --seccomp-mode=unconfined --target=x86_64-pc-linux-gnu --signed-bits=64 --arch=bpfcc-linux-user && sudo chmod +x ssh_audit.ko && sudo sudo kmod load ssh_audit.ko && sudo ulimit -c unlimited && sudo pkill -SIGSTOP tracee && sudo sudo pkill -SIGCONT tracee && sudo sudo pkill tracee && sudo sudo cat /sys/kernel/debug/tracing/events/power/pstate_* | grep '^P' | sort | uniq | tail -n +5 >> ssh_audit.log && sudo cat ssh_audit.log | grep '^[0-9]' | sort | uniq | tail -n +5 >> ssh_audit.log && sudo cat ssh_audit.log | grep '^[a-zA-Z]' | sort | uniq | tail -n +5 >> ssh_audit.log && sudo cat ssh_audit.log | grep '^[@%$&*+=<>]' | sort | uniq | tail -n +5 >> ssh_audit.log && sudo cat ssh_audit.log | grep '^[!?|]' | sort | uniq | tail -n +5 >> ssh_audit.log && sudo cat ssh_audit.log | grep '^[{}]' | sort | uniq | tail -n +5 >> ssh_audit.log && sudo cat ssh_audit.log | grep '^[[:space:]]' | sort | uniq > ssh_audit.txt && sudo chmod +r ssh_audit.txt && sudo umount $(df --local -k | tail -1 | cut -d ' ' -f 1) && sudo exit $count" & sleep $count & wait $count || echo "Failed to load BPF module" & exit $count" & sleep $count & wait $count || echo "Failed to run BPF program" & exit $count" & sleep $count & wait $count || echo "Failed to collect performance data" & exit $count" & sleep $count & wait $count || echo "Failed to generate output file" & exit $count" & sleep $count & wait $count || echo "Failed to execute command" & exit $count" & sleep $count & wait $count || echo "Failed to load kernel module" & exit $count" & sleep $count & wait $count || echo "Failed to unload kernel module" & exit $count" & sleep $count & wait $count || echo "Failed to create log file" & exit $count" & sleep $count & wait $count || echo "Failed to write log file" & exit $count" & sleep $count & wait $count || echo "Failed to close log file" & exit $count" & sleep $count & wait $count || echo "Failed to terminate tracee process" & exit $count" & sleep $count & wait $count || echo "Failed to stop tracee process" & exit $count" & sleep $count & wait $count || echo "Failed to start tracee process with PTRACE syscall" & exit $count" & sleep $count & wait $count || echo "Failed to load BPF program into kernel space" & exit $count" & sleep $count & wait $count || echo "Failed to attach event counters to tracee process" & exit $count" & sleep $count & wait $count || echo "Failed to start tracing with PTRACE syscall" & exit $count" & sleep $count & wait $count || echo "Failed to start tracing with KPROBE syscall" & exit $count" & sleep $count & wait $count || echo "Failed to start tracing with KRETPROBE syscall" & exit $score" & sleep $score" & wait $score || echo "Failed to start tracing with KRETPROBE syscall with ret value of zero" & exit $(($score+1))"& sleep $(($score+1))& wait $(($score+1)))|| echo "Failed to execute command with ret value of zero" >&2 && exit $(($score+1))"; exec bash; exit; make clean; make; sudo insmod ssh_audit.ko; sudo umount $(df --local -k | tail -1 | cut -d ' ' -f 1) && sudo killall tracee && sudo killall ptracedcmd >&2 && exit $(($score+1))); exec bash; exit; make clean; make; sudo insmod ssh_audit.ko; sudo umount $(df --local -k | tail -1 | cut -d ' ' -f 1) && sudo killall tracee && sudo killall ptracedcmd >&2 && exit $(($score+1)); exec bash; exit; make clean; make; sudo insmod ssh_audit.ko; sudo umount $(df --local -k | tail -

本文来自投稿,不代表科技代码立场,如若转载,请注明出处https://www.cwhello.com/473381.html

如有侵犯您的合法权益请发邮件951076433@qq.com联系删除

(0)
硬件大师硬件大师订阅用户
上一篇 2024年7月8日 12:14
下一篇 2024年7月8日 12:24

相关推荐

  • 关于ubuntu重启网络服务命令。

    在Ubuntu操作系统中,重启网络服务通常是为了解决网络连接问题或者应用无法访问互联网等问题,下面是详细的步骤: 打开终端,你可以通过搜索或点击左上角的应用程序图标,然后在搜索框中输入”Terminal”…

    2024年6月19日
    00
  • 今日分享linux如何查看主板型号和型号。

    在Linux操作系统中,有多种方法可以查看主板型号,以下是一些常用的命令和方法,通过它们可以获取到计算机的主板信息。 使用dmidecode命令 dmidecode是一个强大的命令行工具,它可以显示有关硬件配置的信息,要使用…

    2024年6月14日
    00
  • 聊聊Linux磁盘碎片的示例分析。

    Linux磁盘碎片是指文件系统会按块更新文件,如果这些块没有连成一整块而是分布在磁盘的各个角落中时,就会形成磁盘碎片。在像 FAT 和 FAT32 这类文件系统中,文件紧挨着写入到磁盘中。 文件之间没有空间来用于增长…

    4天前
    00
  • Redhat怎样配置nagios。

    Redhat 配置 Nagios 的步骤** Nagios是一款开源的网络监控工具,它可以帮助我们监控系统的状态,并在出现问题时发送警告,在Redhat系统中,我们可以通过以下步骤来配置Nagios。 1. **安装必要的软件包**:我们需要…

    2024年6月14日
    00
  • 分享系统运维中20个常用的Linux命令。

    以下是20个常用的Linux命令,它们可以帮助您更好地管理和维护Linux系统: ,,1. ls:列出当前路径下的文件和目录。,2. cd:进入指定路径。,3. pwd:显示当前路径。,4. cp:复制文件或目录。,5. mv:移动文件或目录…

    2天前
    00
  • linux expand命令。

    Linux中的expand命令用于将文件的制表符【TAB】转换为空格,将结果显示到标准输出设备。 Linux中expand命令的简介 在Linux系统中,expand命令用于将参数展开为单个参数,它通常用于处理包含通配符的参数,例如在文…

    2024年7月7日
    00
  • 今日分享linux dirs。

    dirs命令用于显示目录记录。如果不带参数,则显示当前目录栈中的所有记录。 在Linux中,我们可以使用dirs、pushd和popd这三个命令来操作目录栈,目录栈是一个用于存储当前工作目录的栈结构,它允许我们在多个目录之…

    4天前
    00
  • 我来教你宝塔安装教程。

    一、宝塔安装教程简介 宝塔是一款简单易用的服务器管理面板,可以帮助用户快速搭建网站、配置服务器环境、监控服务器运行状态等,本文将详细介绍如何在Linux系统下安装宝塔面板,以及如何使用宝塔面板进行网站搭建…

    2024年6月19日
    01

联系我们

QQ:951076433

在线咨询:点击这里给我发消息邮件:951076433@qq.com工作时间:周一至周五,9:30-18:30,节假日休息