美文网首页
seccomp学习

seccomp学习

作者: 珍惜Any | 来源:发表于2022-01-08 21:57 被阅读0次

    简介:

    seccomp是Linux的一种安全机制,android 8.1以上使用了seccomp

    主要功能是限制直接通过syscall去调用某些系统函数

    seccomp的过滤模式有两种(strict&filter)

    第一种strict只支持如下四种,如果一旦使用了其他的syscall 则会收到SIGKILL信号

    read()

    write()

    exit()

    rt_sigreturn

    通过下面方式进行设置

    seccomp(SECCOMP_SET_MODE_STRICT)
    prctl (PR_SET_SECCOMP, SECCOMP_MODE_STRICT)。
    

    strict

    #include <fcntl.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <string.h>
    #include <linux/seccomp.h>
    #include <sys/prctl.h>
    
    int main(int argc, char **argv)
    {
    int output = open(“output.txt”, O_WRONLY);
    const char *val = “test”;
    //通过prctl函数设置seccomp的模式为strict
    printf(“Calling prctl() to set seccomp strict mode…\n”);
    
    prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT);
    
    printf(“Writing to an already open file…\n”);
    //尝试写入
    write(output, val, strlen(val)+1);
    printf(“Trying to open file for reading…\n”);
    
    //设置完毕seccomp以后再次尝试open (因为设置了secomp的模式是strict,所以这行代码直接sign -9 信号)
    int input = open(“output.txt”, O_RDONLY);
    printf(“You will not see this message — the process will be killed first\n”);
    }
    

    filter(BPF)

    Seccomp-bpf

    bpf是一种过滤模式,只有在linux高版本会存在该功能

    当某进程调用了svc 首先会进入我们自己写的bpf规则

    通过我们自己的写的规则,进行判断该函数是否被运行调用

    文外言:

    该功能可以配合ptrace+seccomp,当触发SIGSYS信号以后,判断siginfo_t 信号来源是否是SYS_SECCOMP

    直接使用ptrace修改寄存器内容。从而实现指令过滤,完成svc的拦截包括svc before 寄存器内容的修改(参考proot)

    因为如果只使用ptrace去调试目标进程的话,会导致程序卡顿,因为程序不断运行,会进行大量的系统调用。

    从而降低程序运行速度。

    可以seccomp先过滤一部分syscall ,配合ptrace实现修改寄存器,可以大大提升执行效率。

    很多App获取签名的方式本质是通过svc将app读取到内存,然后解析zip,解析里面的签名

    可以使用ptrace去修改svc的路径做到自动化绕过签名检测的功能。

    该方法也是目前主流的注入式攻击手段。

    #include <seccomp.h>
    #include <unistd.h>
    #include <stdio.h>
    #include <errno.h>
    void main(void)
    {
    /* 初始化上下文 */
    scmp_filter_ctx ctx = seccomp_init(SCMP_ACT_KILL);
    
    /* 允许程序退出,如果程序退出没有允许的话,会导致该进程无法退出 */
    printf(“Adding rule : Allow exit_group\n”);
    seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(exit_group), 0);
    
    /* 允许使用get pid 函数(参考测试1),也可以通过下面的方式去不允许使用getpid (测试2)*/
    printf(“Adding rule : Allow getpid\n”);
    seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(getpid), 0);
    
    //printf(“Adding rule : Deny getpid\n”);
    //seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EBADF), SCMP_SYS(getpid), 0);
    
    /* 允许根据 libc 的要求更改数据段大小 */
    printf(“Adding rule : Allow brk\n”);
    seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(brk), 0);
    
    
    /* 最多允许向 fd 1 写入 512 字节 */
    printf(“Adding rule : Allow write upto 512 bytes to FD 1\n”);
    
    seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 2,
    //这个1 是具体的fd
    SCMP_A0(SCMP_CMP_EQ, 1),
    //写入的字节数
    SCMP_A2(SCMP_CMP_LE, 512));
    
    /* 如果写入任何其他 fd,则返回 -EBADF */
    printf(“Adding rule : Deny write to any FD except 1 \n”);
    seccomp_rule_add(ctx, SCMP_ACT_ERRNO(EBADF), SCMP_SYS(write), 1,
    SCMP_A0(SCMP_CMP_NE, 1));
    
    /* load and enforce the filters */
    //开始执行过滤器
    printf(“Load rules and enforce \n”);
    seccomp_load(ctx);
    seccomp_release(ctx);
    printf(“this process is %d\n”, getpid());
    }
    

    测试1:

    [图片上传失败...(image-508d3b-1641650259316)]

    能够打印 PID。

    测试2:

    [图片上传失败...(image-146616-1641650259316)]

    正如预期的那样,getpid 系统调用失败 并且程序被-9 sign杀死

    全部代码参考:

    https://security.stackexchange.com/questions/168452/how-is-sandboxing-implemented/175373

    Docker&seccomp

    docker本身支持seccomp的bpf模式。

    Seccomp 配置文件可以 JSON 文件的形式定义。

    Docker 的默认 seccomp 配置文件是一个阻止 44 个系统调用的白名单。

    Docker 源码配置文件地址如下

    https://github.com/moby/moby/blob/master/profiles/seccomp/default.json

    具体的阻止的系统调用列表:

    https://docs.docker.com/engine/security/seccomp/

    相关文章

      网友评论

          本文标题:seccomp学习

          本文链接:https://www.haomeiwen.com/subject/qsnecrtx.html