SELinux介紹
SELinux: Secure Enhanced Linux, 是美國國家安全局(NSA=The National Security Agency)和SCC(SecureComputing Corporation)開發的 Linux的一個強制訪問控制的安全模塊。2000年以GNU GPL發布,Linux內核2.6版本后集成在內核中 1. ?DAC:Discretionary Access Control自由訪問控制 2. ?MAC:Mandatory Access Control 強制訪問控制?
DAC環境下進程是無束縛的
MAC環境下策略的規則決定控制的嚴格程度
MAC環境下進程可以被限制的
策略被用來定義被限制的進程能夠使用那些資源(文件和端口)? 默認情況下,沒有被明確允許的行為將被拒絕
SELinux工作類型? SELinux有四種工作類型:
strict: centos5,每個進程都受到selinux的控制
targeted: 用來保護常見的網絡服務,僅有限進程受到 selinux控制,只監控容易被入侵的進程,centos4只保護13 個服務,centos5保護88個服務
minimum:centos7,修改的targeted,只對選擇的網絡服務
mls:提供MLS(多級安全)機制的安全性? targeted為默認類型,minimum和mls穩定性不足,未加以應 用,strict已不再使用
查看Selinux狀態
[ root@localhost ~ ]#sestatus
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: enforcing
Policy MLS status: enabled
Policy deny_unknown status: allowed
Max kernel policy version: 28
//或者getenforce
SELinux安全上下文
傳統Linux,一切皆文件,由用戶,組,權限控制訪問
在SELinux中,一切皆對象(object),由存放在inode的擴展屬性域的安全元素所控制其訪問
所有文件和端口資源和進程都具備安全標簽:安全上下文(security context)
安全上下文有五個元素組成
: user:role:type:sensitivity:category
user_u:object_r:tmp_t:s0:c0
實際上下文:存放在文件系統中,ls –Z;ps –
Z 期望(默認)上下文:存放在二進制的SELinux策略庫(映 射目錄和期望安全上下文)中
semanage fcontext –l
[ root@localhost ~]#ll -Z
-rwxr--r-- root root ? 11_copycmd.sh
-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
-rwxr-xr-x. root root unconfined_u:object_r:admin_home_t:s0 reset.sh
//查看文件的擴展選項,-rw-------. :最后帶.的說明設置了selinux策略
——————————————————————————————————————————————————————————————————————————————————————
[ root@localhost ~ ]#ps auxZ
LABEL USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
system_u:system_r:init_t:s0 root 1 0.1 1.3 129268 6616 ? Ss 09:34 0:01 /usr/lib/systemd/systemd --switched-root --system --deserializ
system_u:system_r:kernel_t:s0 root 2 0.0 0.0 0 0 ? S 09:34 0:00 [kthreadd]
system_u:system_r:kernel_t:s0 root 3 0.0 0.0 0 0 ? S 09:34 0:00 [ksoftirqd/0]
system_u:system_r:kernel_t:s0 root 5 0.0 0.0 0 0 ? S< 09:34 0:00 [kworker/0:0H]
system_u:system_r:kernel_t:s0 root 7 0.0 0.0 0 0 ? S 09:34 0:00 [migration/0]
system_u:system_r:kernel_t:s0 root 8 0.0 0.0 0 0 ? S 09:34 0:00 [rcu_bh]
system_u:system_r:kernel_t:s0 root 9 0.0 0.0 0 0 ? R 09:34 0:00 [rcu_sched]
system_u:system_r:kernel_t:s0 root 10 0.0 0.0 0 0 ? S 09:34 0:00 [watchdog/0]
system_u:system_r:kernel_t:s0 root 12 0.0 0.0 0 0 ? S 09:34 0:00 [kdevtmpfs]
system_u:system_r:kernel_t:s0 root 13 0.0 0.0 0 0 ? S< 09:34 0:00 [netns]
//查看進程的selinux策略
安裝semanage,查看策略信息
[ root@localhost ~]#yum -y install policycoreutils-python
//安裝semanage
[ root@localhost ~]#semanage fcontext -l
//列出系統中已知的這些文件他應該有的安全上下文,從selinux數據庫中查找的。
[ root@localhost ~]#semanage fcontext -l|grep "/var/log/messages"
/var/log/messages[^/]* all files system_u:object_r:var_log_t:s0
//數據中的記錄的安全上下文和文件實際的安全上下文如果不一樣就不可用,要求是必須匹配的
//數據庫存放路徑:cd /etc/selinux/targeted/
五個安全元素:
User:指示登錄系統的用戶類型,如root,user_u,system_u,多數本地進程都屬于自由(unconfined(沒有定義的沒有規則的)進程
Role:定義文件,進程和用戶的用途:文件:object_r,進程和用戶:system_r
Type:指定數據類型,規則中定義何種進程類型訪問何種文件Target策略基于type實現,多服務共用:public_content_t
Sensitivity:限制訪問的需要,由組織定義的分層安全級別,如unclassified, secret,top,secret,一個對象有且只有一個sensitivity,分0-15級,s0最低,Target策略默認使用s0
Category:對于特定組織劃分不分層的分類,如FBI Secret,NSA secret, 一個對象可以有多個categroy, c0-c1023共1024個分類, Target 策略不使用category
SELinux策略
對象(object):所有可以讀取的對象,包括文件、目錄和進程 ,端口等?
主體:進程稱為主體(subject)
SELinux中對所有的文件都賦予一個type的文件類型標簽,對 于所有的進程也賦予各自的一個domain的標簽。domain標簽 能夠執行的操作由安全策略里定義
當一個subject試圖訪問一個object,Kernel中的策略執行服 務器將檢查AVC (訪問矢量緩存Access Vector Cache), 在 AVC中,subject和object的權限被緩存(cached),查找“應 用+文件”的安全環境。然后根據查詢結果允許或拒絕訪問?
安全策略:定義主體讀取對象的規則數據庫,規則中記錄了哪 個類型的主體使用哪個方法讀取哪一個對象是允許還是拒絕的 ,并且定義了哪種行為是充許或拒絕
設置SELinux
配置SELinux:
SELinux是否啟用
給文件重新打安全標簽
給端口設置安全標簽
某些操作的布爾型開關
SELinux的日志管理?
SELinux的狀態:
enforcing: 強制,每個受限的進程都必然受限
permissive: 允許,每個受限的進程違規操作不會被禁止 ,
但會被記錄于審計日志
disabled: 禁用
[ root@localhost /]#ls /etc/selinux/config /etc/sysconfig/selinux -l
-rw-r--r-- 1 root root 458 Sep 2 23:33 /etc/selinux/config
lrwxrwxrwx. 1 root root 17 Aug 16 17:18 /etc/sysconfig/selinux -> ../selinux/config
//兩個都是selinux配置文件,只不過一個是軟連接一個是源文件而已
[ root@localhost /]# vim /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.
//selinux啟用起來他的功能會發揮出來,他的策略里面禁止的是不允許實施的
# permissive - SELinux prints warnings instead of enforcing.
//關閉selinux,但是會記錄信息在日志里面(/var/log/messages)
# disabled - No SELinux policy is loaded.
//禁用
SELINUX=enforcing
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
//修改配置文件:SETLINUX=disabled 關閉后要重啟才能生效
//查看狀態selinux,因為修改后必須重啟才行,所有下面還顯示著開啟:
[ root@localhost /etc ]#getenforce
Enforcing
——————————————————————————————————————————————————————————————————————————————————————
[ root@localhost /etc ]#sestatus
SELinux status: enabled :真實狀態(修改了重啟生效)
SELinuxfs mount: /sys/fs/selinux :掛載目錄??
SELinux root directory: /etc/selinux :文件目錄
Loaded policy name: targeted :工作類型
Current mode: enforcing :重啟前狀態
Mode from config file: disabled
Policy MLS status: enabled :真實狀態(修改了重啟生效)
Policy deny_unknown status: allowed
Max kernel policy version: 28
//重啟后,之前系統文件的selinux的5個元素都還存在,但是新創建的文件就沒有setlinux的元素了
配置SELinux: SELinux是否啟用
給文件重新打安全標簽 給端口設置安全標簽 設定某些操作的布爾型開關 SELinux的日志管理
SELinux的狀態:
enforcing: 強制,每個受限的進程都必然受限
permissive: 允許,每個受限的進程違規操作不會被禁止 ,但會被記錄于審計日志
disabled: 禁用
測試日志信息
相關命令:
getenforce: 獲取selinux當前狀態 sestatus :查看selinux狀態 setenforce 0|1
0: 設置為permissive
1: 設置為enforcing ? 配置文件:
/boot/grub/grub.conf 使用selinux=0禁用SELinux
/etc/selinux/config /etc/sysconfig/selinux SELINUX={disabled|enforcing|permissive}
[ root@localhost ~]# logger "this is a telog"
[ root@localhost ~]#查看 /var/log/messages
Sep 3 01:53:41 localhost kernel: usb 2-2.1: SerialNumber: 000650268328
Sep 3 01:53:41 localhost kernel: usb 2-2.1: configuration #1 chosen from 1 choice
Sep 3 02:00:24 localhost root: this is a telog
//想日志服務進程發送上面的一句話
[ root@localhost ~]#systemctl status rsyslog
//日志服務進程
修改文件selinux元素
之前屬性:
[ root@localhost ~]#ls -Z messages
-rw-------. root root unconfined_u:object_r:admin_home_t:s0 messages
[ root@localhost ~]#chcon -t var_log_t /var/log/messages
//修改messages的最后一個屬性
[ root@localhost ~]#logger "hello word"
[ root@localhost ~]#cat /var/log/messages
卻發現沒有,解決方法:
[ root@localhost ~]#service rsyslogd restart
//重啟就好了
復制httpd的index.html文件到別的目錄然后覆蓋回來屬性
[ root@localhost ~]#cp /var/log/html/index.html /root/
[ root@localhost ~]#mv /root/index.html /var/log/html/
//覆蓋后他的屬性會變:
覆蓋前:
[ root@localhost /var/www/html]#ls -Z
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html
[ root@localhost /var/www/html]#curl 172.18.22.77
<h1>hello word</h1>
覆蓋后;
[ root@localhost /]#cp /var/log/html/index.html /root/
admin_home_t:mv /root/index.html /var/log/html/
[ root@localhost /var/www/html]#cp index.html /root
admin_home_t:ls -Z index.html
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 index.html
//從之前的httpd_sys_content_t變成了admin_home_t
[ root@localhost ~]#curl 172.18.22.77
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<head>
<title>Apache HTTP Server Test Page powered by CentOS</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
body {
background-color:.....
//就不是我們剛剛的頁面了,恢復方法
[ root@localhost ~]#semanage fcontext -l |grep "/var/www/"
/var/www/(/.*)? all files system_u:object_r:httpd_sys_content_t:s0
[ root@localhost /var/www/html]#restorecon index.html
//從數據庫中恢復,也可以恢復目錄,restorecon -R /var/www
<h1>hello word</h1>
//成功恢復
創建web目錄并且創建文件/software/web/index.html
給web目錄添加權限到數據庫
[ root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t '/software/web(/.*)?'
//把web這個目錄添加到數據庫,并且元素設置為httpd_sys_content_t
更改端口httpd端口:
[ root@localhost ~]# vim /etc/httpd/conf/httpd.conf :找到Listen 80,修改成想改的端口,這里我修改成2290
[ root@localhost ~]#semanage port -a -t http_port_t -p tcp 2290
//在數據庫中添加了2290端口,來查看
[ root@localhost ~]#[ root@localhost ~]#semanage port -l|grep http_port_t
http_port_t tcp 2290, 80, 81, 443, 488, 8008, 8009, 8443, 9000
[ root@localhost ~]#service httpd restart
//重啟httpd服務,訪問即可:172.18.22.77:2290
[ root@localhost ~]#semange port -d -t http_port_t -p tcp 2290
//從數據庫中刪除剛剛添加的tcp記錄,記錄為2290
更改ssh端口:
[ root@localhost ~]# vim /etc/ssh/sshd_config
Port 22修改成Port 998
[ root@localhost ~]# semanage port -a -t ssh_port_t -p tcp 998
//添加我們修改ssh端口998端口到數據庫中
[ root@localhost ~]#service sshd restart
//重啟ssh服務器,當前終端不會斷,我們重新連接的時候就需要輸入端口了,
[ root@localhost ~]# ssh root@172.18.22.77 -p 998
使用semanage修改端口:
[ root@localhost ~]#semanage port -m -t http_port_t -p tcp 999
//在數據庫中添加http_port_t的端口999
SELinux布爾值? 布爾型規則:
查看bool命令:
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
//查看所有布爾值,
SELinux布爾值查看和修改
[ root@localhost ~]#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
allow_ftpd_anon_write --> off
allow_ftpd_full_access --> off
allow_ftpd_use_cifs --> off
allow_ftpd_use_nfs --> off
allow_gssd_read_tmp --> on
allow_guest_exec_content --> off
allow_httpd_anon_write --> off
[ root@localhost ~]#semanage boolean -l
SELinux boolean State Default Description
State (當前狀態,內存中的狀態,正在生效的狀態)
Default (默認狀態,數據庫中存在磁盤里策略里的狀態)
ftp_home_dir (off , off) Allow ftp to read and write files in the user home directories
smartmon_3ware (off , off) Enable additional permissions needed to support devices on 3ware controllers.
xdm_sysadm_login (off , off) Allow xdm logins as sysadm
查看所有布爾值詳細信息,第二種詳細點
修改布爾值:
setsebool 要修改的名稱=on/off
[ root@localhost ~]#setsebool ftp_home_dir=1 :臨時生效
[ root@localhost ~]#getsebool -a|grep ftp_home_dir
ftp_home_dir --> on
//之前是關閉狀態(off),只是臨時生效
[ root@localhost ~]#setsebool -P ftp_home_dir=1
//-p 永久生效,改策略同時改了數據庫(當前狀態)