SElinux 管理

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

wKiom1fmF47iyQPUAABdUhQYLo0361.png

所有文件和端口資源和進程都具備安全標簽:

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

wKiom1fbzOrzHQ28AAAkpoLvvzk950.png

實驗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,訪問網站

wKioL1fmItHy12G7AAArMrRFIhE685.png

實驗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,訪問網站

wKioL1fmK9jjfI0_AAAvRGHhNak414.png


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

(0)
pingskypingsky
上一篇 2016-09-26 08:06
下一篇 2016-09-26 08:52

相關推薦

  • 千萬不要把 bool 設計成函數參數

    我們有很多Coding Style 或 代碼規范。但這一條可能會經常被我們所遺忘,就是我們經常會在函數的參數里使用bool參數,這會大大地降低代碼的可讀性。不信?我們先來看看下面的代碼。 當你讀到下面的代碼,你會覺得這個代碼是什么意思? widget->repaint(false); 是不要repaint嗎?還是別的什么意思?看了文檔后,我們才知道這個…

    Linux干貨 2016-07-10
  • 第四周作業

    Linux系統文件管理使用案例 1、 復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其它用戶均沒有任何訪問權限。 首先,利用cp命令將/etc/skel目錄復制為/home/tuser1: [root@localhost ~]# cp -r /etc/skel /home/tuser1 隨后通過chmo…

    Linux干貨 2018-03-24
  • 馬哥教育網絡班22期+第1周課程練習

    Linux的發行版以及不同發行版之間的聯系與區別    Linux發行版主要的大分支有三個:dibian  S.U.S.E  redhat 一個操作系統由kernel和application組成。編譯封裝的時候根據不同的application,可以編譯成不同的發行版本。 所有的linux發行版都是基于GPL的版權,它們…

    Linux干貨 2016-08-15
  • rpm包管理

    rpm包管理 由于 RPM 是透過預先編譯打包成為 RPM 文件格式后,再加以安裝的一種方式,還能夠進行數據庫的記載。 所以 RPM 有以下的優點: RPM 內含已經編譯過的程序與配置文件等數據,可以讓用戶免除重新編譯的困擾; RPM 在被安裝前,會先檢查系統的硬盤容量、操作系統版本等,可避免檔案被錯誤安裝; RPM 檔案本身提供軟件版本信息、相依屬性軟件名…

    Linux干貨 2016-08-21
  • M20 – 1- 第二周(2):課堂練習與作業

    課堂練習: 1、顯示/var目錄下所有以l開頭,以一個小寫字母結尾,且中間出現至少一位數字的文件和目錄 [root@centos6 ~]# ls /var/l*[[:digit:]]*[[:lower:]] ls: cannot access /var/l*[[:digit:]]*[[:lower:…

    Linux干貨 2016-08-02
  • redis主從復制(1)— 慢查詢導致復制中斷

    redis的異常行為是一件令人頭疼的問題。redis提供的錯誤日志只提供了一些server的運行狀態信息,而沒有server在故障現場的操作日志,比如執行了什么命令,命令操作對象的數據結構信息。如果redis也有mysql的slow-log,那么很多性能和故障問題也很容易處理。1、redis rdb在redis2.8版本以前,redis主從數據復制在生產上存…

    Linux干貨 2016-04-12
欧美性久久久久