Linux之SELinux
本文包括以下內容
SELinux概念
啟用SELinux
管理文件安全標簽
管理端口標簽
管理SELinux布爾值開關
管理日志
查看SELinux幫助
SELinux介紹
SELinux: Secure Enhanced Linux,是美國國家安全局(NSA=The National Security Agency)和SCC(Secure Computing Corporation)開發的Linux的一個強制訪問控制的安全模塊。2000年以GNU GPL發布,Linux內核2.6版本后集成在內核中
DAC:Discretionary Access Control自由訪問控制
MAC:Mandatory Access Control 強制訪問控制
DAC環境下進程是無束縛的
MAC環境下策略的規則決定控制的嚴格程度
MAC環境下進程可以被限制的
策略被用來定義被限制的進程能夠使用那些資源(文件和端口)
默認情況下,沒有被明確允許的行為將被拒絕
SELinux工作類型
SELinux有四種工作類型:
strict: centos5,每個進程都受到selinux的控制
targeted: 用來保護常見的網絡服務,僅有限進程受到selinux控制,只監控容易被入侵的進程,rhel4只保護13個服務,rhel5保護88個服務
minimum:centos7,修改過的targeted,只對選擇的網絡服務
mls:提供MLS(多級安全)機制的安全性
minimum和mls穩定性不足,未加以應用
SELinux安全上下文
傳統Linux,一切皆文件,由用戶,組,權限控制訪問
在SELinux中,一切皆對象(object),由存放在inode的擴展屬性域的安全元素所控制其訪問
所有文件和端口資源和進程都具備安全標簽:安全上下文(security context)
安全上下文有五個元素組成:
user:role:type:sensitivity:category
user_u:object_r:tmp_t:s0:c0
實際上下文:存放在文件系統中,ls –Z;ps–Z
期望(默認)上下文:存放在二進制的SELinux策略庫(映射目錄和期望安全上下文)中
semanagefcontext–l
五個安全元素
User:指示登錄系統的用戶類型,如root,user_u,system_u,多數本地進程都屬于自由(unconfined)進程
Role:定義文件,進程和用戶的用途:文件:object_r,進程和用戶:system_r
Type:指定數據類型,規則中定義何種進程類型訪問何種文件Target策略基于type實現,多服務共用:public_content_t
Sensitivity:限制訪問的需要,由組織定義的分層安全級別,如unclassified,secret,top,secret, 一個對象有且只有一個sensitivity,分0-15級,s0最低,Target策略默認使用s0
Category:對于特定組織劃分不分層的分類,如FBI Secret,NSA secret, 一個對象可以有多個categroy,c0-c1023共1024個分類,Target 策略不使用category
SELinux策略
對象(object):所有可以讀取的對象,包括文件、目錄和進程,端口等
主體:進程稱為主體(subject)
SELinux中對所有的文件都賦予一個type的文件類型標簽,對于所有的進程也賦予各自的一個domain的標簽。domain標簽能夠執行的操作由安全策略里定義。
當一個subject試圖訪問一個object,Kernel中的策略執行服務器將檢查AVC (訪問矢量緩存Access Vector Cache), 在AVC中,subject和object的權限被緩存(cached),查找“應用+文件”的安全環境。然后根據查詢結果允許或拒絕訪問
安全策略:定義主體讀取對象的規則數據庫,規則中記錄了哪個類型的主體使用哪個方法讀取哪一個對象是允許還是拒絕的,并且定義了哪種行為是充許或拒絕
設置SELinux
配置SELinux:
SELinux是否啟用
給文件重新打安全標簽
給端口設置安全標簽
設定某些操作的布爾型開關
SELinux的日志管理
SELinux的狀態:
enforcing: 強制,每個受限的進程都必然受限
permissive: 允許,每個受限的進程違規操作不會被禁止,但會被記錄于審計日志
disabled: 禁用
配置SELinux
相關命令:
getenforce: 獲取selinux當前狀態
sestatus:查看selinux狀態
setenforce0|1
0: 設置為permissive
1: 設置為enforcing
配置文件:
/boot/grub/grub.conf
使用selinux=0禁用SELinux
/etc/sysconfig/selinux
/etc/selinux/config
SELINUX={disabled|enforcing|permissive}
修改SELinux安全標簽
給文件重新打安全標簽:
chcon[OPTION]… [-u USER] [-r ROLE] [-t TYPE] FILE…
chcon[OPTION]… –reference=RFILE FILE…
-R:遞歸打標;
恢復目錄或文件默認的安全上下文:
restorecon[-R] /path/to/somewhere
默認安全上下文查詢與修改
semanage來自policycoreutils-python包
查看默認的安全上下文
semanagefcontext–l
添加安全上下文
semanagefcontext-a –t httpd_sys_content_t‘/testdir(/.*)?’
restorecon–Rv/testdir
刪除安全上下文
semanagefcontext-d –t httpd_sys_content_t‘/testdir(/.*)?’
1212
查看端口標簽
semanageport –l
添加端口
semanageport -a -t port_label-p tcp|udpPORT
semanage port -a -t http_port_t -p tcp 9527
刪除端口
semanageport -d -t port_label-p tcp|udpPORT
semanage port -d -t http_port_t -p tcp 9527
修改現有端口為新標簽
semanageport -m -t port_label-p tcp|udpPORT
semanageport -m -t http_port_t-p tcp9527
Selinux端口標簽
SELinux布爾值
布爾型規則:
getsebool
setsebool
查看bool命令:
getsebool[-a] [boolean]
semanageboolean–l
semanageboolean-l –C 查看修改過的布爾值
設置bool值命令:
setsebool[-P] booleanvalue(on,off)
setsebool[-P] Boolean=value(0,1)
SELinux日志管理
yum install setroublesshoot*(重啟生效)
將錯誤的信息寫入/var/log/message
grep setroubleshoot/var/log/messages
sealert-l UUID
查看安全事件日志說明
sealert-a /var/log/audit/audit.log
掃描并分析日志
yum -y install selinux-policy-devel(centos7)
yum –y install selinux-policy-doc (centos6)
mandb | makewhatis
man -k _selinux
SELinux幫助
練習
1、啟用SELinux策略并安裝httpd服務,改變網站的默認主目錄為/website,添加SELinux文件標簽規則,使網站可訪問
vim /etc/selinux/config
SELINUX=disabled
reboot 重啟才生效
systemctl status httpd
yum -y install httpd
systemctl start httpd
netstat -tnl
iptalbes -F
mkdir /website
ll -Z /website/ -d
vim /etc/httpd/conf/httpd.conf
Document Root "/website/"
<Directory "/website">
systemctl restart httpd
systemctl status httpd
echo mywebsite > index.html
semanage fcontext -l |grep website
semanage fcontext -a -t httpd_sys_content_t "/website(/.*)?"
ll -Z /website/ -d
restorecon -R /website/
2、修改上述網站的http端口為9527,增加SELinux端口標簽,使網站可訪問
vim /etc/httpd/conf/httpd.conf
Listen 9527
semanager port -l|grep 9527
vim /etc/httpd/conf/httpd.conf
netstat -ntl
httpd -t 檢查語法
systemctl restart httpd
semanger port -a -t http_port_t -p tcp 9527
iptables -F
3、啟用相關的SELinux布爾值,使上述網站的用戶student的家目錄可通過http訪問
在CentOS 7上
vim /etc/httpd/conf.d/usrdir.conf
#UserDir disabled
UserDir public_html
systemctl restart httpd
su -student
mkdir public_html
echo welcome to studenthome > index.html
links x.x.x.x/~student 訪問家目錄
ll -d /home/student
ps aux|grep http
setfacl -m u:apache:x /home/student
在CentOS 6上
vim /etc/httpd/conf/httpd.conf
#UserDir disabled
UserDir public_html
service httpd restart
iptables -F
su -student
mkdir public_html
cd public html
echo studenthomecentos6 > index.html
cd
chmod 711 /home/wang
ll -d /home/wang/
原創文章,作者:dengjian,如若轉載,請注明出處:http://www.www58058.com/48151
內容總結的很全面,但是整個文章看起來沒有i什么層次感,雖然說作者分類過,如果對字體大小,顏色等進行一些調整,會不會看起來更舒服點呢?