SElinux管理
本章內容:
selinux概念:
啟用selinux
管理文件安全標簽
管理端口標簽
管理selinux布爾值開關
管理日志
查看selinux幫助
selinux介紹:
selinux:secure enhanced Linux,是美國國家安全局和scc開發的Linux的一個強制訪問控制的安全模塊。2000年以后以GNUGPL發布,Linux內核2.6版本后集成在內核中。
DAC:自由訪問控制。
MAC:強制訪問控制。
DAC:DAC環境下進程是無束縛的。
MAC:MAC環境下策略的規則決定控制的嚴格程度。
MAC:MAC環境下進程可以被限制的。
策略被用來定義被限制的進程能夠使用那些資源(文件和端口)
默認情況下,沒有明確允許的行為將被拒絕。
selinux的四種工作類型:
strict(嚴格的):對每個進程都受到selinux的控制。
targeted(有針對性的):用來保護常見的網絡服務,僅有限進程受到selinux控制,只監控容易被入侵的進程,rhel4只保護13個服務,rhel5保護88個服務。
minimum:修改過的targeted,只對選擇的網絡服務。
mls:提供mls(多級安全)機制的安全性。
minimum和mls穩定性不足,未加以應用。
selinux安全上下文:
傳統Linux,哲學思想一切皆文件,由用戶、組權限控制訪問。
在selinux中,一切皆對象(object),有存放在inode的擴展屬性域的安全元素所控制其訪問。
所有文件、端口資源和進程都具備安全標簽:安全上下文(security context)
安全上下文由五個元素組成:
user:role:type:sensitivity:category
示例:
[root@centos6 ~]# ls -lZ
-rwxrw-rw-. root root system_u:object_r:admin_home_t:s0 a1.txt.txt
-rw——-. root root system_u:object_r:admin_home_t:s0 anaconda-ks.cfg
-rw-r–r–. root root system_u:object_r:admin_home_t:s0 base.repo
-rwxrw-rw-. root root unconfined_u:object_r:admin_home_t:s0 copycmd.sh
實際上下文:存放在文件系統中;
期望(默認)上下文:存放在二進制的selinux策略庫中(映射目錄和期望安全上下文)中。
* 使用 semanage fcontext -l 命令查看期望的selinux上下文。
selinux中五個安全元素的含義:
user:指示登錄系統的用戶類型,如root,user_u,system_u,多數本地進程都屬于無約束(unconfined)進程。
role:定義文件,進程和用戶的用途:文件:object_r,進程和用戶:system_r。
type:指定數據類型,規則中定義何種進程類型訪問何種文件。targeted策略基于type實現,多服務共用:public_content_t。
public_content_t:只有讀的權限,要想有讀寫權限需改成public_content_rw_t。
sensitivity:限制訪問的需要,由組織定義的分層安全級別,如unclassified,secret,top secret,一個對象有只有一個sensitivity,分0-15級,s0最低,targeted策略默認使用s0。
category:對于特定組織劃分不分層的分類,如FBI secret,NSA secret,一個對象可以有多個categroy,c0-c1023共1024個分類,targeted策略不使用category
示例:
-rwxrw-rw-. root root unconfined_u:object_r:admin_home_t:s0 copycmd.sh
drwxr-xr-x. root root system_u:object_r:admin_home_t:s0 Desktop
selinux策略:
對象(object):所有可以對取的對象,包括文件、目錄和進程、端口等。
主題(subject):selinux中進程被稱為主體。
selinux中對所有的文件賦予一個type的文件類型標簽,對于所有的進程也賦予各自一個domain的標簽。domain標簽能夠執行的操作由安全策略里定義。
當一個subject試圖訪問一個object,kernel中的策略執行服務器將檢查AVC,在AVC中subject和object的權限被緩存,查找“應用+文件”的安全環境。然后根據查找結果允許或拒絕訪問。
安全策略:定義主體讀取對象的規則數據庫,規則中記錄了哪個類型的主體使用哪個方法讀取哪一個對象式允許還是拒絕的,并且定義了哪種行為是允許或拒絕。
配置selinux:
selinux是否啟用;
給文件重新打安全標簽;
給端口設置安全標簽;
設定某些操作的布爾型開關;
selinux的日志管理;
selinux的狀態:
enforcing:強制,每個受限的進程都必然受限。
permissive:允許,每個受限的進程違規操作不會被禁止,但會被記錄于審計日志。
disabled:禁用。
selinux相關命令:
getenforce:獲取selinux當前狀態。
sestatus:獲取selinux當前更詳細的狀態。
setenforce 0|1
0:設置為permissive允許
1:設置為enforcing強制執行selinux策略。
示例:getenforce命令
[root@centos6 ~]# getenforce
Enforcing
[root@centos6 ~]# sestatus
示例:sestatus命令
SELinux status: enabled
SELinuxfs mount: /selinux
Current mode: enforcing
Mode from config file: enforcing
Policy version: 24
Policy from config file: targeted
[root@centos6 ~]#
示例:setenforce命令,命令行臨時啟用/禁用selinux
[root@centos6 Desktop]# setenforce 0
[root@centos6 Desktop]# getenforce
Permissive
[root@centos6 Desktop]# setenforce 1
[root@centos6 Desktop]# getenforce
Enforcing
[root@centos6 Desktop]#
selinux配置文件:
以下兩個絕對路徑都可以進到selinux的配置文件中。
/etc/selinux/config
/etc/sysconfig/selinux 為/etc/selinux/config 的軟鏈接。
在grub配置文件中添加selinux=0禁用selinux:
centos6:在/etc/grub/grub.conf文件中kernel行后添加selinux=0
centos7:在/etc/grub2/grub.cfg文件中Linux16行后添加selinux=0
在selinux的配置文件中禁用selinux:
selinux={enforcing | permissive |disabled }
注意:在Linux grub配置文件中和selinux配置文件中,只要有一個是禁用的,那么selinux就禁用。建議在centos6中裝上setroubleshoot selinux故障排錯包。
示例:安裝setroubleshoot selinux故障排錯包。
[root@centos6 Desktop]# yum -y install setroubleshoot
示例:selinux的配置文件:
[root@centos6 Desktop]# vim /etc/selinux/config
1
2 # This file controls the state of SELinux on the system.
3 # SELINUX= can take one of these three values:
4 # enforcing – SELinux security policy is enforced.
5 # permissive – SELinux prints warnings instead of enforcing.
6 # enforcing – No SELinux policy is loaded.
7 SELINUX=enforcing
8 # SELINUXTYPE= can take one of these two values:
9 # targeted – Targeted processes are protected,
10 # mls – Multi Level Security protection.
11 SELINUXTYPE=targeted
12
修改selinux安全標簽:
給文件重新打安全標簽:
chcon [OPTION]… [-u USER] [-r ROLE] [-l RANGE] [-t TYPE] FILE…
-t:修改文件的標簽類型。
–reference(參考):參考別的文件安全標簽來設置標簽類型。
-R:遞歸的打標簽。
示例:
[root@centos6 html]# pwd
/var/www/html
[root@centos6 html]# chcon -t default_t index.html
[root@centos6 html]# ll -Z
-rw-r–r–. root root unconfined_u:object_r:default_t:s0 index.html
[root@centos6 html]#
示例:參考文件1的安全上下文修改文件2的安全上下文。
chcon –reference file1 file2
恢復目錄或文件默認的安全上下文(根據selinux安全上下文數據庫恢復):
restorecon -R /path/to/somewhere
示例:
[root@centos6 html]# restorecon -R /var/www/html/
[root@centos6 html]# ll -Z
-rw-r–r–. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html
[root@centos6 html]#
默認安全上下文查詢與修改:
semanage來自 policycoreutils-python包
查看默認的安全上下文
semanage fcontext -l
添加安全上下文到selinux安全上下文數據庫:
semanage fcontext -a -t http_sys_context_t “/testdir(/.*)”
刪除安全上下文到selinux安全上下文數據庫:
semanage fcontext -d -t http_sys_context_t “/testdir(/.*)”
selinux端口標簽:
查看端口標簽:
smanage port -l
示例:
[root@centos6 html]# semanage port -l | grep "http"
http_cache_port_t tcp 3128, 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
[root@centos6 html]#
添加端口:
semanage port -a -t 端口標簽 -p [tcp|udp] port
示例:
[root@centos6 html]# semanage port -a -t http_port_t -p tcp 9527
[root@centos6 html]# semanage port -l | grep "http"
http_cache_port_t tcp 3128, 8080, 8118, 8123, 10001-10010
http_cache_port_t udp 3130
http_port_t tcp 9527, 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t tcp 5988
pegasus_https_port_t tcp 5989
[root@centos6 html]#
刪除端口:
semanage port -d -t 端口標簽 -p [tcp|udp] port
示例:
[root@centos6 html]# semanage port -d -t http_port_t -p tcp 9527
[root@centos6 html]# semanage port -l | grep "http"
http_cache_port_t tcp 3128, 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
[root@centos6 html]#
修改現有端口為新標簽:(讓已經定義的端口屬于別的服務)
semanage port -m -t 端口標簽 -p [tcp|udp] port
示例:
[root@centos6 html]# semanage port -a -t http_port_t -p tcp 9527
[root@centos6 html]# semanage port -l | grep "http"
http_cache_port_t tcp 3128, 8080, 8118, 8123, 10001-10010
http_cache_port_t udp 3130
http_port_t tcp 9527, 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t tcp 5988
pegasus_https_port_t tcp 5989
[root@centos6 html]# semanage port -m -t ssh_port_t -p tcp 9527
[root@centos6 html]# semanage port -l | grep "ssh"
ssh_port_t tcp 9527, 22
[root@centos6 html]#
selinux布爾值:
布爾值規則:
getsebool:查看布爾值;
setsebool:設置布爾值;
查看selinux布爾值(bool)命令:
getsebool
getsebool [-a] [boolean]
-a:查看本系統內所有布爾值設置狀態。
semanage boolean -l 查看更詳細的系統布爾值設置。
semanage boolean -l -C 查看修改過的布爾值信息。
示例:查看本系統內所有布爾值設置狀態。
getsebool -a
示例:查看修改過的布爾值信息。
[root@centos6 Desktop]# semanage boolean -l -C
SELinux boolean State Default Description
use_nfs_home_dirs (on , on) Support NFS home directories
[root@centos6 Desktop]#
注:第一個on為系統現在狀態的布爾值,第二個on為系統存入磁盤狀態的布爾值。
設置bool值命令:
setsebool -P boolean value (on,off)
setsebool -P boolean=value (0,1)
示例:
[root@centos6 Desktop]# setsebool -P use_nfs_home_dirs on
[root@centos6 Desktop]# getsebool -a | grep "home"
ftp_home_dir –> off
git_cgi_enable_homedirs –> off
git_system_enable_homedirs –> off
httpd_enable_homedirs –> off
openvpn_enable_homedirs –> on
samba_create_home_dirs –> off
samba_enable_home_dirs –> off
spamd_enable_home_dirs –> on
ssh_chroot_rw_homedirs –> off
use_fusefs_home_dirs –> off
use_nfs_home_dirs –> on
use_samba_home_dirs –> off
[root@centos6 Desktop]#
selinux日志管理:
yum -y install setroubleshoot*(重啟生效)
selinux將錯誤信息寫入到/var/log/massage
使用setroubleshoot作為grub關鍵字進行搜索selinux日志信息。
sealert -l UUID
查看安全事件日志說明:
sealert -a /var/log/audit/audit.log
sealert命令以圖形方式打開selinux報警信息。
示例:
[root@centos6 Desktop]# sealert -a /var/log/audit/audit.log
selinux幫助文檔
yum -y install selinux-policy-devel (centos7)
yum -y install selinux-policy-doc (centos6)
更新man數據庫:
mandb (centos7)
makewhatis (centos6)
原創文章,作者:zhengyibo,如若轉載,請注明出處:http://www.www58058.com/49367