SELinux安全

一、SELinux配置文件

在CentOS 7系統中部署SELinux非常簡單,由於SELinux已經作爲模塊集成到內核中,默認SELinux已經處於激活狀態。對管理員來說,更多的是需要配置與管理SELinux,CentOS 7系統中SELinux全局配置文件爲/etc/sysconfig/selinux,內容如下:

[root@centos7 ~]# vim /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
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

SELinux=enforcing爲SELinux總開關,有效值可以是enforcing、permissive或disabled。

其中,disabled代表禁用SELinux功能,由於SELinux是內核模塊功能,所以如果設置禁用,需要重啓計算機。permissive代表僅警告模式,處於此狀態下時,當主題程序試圖訪問無權限的資源時,SELinux會記錄日誌但不會攔截該訪問,也就是最終訪問是成功的,只是在SELinux日誌中記錄而已。enforcing模式代表強制開啓,SELinux會攔截非法的資源訪問並記錄相關日誌。


使用setenforce可以臨時在enforcing模式與permissive模式之間切換,切換會被立刻應用於當前系統,計算機重啓後無效,永久修改模式需要修改配置文件。

[root@centos7 ~]# setenforce 0			#設置SELinux爲permissive模式
[root@centos7 ~]# setenforce 1			#設置SELinux爲enforcing模式


二、SELinux安全上下文

SELinux會爲進程與文件添加安全信息標籤,如:SELinux用戶、角色、類型以及可選的級別。當運行SELinux後所有這些信息都是訪問控制的依據。下面通過一個實例文件查看SELinux安全上下文,使用ls -Z命令就可以看到文件或目錄的這些上下文信息,而ps aux –Z則可以查看進程的安全上下文信息:

[root@centos7 ~]# ls -Z anaconda-ks.cfg 
-rw-------. root root system_u:object_r:admin_home_t:s0 anaconda-ks.cfg
[root@centos7 ~]# ps aux -Z

SELinux的安全上下文包括

用戶:角色:類型:級別


三、SELinux排錯

不管SELinux策略是允許還是拒絕資源的訪問請求行爲,都會記錄日誌,也就是AVC(Access Vector Cache)。所有SELinux拒絕的消息都會被記錄進日誌,根據系統中安裝運行的服務進程不同,拒絕日誌消息會被記錄到不同的文件中,表6-2列出了進程與日誌文件的關係。

表6-2

日誌文件                      進程

/var/log/audit/audit.log           auditd服務開啓

/var/log/messages                auditd服務關閉,rsyslogd服務開啓

/var/log/audit/audit.log,/var/log/messages安裝setroubleshoot相關軟件包

autitd與rsyslogd同時開啓


對於大多數生產環境中的服務器而言,更多的是沒有部署安裝圖形界面的Linux系統,我們需要手動查看日誌文件。在此建議管理員安裝setroubleshoot相關的軟件包,這樣可以將原本生澀的AVC拒絕日誌轉換爲可讀性比較高的setroubleshoot日誌。查看日誌可以使用如下兩種方法:

[root@centos7 ~]# grep setroubleshoot /var/log/messages
[root@centos7 ~]# grep denied /var/log/audit/audit.log

查看messages日誌會提示,根據黑體字提示運行sealert命令纔可以看到人性化報錯信息。

setroubleshoot: SELinux is preventing /usr/sbin/httpd from read access on the file index.html. For complete SELinux messages. run sealert -l 7082b8b4-70f4-42fb-92ea-08a51299d080

[root@centos7 ~]# sealert -l 7082b8b4-70f4-42fb-92ea-08a51299d080


四、修改安全上下文

有多種方式可以修改與管理SELinux安全上下文,如:chcon、semanage、fcontext以及restorecon命令。

1. chcon命令

描述:修改文件SELinux安全上下文。

用法:chcon [選項] [-u SELinux用戶] [-r 角色] [-l 範圍] [-t 類型] 文件

chcon [選項] --reference=參考文件 文件

選項:-u 修改用戶屬性

-r 修改角色屬性

-l 修改範圍屬性

-t 修改類型屬性

示例:

