ㅁ 제한하는 프로세스
Redhat linux는 sshd 및 httpd 같은 네트워크에서 수신하는 서비스는 거의 모든 제한이 있습니다.
또한 passwd util등의 root 사용자로 실행하고 사용자에 대한 작업을 수행하는 프로세스는 거의 제한이 있습니다.
프로세스에 제한이 있는 프로세스 자체 도메인에서 실행됩니다.
httpd_t 도메인에서 httpd 프로세스가 실행되는 등등 입니다.
제한이 있는 프로세스가 공격자에 의해 위험에 처해도 SELinux 정책 설정에 따라 공격자의 소스에 대한 액세스와 공격으로 인한 손상은 제한됩니다.
ㅁ SELinux 상태 확인 방법
SELinux 활성화 enforcing 모드에서 실행되고, 해당 정책이 사용되고 있는지 확인, 정상적인 출력은 아래와 같음
~]$
sestatus
SELinux status: enabled SELinuxfs mount: /selinux Current mode: enforcing Mode from config file: enforcing Policy version: 24 Policy from config file: targeted
root로 /var/www/html/ 디렉토리에 파일을 생성
~]#
touch /var/www/html/testfile
생성된 파일의 SELinux 확인
~]$
ls -Z /var/www/html/testfile
-rw-r--r-- root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/testfile
기본적으로 Linux 사용자는 제한이 없음
testfile 파일에 SELinux unconfined_u 사용자의 라벨이 붙어 있음
RBAC는 파일이 아닌 프로세스에 사용됨, 역활은 파일에게 의미가 없음
object_r 역할은 파일에 사용되는 일반적인 역할
/proc/ 디렉토리 아래에서는 프로세스와 관련된 파일은 system_r role을 사용하는 경우가 있음
httpd_sys_content_t 타입은 httpd 프로세스가 파일에 액세스 할수 있도록 허용
- Samba가 사용하는 파일등 정확하게 분류되지 않은 파일을 apache http server (httpd)가 읽는것을 방지하는 정책
SELinux의 방지 예시를 보여줌, 이것은 어디까지 예시이며, 실운영 환경에서는 사용하지 말것
httpd 및 wget 패키지가 설치 되어 있음, SELinux 대상 정책이 사용되고, SELinux 가 enforcing 모드에서 실행되는것을 전제로 진행
ㅁ 프로세스 제한하는 예시
root로 httpd 데몬을 시작
~]#
systemctl start httpd.service
서비스가 실행되고 있는지 확인
출력은 다음과 같음, 타임스탬프만 다름
~]$
systemctl status httpd.service
httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled) Active: active (running) since Mon 2013-08-05 14:00:55 CEST; 8s ago
Linux 사용자의 쓰기 권한이 있는 디렉토리로 전환 한후 다음 명령을 실행
기본 설정에 변경이 없으면 이 명령은 성공함
~]$
wget http://localhost/testfile
--2009-11-06 17:43:01-- http://localhost/testfile Resolving localhost... 127.0.0.1 Connecting to localhost|127.0.0.1|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 0 [text/plain] Saving to: `testfile' [ <=> ] 0 --.-K/s in 0s 2009-11-06 17:43:01 (0.00 B/s) - `testfile' saved [0/0]
chcon 명령으로 파일의 레이블을 변경
파일 시스템의 레이블이 바꿔 질이 변경 사항이 손실
파일 시스템 레이블이 바꿔진 경우 이러한 변경으르 영구적으로 유지 하려면 semanage 유틸리티를 사용
이 명령에 대해서는 추후 설명
root 로 다음 명령을 실행하여 유형을 samba에서 사용되는 유형으로 변경
~]#
chcon -t samba_share_t /var/www/html/testfile
변경된것을 확인하려면 다음과 같은 명령어를 실행
~]$
ls -Z /var/www/html/testfile
-rw-r--r-- root root unconfined_u:object_r:samba_share_t:s0 /var/www/html/testfile
현재 DAC 권한은 httpd 프로세스에 의한 testfile에 대한 액세스를 허용하는 것에 유의하십시요
사용자로의 쓰기 권한이 있는 디렉토리로 전환 한 후 다음 명령을 실행합니다.
기본 설정에 변경이 없으면 이 명령은 실패합니다.
~]$
wget http://localhost/testfile
--2009-11-06 14:11:23-- http://localhost/testfile Resolving localhost... 127.0.0.1 Connecting to localhost|127.0.0.1|:80... connected. HTTP request sent, awaiting response... 403 Forbidden 2009-11-06 14:11:23 ERROR 403: Forbidden.
root 로 testfile을 삭제
~]#
rm -i /var/www/html/testfile
httpd 실행이 필요하지 않은 경우 root로 다음 명령을 실행 중지
~]#
systemctl stop httpd.service
이 예에서는 SELinux에서 추가 된 새로운 보안을 설명
2단계에서는 DAC 규칙을 httpd 프로세스에 의한 testfile에 대한 액세스를 허용
파일은 httpd 프로세스에 권한 없는 유형으로 분류되어 있기 때문에, SELinux가 액세스를 거브
auditd 데몬이 실행되면 다음과 같은 오류가 /var/log/audit/audit.log에 기록
type=AVC msg=audit(1220706212.937:70): avc: denied { getattr } for pid=1904 comm="httpd" path="/var/www/html/testfile" dev=sda5 ino=247576 scontext=unconfined_u:system_r:httpd_t:s0 tcontext=unconfined_u:object_r:samba_share_t:s0 tclass=file type=SYSCALL msg=audit(1220706212.937:70): arch=40000003 syscall=196 success=no exit=-13 a0=b9e21da0 a1=bf9581dc a2=555ff4 a3=2008171 items=0 ppid=1902 pid=1904 auid=500 uid=48 gid=48 euid=48 suid=48 fsuid=48 egid=48 sgid=48 fsgid=48 tty=(none) ses=1 comm="httpd" exe="/usr/sbin/httpd" subj=unconfined_u:system_r:httpd_t:s0 key=(null)
또한 다음과 같은 오류가 /var/log/httpd/error_log 에 기록
[Wed May 06 23:00:54 2009] [error] [client 127.0.0.1] (13)Permission denied: access to /testfile denied
ㅁ 제한이 없는 프로세스
제한없는 프로세스가 제한되지 않는 도메인에서 실행됩니다.
예를들면 init에서 실행되는 제한이 없는 서비스는 unconfined_service_t
도메인에서 커널에서 실행되는 제한이 없는 서비스는 kernel_t
도메인에 제한이 없는 Linux 사용자에 의해 실행되는 제한이 없는 서비스는 unconfined_t 도메인에서 실행되는 수 있습니다.
SELinux 활성화 되어 있는지 확인, 실행방법은 위의 SELinux 상태 확인방법을 확인
제한없이 실행중인 경우 apache http server (httpd)가 samba를 위한 데이터에 액서스 할 수 있는 모습을 보여줌
httpd 프로세스는 제한된 httpd_t 도메인에서 실행되는 것을 명심할것
이것은 어디까지 예시입니다.
httpd, wget, dbus, audit 패키지가 설치되어 SELinux 대상 정책이 사용됨
SELinux가 enforcing 모드에서 실행되는 것을 전제로 하고 있음
- 제한없는 프로세스의 예시
chcon 명령으로 파일의 레이블을 변경
파일 시스템의 레이블이 바꿔 질이 변경 사항은 손실
파일 시스템 레이블이 바꿔진 경우 이러한 변경을 영구적으로 유지하려면 semanage 유틸리티를 사용
root 로 다음 명령을 실행하여 유형을 Samba에서 사용되는 유형으로 변경
~]#
chcon -t samba_share_t /var/www/html/testfile
변경을 표시합니다.
~]$
ls -Z /var/www/html/testfile
-rw-r--r-- root root unconfined_u:object_r:samba_share_t:s0 /var/www/html/testfile
다음 명령을 실행하여 httpd 프로세스가 실행되고 있지 않은지 확인
~]$
systemctl status httpd.service
httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled) Active: inactive (dead)
다른 내용이 나온다면 root 사용자로 다음명령을 실행하여 httpd 프로세스를 중지
~]#
systemctl stop httpd.service
httpd 프로세스를 제한없이 실행하려면 root 사용자로 다음 명령을 실행하여 /usr/sbin/httpd 파일의 유형을 제한하는 도메인으로 마이그레이션 하지 않은 것으로 변경
~]#
chcon -t bin_t /usr/sbin/httpd
/usr/sbin/httpd 에 bin_t 유형이 분류되어 있는지 확인
~]$
ls -Z /usr/sbin/httpd
-rwxr-xr-x. root root system_u:object_r:bin_t:s0 /usr/sbin/httpd
root 로 httpd 프로세스를 시작, 성공적으로 시작되었는지 확인
~]#
systemctl start httpd.service
~]#
systemctl status httpd.service
httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled) Active: active (running) since Thu 2013-08-15 11:17:01 CEST; 5s ago
다음 명령을 실행하여, httpd가 unconfined_service_t 도메인에서 실행되고 있는지 확인
~]$
ps -eZ | grep httpd
system_u:system_r:unconfined_service_t:s0 11884 ? 00:00:00 httpd system_u:system_r:unconfined_service_t:s0 11885 ? 00:00:00 httpd system_u:system_r:unconfined_service_t:s0 11886 ? 00:00:00 httpd system_u:system_r:unconfined_service_t:s0 11887 ? 00:00:00 httpd system_u:system_r:unconfined_service_t:s0 11888 ? 00:00:00 httpd system_u:system_r:unconfined_service_t:s0 11889 ? 00:00:00 httpd
Linux 사용자의 쓰기 권한이 있는 디렉토리로 전환 한 후 다음 명령을 실행합니다. 기본설정에 변경이 없으면 이명령을 성공함
~]$
wget http://localhost/testfile
--2009-05-07 01:41:10-- http://localhost/testfile Resolving localhost... 127.0.0.1 Connecting to localhost|127.0.0.1|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 0 [text/plain] Saving to: `testfile.1' [ <=> ]--.-K/s in 0s 2009-05-07 01:41:10 (0.00 B/s) - `testfile.1' saved [0/0]
httpd 프로세스는 samba_share_t 유형의 라벨이 붙은 파일에 액세스하지 않지만 httpd는 제한없는 unconfined_service_t 도메인에서 실행하고 있으며, DAC 규칙을 대체
wget 명령을 성공함, 만약 httpd가 제한된 httpd_t 도메인에서 실행되면, wget 명령은 실패했을것 입니다.
restorecon 유틸리티는 파일의 기본 SELinux 문맥을 복원함
root 다음 명령을 실행하면 /usr/sbin/httpd의 기본 SELinux 문맥이 복원
~]#
restorecon -v /usr/sbin/httpd
restorecon reset /usr/sbin/httpd context system_u:object_r:unconfined_exec_t:s0->system_u:object_r:httpd_exec_t:s0
/usr/sbin/httpd에 httpd_exec_t 유형이 분류되어 있는지 확인
~]$
ls -Z /usr/sbin/httpd
-rwxr-xr-x root root system_u:object_r:httpd_exec_t:s0 /usr/sbin/httpd
root 로 다음명령을 실행하여 httpd를 다시 실행
다시 시작되면 httpd가 제한된 httpd_t 도메인에서 실행하고 있는지 확인
~]#
systemctl restart httpd.service
~]$
ps -eZ | grep httpd
system_u:system_r:httpd_t:s0 8883 ? 00:00:00 httpd system_u:system_r:httpd_t:s0 8884 ? 00:00:00 httpd system_u:system_r:httpd_t:s0 8885 ? 00:00:00 httpd system_u:system_r:httpd_t:s0 8886 ? 00:00:00 httpd system_u:system_r:httpd_t:s0 8887 ? 00:00:00 httpd system_u:system_r:httpd_t:s0 8888 ? 00:00:00 httpd system_u:system_r:httpd_t:s0 8889 ? 00:00:00 httpd
root로 testfile을 삭제
~]#
rm -i /var/www/html/testfile
rm: remove regular empty file `/var/www/html/testfile'? y
httpd 실행이 필요하지 않은경우 root로 다음 명령을 실행하여 httpd를 중지
~]#
systemctl stop httpd.service
이 섹센의 예는 위험에 제한이 있는 과정에서 데이터가 어떻게 보호되는지, 위험에 제한이 없는 과정에서 공격자가 데이터에 엑세스가 쉬운가? 를 보여줍니다.
ㅁ CONFINED AND UNCONFINED USERS
user는 selinux 정책을 사용하여 selinux 사용자에 매핑이 됨
selinux 사용자에게 부과된 제한이 linux 사용자에게 상속이 됨
root 로 semanage login -l 실행하면 linux 사용자 매핑이 표시
~]#
semanage login -l
Login Name SELinux User MLS/MCS Range Service __default__ unconfined_u s0-s0:c0.c1023 * root unconfined_u s0-s0:c0.c1023 * system_u system_u s0-s0:c0.c1023 *
linux 사용자는 기본적으로 __default__ Login에 매핑, unconfined_u 사용자에 매핑
__default__ unconfined_u s0-s0:c0.c1023
신규 Linux 사용자 시스탬엠 추가, 사용자를 SELinux unconfined_u 사용자에 매핑하는 방법에 대한 설명
Linux default root 사용자가 제한없이 실행중이라는것을 전제로 진행
- 신규 사용자의 SELinux unconfined_u 사용자에 매핑 시키기
~]#
useradd newuser
~]#
passwd newuser
Changing password for user newuser. New UNIX password: Enter a password Retype new UNIX password: Enter the same password again passwd: all authentication tokens updated successfully.
logout 후에 새로 생성한 user로 로그인
pam_selinux PAM module이 자동으로 Linux 사용자를 SELinux 사용자에 매핑 (이번 경우는 unconfined_u)로 설정함
확인작업
[newuser@localhost ~]$
id -Z
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
새로운 사용자를 삭제하려면
root로 userdel -r newuser 하게되면 삭제됨
Linux 사용자는 MCS, MLS에 제한을 받음
unconfined_t 도메인에서 자신의 제한된 도메인을 마이그레이션 진행
SELinux 정책에서 정의하고있는 응용 프로그램을 제한없이 Linux 사용자가 실행하면 이러한 제한이 없는 Linux 사용자는 제한이 있는 도메인의 제약에 영향을 받음
Linux 사용자가 제한이 없더라도 응용프로그램에는 여전히 제한이 존제한다라는것
응용프로그램의 취약점이 악용되더라도 정책에서 제한을 할수 있음
제한된 사용자는 각각 unconfined_t 도메인에 대해 제한된 사용자 도메인 제한이 됨
SELinux 정책은 제한된 사용자 도메인에 대해 자신의 목표에 제한이 있는 도메인으로의 전환을 정의 할 수 있음
제한된 Linux 사용자는 대상의 제한이 있는 도메인의 제약의 영향을 받게됨
특별한 권한이 역할에 따라 제한된 사용자와 관련이 있음
- Linux 사용자 권한 표
User | Role | Domain | X Window System | su or sudo | Execute in home directory and /tmp/ (default) | Networking |
---|
sysadm_u | sysadm_r | sysadm_t | yes | su and sudo | yes | yes |
staff_u | staff_r | staff_t | yes | only sudo | yes | yes |
user_u | user_r | user_t | yes | no | yes | yes |
guest_u | guest_r | guest_t | no | no | no | no |
xguest_u | xguest_r | xguest_t | yes | no | no | Firefox only |
user_t, guest_t, xguest_t 도메인 linux 사용자는 setuid 응용프로그램을 실행할수 없음 (ex. passwd)
su 나 sudo setuid 프로그램을 실행할수 없음으로 , 이 응용프로그램을 사용해서 root 가 될수 없음
sysadm_t, staff_t, user_t, xguest_t 도메인 Linux 사용자는 X Window system 및 console을 통해 로그인을 할수 있음
guest_t, xguest_t 도메인 Linux 사용자는 Default가 /tmp/ 내부의 응용프로그램을 실행할수 없음
쓰기 권한이 있는 디렉토리에 있는 응용 프로그램에서 사용자의 권한을 상속하는 응용 프로그램을 실행할수 없음
이렇게 하면 결함이 있는 응용프로그램이나 악성 어플리케이션이 사용자의 파일을 수정하지 못하도록 할수 있음
staff_t, user_t 도메인 linux 기본 사용자는 자신의 홈 디렉토리와 /tmp/ 에 있는 응용 프로그램의 실행이 가능
xguest_t 도메인 linux 사용자의 유일한 네트워크 액세스는 firefox
- 위의 SELinux 사용자 이외에 해당 사용자에 매핑할수 있는 특별한 역활이 있음, 이러한 SELinux는 사용자에게 부여하는것을 결정
webadm_r 은 apache http 서버 관련 selinux 유형의 처리만 가능
dbadm_r 은 MariaDB 및 PostgreSQL 관리 시스탬 관련 SELinux 유형의 처리만 가능
logadm_r 은 syslog 및 auditlog 프로세스 관련 SELinux 유형의 처리만 가능
sysadm_r 은 selinux 처리만 가능
auditadm_r 은 audit 서브 시스템에 관련된 프로세스의 처리만 가능
- 사용 가능한 역할을 나열
~]$
seinfo -r
seinfo 는 setools-console 패키지를 설치해야함
ㅁ The sudo Transition and SELinux Roles
제한된 사용자가 root 권한을 필요로하는 관리 작업을 수행해야 될수도 있음
제한된 사용자가 sudo 명령을 사용하여 제한된 관리자의 SELinux 역할을 확보해야함
sudo 명령은 신뢰할수 있는 사용자에게 관리자의 권한을 부여, 사용자가 sudo 명령을 실행할 경우 사용자의 암호를 요구
사용자가 인증되고 명령이 허용되면 관리자 명령은 root 사용자 인것처럼 실행
SELinux 권한표 에서 보면 staff_u 및 sysadm_u 의 제한이 있는 SELinux 사용자만 기본적으로 sudo 를 사용할수 있도록 되어 있음
해당 사용자가 sudo를 사용하여 명령을 실행하면 사용자의 역할은 /etc/sudoers 설정 파일, /etc/sudoers.d/ 디렉토리의 각 파일에 따라 변경
ㅁ Configuring the sudo Transition
sudo 설정, SELinux_user_r 을 administrator_r 관리자로전환하는 방법을 설명
기존 SELinux 사용자에게 제한된관리자의 역할을 설정하려면 처음 두단계는 생략, 다음 명령은 root 사용자로 실행해야 한다는점은 유의
신규 SELinux 사용자 생성, 해당 사용자에 대해 기본 SELinux 역활 및 부수적인 제한이 있는 관리자 역할을 지정
~]#
semanage user -a -r s0-s0:c0.c1023 -R "default_role_r administrator_r" SELinux_user_u
confined_u SELinux 기본역할, staff_r 에서 제한된 관리자 역할은 webadm_r
~]#
semanage user -a -r s0-s0:c0.c1023 -R "staff_r webadm_r" confined_u
기본 SELinux 정책 컨텍스트 파일을 설치, 예를 들면 staff_u SELinux 사용자와동일한 SELinux 규칙을 준비하려면 staff_u 컨텍스트 파일을 복사
~]#
cp /etc/selinux/targeted/contexts/users/staff_u /etc/selinux/targeted/contexts/users/SELinux_user
신규Linux 사용자를 기존의 Linux 사용자에 매핑
~]#
semanage login -a -s SELinux_user_u -rs0:c0.c1023 linux_user
/etc/sudoers.d/ 디렉토리에 있는 리눅스 사용자와 동일한 이름으로 새구성파일을 생성, 다음 문자열을 추가
~]#
echo "linux_user ALL=(ALL) TYPE=administaror_t ROLE=administrator_r /bin/sh " > /etc/sudoers.d/linux_user
restorecon 을 이용해서 linux_user 홈 디렉토리의 레이블을 이동
~]#
restorecon -R -v /home/linux_user
시스탬 reboot
~]#
systemctl reboot
새로운 Linux user로 로그인 하면 사용자는 기본 SELinux 롤 레이블리 적용
~]$
id -Z
SELinux_user_u:default_role_r:default_role_t:s0:c0.c1023
sudo 를 실행하면, 사용자의 SELinux /etc/sudoers.d/linux_user 로 지정되어 있는 SELinux 역할로 변경
sudo -i 옵션을 사용하면 대화 형 쉘이 실행
~]$
sudo -i
~]#
id -Z
SELinux_user_u:administrator_r:administrator_t:s0-s0:c0.c1023
첫 단계에서 지정한 confined_u SELinux 사용자의 경우는 다음과 같이 출력됨
'Security > Linux - Selinux' 카테고리의 다른 글
selinux 04 (0) | 2016.08.01 |
---|---|
selinux 01, 02 (0) | 2016.06.30 |