Kubernetes/Certificates

[CKS] System Hardening

Operation CWAL 2021. 5. 2. 00:05

AppArmor

K8s Pod에 AppArmor 적용

K8s에서 AppArmor 기능을 사용하기 위해선 다음과 같은 요구사항을 충족해야 한다.

  • Container Runtime이 AppArmor를 지원
  • 모든 Node에 AppArmor 설치
  • 모든 Node에 AppArmor 프로필  존재
  • 컨테이너 단위로 AppArmor 프로필 명시

다음과 같이 Pod Manifest 파일을 작성한다.

kubectl run secure --image=nginx -oyaml --dry-run=client > secure-pod.yaml
vim secure-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  annotations:
    container.apparmor.security.beta.kubernetes.io/secure: localhost/container-nginx
  labels:
    run: secure
  name: secure
spec:
  containers:
  - image: nginx
    name: secure
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

 

 

Restrict a Container's Access to Resources with AppArmor

FEATURE STATE: Kubernetes v1.4 [beta] AppArmor is a Linux kernel security module that supplements the standard Linux user and group based permissions to confine programs to a limited set of resources. AppArmor can be configured for any application to reduc

kubernetes.io

위 설정만으로는 Pod을 생성할 수 없으며, 모든 Node에 'container-nginx'라는 Profile을 생성해야 한다. 아래와 같이 AppArmor 프로필을 /etc/apparmor.d/container-nginx 경로에 생성한다. 참고로 테스트 환경은 1 Master, 1 Worker로 구성되기 때문에, 한번만 작업하면 된다.

#include <tunables/global>


