1. SELinux란?
2. SELinux 모드
3. SELinux 명령어
참고 하면 좋은 사이트 :https://lesstif.gitbook.io/web-service-hardening/selinux
보안 강화 리눅스(SELinux) - web service hardening
http_port_t tcp 9004, 8000, 8080, 10080, 8001, 80, 81, 443, 488, 8008, 8009, 8443, 9000
lesstif.gitbook.io
1. SELinux란 ?
- 리눅스는 소스코드가 공개되어 있기 때문에 보안에 취약합니다.
- SELinux 는 Linux의 보안을 강화해 주는 보안 강화 커널이고 zero-day 공격 및 buffer overflow 등 어플리케이션 취약점으로 인한 해킹을 방지해 주는 핵심 구성요소이다.
- SELinux는 보안을 강화하기 위해 리눅스 시스템에서 사용되는 보안 모듈 중 하나이다.
- SELinux는 Mandatory Access Control (MAC)을 제공하여 프로세스 및 파일에 대한 액세스 권한을 강력하게 제어합니다.(Mandatory Access Control:신뢰하는 접근 컨트롤(?))
- 특정 서비스가 SELinux 때문에 동작하지 않는다면 SELinux 를 끄기 보다는 해당 서비스가 SELinux 하에서 잘 동작하도록 설정을 수정하는걸 권장합니다.
✨ 개인적인 생각으로 보안이란 중요하지만 보안은 우리가 생활함에 있어 편리성과 반비례한다.
보안이 뛰어나면 편리성이 감소하고, 보안이 취약해지면 편리성은 증가한다. 보안이 뛰어나다는것은 설정할것과 관리해야 할게 많아지고 제약도 많이 생기게 됩니다.
2. SELinux의 모드
- Enforcing : 이 모드에서 SELinux는 정책을 강제 적용하고, 정책에 위반되는 액세스를 차단합니다. 이 모드가 시스템의 보안을 최대한 강화하는 기본 모드입니다.
- Permissive : 이 모드에서 SELinux는 정책 위반을 감지하지만, 액세스를 차단하지 않고 로그에 기록만 합니다. 이 모드는 문제 해결이나 정책 테스트 목적으로 사용됩니다.
- Disabled : 이 모드에서 SELinux는 완전히 비활성화되어 정책을 적용하지 않습니다. 시스템 보안이 약화되므로 꼭 필요한 경우가 아니라면 사용하지 않습니다.
3. SELinux의 자주 사용하는 명령어
🔻 SELinux 상태 확인
- sestatus
[root@localhost ~]# sestatus
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: permissive
Mode from config file: permissive
Policy MLS status: enabled
Policy deny_unknown status: allowed
Memory protection checking: actual (secure)
Max kernel policy version: 33
🔻 SELinux 모드 변경
- setenforce
- 일시적이며 현재 세션에서 즉시 SELinux를 모드를 변경합니다.
- Permissive 모드로 변경: 0 , Enforcing 모드로 변경: 1 , Disable 옵션이 없습니다.
✔ setenforce 1
[root@localhost ~]# setenforce 1
[root@localhost ~]# sestatus
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: permissive
Policy MLS status: enabled
Policy deny_unknown status: allowed
Memory protection checking: actual (secure)
Max kernel policy version: 33
✔ setenforce 0
[root@localhost ~]# setenforce 0
[root@localhost ~]# sestatus
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: permissive
Mode from config file: permissive
Policy MLS status: enabled
Policy deny_unknown status: allowed
Memory protection checking: actual (secure)
Max kernel policy version: 33
✨ Current mode 가 변경되어있는지 확인
✔ 영구적으로 변경을 원하시면 /etc/selinux/config
- SELINUX= 원하는 모드
- 파일을 수정후 재부팅을 해야 적용이 됩니다.
[root@localhost ~]# vi /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=permissive
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
🔻 SELinux 보안 컨텍스트 확인
- 파일이나 프로세스의 SELinux 보안 컨텍스트를 확인하려면 ls 명령에 -Z 옵션을 사용합니다.
[root@localhost ~]# ls -Z cproot
unconfined_u:object_r:admin_home_t:s0 cproot
🔻 SELinux 로그
- SELinux 이벤트는 audit 로그에 기록됩니다. 로그 파일은 /var/log/audit/audit.log에 위치합니다.
[root@localhost ~]# ll /var/log/audit/audit.log
-rw-------. 1 root root 1095809 Jan 9 19:40 /var/log/audit/audit.log
- 명령어 : ausearch -m avc
[root@localhost ~]# ausearch -m avc
time->Wed Jan 10 19:25:47 2024
type=PROCTITLE msg=audit(1704932747.347:136): proctitle="/usr/lib/systemd/systemd-logind"
type=PATH msg=audit(1704932747.347:136): item=0 name="/proc/thread-self/attr/fscreate" inode=19332 dev=00:05 mode=0100666 ouid=0 ogid=0 rdev=00:00 obj=system_u:system_r:systemd_logind_t:s0 nametype=NORMAL cap_fp=0 cap_fi=0 cap_fe=0 cap_fver=0 cap_frootid=0
type=CWD msg=audit(1704932747.347:136): cwd="/"
type=SYSCALL msg=audit(1704932747.347:136): arch=c000003e syscall=257 success=yes exit=22 a0=ffffff9c a1=56231d9f4620 a2=80002 a3=0 items=1 ppid=1 pid=786 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) ses=4294967295 comm="systemd-logind" exe="/usr/lib/systemd/systemd-logind" subj=system_u:system_r:systemd_logind_t:s0 key=(null)
type=AVC msg=audit(1704932747.347:136): avc: denied { search } for pid=786 comm="systemd-logind" name="/" dev="sda1" ino=128 scontext=system_u:system_r:systemd_logind_t:s0 tcontext=system_u:object_r:unlabeled_t:s0 tclass=dir permissive=1
avc (SELinux 에러) 메시지를 모두 표시 = 이를 통해 어떤 프로세스가 SELinux 에러를 유발했는지 확인할 수 있습니다.
pid (프로세스 ID), comm (프로세스 이름), name (파일 또는 리소스 이름) 등을 확인하여 어떤 프로세스에서 에러가 발생했는지 추론할 수 있습니다.
- 명령어 : ausearch -m avc | grep "keyword"
이렇게 keyword를 grep 형태로 입력하여 찾을수도 있습니다.
[root@localhost ~]# ausearch -m avc |grep "systemd-logind"
type=SYSCALL msg=audit(1704933141.260:162): arch=c000003e syscall=46 success=yes exit=10 a0=18 a1=7ffc7ed7f420 a2=4000 a3=7ffc7ed7f4c0 items=1 ppid=1 pid=786 auid=4294967295 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=(none) ses=4294967295 comm="systemd-logind" exe="/usr/lib/systemd/systemd-logind" subj=system_u:system_r:systemd_logind_t:s0 key=(null)
type=AVC msg=audit(1704933141.260:162): avc: denied { search } for pid=786 comm="systemd-logind" name="/" dev="sda1" ino=128 scontext=system_u:system_r:systemd_logind_t:s0 tcontext=system_u:object_r:unlabeled_t:s0 tclass=dir permissive=1
'리눅스 > 서버운영' 카테고리의 다른 글
[리눅스] KickStart (1) | 2024.01.23 |
---|---|
[리눅스] 파티션 복사 실습⑧ (완결편) (0) | 2024.01.19 |
[리눅스] 파티션 복사 실습⑦ (퍼미션 비교해보기) (1) | 2024.01.09 |
[리눅스] 파티션 복사 실습⑥ (데몬죽이기) (0) | 2024.01.08 |
[리눅스] 파티션 복사 실습⑤ (SELinux비활성화) (0) | 2024.01.04 |
댓글