SElinux簡介:
SELinux(Secure Enhanced Linux)安全加強型LINUX; ,是一個安全體系結構。它通過LSM(Linux Security Modules)框架被集成到Linux Kernel 2.6.x中。它是NSA (United States National Security Agency)和SELinux社區的聯合項目。SELinux提供了一種靈活的強制訪問控制(MAC)系統,且內嵌於Linux Kernel中。
操作系統有兩類訪問控制:
DAC:自主訪問控制 Discretionary Access Control
標準的linux是DAC
MAC:強制訪問控制 Mandatory Access Control
SElinux 爲linux引入MAC
selinux的運行機制:
一個進程對另外一個被操作資源的操作功能(受限於文件被操作資源和操作資源是否在同一個範圍)
subject operation object
subject: 進程
operation:操作
object:被存取的項目。例如進程 文件。
SElinux規則庫:
用來哪種域能訪問哪種或哪些種類型內的文件
SElinux工作機制:(判斷程序是否能訪問文件)
1.根據安全上下文判斷程序是否可以訪問文件
2.通過selinux規則庫--->判斷程序所屬域是否能訪問此文件的類型;如果接受就訪問,否則就算違反了安全策略,並將此次操作寫入日誌中。
注意:如果在SElinux的規則庫中沒有對某進程訪問某文件進行允許的話,那麼是被禁止的!
selinux的配置文件:
/etc/sysconfig/cofig---->/etc/selinux/config.
一、SELINUX=enforcing|permissive|disabled
enforcing:強制,每個受限的進程必然受限。不受限的未必受限
permissive:啓用授權,每個受限的進程違規操作不會被禁止,但會被記錄於審計日誌中
disabled:關閉;所有進程任意訪問。
注意:disable狀態更改其他狀態必須重啓系統,讓內核做重新打標籤
二、SELINUXTYPE=targeted|strict SELinux的工作策略
strict:每個進程都受到selinux的控制
targeted:僅有限個進程受到selinux控制
(只監控容易被***的進程)
日誌文件:記錄違反策略的行爲
/var/log/audit/audit.log
SELinux的每個文件提供了安全標籤,也爲進程提供了安全標籤
user:role:type
user:selinux的user
role:內核角色
type:類型
相關的命令:
ls -Z: 檢查文件、目錄的安全上下文
[root@localhost ~]# ll -Z -rw-------. root root system_u:object_r:admin_home_t:s0 anaconda-ks.cfg drwxr-xr-x. root root system_u:object_r:admin_home_t:s0 Desktop drwxr-xr-x. root root system_u:object_r:admin_home_t:s0 Documents
ps auZ:查看進程的安全上下文
-rw-r--r--. root root system_u:object_r:admin_home_t:s0 zsh-5.0.2-7.el7_1.2.x86_64.rpm [root@localhost ~]# ps auZ LABEL USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND system_u:system_r:getty_t:s0-s0:c0.c1023 root 1217 0.0 0.0 110028 840 tty1 Ss+ 11:37 0:00 /sbin/agetty --noclear tty1 linux unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 root 1325 0.0 0.3 116684 3496 pts/0 Ss 11:38 0:00 -bash unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 root 1925 0.0 0.1 144124 1724 pts/0 R+ 14:25 0:00 ps auZ
getenforce:獲取selinux當前狀態
[root@localhost ~]# getenforce Enforcing
setenforce 0|1 修改當前selinux狀態
0:設置爲permissive
1: 設置爲enforcing
注意:修改只能是當前狀態處於二者之一,而不能是disable,如果是disable只能修改配置文件。 僅當前系統有效。
chcon:給文件重新達標
chcon [OPTION]... CONTEXT FILE...
chcon [OPTION]... [-u USER] [-r ROLE] [-l RANGE] [-t TYPE] FILE...
chcon [OPTION]... --reference=RFILE FILE...
常用:
chcon [-R] -t type filename
-R:遞歸
-t type:類型
[root@localhost ~]# ll -Z 111.sh -rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 111.sh [root@localhost ~]# chcon -t root_t 111.sh [root@localhost ~]# ll -Z 111.sh -rw-r--r--. root root unconfined_u:object_r:root_t:s0 111.sh
getsebool:查看布爾型設置
getsebool [-a] [boolean] -a boolean
-a:程序名稱
boolean:程序名稱
[root@localhost ~]# getsebool -a abrt_anon_write --> off abrt_handle_event --> off abrt_upload_watch_anon_write --> on antivirus_can_scan_system --> off
[root@localhost ~]# getenforce boinc_execmem Enforcing
setsebool:設置布爾型設置
setsebool [ -P] boolean value | bool1=val1 bool2=val2
-P表示永久有效,沒有的話只是表示當次
setsebool -p name on|off
[root@localhost ~]# getsebool ssh_keysign ssh_keysign --> off [root@localhost ~]# setsebool ssh_keysign 1 [root@localhost ~]# getsebool ssh_keysign ssh_keysign --> on