ebpf学习(7)

本篇介绍

linux在安全领域也用到了ebpf,比如capabilities,seccomp等,本篇记录下在这块的使用。

capabilities

capabilities的使用场景是需要对某个非特权的进程做某个操作的权限,同时又不想给suid权限,毕竟这样会增大攻击面。这时候就可以通过capabilities来完成,由特权进程给目标进程赋予对应的capability。 接下来我们看一个例子,比如http监听80端口是需要权限的:

package main

import (
"net/http"
"log"
)

func main() {
  log.Fatalf("%v", http.ListenAndServe(":80",nil))
}

这个go程序会监听80端口,如果以非root权限身份执行会失败。

./http_demo 
2024/10/06 10:59:42 listen tcp :80: bind: permission denied

接下来利用capsh添加cap_net_bind_service 权限:

sudo capsh --caps='cap_net_bind_service+eip cap_setpcap,cap_setuid,cap_setgid+ep' --keep=1 --user="nobody" --addamb=cap_net_bind_service -- -c "./http_demo"

命令介绍如下:
--caps='cap_net_bind_service+eip cap_setpcap,cap_setuid,cap_setgid+ep'
这是给当前进程配置的权限,由于是root,直接声明就行,添加了用于访问系统端口的cap_net_bind_service,切换用户用的 cap_setpcap,cap_setuid,cap_setgid。
--keep=1
用户切换万后继续保留
--user="nobody"
切换的目标用户是nobody
--addamb=cap_net_bind_service
设置切换用户后继续保留的权限,其余权限会被系统清理

上述命令也包含了eip标识符,含义如下:
e:目标capbility是可用的
i: 可被子进程继承
p:需要被激活

sudo capsh --caps='cap_net_bind_service+eip cap_setpcap,cap_setuid,cap_setgid+ep' --keep=1 --user="nobody" --addamb=cap_net_bind_service -- -c "./http_demo"

这样运行后,就可以执行起来了。
用之前介绍的ss命令就可以看到已经在监听了:

 ss -tulpn |grep http
tcp   LISTEN 0      4096                *:80               *:*    users:(("http_demo",pid=430801,fd=3))

接下来用bpftrace attach下cap_capable:

sudo bpftrace -e 'kprobe:cap_capable { time("%H:%M:%S "); printf("%-6d %-6d %-16s %-4d %d\n", uid, pid, comm,arg2, arg3);}'  |grep http
12:00:12 65534  433571 http_demo        12   2
12:00:12 65534  433571 http_demo        12   2
12:00:12 65534  433571 http_demo        12   2
12:00:12 65534  433571 http_demo        12   2
12:00:12 65534  433571 http_demo        10   0

cap_capable定义如下:

int cap_capable(const struct cred *cred, struct user_namespace *ns,
               int cap, unsigned int opts);

第2个参数是cap:

/* Allows binding to TCP/UDP sockets below 1024 */
/* Allows binding to ATM VCIs below 32 */

#define CAP_NET_BIND_SERVICE 10

/* Allow broadcasting, listen to multicast */

#define CAP_NET_BROADCAST    11

/* Allow interface configuration */
/* Allow administration of IP firewall, masquerading and accounting */
/* Allow setting debug option on sockets */
/* Allow modification of routing tables */
/* Allow setting arbitrary process / process group ownership on
   sockets */
/* Allow binding to any address for transparent proxying (also via NET_RAW) */
/* Allow setting TOS (type of service) */
/* Allow setting promiscuous mode */
/* Allow clearing driver statistics */
/* Allow multicasting */
/* Allow read/write of device-specific registers */
/* Allow activation of ATM control sockets */

#define CAP_NET_ADMIN        12

第三个参数定义:

/* Default (no) options for the capable function */
#define CAP_OPT_NONE 0x0
/* If capable should audit the security request */
#define CAP_OPT_NOAUDIT BIT(1)
/* If capable is being called by a setid function */
#define CAP_OPT_INSETID BIT(2)

这时候就可以和上面的bpftrace信息对上了,记录了CAP_NET_BIND_SERVICE。

Seccomp

Seccomp是Secure Computing,允许在系统调用级别进行过滤,和capability是两个维度。Seccomp也是基于bpf实现的,让每个Seccomp包执行bpf程序,对应的包结构如下:

struct seccomp_data {
int nr;
__u32 arch;
__u64 instruction_pointer;
__u64 args[6];
};

nr是系统调用,arch是体系结构,instruction_pointer是当前的指令地址,args是调用参数,这样就可以基于如上信息的组合做过滤。过滤结果有以下几种:
SECCOMP_RET_KILL_PROCESS:kill当前整个进程
SECCOMP_RET_KILL_THREAD:kill当前线程
SECCOMP_RET_KILL:和SECCOMP_RET_KILL_THREAD一样
SECCOMP_RET_TRAP:不允许进行该系统调用,并且发送SIGSYS信号
SECCOMP_RET_ERRNO:不允许该系统调用,并且返回SECCOMP_RET_DATA
SECCOMP_RET_TRACE:通知tracer进程
SECCOMP_RET_LOG:允许调用,并且打印log
SECCOMP_RET_ALLOW:允许调用

接下来就用一个例子演示下:

#include <errno.h>
#include <linux/audit.h>
#include <linux/bpf.h>
#include <linux/filter.h>
#include <linux/seccomp.h>
#include <linux/unistd.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/prctl.h>
#include <unistd.h>

static int install_filter(int nr, int arch, int error) {
  struct sock_filter filter[] = {
 // 加载arch 字段到累加器中
    BPF_STMT(BPF_LD + BPF_W + BPF_ABS, (offsetof(struct seccomp_data, arch))),
// 将累加器中的arch与传起来的arch对比,如果一样,则执行下一条指令,如果不等,则跳到下一条指令再加3的指令处,也就是ALLOW
    BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, arch, 0, 3),
// 读取nr
    BPF_STMT(BPF_LD + BPF_W + BPF_ABS, (offsetof(struct seccomp_data, nr))),
// 比较nr,如果一样,则执行下一条指令,否则ALLOW
    BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, nr, 0, 1),
    BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ERRNO | (error & SECCOMP_RET_DATA)),
    BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW),
  };
  struct sock_fprog prog = {
    .len = (unsigned short)(sizeof(filter) / sizeof(filter[0])),
    .filter = filter,
  };
  if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) {
    perror("prctl(PR_SET_SECCOMP)");
    return 1;
  }
  return 0;
}

int main(int argc, char const *argv[]) {
  if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
    perror("prctl(NO_NEW_PRIVS)");
    return 1;
  }
  install_filter(__NR_write, AUDIT_ARCH_X86_64, EPERM);
  return system(argv[1]);
}

这儿需要注意下seccomp用的是cbpf,而不是ebpf,因此就无法直接使用寄存器了,只能按照cbpf的宏来操作。上述例子的作用就是禁止写系统调用,我们可以用ls演示下,结果如下:

 ls 
seccomp.c  secomp.bin
./secomp.bin ls

用strace 跟踪下:

strace -f ./secomp.bin ls
[pid 461191] write(2, "ls: ", 4)        = -1 EPERM (Operation not permitted)
[pid 461191] write(2, "write error", 11) = -1 EPERM (Operation not permitted)
[pid 461191] write(2, "\n", 1)          = -1 EPERM (Operation not permitted)
[pid 461191] exit_group(2)              = ?
[pid 461191] +++ exited with 2 +++

可以看到write被禁止了。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容