linux--selinux

1、Selinux

        SELinux(Security-Enhanced Linux,安全增強型Linux)是美國國家安全局(NAS)對於強制訪問控制的實現,在這種訪問控制體系的限制下,進程只能訪問哪些在他的任務中所需的文件。SELinux在類型強制服務器中合併了多級安全性或一種可選的多類策略,並採用了基於角色的控制概念。

        在SELinux中定義了許多類型(TYPE),每一個進程、文件、設備等都必須標識他所屬的類型進程只能讀取相同類型的文件,如果沒有相關類型,並且SELinux不允許讀取的時候,則無法讀取文件。SELinux除了約束進程讀取文件的能力之外,還限制進程對設備、網絡聯機、通信端口、跨進程通信的讀取能力,並提供更細緻的讀取控制。

       在SELinux中沒有root這個概念,安全策略是由管理員來定義的,任何軟件都無法取代它。這意味着那些潛在的惡意軟件所能造成的損害可以被控制在最小。一般情況下只有非常注重數據安全的企業級用戶纔會使用SELinux。

2、原理簡介

       當一個subject(如: 一個應用)試圖訪問一個object(如:一個文件),Kernel中的策略執行服務器將檢查AVC (Access Vector Cache), 在AVC中,subject和object的權限被緩存(cached)。如果基於AVC中的數據不能做出決定,則請求安全服務器,安全服務器在一個矩陣中查找“應用+文件”的安全環境。然後根據查詢結果允許或拒絕訪問,拒絕消息細節位於/var/log/messages中。

3、基本概念

策略:

         SELinux策略是用來定義SELinux的讀取規則,比如哪一個安全上下文的進程允許或禁止讀取哪一個對象。Linux會把每一個Linux策略類型的資料存儲在/etc/selinux策略類型名目錄中。

安全上下文:

       安全上下文是一個簡單的、一致的訪問控制屬性,在SELinux中,類型標識符是安全上下文的主要組成部分,由於歷史原因,一個進程的類型通常被稱爲一個域(domain),"域"和"域類型"意思都一樣,我們不必苛刻地去區分或避免使用術語域,通常,我們認爲【域】、【域類型】、【主體類型】和【進程類型】都是同義的,即都是安全上下文中的“TYPE”。

        所有操作系統訪問控制都是以關聯的客體和主體的某種類型的訪問控制屬性爲基礎的。在SELinux中,訪問控制屬性叫做安全上下文。所有客體(文件、進程間通訊通道、套接字、網絡主機等)和主體(進程)都有與其關聯的安全上下文,一個安全上下文由三部分組成:用戶、角色和類型標識符。常常用下面的格式指定或顯示安全上下文:

USER:ROLE:TYPE[LEVEL[:CATEGORY]]

        安全上下文中的用戶和角色標識符除了對強制有一點約束之外對類型強制訪問控制策略沒什麼影響,對於進程,用戶和角色標識符顯得更有意義,因爲它們是用於控制類型和用戶標識符的聯合體,這樣就會與Linux用戶賬號關聯起來;然而,對於客體,用戶和角色標識符幾乎很少使用,爲了規範管理,客體的角色常常是object_r,客體的用戶常常是創建客體的進程的用戶標識符,它們在訪問控制上沒什麼作用。

4、Selinux相關操作

1)selinux狀態查看與配置

[root@VM_0_11_centos ~]# 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=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted


[root@VM_0_11_centos ~]#

SELINUX=disabled

#此項定義selinux狀態。

#enforcing—是強制模式系統受selinux保護。就是你違反了策略,你就無法繼續操作下去#permissive—是提示模式系統不會受到selinux保護,只是收到警告信息。

#permissive就是Selinux有效,但是即使你違反了策略的話它讓你繼續操作,但是把你的違反的內容記錄下來(警告信息)

#disabled—禁用selinux。

