Seccomp

Leer AWS-hacking van nul tot held met htARTE (HackTricks AWS Red Team Expert)!

Ander maniere om HackTricks te ondersteun:

Basiese Inligting

Seccomp, wat staan vir Secure Computing Mode, is 'n sekuriteitskenmerk van die Linux-kernel wat ontwerp is om stelseloproepe te filtreer. Dit beperk prosesse tot 'n beperkte stel stelseloproepe (exit(), sigreturn(), read() en write() vir reeds-geopen lêerbeskrywers). As 'n proses probeer om iets anders te roep, word dit deur die kernel beëindig deur gebruik te maak van SIGKILL of SIGSYS. Hierdie meganisme virtualiseer nie hulpbronne nie, maar isoleer die proses daarvan.

Daar is twee maniere om seccomp te aktiveer: deur die prctl(2) stelseloproep met PR_SET_SECCOMP, of vir Linux-kernel 3.17 en hoër, die seccomp(2) stelseloproep. Die ouer metode om seccomp te aktiveer deur na /proc/self/seccomp te skryf, is verouderd en is vervang deur prctl().

'n Verbetering, seccomp-bpf, voeg die vermoë by om stelseloproepe te filtreer met 'n aanpasbare beleid deur gebruik te maak van Berkeley Packet Filter (BPF) reëls. Hierdie uitbreiding word benut deur sagteware soos OpenSSH, vsftpd, en die Chrome/Chromium-webblaaier op Chrome OS en Linux vir buigsame en doeltreffende stelseloproep-filtrering, as 'n alternatief vir die nou nie-ondersteunde systrace vir Linux.

Oorspronklike/Strikte Modus

In hierdie modus laat Seccomp slegs die stelseloproepe toe exit(), sigreturn(), read() en write() na reeds-geopen lêerbeskrywers. As enige ander stelseloproep gemaak word, word die proses doodgemaak deur gebruik te maak van 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_strict

int main(int argc, char **argv)
{
int output = open("output.txt", O_WRONLY);
const char *val = "test";

//enables strict seccomp mode
printf("Calling prctl() to set seccomp strict mode...\n");
prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT);

//This is allowed as the file was already opened
printf("Writing to an already open file...\n");
write(output, val, strlen(val)+1);

//This isn't allowed
printf("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

Hierdie modus maak dit moontlik om sistemaanroepings te filter deur gebruik te maak van 'n konfigureerbare beleid wat geïmplementeer word deur gebruik te maak van Berkeley Packet Filter reëls.

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 -lseccomp

void main(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 -9
printf("this process is %d\n", getpid());
}

Seccomp in Docker

Seccomp-bpf word deur Docker ondersteun om die syscalls van die houers te beperk en sodoende die oppervlakte te verminder. Jy kan die syscalls wat standaard geblokkeer word vind by https://docs.docker.com/engine/security/seccomp/ en die standaard seccomp-profiel kan hier gevind word https://github.com/moby/moby/blob/master/profiles/seccomp/default.json. Jy kan 'n docker-houer uitvoer met 'n verskillende seccomp-beleid met:

docker run --rm \
-it \
--security-opt seccomp=/path/to/seccomp/profile.json \
hello-world

As jy byvoorbeeld 'n houer wil verbied om sekere syscall soos uname uit te voer, kan jy die verstek profiel aflaai vanaf https://github.com/moby/moby/blob/master/profiles/seccomp/default.json en net die uname string uit die lys verwyder. As jy seker wil maak dat 'n sekere binêre lêer nie binne 'n Docker-houer werk nie, kan jy strace gebruik om die syscalls wat die binêre lêer gebruik, te lys en dit dan verbied. In die volgende voorbeeld word die syscalls van uname ontdek:

docker run -it --security-opt seccomp=default.json modified-ubuntu strace uname

As jy Docker net gebruik om 'n toepassing te begin, kan jy dit profiler met strace en slegs die syscalls toelaat wat dit nodig het.

Voorbeeld Seccomp-beleid

Voorbeeld van hier

Om die Seccomp-funksie te illustreer, skep ons 'n Seccomp-profiel wat die "chmod" stelseloproep deaktiveer soos hieronder.

{
"defaultAction": "SCMP_ACT_ALLOW",
"syscalls": [
{
"name": "chmod",
"action": "SCMP_ACT_ERRNO"
}
]
}

In die bogenoemde profiel het ons die verstekaksie op "toelaat" gestel en 'n swartlys geskep om "chmod" te deaktiveer. Om meer veilig te wees, kan ons die verstekaksie op "afwerp" stel en 'n witlys skep om selektief stelseloproepe toe te laat. Die volgende uitset toon die "chmod" oproep wat 'n fout teruggee omdat dit gedeaktiveer is in die seccomp-profiel.

$ docker run --rm -it --security-opt seccomp:/home/smakam14/seccomp/profile.json busybox chmod 400 /etc/hosts
chmod: /etc/hosts: Operation not permitted

Die volgende uitset toon die "docker inspect" wat die profiel vertoon:

"SecurityOpt": [
"seccomp:{\"defaultAction\":\"SCMP_ACT_ALLOW\",\"syscalls\":[{\"name\":\"chmod\",\"action\":\"SCMP_ACT_ERRNO\"}]}"
],

Deaktiveer dit in Docker

Begin 'n houer met die vlag: --security-opt seccomp=unconfined

Vanaf Kubernetes 1.19, is seccomp standaard geaktiveer vir alle Pods. Die verstek seccomp profiel wat op die Pods toegepas word, is die "RuntimeDefault" profiel, wat voorsien word deur die houer runtime (bv. Docker, containerd). Die "RuntimeDefault" profiel laat die meeste stelseloproepe toe terwyl dit 'n paar blokkeer wat as gevaarlik beskou word of nie algemeen deur houers benodig word nie.

Leer AWS hacking van nul tot held met htARTE (HackTricks AWS Red Team Expert)!

Ander maniere om HackTricks te ondersteun:

Last updated