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 ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.
๊ธฐ๋ณธ ์ ๋ณด
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 ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.