(1)修改文件安全上下文。

[root@centos7 ~]# cp --preserve=all /etc/passwd /root/ #複製文件(保留上下文信息)
[root@centos7 ~]# ls -Z /root/passwd			 #查看文件SELinux安全上下文
[root@centos7 ~]# chcon -t admin_home_t /root/passwd #修改文件安全上下文中的類型
[root@centos7 ~]# ls -Z /root/passwd

(2)修改目錄安全上下文。

[root@centos7 ~]# chcon -R -t admin_home_t /root/	  #遞歸修改目錄安全上下文

(3)根據參考文件修改目標文件安全上下文。

[root@centos7 ~]# chcon --reference=/etc/passwd /root/passwd

通過chcon修改的安全上下文並不是SELinux預設的安全上下文,當文件系統重置SELinux安全標籤或使用restorecon命令重置指定目錄的安全標籤後,所有文件與目錄的安全標籤會被還原爲系統預設值,如果需要修改SELinux默認的預設安全上下文,需要使用semanage命令添加或修改。

五、semanage命令

描述:SELinux策略管理工具。

用法:semanage fcontext [-S store] -{a|d|m|l|n|D} [-frst] file_spec

選項:-a,--add添加預設安全上下文

-d,--delete刪除指定的預設安全上下文

-D,--deleteall    刪除所有的預設自定義上下文

-m,--modify修改指定的預設安全上下文

-l,--list     顯示預設安全上下文

-n,--noheading     不顯示頭部信息

示例:

(1)查看SELinux策略默認的預設安全上下文信息,系統將列出策略中定義的所有目錄與安全上下文信息。

[root@centos7 ~]#semanage fcontext -l

(2)修改策略,添加一條新的預設安全上下文信息。

[root@centos7 ~]# semanage fcontext -a -t samba_share_t /test/test.txt
[root@centos7 ~]# mkdir /test; touch /test/test.txt
[root@centos7 ~]# ls -Z /test/test.txt


(3)使用restorecon命令,還原test.txt文件的安全上下文爲預設值。

[root@centos7 ~]# restorecon /test/test.txt
[root@centos7 ~]# ls -Z /test/test.txt


(4)遞歸設置目錄的預設安全上下文。

[root@centos7 ~]# semanage fcontext -a -t httpd_sys_content_t "/site/www(/.*)?"
[root@centos7 ~]# mkdir -p /site/www/{web1,web2}
[root@centos7 ~]# touch /site/www/{web1,web2}/index.html
[root@centos7 ~]# ls -RZ /site/www
[root@centos7 ~]# restorecon -R /site/


(5)刪除預設安全上下文。

[root@centos7 ~]# semanage fcontext -d /test/ test.txt

(6)檢查預設SELinux安全上下文。

[root@centos7 ~]# matchpathcon /site/www/

6.2.6 查看與修改布爾值

SELinux布爾值可以實時被修改。如,你可以在不重新加載或編譯SELinux策略的情況下允許服務訪問NFS文件系統。getsebool是用來查看SELinux布爾值的命令,用法比較簡單,-a選項用來查看所有的布爾值。一般建議管理員通過管道過濾自己需要的布爾值參數,如getsebool -a |grep ftp過濾與FTP相關的布爾值信息,顯示效果中左側爲關鍵詞,右側爲開關,on代表開,off代表關,具體命令如下。

[root@centos7 ~]# getsebool -a
abrt_anon_write  off
abrt_handle_event  off
allow_console_login  on
allow_cvs_read_shadow  off
allow_daemons_dump_core  on
allow_daemons_use_tcp_wrapper  off
allow_daemons_use_tty  on
allow_domain_fd_use  on
… …部分內容省略… …


修改SELinux布爾值狀態也非常簡單,使用setsebool name X即可實現。其中,name是布爾值名稱,X代表on或off。默認setsebool命令修改的布爾值參數會立即生效,但計算機重啓後會被還原,如果希望永久修改,需要使用-p參數。

[root@centos7 ~]# setsebool ftp_home_dir on
[root@centos7 ~]# setsebool -p ftp_home_dir on


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章