天天看點

淺析 SELinux

為什麼有的時候我們非要把SELinux=disabled設定成這樣

什麼是SELinux?(通路控制體系,實作系統安全性達到軍方級别)

怎麼用呢?

[root@hack1 ~]# cat /etc/redhat-release 

CentOS release 6.6 (Final)

[root@hack1 ~]# uname -a

Linux hack1 2.6.32-504.12.2.el6.i686 #1 SMP Wed Mar 11 19:05:53 UTC 2015 i686 i686 i386 GNU/Linux

[root@hack1 ~]# 這是我的系統

[root@hack1 selinux]# ls

config  restorecond.conf  restorecond_user.conf  semanage.conf  targeted

[root@hack1 selinux]# pwd

/etc/selinux

[root@hack1 selinux]# cat 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 these two values:

#     targeted - Targeted processes are protected,

#     mls - Multi Level Security protection.

SELINUXTYPE=targeted 

[root@hack1 selinux]#

現在解釋目錄在/etc/selinux下,配置檔案config

SELINUX可以有三個參數

enforcing級别:Linux下selinux所設定的安全政策都會被啟用.所有與selinux安全政策有關的服務或者程式都會被政策阻止.

permissive級别:Linux下selinux所設定的安全政策都會被啟動,但是所有與selinux安全政策有關的服務或者程式不會被政策組織,但是會收到警告.

disabled級别:關閉selinux,相當于系統沒有安裝selinux一樣.

怎麼實作政策呢?

常用的指令

[root@hack1 selinux]# getsebool -a #檢視系統中所有“開關形式”的規則

abrt_anon_write --> off

abrt_handle_event --> off

allow_console_login --> on

allow_cvs_read_shadow --> off

。。。。。。

[root@hack1 selinux]# getenforce #檢視selinux 的status

Enforcing

[root@hack1 selinux]#setenforce 0/1        #設定selinux的status(臨時生效)

ls -Z                  #檢視檔案的标簽

[root@hack1 selinux]# ls -Z 可以看出不同的标簽

-rw-r--r--. root root system_u:object_r:selinux_config_t:s0 config

-rw-r--r--. root root system_u:object_r:selinux_config_t:s0 restorecond.conf

-rw-r--r--. root root system_u:object_r:selinux_config_t:s0 restorecond_user.conf

[root@hack1 selinux]# ls -Z /root

-rw-------. root root system_u:object_r:admin_home_t:s0 anaconda-ks.cfg

-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

[root@hack1 selinux]# ls -Z /home/lxf/

-rw-rw-r--. lxf lxf unconfined_u:object_r:user_home_t:s0 c.log

[root@hack1 selinux]# 

ps auxZ             #檢視程序的标簽(使用者:角色:類型)

ps auxZ | grep httpd #檢視httpd的标簽

ls -dZ /var/www/html #檢視此目錄的标簽(連結裡邊的案例就是通過copy不同地方的檔案,不能實作同樣的通路,原因就來源于selinux

                           #上面程序和目錄被selinux定義的類型相同,是以能被通路

 chcon  -t             #改檔案類型

            -R             #該目錄類型(遞歸修改)

           --reference

 chcon -R --reference=/var/www/html /www #參照/www更改/var/www/html檔案類型

 chcon -t default_t /www/index.html   #更改檔案類型為default_t

 restorecon -R         #恢複目錄的檔案原有預設屬性

                  -F         #強制恢複

                  -v         #顯示資訊

restorecon -R -v -F /www              #強制恢複/www目錄的預設屬性

semanage  fcontext                    #更改檔案安全上下文(永久性更改)

getsebool -a | grep httpd  #檢視httpd服務能用的“開關”

如何開啟/關閉這些規則:

 setsebool httpd_enable_cgi=1     #開啟

 setsebool httpd_enable_cgi=1     #關閉

[root@hack1 selinux]# getsebool -a |grep httpd 通過這個應該可以知道為什麼關閉selinux

allow_httpd_anon_write --> off

allow_httpd_mod_auth_ntlm_winbind --> off

allow_httpd_mod_auth_pam --> off

allow_httpd_sys_script_anon_write --> off

httpd_builtin_scripting --> on

httpd_can_check_spam --> off

httpd_can_network_connect --> off

httpd_can_network_connect_cobbler --> off

httpd_can_network_connect_db --> off

httpd_can_network_memcache --> off

httpd_can_network_relay --> off

httpd_can_sendmail --> off

httpd_dbus_avahi --> on

httpd_dbus_sssd --> off

httpd_enable_cgi --> on

httpd_enable_ftp_server --> off

httpd_enable_homedirs --> off

httpd_execmem --> off

httpd_manage_ipa --> off

httpd_read_user_content --> off

httpd_run_preupgrade --> off

httpd_run_stickshift --> off

httpd_serve_cobbler_files --> off

httpd_setrlimit --> off

httpd_ssi_exec --> off

httpd_tmp_exec --> off

httpd_tty_comm --> on

httpd_unified --> on

