Seccomp

Tip

AWS ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ:HackTricks Training AWS Red Team Expert (ARTE)
GCP ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ: HackTricks Training GCP Red Team Expert (GRTE) Azure ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks ์ง€์›ํ•˜๊ธฐ

๊ธฐ๋ณธ ์ •๋ณด

Seccomp๋Š” Secure Computing mode์˜ ์•ฝ์ž๋กœ, ์‹œ์Šคํ…œ ํ˜ธ์ถœ์„ ํ•„ํ„ฐ๋งํ•˜๊ธฐ ์œ„ํ•ด ์„ค๊ณ„๋œ Linux ์ปค๋„์˜ ๋ณด์•ˆ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ํ”„๋กœ์„ธ์Šค๋ฅผ ์ œํ•œ๋œ ์‹œ์Šคํ…œ ํ˜ธ์ถœ ์ง‘ํ•ฉ(exit(), sigreturn(), read(), ๋ฐ write() ์ด๋ฏธ ์—ด๋ฆฐ ํŒŒ์ผ ์„ค๋ช…์ž์— ๋Œ€ํ•ด)์œผ๋กœ ์ œํ•œํ•ฉ๋‹ˆ๋‹ค. ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋‹ค๋ฅธ ํ˜ธ์ถœ์„ ์‹œ๋„ํ•˜๋ฉด ์ปค๋„์— ์˜ํ•ด SIGKILL ๋˜๋Š” SIGSYS๋กœ ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค. ์ด ๋ฉ”์ปค๋‹ˆ์ฆ˜์€ ๋ฆฌ์†Œ์Šค๋ฅผ ๊ฐ€์ƒํ™”ํ•˜์ง€ ์•Š๊ณ  ํ”„๋กœ์„ธ์Šค๋ฅผ ์ด๋กœ๋ถ€ํ„ฐ ๊ฒฉ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

Seccomp๋ฅผ ํ™œ์„ฑํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค: PR_SET_SECCOMP์™€ ํ•จ๊ป˜ prctl(2) ์‹œ์Šคํ…œ ํ˜ธ์ถœ์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜, Linux ์ปค๋„ 3.17 ์ด์ƒ์—์„œ๋Š” seccomp(2) ์‹œ์Šคํ…œ ํ˜ธ์ถœ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. /proc/self/seccomp์— ์“ฐ๋Š” ์˜ค๋ž˜๋œ ๋ฐฉ๋ฒ•์€ prctl()์„ ์„ ํ˜ธํ•˜์—ฌ ๋” ์ด์ƒ ์‚ฌ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

ํ–ฅ์ƒ๋œ ๊ธฐ๋Šฅ์ธ seccomp-bpf๋Š” Berkeley Packet Filter (BPF) ๊ทœ์น™์„ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž ์ •์˜ ์ •์ฑ…์œผ๋กœ ์‹œ์Šคํ…œ ํ˜ธ์ถœ์„ ํ•„ํ„ฐ๋งํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ์ด ํ™•์žฅ์€ OpenSSH, vsftpd ๋ฐ Chrome OS์™€ Linux์˜ Chrome/Chromium ๋ธŒ๋ผ์šฐ์ €์™€ ๊ฐ™์€ ์†Œํ”„ํŠธ์›จ์–ด์—์„œ ์œ ์—ฐํ•˜๊ณ  ํšจ์œจ์ ์ธ ์‹œ์Šคํ…œ ํ˜ธ์ถœ ํ•„ํ„ฐ๋ง์„ ์œ„ํ•ด ํ™œ์šฉ๋˜๋ฉฐ, ์ด์ œ ์ง€์›๋˜์ง€ ์•Š๋Š” Linux์˜ systrace์— ๋Œ€ํ•œ ๋Œ€์•ˆ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

์›๋ณธ/์—„๊ฒฉ ๋ชจ๋“œ

์ด ๋ชจ๋“œ์—์„œ Seccomp๋Š” ์˜ค์ง syscalls exit(), sigreturn(), read() ๋ฐ write()๋ฅผ ์ด๋ฏธ ์—ด๋ฆฐ ํŒŒ์ผ ์„ค๋ช…์ž์— ๋Œ€ํ•ด์„œ๋งŒ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ syscalls๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํ”„๋กœ์„ธ์Šค๋Š” SIGKILL๋กœ ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค.

#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

์ด ๋ชจ๋“œ๋Š” ๋ฒ„ํด๋ฆฌ ํŒจํ‚ท ํ•„ํ„ฐ ๊ทœ์น™์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„๋œ ๊ตฌ์„ฑ ๊ฐ€๋Šฅํ•œ ์ •์ฑ…์„ ์‚ฌ์šฉํ•˜์—ฌ ์‹œ์Šคํ…œ ํ˜ธ์ถœ์„ ํ•„ํ„ฐ๋งํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.

