SELinux詳解及其優缺點

SELinux簡介:

       SELinux:Secure Enahanced Linux 安全增強的Linux

SELinux2.6版本的Linux內核中提供的強制訪問控制(MAC)系統。對于目前可用的Linux安全模塊來說,SELinux是功能最全面,而且測試最充分的,它是在20年的MAC研究基礎上建立的。SELinux在類型強制服務器中合并了多級安全性或一種可選的多類策略,并采用了基于角色的訪問控制概念。

大部分使用SELinux的人使用的都是SELinux就緒的發行版,例如Fedora、RedHatEnterpriseLinux(RHEL)、DebianCentos。它們都是在內核中啟用SELinux的,并且提供一個可定制的安全策略,還提供很多用戶層的庫和工具,它們都可以使用SELinux的功能。   

SELinux是一種基于域類型模型(domain-type)的強制訪問控制(MAC)安全系統,它由NSA編寫并設計成內核模塊包含到內核中,相應的某些安全相關的應用也被打了SELinux的補丁,最后還有一個相應的安全策略。  

眾所周知,標準的UNⅨ安全模型是任意的訪問控制“DAC。就是說,任何程序對其資源享有完全的控制權。假設某個程序打算把含有潛在重要信息的文件扔到/tmp目錄下,那么在DAC情況下沒人能阻止他!  

MAC情況下的安全策略完全控制著對所有資源的訪問。這是MACDAC本質的區別。  

SELinux提供了比傳統的UNⅨ權限更好的訪問控制。

工作于Linux內核中


DAC:自主訪問控制

基本要素:

    主體:主動的實體,用戶、服務、進程等

    客體: 被動的實體,包括數據、文件等

    規則:主體對客體訪問的策略集合

基本類型:

    DAC訪問控制:自主訪問控制

    通過權限列表(訪問控制列表)來限定特定主體對特定客體可以執行什么操作。

特點:

    1、每個主體都擁有一個用戶名或組來獲取操作權限

    2、每個客體都擁有一個限定主體對其訪問的訪問控制列表

    3、訪問時,基于訪問控制列表檢查主體用戶標示以實現授權或拒絕。

進程的訪問權限

    進程擁有超過其本該擁有的權限

管理員的訪問權限

    管理員擁有超過其本該有用的權限

普通用戶的訪問權限


MAC:強制訪問控制

特點:

    1、主體被分配一個安全等級 機密

    2、客體被分配一個安全等級 絕密

    3、訪問時,對主體和客體的安全級別進行對比,最終獲得授權或拒絕。

         sandbox 沙箱 虛擬化技術

模式切換:

得到當前的SELinux

[root@localhost ~]#getenforce
Enforcing

更改當前SELinux值,后面可跟1|0

[root@localhost ~]#setenforce
usage:  setenforce [ Enforcing | Permissive | 1 | 0 ]

顯示當前的SELinux信息

[root@localhost ~]#sestatus
SELinux status:                 enabled
SELinuxfs mount:                /selinux
Current mode:                   enforcing
Mode from config file:          enforcing
Policy version:                 24
Policy from config file:        targeted

[root@localhost ~]#sestatus -v
SELinux status:                 enabled
SELinuxfs mount:                /selinux
Current mode:                   enforcing
Mode from config file:          enforcing
Policy version:                 24
Policy from config file:        targeted
 
Process contexts:
Current context:                unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
Init context:                   system_u:system_r:init_t:s0
/sbin/mingetty                  system_u:system_r:getty_t:s0
/usr/sbin/sshd                  system_u:system_r:sshd_t:s0-s0:c0.c1023
 
File contexts:
Controlling term:               unconfined_u:object_r:user_devpts_t:s0
/etc/passwd                     system_u:object_r:etc_t:s0
/etc/shadow                     system_u:object_r:shadow_t:s0
/bin/bash                       system_u:object_r:shell_exec_t:s0
/bin/login                      system_u:object_r:login_exec_t:s0
/bin/sh                    system_u:object_r:bin_t:s0 -> system_u:object_r:shell_exec_t:s0
/sbin/agetty                    system_u:object_r:getty_exec_t:s0
/sbin/init                      system_u:object_r:init_exec_t:s0
/sbin/mingetty                  system_u:object_r:getty_exec_t:s0
/usr/sbin/sshd                  system_u:object_r:sshd_exec_t:s0

配置文件:/etc/sysconfig/selinux

[root@localhost ~]# cat /etc/sysconfig/selinux
# 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 these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
  /etc/selinux/config

SELinux兩種級別

strict:嚴格模式,每個進程都會受到SELinux的控制

targeted:寬松模式,僅有限的進程受到SELinux的控制

只監控容易被入侵的進程


查看文件安全標簽(安全上下文)

[root@localhost ~]# ls -Z
-rw-------. root root system_u:object_r:admin_home_t:s0 anaconda-ks.cfg
drwxr-xr-x. root root unconfined_u:object_r:admin_home_t:s0 Desktop
drwxr-xr-x. root root unconfined_u:object_r:admin_home_t:s0 Documents
drwxr-xr-x. root root unconfined_u:object_r:admin_home_t:s0 Downloads
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 file
-rw-r--r--. root root system_u:object_r:admin_home_t:s0 install.log
-rw-r--r--. root root system_u:object_r:admin_home_t:s0 install.log.syslog
drwxr-xr-x. root root unconfined_u:object_r:admin_home_t:s0 Music
drwxr-xr-x. root root unconfined_u:object_r:admin_home_t:s0 Pictures
drwxr-xr-x. root root unconfined_u:object_r:admin_home_t:s0 Public
drwxr-xr-x. root root unconfined_u:object_r:admin_home_t:s0 Templates
drwxr-xr-x. root root unconfined_u:object_r:admin_home_t:s0 Videos

查看進程安全標簽

[root@localhost ~]# ps -aux
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.1  19344  1548 ?        Ss   13:36   0:01 /sbin/init
root          2  0.0  0.0      0     0 ?        S    13:36   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        S    13:36   0:00 [migration/0]
root          4  0.0  0.0      0     0 ?        S    13:36   0:00 [ksoftirqd/0]
root          5  0.0  0.0      0     0 ?        S    13:36   0:00 [stopper/0]
root          6  0.0  0.0      0     0 ?        S    13:36   0:00 [watchdog/0]
root          7  0.0  0.0      0     0 ?        S    13:36   0:00 [migration/1]
root          8  0.0  0.0      0     0 ?        S    13:36   0:00 [stopper/1]
root          9  0.0  0.0      0     0 ?        S    13:36   0:00 [ksoftirqd/1]
root         10  0.0  0.0      0     0 ?        S    13:36   0:00 [watchdog/1]
root         11  0.0  0.0      0     0 ?        S    13:36   0:00 [migration/2]
root         12  0.0  0.0      0     0 ?        S    13:36   0:00 [stopper/2]
root         13  0.0  0.0      0     0 ?        S    13:36   0:00 [ksoftirqd/2]
root         14  0.0  0.0      0     0 ?        S    13:36   0:00 [watchdog/2]
root         15  0.0  0.0      0     0 ?        S    13:36   0:00 [migration/3]
root         16  0.0  0.0      0     0 ?        S    13:36   0:00 [stopper/3]

SELinux為每個文件提供了安全標簽,也為進程提供了安全標簽

 

SELinux規則庫

規則:哪種域能訪問哪種或哪些種類型的文件

 

配置SELinux

SELinux是否啟用

給文件重新打標簽

設定某些布爾型特性

 

SELinux的狀態

enforcing 強制開啟 每個受限的進程都必然受限

permissive 啟用 不會阻止訪問,但會記錄日志

disabled 關閉

 

getenforce 獲取SELinux當前狀態

setenforce 0|1

0:設置為:permissive

1:設置為:enforcing

此設定臨時生效,重啟后無效

 

給文件重新打標簽

chcon  -R  遞歸打標簽

還原文件的默認標簽

restorecon  -R  遞歸還原標簽

 

布爾型規則:

添加/刪除進程或服務本身開啟的功能模塊

getsebool  -a 查看所有布爾值

[root@localhost ~]# getsebool -a
abrt_anon_write --> off
abrt_handle_event --> off
allow_console_login --> on
allow_cvs_read_shadow --> off
allow_daemons_dump_core --> on
allow_daemons_use_tcp_wrapper --> off
allow_daemons_use_tty --> on
allow_domain_fd_use --> on
allow_execheap --> off
allow_execmem --> on
allow_execmod --> on
allow_execstack --> on

getsebool  -p  ftp_home_dir  on/off

將修改結果保存至本地磁盤

日志文件:/var/log/audit/audit.log

 

SELinux優點:

SELinux系統比起通常的Linux系統來,安全性能要高的多,它通過對于用戶,進程權限的最小化,即使受到攻擊,進程或者用戶權限被奪去,也不會對整個系統造成重大影響。  

對訪問的控制徹底化  

特點1MAC(MandatoryAccessControl)―――對訪問的控制徹底化  對于所有的文件,目錄,端口這類的資源的訪問,都可以是基于策略設定的,這些策略是由管理員定制的、一般用戶是沒有權限更改的。  

對于進程只賦予最小的權限  

特點2TETypeEnforcement)―――對于進程只賦予最小的權限  Te概念在SELinux里非常的重要。它的特點是對所有的文件都賦予一個叫type的文件類型標簽,對于所有的進程也賦予各自的一個叫domain的標簽。Domain標簽能夠執行的操作也是由accessvector在策略里定好的。  

我們熟悉的apache服務器,httpd進程只能在httpd_t里運行,這個httpd_tdomain能執行的操作,比如能讀網頁內容文件賦予httpd_sys_content_t,密碼文件賦予shadow_t,TCP80端口賦予http_port_t等等。如果在accessvector里我們不允許http_t來對http_port_t進行操作的話,Apache啟動都啟動不了。反過來說,我們只允許80端口,只允許讀取被標為httpd_sys_content_t的文件,httpd_t就不能用別的端口,也不能更改那些被標為httpd_sys_content_t的文件(readonly)。  

防止權限升級  

特點3domain遷移――防止權限升級  

在用戶環境里運行點對點下載軟件azureus,你當前的domainfu_t,但是,你考慮到安全問題,你打算讓他在azureus_t里運行,你要是在terminal里用命令啟動azureus的話,它的進程的domain就會默認繼承你實行的shellfu_t。  

有了domain遷移的話,我們就可以讓azureus在我們指定的azureus_t里運行,在安全上面,這種做法更可取,它不會影響到你的fu_t。

對于用戶只賦予最小的權限

特點4RBACrolebaseaccesscontrol)―――――對于用戶只賦予最小的權限  對于用戶來說,被劃分成一些ROLE,即使是ROOT用戶,你要是不在sysadm_r里,也還是不能實行sysadm_t管理操作的。因為,那些ROLE可以執行那些domain也是在策略里設定的。ROLE也是可以遷移的,但是也只能按策略規定的遷移。

 

SELinux缺點:

雖然Linux比起Windows來說,它的可靠性,穩定定要好得多,但是他也是和其他的UNIX一樣,有以下這些不足之處。  

存在特權用戶root  

任何人只要得到root的權限,對于整個系統都可以為所欲為。這一點Windows也一樣。  

對于文件的訪問權的劃分不夠細  

linux系統里,對于文件的操作,只有「所有者」,「所有組」,「其他」這3類的劃分。  

對于「其他」這一類里的用戶再細細的劃分的話就沒有辦法了。  

SUID程序的權限升級  

如果設置了SUID權限的程序有了漏洞的話,很容易被攻擊者所利用。   DAC(DiscretionaryAccessControl)問題  

文件目錄的所有者可以對文件進行所有的操作,這給系統整體的管理帶來不便。  

對于以上這些的不足,防火墻,入侵檢測系統都是無能為力的。  

在這種背景下,對于訪問權限大幅強化的OSSELinux來說,它的魅力的無窮的。

 

       由于本人對于SELinux理解有限,也是參考了大量文檔后所寫的此文章,若果有對SELinux有深入了解的朋友可以一起交流,或者本文有理解有誤的地方,歡迎留言指出

 

原創文章,作者:Linux.rookie,如若轉載,請注明出處:http://www.www58058.com/76648

(0)
Linux.rookieLinux.rookie
上一篇 2017-05-21
下一篇 2017-05-21

相關推薦

  • DNS服務器搭建示例

    DNS服務器搭建示例 負責解析magedu.com域名,能夠對一些主機名進行正向解析和逆向解析 配置主配置文件 [root@slave1 etc]# vim /etc/named.conf options { listen-on port 53 { 192.168.91.132; }; // listen-on-v6 port 53 { ::1; }; di…

    2017-09-16
  • 文件查找與解壓縮

    文件查找: which 查找可以執行文件,只搜索$PATH里的目錄 $ which ls /bin/ls which只搜索$PATH里的目錄,如果搜索當前目錄的文件是沒有任何結果的 $ ls -l 總用量 0 -rw-rw-r– 1 huanghao huanghao 0 3月 1 12:24 a.txt $ which a.txt wherei…

    Linux干貨 2017-08-04
  • N22-妙手-第八周課程練習

    1、請描述網橋、集線器、二層交換機、三層交換機、路由器的功能、使用場景與區別。     網橋:         一種網絡設備,負責網絡橋接,將網絡的多個網段在數據鏈路層連接起來。     集線器: &…

    Linux干貨 2016-11-07
  • 知識提煉之0714

    history          history[-c] [-d offset] [n]          history-anrw [filename]    &nbs…

    Linux干貨 2017-07-14
  • N26-第三周作業

    N26-第三周作業 一、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。 [root@promote ~]# who root     tty1         2017-03-10 22:36 lxk…

    Linux干貨 2017-03-11
  • 17周博客作業 LVS+nginx+php(nginx與php分離)

    1、結合圖形描述LVS的工作原理; LVS是一種用軟件實現的LB集群; 作用于傳輸層; LVS工作原理:通過Director調度把用戶請求按照分配算法分配給后端的Real server,然后由后端Real server響應客戶請求; lvs模式分為三種。 lvs-nat: 多目標的DNAT:通過將請求報文的目標地址和目標端口修改為挑選出某…

    Linux干貨 2016-11-07
欧美性久久久久