SELinux簡介:
SELinux:Secure Enahanced Linux 安全增強的Linux
SELinux是2.6版本的Linux內核中提供的強制訪問控制(MAC)系統。對于目前可用的Linux安全模塊來說,SELinux是功能最全面,而且測試最充分的,它是在20年的MAC研究基礎上建立的。SELinux在類型強制服務器中合并了多級安全性或一種可選的多類策略,并采用了基于角色的訪問控制概念。
大部分使用SELinux的人使用的都是SELinux就緒的發行版,例如Fedora、RedHatEnterpriseLinux(RHEL)、Debian或Centos。它們都是在內核中啟用SELinux的,并且提供一個可定制的安全策略,還提供很多用戶層的庫和工具,它們都可以使用SELinux的功能。
SELinux是一種基于域–類型模型(domain-type)的強制訪問控制(MAC)安全系統,它由NSA編寫并設計成內核模塊包含到內核中,相應的某些安全相關的應用也被打了SELinux的補丁,最后還有一個相應的安全策略。
眾所周知,標準的UNⅨ安全模型是“任意的訪問控制“DAC。就是說,任何程序對其資源享有完全的控制權。假設某個程序打算把含有潛在重要信息的文件扔到/tmp目錄下,那么在DAC情況下沒人能阻止他!
而MAC情況下的安全策略完全控制著對所有資源的訪問。這是MAC和DAC本質的區別。
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系統來,安全性能要高的多,它通過對于用戶,進程權限的最小化,即使受到攻擊,進程或者用戶權限被奪去,也不會對整個系統造成重大影響。
對訪問的控制徹底化
特點1:MAC(MandatoryAccessControl)―――對訪問的控制徹底化 對于所有的文件,目錄,端口這類的資源的訪問,都可以是基于策略設定的,這些策略是由管理員定制的、一般用戶是沒有權限更改的。
對于進程只賦予最小的權限
特點2:TE(TypeEnforcement)―――對于進程只賦予最小的權限 Te概念在SELinux里非常的重要。它的特點是對所有的文件都賦予一個叫type的文件類型標簽,對于所有的進程也賦予各自的一個叫domain的標簽。Domain標簽能夠執行的操作也是由accessvector在策略里定好的。
我們熟悉的apache服務器,httpd進程只能在httpd_t里運行,這個httpd_t的domain能執行的操作,比如能讀網頁內容文件賦予httpd_sys_content_t,密碼文件賦予shadow_t,TCP的80端口賦予http_port_t等等。如果在accessvector里我們不允許http_t來對http_port_t進行操作的話,Apache啟動都啟動不了。反過來說,我們只允許80端口,只允許讀取被標為httpd_sys_content_t的文件,httpd_t就不能用別的端口,也不能更改那些被標為httpd_sys_content_t的文件(readonly)。
防止權限升級
特點3:domain遷移――防止權限升級
在用戶環境里運行點對點下載軟件azureus,你當前的domain是fu_t,但是,你考慮到安全問題,你打算讓他在azureus_t里運行,你要是在terminal里用命令啟動azureus的話,它的進程的domain就會默認繼承你實行的shell的fu_t。
有了domain遷移的話,我們就可以讓azureus在我們指定的azureus_t里運行,在安全上面,這種做法更可取,它不會影響到你的fu_t。
對于用戶只賦予最小的權限
特點4:RBAC(rolebaseaccesscontrol)―――――對于用戶只賦予最小的權限 對于用戶來說,被劃分成一些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