MAC 모델을 사용하여 리눅스 시스템의 보안을 강화하기 위한 커널 모듈
SELinux 정책을 관리하고 문제를 해결하기 위한 유틸리티 모음 설치
$ dnf install -y policycoreutils-python-utils-3.6-2.1.el9.noarch
| 상태 | 의미 |
|---|---|
| Enforcing (적용 모드) | SELinux 보안 정책 규칙이 엄격하게 적용되어 허용되지 않은 액세스는 차단 |
| Permissive (허용 모드) | SELinux 보안 정책이 적용되지만 위반 사항은 로그에만 기록되고 실제로는 차단되지 않는다. |
| Disabled (비활성화 모드) | SELinux가 완전히 비활성화되어 보안 정책이 적용되지 않는다. |
SELinux 상태(Enforcing, Permissive, Disabled)를 확인
$ getenforce
Enforcing
SELINUX= 값을 변경하여 SELinux를 활성화/비활성화할 수 있다.
현재 SELinux 모드를 변경
# Permissive 모드로 변경
$ sudo setenforce 0
# Enforcing 모드로 변경
$ sudo setenforce 1
프로세스, 파일, 디렉터리, 포트 등)에 할당된 보안 레이블로 이 컨텍스트를 통해 SELinux는 객체 간의 액세스를 제어한다.
user:role:type:level
| 값 | 설명 |
|---|---|
| user | 객체를 소유한 SELinux 사용자 |
| role | 객체에 할당된 SELinux 역할 |
| type | 객체의 SELinux 도메인 유형 |
| level | 다수준 보안(Multi-Level Security) 수준 (옵션) |
# context check
$ ls -Zd /selinux-test/
unconfined_u:object_r:default_t:s0 /selinux-test/
chcon
<option>FILENAME
# chcon
$ chcon -t 'tmp_t' /selinux-test/
$ ls -Zd /selinux-test/
unconfined_u:object_r:tmp_t:s0 /selinux-test/
# chcon -R
$ chcon -R -t 'admin_home_t' /selinux-test/
# 디렉토리 내부의 파일까지 변경
$ ls -Z /selinux-test/
unconfined_u:object_r:admin_home_t:s0 first-dir
unconfined_u:object_r:admin_home_t:s0 first-file
# restorecon
$ restorecon -R /selinux-test/
$ ls -dZ /selinux-test/
unconfined_u:object_r:default_t:s0 /selinux-test/
# selinux-test 디렉토리에 생성되는 모든 파일에 `tmp_t` 컨텍스트 유형을 부여
$ semanage fcontext -a -t 'tmp_t' /selinux-test
# 설정 내용 확인
$ semanage fcontext -l |grep selinux-test
/selinux-test all files system_u:object_r:tmp_t:s0
# 적용 확인
$ touch /selinux-test/fcontext-test
$ ls -dZ /selinux-test/fcontext-test
unconfined_u:object_r:user_tmp_t:s0 /selinux-test/fcontext-test
임의의 포트를 사용하기 위해 포트 레이블 지정
$ semanage port -l | grep http
http_cache_port_t tcp 8080, 8118, 8123, 10001-10010
http_cache_port_t udp 3130
http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t tcp 5988
pegasus_https_port_t tcp 5989
semanage port [-a | -m | -d ] -t port-type -p [tcp | udp] port number
| 옵션 | 의미 |
|---|---|
| -a | add |
| -m | modify |
| -d | delete |
| -t | type |
| -p | protocol |
#http_port_t 포트 레이블에 8888/tcp 포트 추가
$ semanage port -a -t 'http_port_t' 8888 -p tcp
/etc/sshd/sshd_config 에서 21번줄의 port 번호를 바꾸고 재시작하면 오류가 발생한다.
$ vi /etc/ssh/sshd_config
$ systemctl restart sshd
Job for sshd.service failed because the control process exited with error code.
See "systemctl status sshd.service" and "journalctl -xeu sshd.service" for details.
아무 포트나 사용되는것을 방지하기 위함이며 아래와 같이 명령을 입력하면 적용 가능하다.
semanage port -a -t 'ssh_port_t' 2222 -p tcp
시스템이 운영중일 때 정책의 동작을 변경할 수 있는 스위치 같은 기능
http와 MariaDB는 서로 함께 사용할 때 컨텍스트 유형이 달라 접근할 수 없지만 Boolean을 사용해 정책의 동작 범위를 수정한다.
getsebool [ -a | boolean-name ]
-a : 전체 출력
# boolean-name을 지정하여 조회
$ getsebool httpd_can_network_connect_db
httpd_can_network_connect_db --> off
setsebool [-p] boolean-name [ on | off ]
setsebool -m [ -0 | -1 ] boolean-name
# httpd_can_network_connect_db 활성화
$ setsebool httpd_can_network_connect_db on
$ getsebool httpd_can_network_connect_db
httpd_can_network_connect_db --> on
# httpd_can_network_connect_db 비활성화
$ semanage boolean -m -0 httpd_can_network_connect_db
$ getsebool httpd_can_network_connect_db
httpd_can_network_connect_db --> off