Seccomp(Secure Computing mode의 약자)는 Linux 커널의 보안 기능으로 시스템 호출을 필터링하는 것을 목적으로 설계되었습니다. 이는 프로세스를 제한된 시스템 호출 집합(exit(), sigreturn(), read(), write())으로 제한합니다. 프로세스가 다른 호출을 시도하면 커널에 의해 SIGKILL 또는 SIGSYS를 사용하여 종료됩니다. 이 메커니즘은 리소스를 가상화하지 않고 프로세스를 리소스로부터 격리시킵니다.
seccomp를 활성화하는 두 가지 방법이 있습니다: prctl(2) 시스템 호출과 PR_SET_SECCOMP를 사용하거나 Linux 커널 3.17 이상의 경우 seccomp(2) 시스템 호출을 사용합니다. /proc/self/seccomp에 쓰는 방식으로 seccomp를 활성화하는 구식 방법은 prctl()을 선호하는 방식으로 대체되었습니다.
seccomp-bpf라는 개선된 모드는 Berkeley Packet Filter (BPF) 규칙을 사용하여 시스템 호출을 필터링하는 기능을 추가합니다. 이 확장은 OpenSSH, vsftpd 및 Chrome OS 및 Linux에서 Chrome/Chromium 브라우저와 같은 소프트웨어에서 사용되며 유연하고 효율적인 시스콜 필터링을 제공합니다. 이는 Linux에서 더 이상 지원되지 않는 systrace에 대한 대안입니다.
원래/엄격 모드
이 모드에서 Seccomp는 이미 열려있는 파일 디스크립터에 대해서만 exit(), sigreturn(), read(), write() 시스콜을 허용합니다. 다른 시스콜을 호출하면 프로세스가 SIGKILL을 사용하여 종료됩니다.
seccomp_strict.c
#include<fcntl.h>#include<stdio.h>#include<unistd.h>#include<string.h>#include<linux/seccomp.h>#include<sys/prctl.h>//From https://sysdig.com/blog/selinux-seccomp-falco-technical-discussion///gcc seccomp_strict.c -o seccomp_strictintmain(int argc,char**argv){int output =open("output.txt", O_WRONLY);constchar*val ="test";//enables strict seccomp modeprintf("Calling prctl() to set seccomp strict mode...\n");prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT);//This is allowed as the file was already openedprintf("Writing to an already open file...\n");write(output, val, strlen(val)+1);//This isn't allowedprintf("Trying to open file for reading...\n");int input =open("output.txt", O_RDONLY);printf("You will not see this message--the process will be killed first\n");}
Seccomp-bpf
이 모드는 Berkeley Packet Filter 규칙을 사용하여 구성 가능한 정책을 구현하여 시스템 호출을 필터링하는 것을 허용합니다.
seccomp_bpf.c
#include<seccomp.h>#include<unistd.h>#include<stdio.h>#include<errno.h>//https://security.stackexchange.com/questions/168452/how-is-sandboxing-implemented/175373//gcc seccomp_bpf.c -o seccomp_bpf -lseccompvoidmain(void) {/* initialize the libseccomp context */scmp_filter_ctx ctx =seccomp_init(SCMP_ACT_KILL);/* allow exiting */printf("Adding rule : Allow exit_group\n");seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(exit_group),0);/* allow getting the current pid *///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);/* allow changing data segment size, as required by glibc */printf("Adding rule : Allow brk\n");seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(brk),0);/* allow writing up to 512 bytes to fd 1 */printf("Adding rule : Allow write upto 512 bytes to FD 1\n");seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write),2,SCMP_A0(SCMP_CMP_EQ,1),SCMP_A2(SCMP_CMP_LE,512));/* if writing to any other fd, return -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);//Get the getpid is denied, a weird number will be returned like//this process is -9printf("this process is %d\n", getpid());}
위의 프로필에서는 기본 동작을 "허용"으로 설정하고 "chmod"를 비활성화하기 위해 블랙리스트를 만들었습니다. 더 안전하게 하기 위해 기본 동작을 거부로 설정하고 시스템 호출을 선택적으로 활성화하기 위해 화이트리스트를 만들 수 있습니다.
다음 출력은 seccomp 프로필에서 비활성화되어 있기 때문에 "chmod" 호출이 오류를 반환하는 것을 보여줍니다.
플래그 --security-opt seccomp=unconfined를 사용하여 컨테이너를 실행합니다.
Kubernetes 1.19부터는 모든 Pod에 대해 seccomp가 기본적으로 활성화되어 있습니다. 그러나 Pod에 적용되는 기본 seccomp 프로필은 "RuntimeDefault" 프로필입니다. 이 프로필은 컨테이너 런타임(Docker, containerd 등)에서 제공됩니다. "RuntimeDefault" 프로필은 대부분의 시스템 호출을 허용하면서 컨테이너에게서 위험하거나 일반적으로 필요하지 않은 몇 가지 시스템 호출을 차단합니다.
htARTE (HackTricks AWS Red Team Expert)를 통해 AWS 해킹을 처음부터 전문가까지 배워보세요!
HackTricks를 지원하는 다른 방법:
회사를 HackTricks에서 광고하거나 HackTricks를 PDF로 다운로드하려면 SUBSCRIPTION PLANS를 확인하세요!