第十單元-selinux對文件的控制
一、修改安全上下文
/etc/sysconfig/selinux文件內容
# 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=enforcing ##selinux狀態
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
查看selinux狀態命令:
gentenforce
三種狀態:
disabled 關閉狀態
Enforcing 強制狀態
Permissive 警告狀態
切換狀態命令:
setenforce 1|0 【1爲強制,0爲警告】
1.臨時修改 chcon -t 安全上下文類型 被修改文件
一次性定製安全上下文,執行 restorecon 刷新後還原
[root@localhost westos]# getenforce
Permissive
[root@localhost westos]# mv /home/westos/westos /var/ftp/pub ##將westos移入到指定目錄下
[root@localhost westos]# ls -Z /var/ftp/pub ##查看/var/ftp/pub中文件的安全上下文
-rw-rw-r--. ftp ftp system_u:object_r:public_content_t:s0 group
-rw-r--r--. westos ftp system_u:object_r:public_content_t:s0 profile
drwxrwxr-x. ftp ftp system_u:object_r:public_content_t:s0 test
-rw-r--r--. root root unconfined_u:object_r:mnt_t:s0 westos
[root@localhost westos]# ps auxZ | grep vsftpd ##檢測幾個文件區別
system_u:system_r:ftpd_t:s0-s0:c0.c1023 root 1308 0.0 0.0 52760 568 ? Ss 05:06 0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 root 2369 0.0 0.0 112644 936 pts/0 R+ 05:14 0:00 grep --color=auto vsftpd
示圖:檢測區別
示圖:警告模式下可以觀察到轉入的文件westos
[root@localhost westos]# setenforce 1 ##轉爲強制模式
[root@localhost westos]# getenforce
Enforcing
[root@localhost westos]# chcon -t public_content_t /var/ftp/pub/westos ##將/var/ftp/pub下的westos文件的安全上下文修改爲這個目錄下可被識別認可的安全上下文
[root@localhost westos]# ps auxZ | grep vsftpd
system_u:system_r:ftpd_t:s0-s0:c0.c1023 root 1308 0.0 0.0 52760 568 ? Ss 05:06 0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
system_u:system_r:ftpd_t:s0-s0:c0.c1023 nobody 2494 0.0 0.0 54856 1436 ? Ss 05:19 0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
system_u:system_r:ftpd_t:s0-s0:c0.c1023 ftp 2496 0.0 0.0 56964 1452 ? S 05:19 0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 root 2555 0.0 0.0 112644 936 pts/0 R+ 05:20 0:00 grep --color=auto vsftpd
[root@localhost westos]# ls -Z /var/ftp/pub ##可以觀察到三個文件的安全上下文全部一樣
-rw-rw-r--. ftp ftp system_u:object_r:public_content_t:s0 group
-rw-r--r--. westos ftp system_u:object_r:public_content_t:s0 profile
drwxrwxr-x. ftp ftp system_u:object_r:public_content_t:s0 test
-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 westos
示圖:測試
2.永久修改文件的上下文 semanage fcontext
semanage 命令 -l ##查看
• restorecon 是 policycoreutil 軟件包的一部分
• semanage 是 policycoreutil-python 軟件包的一部分
[root@localhost yum.repos.d]# ls -lZ /var/ftp ##安全上下文爲public_content_t
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 pub
[root@localhost yum.repos.d]# ls -lZ /westos ##安全上下文爲default_t
-rw-r--r--. root root system_u:object_r:default_t:s0 westosfile1
-rw-r--r--. root root system_u:object_r:default_t:s0 westosfile2
-rw-r--r--. root root system_u:object_r:default_t:s0 westosfile3
-rw-r--r--. root root system_u:object_r:default_t:s0 westosfile4
-rw-r--r--. root root system_u:object_r:default_t:s0 westosfile5
[root@localhost yum.repos.d]# semanage fcontext -l | grep westos ##特定搜索查看文件或目錄全上下文,westos中沒有符合要求的安全上下文
[root@localhost yum.repos.d]# semanage fcontext -l | grep var/ftp ##在var/ftp中符合要求的安全上下文的文件
/var/ftp(/.*)? all files system_u:object_r:public_content_t:s0
/var/ftp/bin(/.*)? all files system_u:object_r:bin_t:s0
/var/ftp/etc(/.*)? all files system_u:object_r:etc_t:s0
/var/ftp/lib(/.*)? all files system_u:object_r:lib_t:s0
/var/ftp/lib/ld[^/]*\.so(\.[^/]*)* regular file system_u:object_r:ld_so_t:s0
示圖:
[root@localhost yum.repos.d]# semanage fcontext
-a -t public_content_t '/westos(/.*)?' ##-a添加,-t類型,將/westos中的所有文件的安全上下文修改爲public_content_t
[root@localhost yum.repos.d]# semanage fcontext -l | grep westos ##修改完畢後並沒有立馬更改
/westos(/.*)? all files system_u:object_r:public_content_t:s0
[root@localhost yum.repos.d]# semanage fcontext -l | grep var/ftp
[root@localhost yum.repos.d]# ls -lZ /westos
-rw-r--r--. root root system_u:object_r:default_t:s0 westosfile1
-rw-r--r--. root root system_u:object_r:default_t:s0 westosfile2
-rw-r--r--. root root system_u:object_r:default_t:s0 westosfile3
-rw-r--r--. root root system_u:object_r:default_t:s0 westosfile4
-rw-r--r--. root root system_u:object_r:default_t:s0 westosfile5
[root@localhost yum.repos.d]# restorecon -RvvF /westos ##重新加載/westos
restorecon reset /westos context system_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/westosfile1 context system_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/westosfile2 context system_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/westosfile3 context system_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/westosfile4 context system_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/westosfile5 context system_u:object_r:default_t:s0->system_uobject_r:public_content_t:s0
[root@localhost yum.repos.d]#
[root@localhost ~]# semanage fcontext -l | grep westos ##可以查看到目錄下所有的文件的安全上下文全部更改成功,all files現在存在的和即將建立的全部都會爲被更改後的安全上下文
/westos(/.*)? all files system_u:object_r:public_content_t:s0
示圖:
實驗效果:
設置了/westos目錄下所有的和即將所有的文件全部都會是設定的安全上下文pubic_content_t,所以在執行restorecon -RvvF後,會自定將westos中的所有文件全部刷成所設置的
示圖:
二、管理 SELinux 布爾值
• SELinux 布爾值是更改 SELinux 策略行爲的開關。SELinux 布爾值是可以啓用或禁用的規則。安全管理員可以使用 SELinux 布爾值來調整策略 , 以有選擇地進行調整
• 許多軟件包都具有 man page *_selinux(8), 其中詳細說明了所使用的一些布爾值 ; man -k ‘_selinux’ 可以輕鬆地找到這些手冊
• getsebool 用於顯示布爾值 , setsebool 用於修改布爾值
• setsebool -P 修改 SELinux 策略 , 以永久保留修改。semanage boolean -l 將顯示布爾值是否永久
[root@localhost ~]# getenforce ##強制模式下
Enforcing
[root@localhost ~]# ll /home
total 0
dr-x------. 4 student student 84 Jul 10 2014 student
dr-x------. 4 westos westos 101 Apr 23 01:41 westos
[root@localhost ~]# chmod u+w /home/student ##家用戶的家目錄擁有寫權限
效果:
[root@foundation33 ~]# lftp 172.25.254.133 -u student
Password:
lftp [email protected]:~> ls
ls: Login failed: 530 Login incorrect. ##家用戶的上傳受限
lftp [email protected]:~> quit
[root@localhost ~]# setenforce 0 ##警告模式下
[root@localhost ~]# getenforce
Permissive
效果:
[root@foundation33 ~]# lftp 172.25.254.133 -u student
Password:
lftp [email protected]:~> ls
lftp [email protected]:~> put /etc/passwd
2367 bytes transferred ##上傳成功
lftp [email protected]:~> ls
-rw-r--r-- 1 1000 1000 2367 Apr 29 02:07 passwd
lftp [email protected]:~> rm passwd
rm ok, `passwd` removed
lftp [email protected]:~> quit
[root@foundation33 ~]#
[root@localhost ~]# getsebool -a | grep ftp ##查看所有文件的安全服務
ftp_home_dir --> off ##家用戶的指定家目錄服務關閉
ftpd_anon_write --> off
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> off
ftpd_use_cifs --> off
ftpd_use_fusefs --> off
ftpd_use_nfs --> off
ftpd_use_passive_mode --> off
httpd_can_connect_ftp --> off
httpd_enable_ftp_server --> off
sftpd_anon_write --> off
sftpd_enable_homedirs --> off
sftpd_full_access --> off
sftpd_write_ssh_home --> off
tftp_anon_write --> off
tftp_home_dir --> off
[root@localhost ~]# setsebool -P ftp_home_dir 1 ##-P永久設置,on關閉
[root@localhost ~]# getsebool -a | grep ftp
ftp_home_dir --> on
ftpd_anon_write --> off
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> off
ftpd_use_cifs --> off
ftpd_use_fusefs --> off
ftpd_use_nfs --> off
ftpd_use_passive_mode --> off
httpd_can_connect_ftp --> off
httpd_enable_ftp_server --> off
sftpd_anon_write --> off
sftpd_enable_homedirs --> off
sftpd_full_access --> off
sftpd_write_ssh_home --> off
tftp_anon_write --> off
tftp_home_dir --> off
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]# setenforce 1
[root@localhost ~]# getenforce
Enforcing
效果:
[root@foundation33 ~]# lftp 172.25.254.133 -u student
Password:
lftp [email protected]:~> ls
lftp [email protected]:~> put /etc/passwd
2367 bytes transferred ##上傳成功
lftp [email protected]:~> ls
-rw-r--r-- 1 1000 1000 2367 Apr 29 02:07 passwd
lftp [email protected]:~> rm passwd
rm ok, `passwd` removed
lftp [email protected]:~> quit
[root@foundation33 ~]#
三、監控 SELinux 衝突
• 必須安裝 setroubleshoot-server 軟件包 , 才能將SELinux 消息發送至 /var/log/messages
• etroubleshoot-server 偵聽/var/log/audit/audit.log 中的審覈信息並將簡短摘要發送至 /var/log/messages
• 摘要包括 SELinux 衝突的唯一標識符 ( UUIDs ),可用於收集更多信息。
Sealert -l UUID 用於生成特定事件的報告。
Sealert -a /var/log/audit/audit.log 用於在該文件中生成所有事件的報告效果: