SELinux
·SELinux: Secure Enhanced linux,工作與Linux內核中
·DAC:Discretionary Access Control 自由訪問控制
·MAC:Mandatory Access Control 強制訪問控制
DAC環境下進程是無束縛的
MAC環境下策略的規則決定控制的嚴格程度
MAC環境下進程可以被限制的
策略被用來定義被限制的進程能夠使用那些資源(文件和端口)
默認情況下,沒有被明確允許的行為將被拒絕
·SELinux有四種工作級別:
strict:每個進程都受到selinux的控制
targeted:僅有限個進程受到selinux控制
只監控容易被入侵的進程
minimum:centos7,修改過的targeted,只對選擇的網絡服務
mls:提供MLS(多級安全)機制的安全性
minimum和mls穩定性不足,未加以應用
·sandbox:
傳統Linux,一切皆文件,由用戶,組,權限控制訪問
在SELinux 中,一切皆對象(object),由存放在inode的擴展屬性域的安全元素所控制其訪問
所有文件和端口資源和進程都具備安全標簽:安全上下文(security context)
格式:
subject operation object
subject:進程
object:進程,文件,
文件:open,read,write,close,chown,chmod
subject:domain
object:type
SELinux為每個文件提供了安全標簽,也為進程提供了安全標簽
user:role:type
user:SELinux的user
role:SELinux的角色
type:SELinux的的類型
實際上下文:存放在文件系統中,
ls –Z;ps –Z
期望(默認)上下文:存放在二進制的SELinux策略庫(映射目錄和期望安全上下文)中
semanage fcontext –l
SELinux規則庫:
規則:那種域能訪問那種或那些種類型的文件
·配置SELinux:
SELinux是否啟用
給文件重新打標
給端口設置安全標簽
設定某些操作的布爾型特性
SELinux的日志管理
1、SELinux的狀態:
·enforcing:強制,每個受限的進程都必然受限
·permissive:啟用,每個受限的進程違規操作不會被禁止,但會被記錄于審計日志
·disabled:關閉
·相關命令:
sestatus
查看selinux的狀態
getenforce:獲取當前selinux狀態
setenforce 0|1
0:設置為permissive
1:設置為enforcing
此設定:重啟系統后無效
配置文件:
/etc/selinux/config,/etc/sysconfig/selinux
SELinux={disabled|permissive|enforcing}
/boot/grub/grub.conf
在kernel行追加內核參數:使用selinux=0禁用SELinux
2、給配置文件重新打標:
chcon
chcon [OPTION]… CONTEXT FILE…
chcon [OPTION]… [-u USER] [-r ROLE] [-l RANGE] [-t TYPE] FILE…
chcon [OPTION]… –reference=RFILE FILE…
-R:遞歸打標
3、還原文件的默認標簽:
restorecon [-R] /path/to/somewhere
4、默認安全上下文查詢與修改
·需要用到的包:semanage來自policycoreutils-python包
·查看默認的安全上下文
semanage fcontext –l
·添加安全上下文
semanage fcontext -a –t httpd_sys_content_t '/testdir(/.*)?'
restorecon –Rv /testdir
·刪除安全上下文
semanage fcontext -d –t httpd_sys_content_t '/testdir(/.*)?'
·查看端口標簽
semanage port –l
·添加端口
semanage port -a -t port_label -p tcp|udp PORT
semanage port -a -t http_port_t -p tcp 9527
·刪除端口
semanage port -d -t port_label -p tcp|udp PORT
semanage port -d -t http_port_t -p tcp 9527
·修改現有端口為新標簽
semanage port -m -t port_label -p tcp|udp PORT
semanage port -m -t http_port_t -p tcp 9527
5、布爾型規則:
getsebool
setsebool
·查看bool值命令:
getsebool [-a] [boolean]
semanage boolean –l
semanage boolean -l –C
·設置bool 值命令:
setsebool [-P] boolean value (on,off)
setsebool [-P] Boolean=value (0 ,1)
6、SELinux日志管理:
安裝包:yum install setroublesshoot* (重啟生效)
將錯誤的信息寫入/var/log/message
grep setroubleshoot /var/log/messages
sealert -l UUID
查看安全事件日志說明
sealert -a /var/log/audit/audit.log
掃描并分析日志
7、SELinux幫助:
yum -y install selinux-policy-devel (centos7)
yum -y install selinux-policy-doc (centos6)
mandb | makewhatis
man -k _selinux
練習:
1、啟用SELinux策略并安裝httpd服務,改變網站的默認主目錄為/website,添加SELinux文件標簽規則,使網站可訪問
2、修改上述網站的http端口為9527,增加SELinux端口標簽,使網站可訪問
3、啟用相關的SELinux布爾值,使上述網站的用戶student的家目錄可通過http訪問
原創文章,作者:megedugao,如若轉載,請注明出處:http://www.www58058.com/47442
總結的很好,理論與實踐相結合起來了,贊一個。