Selinux:強制訪問控制的安全模塊,linux內核2.6版本后集成在內核中。
selinux的四種工作類型:
strict:centos5中,每隔進程都受到selinux的控制,策略認識就允許,不認識就拒絕。
targeted:用來保護常見的網絡服務,僅有限進程受到selinux控制,只監控容易被入侵的進程,rhel4只保護13個服務,rhel保護88個服務,系統默認該模式。
minimum:centos7,修改過的targeted,只對選擇的網絡服務
mls:提供MLS(多級安全)機制的安全性
注意:minimum和mls穩定性不足,未加以應用
selinux 不同策略及違反策略后的動作:
root@cenots6.8 ~ # 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=enforcing # SELINUXTYPE= can take one of these two values: # targeted - Targeted processes are protected, # mls - Multi Level Security protection. SELINUXTYPE=targeted ###默認工作類型
setenforce 0 將當前模式由 enforcing 由切換到permissive,臨時更改,更改后文件里面還是默認值enforcing(該命令不能切換到disable,如需更改需要修改配置文件,然后重啟),setenforce 1切換成enforcing。
root@cenots6.8 ~ # setenforce 0 root@cenots6.8 ~ # sestatus SELinux status: enabled SELinuxfs mount: /selinux Current mode: permissive Mode from config file: enforcing Policy version: 24 Policy from config file: targeted
selinux的啟用與禁用:
禁用方法1、將/etc/selinux/config 文件中的SELINUX=enforcing狀態改為disable,重啟。
禁用方法2、在/boot/grub/grub.cof 文件中在內核參數處添加,selinux=0,重啟
注意:在這兩個文件中,只要有一個禁用就禁用。
查看當正在生效的策略:getenforce
root@cenots6.8 ~ # getenforce Enforcing
查看當前策略設置的詳細信息:sestatus
root@cenots6.8 ~ # sestatus SELinux status: enabled SELinuxfs mount: /selinux Current mode: enforcing ###當前生效的 Mode from config file: enforcing ###/etc/selinux/config文件中的設置狀態 Policy version: 24 Policy from config file: targeted ###工作類型
selinux安全上下文:
傳統linux中,一切皆文件,由用戶,組,權限控制訪問。
在selinux中,一切皆對象(object),由存放在inode的擴展屬性域的安全元素所控制其訪問。
所有文件和端口資源的進程都具備安全標簽,安全上下文(security context)
查看安全上下文信息:ls -Z;ps -Z
期望(默認)上下文:存放在二進制的Selinux策略庫中
semanage fcontext -l
安全上下文有五個元素組成:
user:role:type:sensitivty:category
user_u:object_r:tmp_t:s0:c0
五元素詳解:
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:對于特定的組織劃分不分層的分類,一個對象可以有多個categroy,c0-c1023共1024個分類,target策略不使用category。
-rwxr-xr-x. root root system_u:object_r:admin_home_t:s0 install.log.syslog -rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 ks.cfg drwxr-xr-x. root root system_u:object_r:admin_home_t:s0 公共的
selinux策略
對象(object):所有可以讀取的對象,包括文件,目錄和進程,端口等。
主體:進程成為主體(subject)
selinux中的所有的文件都賦予了一個type的文件類型的標簽,對于所有的進程也賦予各自的一個domain的標簽,domain標簽能夠執行的操作由安全策略里定義。
當一個subject試圖訪問一個object, Kernel中的策略執行服務器將檢查AVC (訪問矢量緩存Access Vector Cache), 在AVC中, subject和object的權限被緩存(cached),查找“應用+文件”的安全環境。然后根據查詢結果允許或拒絕訪問。
安全策略:定義主機讀取對象的規則數據庫,規則中記錄了哪個類型的主機使用哪個方法讀取哪一個對象是允許還是拒絕的,并且定義了哪種行為是允許或拒絕。
舉例:
1、 A主機開啟http服務,在/var/html/index.html該網頁其他主機可以正常訪問,現在將index
。html,做如下操作:a、cp index.html /root
b、mv /root/index.html ./
此時,其他主機就不能正常訪問了。
原因:標簽發生變化導致,服務被拒絕。
root@centos7.2 /var/www/html # ll -Z -rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 index.html -rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index2.html root@centos7.2 ~ # semanage fcontext -l |grep "/var/www" /var/www(/.*)? all files system_u:object_r:httpd_sys_content_t:s0 /var/www(/.*)?/logs(/.*)? all files system_u:object_r:httpd_log_t:s0
注意:拷貝到哪里就會繼承其父目錄的標簽,mv移動會保留原父目錄的標簽,所以,第二步cp就不會出現該問題了。
2、 此時系統任務http服務放在/var/www/html目錄中,如果想改成/website 目錄中,則首先要修改vim /etc/httpd/conf/httpd.conf,Document Root "/var/www/html" 改成/website ,重啟服務,由于/website是我們自己創建的,在策略數據庫中是查不到的,因此需要 chcon –reference /var/www/html/ /website (不能使用restorecon,因為數據庫中沒有,加到數據庫中才可以使用),重啟服務就可以訪問了。
如何查看文件或端口錄原有的標簽是什么呢?—— semanage fcontex | port -l(數據庫中查找)
如果文件的標簽發生變化,如何恢復為默認值呢?—–restorecon fileName (通過查找semanage數據庫恢復默認的標簽 )
—–restorecon -R dirName 恢復目錄及其里面所有文件的默認標簽
知道標簽名,如何手動修改?———— chcon -t default_t fileName
參考前面文件的標簽進行修改——————chcon -R –reference dirName1 dirName2
策略數據庫中添加或刪除文件或目錄————semange fcontext -a -t http_sys_content_t "/website(/.*)?"
——semange fcontext -d -t http_sys_content_t "/website(/.*)?"
——(/.*)?—-指該目錄下的任意文件或目錄
策略數據庫中添加端口號————-semanage port -a -t http_prot -p tcp 8888 將8888加入到策略數據庫中
修改策略數據庫中的端口號給別的端口–semanage prot -m -t ssh_prot_t -p tcp 8888 將原來的http的8888端口號給ssh
selinux布爾值
有些服務或行為系統認為是危險的就拒絕,比如ftp上傳默認是不允許的,下載是可以的,這些行為就是由bool值控制的
查看bool值命令:getsebool -a 、semanage boolean -l 、
root@centos7.2 ~ # getsebool -a | grep ftp ftp_home_dir --> off ftpd_anon_write --> off ###ftp上傳權限被拒絕 ftpd_connect_all_unreserved --> off ftpd_connect_db --> off ftpd_full_access --> off ftpd_use_cifs --> off
root@centos7.2 ~ # semanage boolean -l | grep ftp SELinux 布爾值 狀態 默認 描述 sftpd_enable_homedirs (關 , 關) Allow sftpd to enable homedirs httpd_can_connect_ftp (關 , 關) Allow httpd to can connect ftp ftpd_use_passive_mode (關 , 關) Allow ftpd to use passive mode ftpd_use_nfs (關 , 關) Allow ftpd to use nfs ftpd_connect_all_unreserved (關 , 關) Allow ftpd to connect all unreserved sftpd_full_access (關 , 關) Allow sftpd to full access ftpd_anon_write (關 , 關) Allow ftpd to anon write 狀態:當前狀態 默認:值文件中的設置
設置selinux布爾值:
setsebool httpd_can_network_relay =1 設置httpd_can_network_relay 狀態為1(臨時生效)
setsebool -P httpd_can_network_relay on|off 設置httpd_can_network_relay 狀態和默認值都為1(永久有效)
semanage boolean -l -C 查看改過的boolean值
原創文章,作者:Naruto,如若轉載,請注明出處:http://www.www58058.com/47359