SELinux: Secure Enhanced Linux(安全強化的linux)
SElinux安全上下文是由五個元素組成的:
①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
所有文件和端口資源和進程都具備安全標簽:
user:role:type:sensitivity:category user_u:object_r:tmp_t:s0:c0 [root@CentOS 7 ~]#ll -Z file1 -rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 file1
.代表受SElinux保護和控制(安全標簽)
SElinu策略的文件:
[root@CentOS 7 ~]#cat /etc/selinux/config # 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 three two values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted 策略
getenforce: 獲取selinux當前狀態
[root@CentOS 7 ~]#getenforce Enforcing
sestatus:查看selinux狀態
[root@CentOS 7 ~]#sestatus SELinux status: enabled 是否啟動SElinux SELinuxfs mount: /sys/fs/selinux 掛載點 SELinux root directory: /etc/selinux Loaded policy name: targeted Current mode: enforcing 目前的模式 Mode from config file: enforcing 配置文件指定的模式 Policy MLS status: enabled Policy deny_unknown status: allowed Max kernel policy version: 28
setenforce:修改策略 只是臨時生效
[root@CentOS 7 ~]#setenforce 0: 設置為permissive 1: 設置為enforcing
SElinux的關閉:
在 /etc/selinux/config 配置文件里修改: 永久生效
[root@CentOS 7 ~]#vim /etc/selinux/config SELINUX=disabled 調整 enforcing|disabled|permissive SELINUXTYPE=targeted 調整 targeted|strict
改變了策略需要策略,因為SElinux是要整合到內核中去的,也可以在內核文件中禁用SEinux,在
[root@CentOS 7 ~]#vim /boot/grub2/grub.cfg linux16 /vmlinuz-3.10.0-327.el7.x86_64 root=UUID=da202a08-08d9-4dde-aa75-9c357b2d88bd ro net.ifnames=0 rhgb quiet selinux=0
修改配置文件后也需要重啟,以后開機時就會略過該選項。
SElinux的開啟:
開啟的方式正好與關閉相反,在/etc/selinux/config文件中SELINUX=enforcing,SELINUXTYPE=targeted,并且到/boot/grub2/grub.cfg文件中看內核里是否有 selinux=0 這行標記,有就把它刪除。selinux=0指定給內核時,則內核會自動忽略/etc/selinux/config的設置值,而直接略過SElinux 的加載,模式會變成disabled。
注意:如果從disable轉到啟動模式時,由于系統必須要針對文件寫入安全上下文的信息,因此開機過程會花費不少時間在等待重新打標簽,在寫完后還得要再次重啟一次。
實驗1、啟用SELinux策略并安裝httpd服務,改變網站的默認主目錄為/website,添加SELinux文件標簽規則,使網站可訪問
1,安裝httpd服務
[root@CentOS 7 ~]#yum -y install httpd
2,啟動httpd服務
[root@CentOS 7 ~]#systemctl start httpd
3,查看端口:
[root@CentOS 7 ~]#netstat -tnl Proto Recv-Q Send-Q Local Address Foreign Address State tcp6 0 0 :::80 :::* LISTEN
4,清除防火墻:
[root@CentOS 7 ~]#iptables -F
網頁的文件存放在 /var/www/html 目錄下
恢復目錄或文件默認的安全上下文:
restorecon[-R] /path/to/somewhere
[root@CentOS 7 html]#echo hello work > index.html 網頁可以正常訪問 [root@CentOS 7 html]#cp index.html /root [root@CentOS 7 html]#mv /root/index.html . mv: overwrite ‘./index.html’? y [root@CentOS 7 html]#ll -Z index.html 網頁不可以正常訪問,文件在移動時將原文件的屬性改變,附帶所在目錄的屬性 -rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 index.html [root@CentOS 7 html]#echo hello work > index2.html [root@CentOS 7 html]#ll -Z index2.html 網頁可以正常訪問 -rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index2.html [root@CentOS 7 html]#cp index2.html /root [root@CentOS 7 html]#cp /root/index2.html . cp: overwrite ‘./index2.html’? y [root@CentOS 7 html]#ll -Z index2.html 網頁可以正常訪問,文件在復制時會保留原文件的屬性 -rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index2.html
期望(默認)上下文:存放在二進制的SELinux策略庫(映射目錄和期望安全上下文)中
semanagefcontext–l
[root@CentOS 7 html]#semanage fcontext -l |grep "/var/www" /var/www(/.*)? all files system_u:object_r:httpd_sys_content_t:s0 [root@CentOS 7 html]#restorecon index.html 修改文件的標簽 [root@CentOS 7 html]#ll -Z -rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index2.html -rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html [root@CentOS 7 ~]#restorecon -R /var/www/html 修改目錄的標簽 [root@CentOS 7 html]#chcon --reference index2.html index.html 參考某文件或目錄修改屬性
5,將web服務的網頁的默認目錄修改為/website
[root@CentOS 7 html]#vim /etc/httpd/conf/httpd.conf 修改配置文件
DocumentRoot "/var/www/html" 修改為/website # Relax access to content within /var/www. <Directory "/var/www"> 修改為/website AllowOverride None # Allow open access: Require all granted </Directory>
[root@CentOS 7 html]#mkdir /website [root@CentOS 7 html]#cd /website/ [root@CentOS 7 website]#echo hello work > index.html [root@CentOS 7 website]#ll -Z -rw-r--r--. root root unconfined_u:object_r:default_t:s0 mywebsite [root@CentOS 7 website]#systemctl restart httpd [root@CentOS 7 website]#chcon -R --reference /var/www/html /website [root@CentOS 7 website]#ll -dZ /website /var/www/html drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /website
實驗2、修改上述網站的http端口為9527,增加SELinux端口標簽,使網站可訪問
SElinux端口標簽:
查看端口標簽
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
1,修改httpd端口號為9527
[root@CentOS 7 ~]#vim /etc/httpd/conf/httpd.conf Listen 9527
2,增加SElinux端口標簽
[root@CentOS 7 ~]#semanage port -a -t http_port_t -p tcp 9527 [root@CentOS 7 html]#echo "welcome" > index.html
3,重啟服務
[root@CentOS 7 ~]#systemctl restart httpd [root@CentOS 7 html]#netstat -tnl tcp6 0 0 :::9527 :::* LISTEN
4,訪問網站
實驗3、啟用相關的SELinux布爾值,使上述網站的用戶student的家目錄可通過http訪問
SELinux布爾值
布爾型規則:
getsebooll;
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)
1,編輯配置文件
[root@CentOS 7 ~]#vim /etc/httpd/conf.d/userdir.conf # UserDir disabled UserDir public_html
2,重啟httpd服務
[root@CentOS 7 ~]#systemctl restart httpd
3,在student家目錄下創建網頁文件
[root@CentOS 7 ~]#mkdir /home/student/public_html [root@CentOS 7 ~]#echo "this is student's home" > /home/student/public_html/index.html
4,啟用相關的SElinux布爾值
[root@CentOS 7 ~]#setsebool -P httpd_enable_homedirs on
5,設置apache對用戶student的家目錄有執行權限
[root@CentOS 7 ~]#setfacl -m u:apache:x /home/student
6,訪問網站
SELinux日志管理
將錯誤的信息寫入/var/log/message
yum install setroublesshoot*(重啟生效)
查看安全事件日志說明
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 g
man -k selinux
原創文章,作者:pingsky,如若轉載,請注明出處:http://www.www58058.com/48887