[root@VM_0_11_centos ~]# ll /etc/selinux/
total 20
-rw-r--r--. 1 root root  541 Dec 11 08:43 config
drwx------. 2 root root 4096 Dec  8 08:31 final
-rw-r--r--. 1 root root 2321 Oct 30  2018 semanage.conf
drwxr-xr-x. 7 root root 4096 Dec  8 08:31 targeted
drwxr-xr-x. 2 root root 4096 Oct 30  2018 tmp
[root@VM_0_11_centos ~]#

SELINUXTYPE=targeted

#此項定義selinux使用哪個策略模塊保護系統。

targeted:保護常見的網絡服務,是SELinux的默認值

minimum:SELinux最低基本策略

mls:提供符合MLS機制的安全性

2)查詢selinux狀態

[root@VM_0_11_centos ~]# sestatus
SELinux status:                 disabled
[root@VM_0_11_centos ~]# getenforce
Disabled
[root@VM_0_11_centos ~]#

3)設置selinux狀態,0關閉,1打開

        使用setenforce可以臨時在enforcing模式與permissive模式之間切換,切換會被立刻應用於當前系統,計算機重啓後無效,永久修改模式需要修改配置文件。

[root@VM_0_11_centos ~]# setenforce 0
setenforce: SELinux is disabled

4)查看安全上下文(-Z參數)

[root@VM_0_11_centos selinux]# ll -Z
-rw-r--r--. root root system_u:object_r:unlabeled_t:s0 config
drwx------. root root system_u:object_r:selinux_config_t:s0 final
-rw-r--r--. root root system_u:object_r:selinux_config_t:s0 semanage.conf
drwxr-xr-x. root root system_u:object_r:selinux_config_t:s0 targeted
drwxr-xr-x. root root system_u:object_r:selinux_config_t:s0 tmp
[root@VM_0_11_centos selinux]#
[root@VM_0_11_centos selinux]# ps -Z
[root@VM_0_11_centos selinux]# id -Z
id: --context (-Z) works only on an SELinux-enabled kernel
[root@VM_0_11_centos selinux]#

5)修改安全上下文

chcon命令

6)查看策略

#策略與目錄同名
[root@VM_0_11_centos selinux]# ll /etc/selinux/
total 20
-rw-r--r--. 1 root root  541 Dec 11 08:43 config
drwx------. 2 root root 4096 Dec  8 08:31 final
-rw-r--r--. 1 root root 2321 Oct 30  2018 semanage.conf
drwxr-xr-x. 7 root root 4096 Dec  8 08:31 targeted
drwxr-xr-x. 2 root root 4096 Oct 30  2018 tmp
[root@VM_0_11_centos selinux]#
使用什麼策略就安裝對應的策略包
[root@VM_0_11_centos selinux]# yum -y install selinux-policy-minimum
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile


多了一個minimum目錄
[root@VM_0_11_centos selinux]# ll /etc/selinux/
total 24
-rw-r--r--. 1 root root  541 Dec 11 08:43 config
drwx------. 2 root root 4096 Feb 21 20:07 final
drwxr-xr-x  7 root root 4096 Feb 21 20:07 minimum
-rw-r--r--. 1 root root 2321 Oct 30  2018 semanage.conf
drwxr-xr-x. 7 root root 4096 Dec  8 08:31 targeted
drwxr-xr-x. 2 root root 4096 Oct 30  2018 tmp
[root@VM_0_11_centos selinux]#


7)相關日誌信息

       不管SELinux策略是允許還是拒絕資源的訪問請求行爲,都會記錄日誌,也就是AVC(Access Vector Cache)。所有SELinux拒絕的消息都會被記錄進日誌,根據系統中安裝運行的服務進程不同,拒絕日誌消息會被記錄到不同的文件中。  

日誌文件                      進程

/var/log/audit/audit.log           auditd服務開啓

/var/log/messages                auditd服務關閉,rsyslogd服務開啓

/var/log/audit/audit.log,/var/log/messages安裝setroubleshoot相關軟件包autitd與rsyslogd同時開啓

  

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章