profile container-nginx flags=(attach_disconnected,mediate_deleted) {
  #include <abstractions/base>

  network inet tcp,
  network inet udp,
  network inet icmp,

  deny network raw,

  deny network packet,

  file,
  umount,

  deny /bin/** wl,
  deny /boot/** wl,
  deny /dev/** wl,
  deny /etc/** wl,
  deny /home/** wl,
  deny /lib/** wl,
  deny /lib64/** wl,
  deny /media/** wl,
  deny /mnt/** wl,
  deny /opt/** wl,
  deny /proc/** wl,
  deny /root/** wl,
  deny /sbin/** wl,
  deny /srv/** wl,
  deny /tmp/** wl,
  deny /sys/** wl,
  deny /usr/** wl,

  audit /** w,

  /var/run/nginx.pid w,

  /usr/sbin/nginx ix,

  deny /bin/dash mrwklx,
  deny /bin/sh mrwklx,
  deny /usr/bin/top mrwklx,


  capability chown,
  capability dac_override,
  capability setuid,
  capability setgid,
  capability net_bind_service,

  deny @{PROC}/* w,   # deny write for all files directly in /proc (not in a subdir)
  # deny write to files not in /proc/<number>/** or /proc/sys/**
  deny @{PROC}/{[^1-9],[^1-9][^0-9],[^1-9s][^0-9y][^0-9s],[^1-9][^0-9][^0-9][^0-9]*}/** w,
  deny @{PROC}/sys/[^k]** w,  # deny /proc/sys except /proc/sys/k* (effectively /proc/sys/kernel)
  deny @{PROC}/sys/kernel/{?,??,[^s][^h][^m]**} w,  # deny everything except shm* in /proc/sys/kernel/
  deny @{PROC}/sysrq-trigger rwklx,
  deny @{PROC}/mem rwklx,
  deny @{PROC}/kmem rwklx,
  deny @{PROC}/kcore rwklx,

  deny mount,

  deny /sys/[^f]*/** wklx,
  deny /sys/f[^s]*/** wklx,
  deny /sys/fs/[^c]*/** wklx,
  deny /sys/fs/c[^g]*/** wklx,
  deny /sys/fs/cg[^r]*/** wklx,
  deny /sys/firmware/** rwklx,
  deny /sys/kernel/security/** rwklx,
}

다음 명령어를 사용하여 프로필을 적용한다. 

apparmor_parser /etc/apparmor.d/container-nginx
aa-status

seccomp

K8s Pod에 seccomp 적용

모든 Worker에 다음과 같은 작업을 진행한다.

1. kubelet이 위치한 경로(/var/lib/kubelet)에 seccomp/profiles라는 디렉토리를 생성

mkdir -p /var/lib/kubelet/seccomp
vim /var/lib/kubelet/seccomp/profiles/default.json

2. seccomp 프로필 파일 default.json 추가

{
    "defaultAction": "SCMP_ACT_ERRNO",
    "architectures": [
        "SCMP_ARCH_X86_64",
        "SCMP_ARCH_X86",
        "SCMP_ARCH_X32"
    ],
    "syscalls": [
        {
            "names": [
                "accept4",
                "epoll_wait",
                "pselect6",
                "futex",
                "madvise",
                "epoll_ctl",
                "getsockname",
                "setsockopt",
                "vfork",
                "mmap",
                "read",
                "write",
                "close",
                "arch_prctl",
                "sched_getaffinity",
                "munmap",
                "brk",
                "rt_sigaction",
                "rt_sigprocmask",
                "sigaltstack",
                "gettid",
                "clone",
                "bind",
                "socket",
                "openat",
                "readlinkat",
                "exit_group",
                "epoll_create1",
                "listen",
                "rt_sigreturn",
                "sched_yield",
                "clock_gettime",
                "connect",
                "dup2",
                "epoll_pwait",
                "execve",
                "exit",
                "fcntl",
                "getpid",
                "getuid",
                "ioctl",
                "mprotect",
                "nanosleep",
                "open",
                "poll",
                "recvfrom",
                "sendto",
                "set_tid_address",
                "setitimer",
                "writev"
            ],
            "action": "SCMP_ACT_ALLOW"
        }
    ]
}

 

3. 테스트를 위해 다음과 같이 Pod Manifest 파일을 작성한다. localhostProfile 필드에 '/var/lib/kubelet/seccomp' 을 생략한 나머지 profile 파일 경로를 지정해야 한다.

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: secure
  name: secure
spec:
  seccompProfile:
    type: Localhost
    localhostProfile: profiles/default.json
  containers:
  - image: nginx
    name: secure
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

 

 

Restrict a Container's Syscalls with Seccomp

FEATURE STATE: Kubernetes v1.19 [stable] Seccomp stands for secure computing mode and has been a feature of the Linux kernel since version 2.6.12. It can be used to sandbox the privileges of a process, restricting the calls it is able to make from userspac

kubernetes.io

 

공격 영역(Attack Surface) 줄이기

Kubernetes 보단 일반적인 Linux 사용법을 다루는 항목으로 그닥 어렵지 않고, 다음과 같이 기초적인 내용만 알면 된다.

systemctl을 사용하여 snapd 서비스 비활성화하기

systemctl status snapd
systemctl stop snapd
systemctl list-units --type=service --state=running | grep snapd
systemctl disable snapd

서비스 설치 및 추적

vsftpd(FTP 서버), smbd(samba)를 설치하고, 서비스 및 프로세스를 확인해보자.

apt update
apt install vsftpd samba

systemctl start vsftpd
systemctl status vsftpd
systemctl start smbd
systemctl status smbd

ps aux | grep vsftpd
ps aux | grep smbd

어떤 프로세스가 21번 포트를 사용하고 있는 추적할 수 있다.

netstat -plnt | grep 21
lsof -i :21

21번 포트는 vsftpd가 사용하고 있는 것을 알 수 있다. systemctl 명령어를 사용하여 해당 서비스를 비활성화하자.

systemctl status vsftpd
systemctl stop vsftpd
systemctl list-units --type=service --state=running | grep vsftpd
systemctl disable snapd

 

Linux 사용자 조사

현재 사용자 이름은 다음 명령어로 확인할 수 있다.

whoami

시스템의 모든 사용자 리스트는 /etc/passwd 파일을 통해 조회 가능하다.

cat /etc/passwd

새로운 사용자는 다음 명령어를 통해 추가할 수 있다. 비밀번호 및 사용자 정보 등을 추가로 입력해야 한다.

adduser <username>

참고로 신규 사용자는 sudoer 파일에 추가되지 않았기 때문에, 'sudo' 명령어를 사용할 수 없다.

'Kubernetes > Certificates' 카테고리의 다른 글

[CKS] Runtime Security  (0) 2021.05.08
[CKS] Minimize Microservice Vulnerabilities  (0) 2021.05.03
[CKS] Cluster Hardening  (0) 2021.05.01
[CKS] Cluster Setup  (0) 2021.04.30
Certified Kubernetes Security Specialist (CKS)  (0) 2021.04.27