M25_第八周:SElinux

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 永久生效,改策略同時改了數據庫(當前狀態)
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,967評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,273評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,870評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,742評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,527評論 6 407
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,010評論 1 322
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,108評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,250評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,769評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,656評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,853評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,371評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,103評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,472評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,717評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,487評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,815評論 2 372

推薦閱讀更多精彩內容

  • 歷史 Android的安全模型是基于一部分應用程序沙箱(sandbox)的概念, 每個應用程序都運行在自己的...
    惡魔殿下_HIM閱讀 34,773評論 4 24
  • 1、第八章 Samba服務器2、第八章 NFS服務器3、第十章 Linux下DNS服務器配站點,域名解析概念命令:...
    哈熝少主閱讀 3,750評論 0 10
  • SELinux的基本概念設置SELinux 一、SELinux的基本概念 (一)定義: SELinux:Secur...
    哈嘍別樣閱讀 569評論 0 1
  • 一個程序被加載到內存當中運行,那么在內存內的那個數據就被稱為進程(process)。進程是操作系統上非常重要的概念...
    Zhang21閱讀 1,982評論 0 12
  • 1.SELinux: Secure Enhanced Linux ? DAC:Discretionary Acce...
    尛尛大尹閱讀 715評論 0 1