#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());
}

Docker์—์„œ์˜ Seccomp

Seccomp-bpf๋Š” Docker์—์„œ syscalls๋ฅผ ์ œํ•œํ•˜์—ฌ ์ปจํ…Œ์ด๋„ˆ์˜ ๊ณต๊ฒฉ ํ‘œ๋ฉด์„ ํšจ๊ณผ์ ์œผ๋กœ ์ค„์ด๋Š” ๊ฒƒ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. https://docs.docker.com/engine/security/seccomp/์—์„œ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ฐจ๋‹จ๋œ syscalls๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ธฐ๋ณธ seccomp ํ”„๋กœํ•„์€ https://github.com/moby/moby/blob/master/profiles/seccomp/default.json์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‹ค๋ฅธ seccomp ์ •์ฑ…์œผ๋กœ ๋„์ปค ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

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

์ปจํ…Œ์ด๋„ˆ๊ฐ€ uname๊ณผ ๊ฐ™์€ syscall์„ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์„ ๊ธˆ์ง€ํ•˜๋ ค๋ฉด https://github.com/moby/moby/blob/master/profiles/seccomp/default.json์—์„œ ๊ธฐ๋ณธ ํ”„๋กœํ•„์„ ๋‹ค์šด๋กœ๋“œํ•˜๊ณ  ๋ชฉ๋ก์—์„œ uname ๋ฌธ์ž์—ด์„ ์ œ๊ฑฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
์–ด๋–ค ๋ฐ”์ด๋„ˆ๋ฆฌ๊ฐ€ ๋„์ปค ์ปจํ…Œ์ด๋„ˆ ๋‚ด์—์„œ ์ž‘๋™ํ•˜์ง€ ์•Š๋„๋ก ํ•˜๋ ค๋ฉด strace๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ”์ด๋„ˆ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” syscalls๋ฅผ ๋‚˜์—ดํ•œ ๋‹ค์Œ ์ด๋ฅผ ๊ธˆ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋‹ค์Œ ์˜ˆ์ œ์—์„œ๋Š” uname์˜ syscalls๊ฐ€ ๋ฐœ๊ฒฌ๋ฉ๋‹ˆ๋‹ค:

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

Note

๋งŒ์•ฝ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด Docker๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋ผ๋ฉด, **strace**๋กœ ํ”„๋กœํŒŒ์ผ๋งํ•˜๊ณ  ํ•„์š”ํ•œ ์‹œ์Šคํ…œ ํ˜ธ์ถœ๋งŒ ํ—ˆ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ Seccomp ์ •์ฑ…

์—ฌ๊ธฐ์—์„œ ์˜ˆ์ œ

Seccomp ๊ธฐ๋Šฅ์„ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด, ์•„๋ž˜์™€ ๊ฐ™์ด โ€œchmodโ€ ์‹œ์Šคํ…œ ํ˜ธ์ถœ์„ ๋น„ํ™œ์„ฑํ™”ํ•˜๋Š” Seccomp ํ”„๋กœํŒŒ์ผ์„ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

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

์œ„ ํ”„๋กœํ•„์—์„œ๋Š” ๊ธฐ๋ณธ ๋™์ž‘์„ โ€œํ—ˆ์šฉโ€œ์œผ๋กœ ์„ค์ •ํ•˜๊ณ  โ€œchmodโ€œ๋ฅผ ๋น„ํ™œ์„ฑํ™”ํ•˜๋Š” ๋ธ”๋ž™๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค. ๋” ์•ˆ์ „ํ•˜๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ๊ธฐ๋ณธ ๋™์ž‘์„ ๋“œ๋กญ์œผ๋กœ ์„ค์ •ํ•˜๊ณ  ์‹œ์Šคํ…œ ํ˜ธ์ถœ์„ ์„ ํƒ์ ์œผ๋กœ ํ™œ์„ฑํ™”ํ•˜๋Š” ํ™”์ดํŠธ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๋‹ค์Œ ์ถœ๋ ฅ์€ seccomp ํ”„๋กœํ•„์—์„œ ๋น„ํ™œ์„ฑํ™”๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— โ€œchmodโ€ ํ˜ธ์ถœ์ด ์˜ค๋ฅ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

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

๋‹ค์Œ ์ถœ๋ ฅ์€ ํ”„๋กœํŒŒ์ผ์„ ํ‘œ์‹œํ•˜๋Š” โ€œdocker inspectโ€œ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค:

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

Tip

AWS ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ:HackTricks Training AWS Red Team Expert (ARTE)
GCP ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ: HackTricks Training GCP Red Team Expert (GRTE) Azure ํ•ดํ‚น ๋ฐฐ์šฐ๊ธฐ ๋ฐ ์—ฐ์Šตํ•˜๊ธฐ: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks ์ง€์›ํ•˜๊ธฐ