linun——SElinux的简单理解
SElinux簡單理解
一.什么是SElinux?
SELinux 全稱 Security Enhanced Linux (安全強化 Linux),是 MAC (Mandatory Access Control,強制訪問控制系統)的一個實現.目的在于明確的指明某個進程可以訪問哪些資源(文件、網絡端口等)。舉例來說,系統上的 Apache 被發現存在一個漏洞,使得某遠程用戶可以訪問系統上的敏感文件(比如 /etc/passwd 來獲得系統已存在用戶) ,而修復該安全漏洞的 Apache 更新補丁尚未釋出。此時 SELinux 可以起到彌補該漏洞的緩和方案。因為 /etc/passwd 不具有 Apache 的 訪問標簽,所以 Apache 對于 /etc/passwd 的訪問會被 SELinux 阻止。 相比其他強制性訪問控制系統,SELinux 有如下優勢:
??? 控制策略是可查詢而非程序不可見的。
??? 可以熱更改策略而無需重啟或者停止服務。
??? 可以從進程初始化、繼承和程序執行三個方面通過策略進行控制。
??? 控制范圍覆蓋文件系統、目錄、文件、文件啟動描述符、端口、消息接口和網絡接口。
本博文將會對安全上下文、網絡接口等進行一個簡單的說明。
二、安全上下文(Security?Context)
安全上下文是一個簡單的、一致的訪問控制屬性,在SELinux中,類型標識符是安全上下文的主要組成部分,,一個進程的類型通常被稱為一個域,"域"和"域類型"意思都一樣,即都是安全上下文中的“TYPE”。
SELinux對系統中的許多命令做了修改,通過添加一個-Z選項顯示客體和主體的安全上下文。
安全上下文的查看
id?-Z?顯示shell的安全上下文
ps?-Z?檢查進程的安全上下文
ls -Z? 檢查文件、目錄的安全上下文?
[root@localhost ~]# id -Z unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 [root@localhost ~]# ps -Z LABEL PID TTY TIME CMD unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 2117 pts/0 00:00:00 bash unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 2149 pts/0 00:00:00 dbus-l unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 2184 pts/0 00:00:00 ps [root@localhost ~]# ls /var/ftp/ pub [root@localhost ~]# ls -Z /var/ftp/ ##檢查目錄/var/ftp/的安全上下文 drwxr-xr-x. root root system_u:object_r:public_content_t:s0 pub [root@localhost ~]# 注意:cp:會重新生成安全上下文
mv:安全上下文則不變
[root@localhost mnt]# ls -Z tutu butterfly -rw-r--r--. root root unconfined_u:object_r:mnt_t:s0 butterfly -rw-r--r--. root root unconfined_u:object_r:mnt_t:s0 tutu [root@localhost mnt]# ls -Z /var/ftp drwxr-xr-x. root root system_u:object_r:public_content_t:s0 pub [root@localhost mnt]# cp tutu /var/ftp [root@localhost mnt]# mv butterfly /var/ftp [root@localhost mnt]# ls -Z /var/ftp -rw-r--r--. root root unconfined_u:object_r:mnt_t:s0 butterfly drwxr-xr-x. root root system_u:object_r:public_content_t:s0 pub -rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 tutu [root@localhost mnt]#安全上下文實驗,lftp查看,以及更改安全上下文類型:
目錄/var/ftp下的文件安全上下文與/var/ftp的安全上下文不符合的,用lftp查看時,顯示不出來
目錄/var/ftp下的文件安全上下文與/var/ftp的安全上下文符合的,用lftp查看時,可以顯示出來
[root@localhost ftp]# pwd ##顯示當前所在位置 /var/ftp [root@localhost ftp]# touch tutu1 [root@localhost ftp]# ls -Z drwxr-xr-x. root root system_u:object_r:public_content_t:s0 pub -rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 tutu1 [root@localhost ftp]# touch /mnt/tutu2 [root@localhost ftp]# mv /mnt/tutu2 /var/ftp ##只能移動,不能復制,復制過去文件的安全上下文會改變成目標目錄的安全上下文 [root@localhost ftp]# ls -Z drwxr-xr-x. root root system_u:object_r:public_content_t:s0 pub -rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 tutu1 -rw-r--r--. root root unconfined_u:object_r:mnt_t:s0 tutu2 [root@localhost ftp]# lftp 172.25.254.229 lftp 172.25.254.229:~> ls ##顯示目錄/var/ftp下的文件、目錄等。文件tutu2的安全上下文不符合,所以顯示不出 drwxr-xr-x 2 0 0 6 Mar 07 2014 pub -rw-r--r-- 1 0 0 0 May 14 01:24 tutu1 lftp 172.25.254.229:/> quit [root@localhost ftp]# semanage fcontext -l | grep /var/ftp ##查看目錄/var/ftp的所有內容的安全上下文 /var/ftp(/.*)? all files system_u:object_r:public_content_t:s0 /var/ftp/bin(/.*)? all files system_u:object_r:bin_t:s0 /var/ftp/etc(/.*)? all files system_u:object_r:etc_t:s0 /var/ftp/lib(/.*)? all files system_u:object_r:lib_t:s0 /var/ftp/lib/ld[^/]*\.so(\.[^/]*)* regular file system_u:object_r:ld_so_t:s0 [root@localhost ftp]# chcon -t public_content_t tutu2 ##更改文件tutu2的安全上下文類型為public_content_t [root@localhost ftp]# ls -Z drwxr-xr-x. root root system_u:object_r:public_content_t:s0 pub -rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 tutu1 -rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 tutu2 [root@localhost ftp]# lftp 172.25.254.229 lftp 172.25.254.229:~> ls drwxr-xr-x 2 0 0 6 Mar 07 2014 pub -rw-r--r-- 1 0 0 0 May 14 01:24 tutu1 -rw-r--r-- 1 0 0 0 May 14 01:25 tutu2 lftp 172.25.254.229:/> quit [root@localhost ftp]# 注意:chcon -t?? ##臨時更改文件的安全上下文,當selinux值改變后,安全上下文被還原下面用實驗證明:
[root@localhost ~]# mkdir /tutu/ [root@localhost ~]# touch /tutu/butterfly [root@localhost ~]# vim /etc/vsftpd/vsftpd.conf ##編輯該配置文件,添加anon_root=/tutu [root@localhost ~]# systemctl restart vsftpd ##每次編輯該配置文件后,都需要重啟vsftpd服務 [root@localhost ~]# lftp 172.25.254.229 lftp 172.25.254.229:~> ls lftp 172.25.254.229:/> ls lftp 172.25.254.229:/> quit [root@localhost ~]# setenforce 0 ##修改selinux的值為警告,不需要重啟,直接生效 [root@localhost ~]# lftp 172.25.254.229 lftp 172.25.254.229:~> ls -rw-r--r-- 1 0 0 0 May 14 02:02 butterfly lftp 172.25.254.229:/> quit [root@localhost ~]# ls -Z /tutu/ -d drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /tutu/ [root@localhost ~]# chcon -t public_content_t /tutu -R ##更改文件tutu2的安全上下文類型為public_content_t -R(recursive)——operate on files and directories recursively [root@localhost ~]# ls -Z /tutu/ ##顯示目錄下文件的安全上下文 -rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 butterfly [root@localhost ~]# ls -Z /tutu/ -d ##顯示當前目錄的安全上下文 drwxr-xr-x. root root unconfined_u:object_r:public_content_t:s0 /tutu/ [root@localhost ~]# vim /etc/sysconfig/selinux ##修改selinux的值為disabled [root@localhost ~]# reboot ##重啟,使selinux的值生效 [root@localhost ~]# vim /etc/sysconfig/selinux ##修改selinux的值為enforcing [root@localhost ~]# reboot [root@localhost ~]# ls -Zd /tutu/ drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /tutu/更改目錄本身及其內容的安全上下文類型
[root@localhost ~]# ls -Zd /tutu/ drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /tutu/ [root@localhost ~]# ls -Zd /var/ftp/ drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /var/ftp/ [root@localhost ~]# semanage fcontext -l | grep /var/ftp ##查看目錄/var/ftp的所有內容的安全上下文 /var/ftp(/.*)? all files system_u:object_r:public_content_t:s0 /var/ftp/bin(/.*)? all files system_u:object_r:bin_t:s0 /var/ftp/etc(/.*)? all files system_u:object_r:etc_t:s0 /var/ftp/lib(/.*)? all files system_u:object_r:lib_t:s0 /var/ftp/lib/ld[^/]*\.so(\.[^/]*)* regular file system_u:object_r:ld_so_t:s0 [root@localhost ~]# semanage fcontext -l | grep /tutu ##查看目錄/tutu本身及其內容的安全上下文,目錄/tutu在默認情況下沒有安全上下文 [root@localhost ~]# semanage fcontext -a -t public_content_t '/tutu(/.*)?' ##-a 添加 -t 更改類型 ##更改目錄/tutu本身及其內容的安全上下文類型 ##/tutu(/.*)? === /tutu/* === /tutu/*/*/*.... 祥見正則表達式 [root@localhost ~]# semanage fcontext -l | grep /tutu /tutu(/.*)? all files system_u:object_r:public_content_t:s0 [root@localhost ~]# restorecon -FvvR /tutu/ ##用來恢復SElinux文件屬性,即恢復文件的安全上下文 restorecon reset /tutu context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0 restorecon reset /tutu/butterfly context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0 [root@localhost ~]# lftp 172.25.254.229 lftp 172.25.254.229:~> ls -rw-r--r-- 1 0 0 0 May 14 02:02 butterfly lftp 172.25.254.229:/> quit [root@localhost ~]# touch /.autorelabel ##建立該文件的作用是使得系統在重啟時就會對整個文件系統進行relabeling [root@localhost ~]#命令restorecon參數祥解
-i:忽略不存在的文件 -f:infilename 文件 infilename 中記錄要處理的文件 -e:directory 排除目錄 -R/-r:遞歸處理目錄 -n:不改變文件標簽 -o/outfilename:保存文件列表到 outfilename,在文件不正確的情況下 -v:將過程顯示到屏幕上 -F:強制恢復文件安全語境semanage fcontext -a -t public_content_t '/tutu/' ##該命令執行后,看上去將安全上下文更改成功了,實際上并沒有 semanage fcontext -a -t public_content_t '/tutu(/.*)?' ##該命令執行后,是將本目錄及其內容的安全上下文進行成功更改
證明實驗:
[root@localhost ~]# mkdir /tutu [root@localhost ~]# ls -Zd /tutu/ drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /tutu/ [root@localhost ~]# semanage fcontext -a -t public_content_t '/tutu/' [root@localhost ~]# semanage fcontext -l | grep /tutu /tutu/ all files system_u:object_r:public_content_t:s0 [root@localhost ~]# touch /tutu/butterbly{1..3} [root@localhost ~]# restorecon -FvvR /tutu/ restorecon reset /tutu context unconfined_u:object_r:default_t:s0->system_u:object_r:default_t:s0 restorecon reset /tutu/butterbly1 context unconfined_u:object_r:default_t:s0->system_u:object_r:default_t:s0 restorecon reset /tutu/butterbly2 context unconfined_u:object_r:default_t:s0->system_u:object_r:default_t:s0 restorecon reset /tutu/butterbly3 context unconfined_u:object_r:default_t:s0->system_u:object_r:default_t:s0 [root@localhost ~]# ls -Z /tutu/ -rw-r--r--. root root system_u:object_r:default_t:s0 butterbly1 -rw-r--r--. root root system_u:object_r:default_t:s0 butterbly2 -rw-r--r--. root root system_u:object_r:default_t:s0 butterbly3 [root@localhost ~]# semanage fcontext -a -t public_content_t '/tutu(/.*)?' [root@localhost ~]# restorecon -FvvR /tutu/ restorecon reset /tutu context system_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0 restorecon reset /tutu/butterbly1 context system_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0 restorecon reset /tutu/butterbly2 context system_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0 restorecon reset /tutu/butterbly3 context system_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0 [root@localhost ~]# ls -Z /tutu/ -rw-r--r--. root root system_u:object_r:public_content_t:s0 butterbly1 -rw-r--r--. root root system_u:object_r:public_content_t:s0 butterbly2 -rw-r--r--. root root system_u:object_r:public_content_t:s0 butterbly3 [root@localhost ~]#三、關于/var/log/messages和/var/log/audit/audit.log在vsftpd服務方面的區別
/var/log/messages??? ##日志的存放,當訪問ftp://ip出現報錯時,此文件中會有報錯的解決方法,但是安全性不高
/var/log/audit/audit.log??? ##審計消息存放處,沒有報錯的解決方法
[root@localhost ~]# cd /var/ftp/ [root@localhost ftp]# ls butterfly pub tutu [root@localhost ftp]# rm -fr * [root@localhost ftp]# > /var/log/messages [root@localhost ftp]# cat /var/log/messages [root@localhost ftp]# touch /mnt/tutu [root@localhost ftp]# ls /mnt/ tutu [root@localhost ftp]# mv /mnt/tutu /var/ftp/ [root@localhost ftp]# ls -Z -rw-r--r--. root root unconfined_u:object_r:mnt_t:s0 tutu [root@localhost ftp]# getenforce Enforcing [root@localhost ftp]# lftp 172.25.254.229 lftp 172.25.254.229:~> ls lftp 172.25.254.229:/> quit [root@localhost ftp]# > /var/log/messages [root@localhost ftp]# cat /var/log/messages [root@localhost ftp]# getenforce Permissive [root@localhost ftp]# lftp 172.25.254.229 ##當SElinux的值為警告時,還是可以看到目錄/var/ftp下的文件,但是會有報錯,報錯信息在/var/log/messages中 lftp 172.25.254.229:~> ls -rw-r--r-- 1 0 0 0 May 16 09:59 tutu lftp 172.25.254.229:/> quit可以用瀏覽器測試:ftp://172.25.254.229當SElinux值為Enforcing時,看不到任何文件
當SElinux值為Permissive時,可以看到文件
然后查看日志結果如下
##設置SElinux值為Permissive后,用瀏覽器測試 [root@localhost ftp]# cat /var/log/messages ##日志內容略其他部分,以下的日志內容就是報錯的解決方法 Then execute: restorecon -v '$FIX_TARGET_PATH'***** Plugin catchall (7.64 confidence) suggests **************************If you believe that vsftpd should be allowed getattr access on the file by default. Then you should report this as a bug. You can generate a local policy module to allow this access. Do allow this access for now by executing: # grep vsftpd /var/log/audit/audit.log | audit2allow -M mypol # semodule -i mypol.pp [root@localhost ftp]# cat /var/log/audit/audit.log type=AVC msg=audit(1526469623.797:1978): avc: denied { getattr } for pid=1269 comm="vsftpd" path="/tutu" dev="vda1" ino=8846773 scontext=system_u:system_r:ftpd_t:s0-s0:c0.c1023 tcontext=unconfined_u:object_r:mnt_t:s0 tclass=file type=SYSCALL msg=audit(1526469623.797:1978): arch=c000003e syscall=6 success=yes exit=0 a0=7f19dfeba660 a1=7f19dfec27e0 a2=7f19dfec27e0 a3=0 items=0 ppid=1267 pid=1269 auid=4294967295 uid=14 gid=50 euid=14 suid=14 fsuid=14 egid=50 sgid=50 fsgid=50 tty=(none) ses=4294967295 comm="vsftpd" exe="/usr/sbin/vsftpd" subj=system_u:system_r:ftpd_t:s0-s0:c0.c1023 key=(null) [root@localhost ftp]# restorecon -v /var/ftp/* restorecon reset /var/ftp/tutu context unconfined_u:object_r:mnt_t:s0->unconfined_u:object_r:public_content_t:s0 [root@localhost ftp]# > /var/log/messages [root@localhost ftp]# > /var/log/audit/audit.log ##此時進行瀏覽器測試 [root@localhost ftp]# cat /var/log/audit/audit.log [root@localhost ftp]# cat /var/log/messages [root@localhost ftp]# lftp 172.25.254.229 lftp 172.25.254.229:~> ls -rw-r--r-- 1 0 0 0 May 16 09:59 tutu lftp 172.25.254.229:/> quit [root@localhost ftp]# ls -Z -rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 tutu利用日志中的解決方法解決后,再用瀏覽器訪問:ftp://172.25.254.229四、協議端口
1.什么是協議端口?
如果把IP地址比作一間房子,端口就是出入這間房子的門。真正的房子只有幾個門,但是一個IP地址的端口可以有65536(即:2^16)個之多,端口是通過端口號來標記的,端口號只有整數,范圍是從0 到65535(2^16-1)。本地操作系統會給那些有需求的進程分配協議端口(即我們常說的端口),每個協議端口有一個正整數標識,如:80,139,445,等等。當目的主機接收到數據包后,將根據報文首部的目的端口號,把數據發送到相應端口,而與此端口相對應的那個進程將會領取數據并等待下一組數據的到來。
2.協議端口的查找與更改
該實驗說明了更改的端口需要添加,才會生效
注意:新添加的端口不能是系統中已存在的!!!
[root@localhost ~]# yum install httpd -y Loaded plugins: langpacks ##安裝過程略 Complete! [root@localhost ~]# cat /etc/services | grep http ##查看httpd服務的所有端口(信息較為詳細) # http://www.iana.org/assignments/port-numbers http 80/tcp www www-http # WorldWideWeb HTTP http 80/udp www www-http # HyperText Transfer Protocol http 80/sctp # HyperText Transfer Protocol https 443/tcp # http protocol over TLS/SSL https 443/udp # http protocol over TLS/SSL https 443/sctp # http protocol over TLS/SSL ##信息比較多,這里只顯示部分 [root@localhost ftp]# semanage port -l | grep http ##列出httpd的所有端口,沒有上一個命令的信息詳細 http_cache_port_t tcp 8080, 8118, 8123, 10001-10010 http_cache_port_t udp 3130 http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000 pegasus_http_port_t tcp 5988 pegasus_https_port_t tcp 5989 [root@localhost ftp]# [root@localhost ~]# vim /etc/httpd/conf/httpd.conf ##修改httpd協議的端口,第42行,該實驗是將80改為6666 [root@localhost ~]# systemctl restart httpd Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details. ##重啟httpd失敗,查看SElinux值 [root@localhost ~]# getenforce Enforcing [root@localhost ~]# setenforce 0 ##設置SElinux值為Permissive [root@localhost ~]# systemctl restart httpd ##重啟httpd服務成功 [root@localhost ~]# semanage port -l | grep http ##列出httpd的所有端口,可見沒有6666這個端口 http_cache_port_t tcp 8080, 8118, 8123, 10001-10010 http_cache_port_t udp 3130 http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000 pegasus_http_port_t tcp 5988 pegasus_https_port_t tcp 5989 [root@localhost ~]# semanage port -a -t http_port_t -p tcp 6666 ##-a 添加,-t 類型,-p portocol協議 ##將6666添加為httpd的tcp協議的http_port_t的端口 [root@localhost ~]# semanage port -l | grep http http_cache_port_t tcp 8080, 8118, 8123, 10001-10010 http_cache_port_t udp 3130 http_port_t tcp 6666, 80, 81, 443, 488, 8008, 8009, 8443, 9000 ##添加6666端口成功 pegasus_http_port_t tcp 5988 pegasus_https_port_t tcp 5989 [root@localhost ~]# setenforce 1 [root@localhost ~]# systemctl restart httpd ##當SElinux值為Enforcing時,成功重啟服務 [root@localhost ~]#總結
以上是生活随笔為你收集整理的linun——SElinux的简单理解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux——管理系统设备之vsftpd
- 下一篇: linux——系统排错之引导恢复shel