SELinux:Secure Enhanced Linux,是美國國家安全局(NSA=The National Security Agency)和SCC(Secure Computing Courporation)開發的LInux的一個強制控制的安全模塊。2000年以GNU GPL發佈,Linux內核2.6版本後繼承在內核中。html
訪問控制類別:python
DAC:Discretionary Access Control 自由訪問控制linux
MAC:Mandatory Access Control 強制訪問控制web
DAC和MAC的特色:數據庫
DAC環境下進程是無束縛的vim
MAC環境下策略的規則決定控制的嚴格程度centos
MAC環境下進程能夠被限定的緩存
策略被用來定義被限定的進程可以使用那些資源(文件和端口)安全
默認狀況下,沒有被明確容許的行爲將被拒絕bash
SElinux安全上下文工做類型:
strict:centos5,每一個進程都受到selinux的控制
targeted:用來保護常見的網絡服務,僅有限進程受到 selinux控制,
只監控容易被***的進程,rhel4只保護13個服務,rhel5保護88個服務
minimum:centos7,修改過的targeted,只對選擇的網絡服務。
mls:提供MLS(多級安全)機制的安全性
minimum和mls穩定信不足,未加以應用
爲何會用SElinux
傳統的 Linux中一切皆文件,由用戶、組、權限控制訪問。而在SELinux中一切皆對象(object),由存放在inde表的擴展屬性域的安全元素所控制其訪問。
安全上下文基本格式說明
全部的文件和端口資源和進程都具有安全標籤:安全上下文(security context)組成元素:
user:role:type:sesitivity:category,如
user_u:object_r:tmp_t:s0:c0
實際上下文存放在文件系統中,進程、文件、已經用戶等都有相應的安全上下文:
例子:
#查看指定文件的安全上下文 ls -Z
#查看進程的安全上下文件 ps Z
#查看用戶安全上下文
指望(默認)上下文:
存放在二進制的SELinux策略庫(映射目錄和指望安全上下文)中:
使用命令
semanage fcontext -l # 能夠查看全部默認策略
SELinux策略:
對象(object):全部能夠讀取的對象,包括文件、目錄和進程,端口等
主體:進程稱爲主體(subject)
SELinux中對全部的文件都賦予一個type的文件類型標籤,對於全部的進程也賦予各自的一個domain的標籤。domain標籤可以執行的操做由安全策略裏定義。
當一個subject試圖訪問一個object,Kernel中的策略執行服務器將檢查AVC(訪問矢量緩存Access Vector Cache),在AVC中,subject和object的權限被緩存(cached),查找「應用+文件」的安全環境。而後根據查詢結果運行或拒絕訪問。
安全策略:定義主體讀取對象的規則數據庫,規則中記錄了哪一個類型的主體使用哪一個方法讀取哪一項對象是容許仍是拒絕的,而且定義了那種行爲是運行或拒絕。
設置SELinux模式及開關使用命令:getnforce status setenforce
getenforce:#獲取selinux的當前狀態
sestatus: #查看selinux完整狀態
setenforce 0|1#開啓或關閉
0:設置爲permissive
1:設置爲enforcing
配置文件:
/boot/grub/grub.conf在對應的kernel內核參數後面追加
selinux=1 (啓用) selinux=0(禁用)
/etc/sysconfig/selinux
/etc/selinux/config
SELINUX變量有3中參數:
SELINUX={disabled|enforcing|permissive}
基本使用實例:
#關閉當前selinux功能
[root@localhost ~]# setenforce 0
#查看當前selinux狀態
[root@localhost ~]# getenforce Permissive
#啓用當前selinux功能
[root@localhost ~]# setenforce 1
#再次查看當前selinux功能
[root@localhost ~]# getenforce
解析:這裏的設置都是當即生效,使用此命令只能開啓或關閉,並不能禁用 selinux。
#查看目前系統完整的selinux狀態
[root@localhost ~]# sestatus
解析:由於selinux是須要內核或內核模塊來支持的,那麼也就須要對應的配置文件來提供讀取,這裏說明了其主配置目錄爲/etc/selinux,而當前狀態爲enabled啓用狀態,也就是說selinux仍是能夠經過命令來設置是否開啓。而當前模式表示其selinux的具體管理方式。
enforcing: 表示徹底安裝selinux規則在處理。
permissive:表示只是根據selinux規則來進行警告,而不進行權限處理,但會記錄。
修改文件的SELinux標籤的命令:chcon restorecon
chcon 命令
chcon - change file SELinux security context
更改文件的selinux安全上下文件
選項與用法:
chcon [option]... CONTXT /path/to/file... #直接對指定的文件寫完整的安全上下文字段
chcon [option]... [-u USER] [-r ROLE] [-l RANGE] [-t TYPE] FILE... #根據上下文類型更改
chcon [option]... --reference=RFILE FILE.... #參考RFILE的上下文修改FILE的上下文
-R :若是是目錄,將同時遞歸設置該目錄下全部文件
restorecon 命令
restorecon - restore file(s) default SELinux security contexts.
恢復文件或目錄默認的SELinux安全上下文
選項與用法:
restorencon [option] /path/to/somewhere
-R:遞歸恢復處理
SELinux規則管理工具命令:semanage
semanage - SELinux Policy Management tool
semange 命令 注:此命令來自policycoreutils-python RPM包
默認安全文件規則的查詢和修改:semanage
semanage 命令
semanage fcontext [-S store] -{a|d|m|l|n|D} [-frstN] file_spec
用法與實例:
查詢信息
#查看默認全部安全上下文列表
semanage fcontext -l
#添加安全上下文
semanage fcontext -a -t httpd_sys_content_t ‘/testdir(/.*)?’
#刪除安全上下文
semanage fcontext -d -t httpd_sys_content_t ‘/testdir(/,*)?’
端口標籤的設置
#查詢端口標籤
semanage port -l
#添加端口
semanage port -a -t port_label -p tcp|udp POST 例如:
semanage port -a -t http_port_t -p tcp 9527
#刪除端口
semanage port -d -t port_label -p tcp|udp POST 例如:
semanage port -d -t http_port -t -p tcp 9527
#修改現有端口爲新標籤
semanage port -m -t port_label -p tcp|udp POST 例如:
semanage port -m -t http_port_t -p tcp 9527
布爾值規則命令:getsebool setsebool
SELinux布爾值設置
查看bool命令:
getsebool [-a] [boolean]
semanage boolean -l
semanage boolean -l -C #查看修改過的布爾值
設置bool值命令:
setsebool [-P] boolean value (on,off)
setsebool [-P] Boolean=value (0,1)
SELinux日誌管理:setrublesshhoot 命令
yum install setrublesshooot* (重啓後生效)
setrub會將錯誤信息寫入/var/log/message,使用下面方式查看:
grep ‘setroubleshoot’ /var/log/messages
sealert -l UUID
查看安全時間日誌說明
sealert -a /var/log/audit/audit.log
掃描並分析日誌
獲取SELinux的相關幫助信息
yum -y install selinux-policy-devel #centos7下
yum -y install selinux-policy-doc #centos6下
mandb | makewhatis #更新man文檔數據庫
man -k _selinux #列出selinux關鍵字相關文檔
實戰案例:
一、啓用SELinx策略並安裝httpd服務,改變網站的默認主目錄爲/website,添加SELinux文件標籤規則,使網站可訪問。
#使用yum原安裝本地ISO鏡像中httpd便可
[root@mzf ~]# yum install httpd -y
#修改配置主配置文件,將DocumentRoot 對應的路徑和Direcotry標標籤目錄改成/webbsite
[root@mzf ~]# vim /etc/httpd/conf/httpd.conf
注意:這兩項都要修改,纔會完整生效。
#查看修改後的結果
[root@mzf ~]# grep '[[:space:]]*DocumentRoot.*\".*\"' /etc/httpd/conf/httpd.conf DocumentRoot "/website"
#新建/website目錄並給其目錄添加文件標籤規則
[root@mzf ~]# [ ! -d '/website' ] && mkdir /website
#查看原首頁目錄/var/www/html 的文件規則類型
[root@mzf ~]# ls -dZ /var/www/html/ drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/
#新建index.html文件內容以下
[root@mzf website]# cat index.html <html> <head> <title>Welcome to Website directry!!!</title> </head> <body><div><ul> <li>Welcome to Website directry!!!</li> </ul></div></body> </html>
#查看原有的/var/www目錄下的默認規則
[root@mzf website]# semanage fcontext -l | grep --exclude='.*?' '/var/www(/.*)?' /var/www(/.*)? all files system_u:object_r:httpd_sys_content_t:s0
#給/website及其下文件設置文件規則類型,3種方法
(1)執行設置特定值,使用chcon命令
[root@mzf website]# chcon -t httpd_sys_content_t -R /website/
(2)參考/var/www/html目錄的規則來設定
[root@mzf website]# chcon -R --reference='/var/www/html/' /website/
(3)執行給目錄添加規則,而後對目錄進行遞歸默認規則還原
[root@mzf website]# semanage fcontext -l | grep -F '/var/www(/.*)?' | cut -d':' -f3 httpd_sys_content_t [root@mzf website]# semanage fcontext -a -t httpd_sys_content_t '/website(/.*)?' [root@mzf website]# semanage fcontext -l | grep -F '/website(/.*)?' | cut -d':' -f3 httpd_sys_content_t [root@mzf website]# restorecon -R /website/ [root@mzf website]# ls -aZ --ignore='..' /website drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 . -rw-r--r--. root root system_u:object_r:httpd_sys_content_t:s0 index.html
解析:上述三種方法中,前兩種若是執行了restorecon還原默認,而默認selinux規則裏沒有預設,那麼會還原其它的默認值,而第三者方法採用直接在selinux規則中對此目錄添加預設規則,這樣不管怎麼修改,只要使用restorecon就能夠還原回來。
#啓用selinux功能
[root@mzf website]# setenforce 1
#重啓httpd服務
[root@mzf website]# service httpd restart
#使用另外一條主機使用links工具訪問其主頁
[root@mageedu ~]# links http://192.168.20.136
解析:顯示出/website新建的index.html文件內容表示成功。
二、修改上述網站的http端口爲9527,增長SELinux端口標籤,使網站可訪問
#修改httpd服務配置文件中Listen監聽端口
[root@mzf ~]# sed -i 's/^\([[:space:]]*Listen\)[[:space:]]\+[0-9]\+/\1 9527/' /etc/httpd/conf/httpd.conf [root@mzf ~]# sed -n '/^[[:space:]]*Listen[[:space:]]\+[0-9]\+/p' /etc/httpd/conf/httpd.conf Listen 9527
#由於上面已經重啓了服務,此次使用發送SIGHUP信號來從新調用httpd服務
[root@mzf ~]# pkill -SIGHUP 'httpd'
#查看原來的httpd默認監聽端口80的端口標籤類型
[root@mzf ~]# semanage port -l | grep '\<80\>' http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
#添加9527端口進入此標籤類型
[root@mzf ~]# semanage port -a -t http_port_t -p tcp 9527
#查看9527端口是否在此列表
[root@mzf ~]# semanage port -l | grep '\<9527\>' http_port_t tcp 9527, 80, 81, 443, 488, 8008, 8009, 8443, 9000
#修改一下/website/index.html的內容
[root@mzf ~]# cat /website/index.html <html> <head> <title>Welcome to Website directry!!!</title> </head> <body><div><ul> <li>This is next two!!!</li> </ul></div></body> </html>
#使用其它主機經過links統計進行訪問9527端口
[root@mageedu ~]# links http://192.168.20.136:9527
解析:這裏顯示和上面修改的body標籤裏的內容同樣,說明已經生效。
三、啓用相關的SElinux布爾值,使上述網站的用戶student的家目錄可經過http訪問
#添加httpd.conf文件的用戶目錄訪問配置
說明:找到<If Modulemod_userdir.c>標籤在裏面註釋掉UserDir disabled此行,而後添加一個UserDir public_html行,表示在使用用戶加目錄下有個此文件會被訪問。
#在student 用戶家目錄新建public_html目錄
[root@mzf ~]# cd /home/student/ [root@mzf student]# mkdir -m 711 public_html
#拷貝/website/index.html到此目錄下並修改
[root@mzf student]# cp /website/index.html ./public_html/
#修改index.html文件的li標籤裏的內容,隨便改,爲了區別顯示
[root@mzf student]# grep '<li.*/li>' ./public_html/index.html <li>This is stuent home</li>
#從新給httpd發送SIG信號
[root@mzf student]# killall -HUP -r 'httpd'
#再次訪問links
解析:這是由於沒有開啓相關的httpd用戶家目錄布爾值致使的,因而修改其boolean值。
#查看httpd的用戶目錄讀取,off爲關閉,及提供此功能
[root@mzf student]# getsebool -a | grep 'http.*user' httpd_read_user_content --> off
#修改成on,表示啓用訪問用戶家目錄
[root@mzf student]# setsebool httpd_read_user_content on
#再次訪問
解析:這就成功了,在目錄自己對其它人有相應的執行權限之外,仍是要添加一個boolean。