httpd_use_cifs --> off

httpd_use_fusefs --> off

httpd_use_gpg --> off

httpd_use_nfs --> off

httpd_use_openstack --> off

httpd_verify_dns --> off

擴充閱讀

SE Linux 可以為你的系統提供較棒的安全防護。 使用者能被配置設定預先定義好的角色,以便他們不能存取檔案或者通路他們不擁有的程式。 這可不是簡單的 " chmod 777" 同等物操作。 這在角色, 或他所在的安全上下文已經限制接觸的檔案和其他的資源的使用者定義中是不同于一般的 Unix 許可權限的,除了在一種比較受限制的流行之外。 帶一個使用者的.rhosts 檔案在一個一般的 Unix 系統上申請。 如果他們使它成為任何人可寫入 , 那麼任何能登入的人都可以作危險的操作。 在 SE Linux 之下,你能控制其它使用者是否有能力改變他們的.rhosts檔案, 以及阻止其他的人寫入 , 就算擁有者已經使它成為任何人可寫入。 

一個通常的疑問是 SE Linux 的權限設定如何與标準的 Unix 的權限設定共存。 當你做特定的操作的時候, Unix 權限首先被檢查。 如果他們允許你的操作 , 那麼然後, SE Linux 将會檢查并且允許或拒絕使用者的使用。 但是如果 Unix 許可不讓你做某事,在那裡的運作的操作被禁止和 SE Linux 檢查沒關系。  另外的一個例子是,如果有一個設定了SUID的可執行檔案,如/usr/bin/passwd 他可以運作指令chmod 666 /etc/shadow,SE Linux 會阻止任何人非法的這樣設定檔案。   

2.2 術語 

接下來的術語将在本文當中經常出現, 也是來自SE Linux的基本概念. It is somewhat tricky to define one word without including the other terms so I realise my definitions include things that need defining(譯者注:這句實在不敢亂譯,sorry。不過不耽誤學習;-)) )  

2.2.1 (身份)identity 

在 SE Linux 中,身份的概念不同于傳統的Unix uid (user id). 它們可以共存于一個系統, 但卻是十分不同的概念。 在SE Linux中的身份是安全上下文的一部分,它會影響哪個域可以進入,也就是本質上的可以被執行。一個SE Linux 的身份(identity)會跟标準的Unix登入名有很相似的文本表示 (大部分情況下它們是這樣), 無論如何,了解它們是兩個完全不同的概念是很重要的。 運作su 指令不會改變SE Linux中的身份(identity)。(譯者注:我在紅帽系統中做的實驗卻不是這樣,不過這無所謂,可能紅帽系統的政策設定不同,我目前還沒來得及研究具體是什麼問題,隻是猜測。) 

 6

Getting Started with SE Linux HOWTO:the new SE Linux 

舉例:  

一個無特權使用者 faye 運作 id 指令 (在啟動SE Linux的情況下) 可以看到使用者的安全上下文:  

context=faye:user_r:user_t 

安全上下文中的身份部分就是 "faye"。 現在, 如果 faye su切換成 root 再運作id, 他将發現安全上下文仍然是: 

身份保持相同, 跟沒切換到root時一樣。, 不管怎樣,如果faye 身份被允許進入sysadm_r 角色并轉換成了sysadm_r (這裡可以使用newrole -r 指令),再運作id 指令,他将看到:  

context=faye:sysadm_r:sysadm_t 

身份字段保持一樣但是角色和域(第二和第三字段)的字段已經變了。這樣保持身份的方式是使用者職責所必需的。身份将影響系統決定哪個角色和域可以被什麼身份所使用,這将對系統安全期決定性的作用。  

2.2.2 域 

所有程序都在域中運作。域直接決定了程序的通路。 域基本上是一個程序允許做的操作的清單, 或者說它決定了一個程序可以對哪些類型進行操作。 域就好像一個标準UNIX的uid的概念。 假設一個屬于root使用者的可執行程式被設定了setuid。 在這個系統上的任何使用者,隻要可以執行這個程式,它就有可能獲得root的權限。這是一個很大的安全漏洞。 再有SE Linux的系統上, 如果一個正在執行的程序想要轉換進入特權域執行時,如果這個程序的角色被設定成不允許進入特權與的話, 這個程序就不能執行。  

常見的例子是sysadm_t是系統管理域, user_t 是無特權使用者域。 Init運作在init_t 域, named 運作在 named_t 域。 

2.2.3 類型 

類型配置設定給一個對象并決定誰可以通路這個對象。 它的定義和域基本相同, 不同就是域是對程序的應用而類型是配置設定給目錄,檔案,和套接字的。  

2.2.4 角色 

角色決定了那些域可以使用。 有關哪些與可以被哪些角色使用可以預先定義在政策的配置檔案裡。如果一個政策資料庫中定義了一個角色不可以使用一個域, 它将被拒絕。 

 7

本文轉自 aklaus 51CTO部落格,原文連結:http://blog.51cto.com/aklaus/1666305

繼續閱讀