Linux之SELinux

 

Linux之SELinux

 

 

本文包括以下內容
SELinux概念
啟用SELinux
管理文件安全標簽
管理端口標簽
管理SELinux布爾值開關
管理日志
查看SELinux幫助

 

SELinux介紹
SELinux: Secure Enhanced Linux,是美國國家安全局(NSA=The National Security Agency)和SCC(Secure Computing Corporation)開發的Linux的一個強制訪問控制的安全模塊。2000年以GNU GPL發布,Linux內核2.6版本后集成在內核中
DAC:Discretionary Access Control自由訪問控制
MAC:Mandatory Access Control 強制訪問控制
DAC環境下進程是無束縛的
MAC環境下策略的規則決定控制的嚴格程度
MAC環境下進程可以被限制的
策略被用來定義被限制的進程能夠使用那些資源(文件和端口)
默認情況下,沒有被明確允許的行為將被拒絕

 

SELinux工作類型
SELinux有四種工作類型:
strict: centos5,每個進程都受到selinux的控制
targeted: 用來保護常見的網絡服務,僅有限進程受到selinux控制,只監控容易被入侵的進程,rhel4只保護13個服務,rhel5保護88個服務
minimum:centos7,修改過的targeted,只對選擇的網絡服務
mls:提供MLS(多級安全)機制的安全性
minimum和mls穩定性不足,未加以應用

 

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策略庫(映射目錄和期望安全上下文)中
semanagefcontext–l

 

五個安全元素
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: 禁用

 

配置SELinux
相關命令:
getenforce: 獲取selinux當前狀態
sestatus:查看selinux狀態
setenforce0|1
0: 設置為permissive
1: 設置為enforcing
配置文件:
/boot/grub/grub.conf
使用selinux=0禁用SELinux
/etc/sysconfig/selinux
/etc/selinux/config
SELINUX={disabled|enforcing|permissive}

 

修改SELinux安全標簽
給文件重新打安全標簽:
chcon[OPTION]… [-u USER] [-r ROLE] [-t TYPE] FILE…
chcon[OPTION]… –reference=RFILE FILE…
-R:遞歸打標;
恢復目錄或文件默認的安全上下文:
restorecon[-R] /path/to/somewhere

 

默認安全上下文查詢與修改
semanage來自policycoreutils-python包
查看默認的安全上下文
semanagefcontext–l
添加安全上下文
semanagefcontext-a –t httpd_sys_content_t‘/testdir(/.*)?’
restorecon–Rv/testdir
刪除安全上下文
semanagefcontext-d –t httpd_sys_content_t‘/testdir(/.*)?’
1212
查看端口標簽
semanageport –l
添加端口
semanageport -a -t port_label-p tcp|udpPORT
semanage port -a -t http_port_t -p tcp 9527
刪除端口
semanageport -d -t port_label-p tcp|udpPORT
semanage port -d -t http_port_t -p tcp 9527
修改現有端口為新標簽
semanageport -m -t port_label-p tcp|udpPORT
semanageport -m -t http_port_t-p tcp9527
Selinux端口標簽

 

SELinux布爾值
布爾型規則:
getsebool
setsebool
查看bool命令:
getsebool[-a] [boolean]
semanageboolean–l
semanageboolean-l –C 查看修改過的布爾值
設置bool值命令:
setsebool[-P] booleanvalue(on,off)
setsebool[-P] Boolean=value(0,1)

 

SELinux日志管理
yum install setroublesshoot*(重啟生效)
將錯誤的信息寫入/var/log/message
grep setroubleshoot/var/log/messages
sealert-l UUID
查看安全事件日志說明
sealert-a /var/log/audit/audit.log
掃描并分析日志
yum -y install selinux-policy-devel(centos7)
yum –y install selinux-policy-doc (centos6)
mandb | makewhatis
man -k _selinux
SELinux幫助

 

 

練習

1、啟用SELinux策略并安裝httpd服務,改變網站的默認主目錄為/website,添加SELinux文件標簽規則,使網站可訪問

 

vim /etc/selinux/config
SELINUX=disabled
reboot 重啟才生效

systemctl status httpd
yum -y install httpd
systemctl start httpd
netstat -tnl
iptalbes -F

mkdir /website
ll -Z /website/ -d
vim /etc/httpd/conf/httpd.conf
Document Root "/website/"
<Directory "/website">
systemctl restart httpd
systemctl status httpd
echo mywebsite > index.html
semanage fcontext -l |grep website
semanage fcontext -a -t httpd_sys_content_t "/website(/.*)?"
ll -Z /website/ -d
restorecon -R /website/

 

2、修改上述網站的http端口為9527,增加SELinux端口標簽,使網站可訪問

 

vim /etc/httpd/conf/httpd.conf
Listen 9527
semanager port -l|grep 9527
vim /etc/httpd/conf/httpd.conf
netstat -ntl
httpd -t 檢查語法
systemctl restart httpd
semanger port -a -t http_port_t -p tcp 9527
iptables -F

 

3、啟用相關的SELinux布爾值,使上述網站的用戶student的家目錄可通過http訪問

 

在CentOS 7上
vim /etc/httpd/conf.d/usrdir.conf
#UserDir disabled
UserDir public_html
systemctl restart httpd
su -student
mkdir public_html
echo welcome to studenthome > index.html
links x.x.x.x/~student 訪問家目錄
ll -d /home/student
ps aux|grep http
setfacl -m u:apache:x /home/student

 

在CentOS 6上
vim /etc/httpd/conf/httpd.conf
#UserDir disabled
UserDir public_html
service httpd restart
iptables -F
su -student
mkdir public_html
cd public html
echo studenthomecentos6 > index.html
cd
chmod 711 /home/wang
ll -d /home/wang/

 

 

 

 

 

 

 

 

  

原創文章,作者:dengjian,如若轉載,請注明出處:http://www.www58058.com/48151

(0)
dengjiandengjian
上一篇 2016-09-20 23:11
下一篇 2016-09-21 01:52

相關推薦

  • 8-5作業

    4、取本機ip地址 ifconfig | grep -o "1[0-9]\+\.[0-9]\+\.[0-9]\+\.[1-2][0-5][0-4]" 5、取各分區利用率的數值 df | grep "/dev/sda"|tr -s ' ' |cut -d" " -f5 |tr -d…

    Linux干貨 2016-08-10
  • 【26期】Linux第一周學習小總結

        知識不在長短,而在于其中的精煉程度,字典每個字沒有一頁的篇幅,卻被大面積推廣,之所以沒有再把學到的全部搬運到博客上來,就是為了給大家一點可看的東西,也許我自己認為的精煉是有點簡短了,那我就再更新一下,再復習一下,學到的whatis 和 man命令。     第一周匆匆而過,學過的知識,過遍腦…

    2017-07-14
  • 初學Linux之shell腳本編程

    shell程序的特點;shell腳本的結構和格式要求;變量;算術運算和邏輯運算;條件測試;防止擴展和shell登錄的相關配置文件

    2018-01-01
  • linux基礎學習第十一天(shell循環、軟件包管理)

    2016-08-16 授課內容: shell腳本基礎:循環,for,while,until(未講完) 軟件包管理(未講完): ?軟件運行環境 ?軟件包基礎 ?Rpm包管理 循環執行 將某代碼段重復運行多次 重復運行多少次: 循環次數事先已知 循環次數事先未知 有進入條件和退出條件 循環控制語句: break:【提結束循環,循環將不再執行】指前面的循環退出,退…

    Linux干貨 2016-08-18
  • 計算機原理

    計算機由CPU、存儲器、輸入設備、輸出設備組成。 CPU的功能:對數據運算加工,控制設備等 存儲器的功能:存儲數據,加載程序 輸入設備:下指令,提供數據等 輸出設備:輸出數據加工的結果 linux發行版本:Debian:ubuntu、knopix Slackware:S.u.S.E、SLES、openSUSE RedHat:Redhat 9.0、RedHat…

    Linux干貨 2017-12-04
  • linux基礎知識之nmcli

           CentOS7才有的功能網絡組:將多塊網卡聚合在一起的方法,從而實現冗錯和提高吞吐量        網絡組不同于舊版中的bonding技術,提供了更好的性能和擴展性        網路組由內核驅動和teamd守護進程實現 &n…

    Linux干貨 2016-09-07

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-09-22 10:24

    內容總結的很全面,但是整個文章看起來沒有i什么層次感,雖然說作者分類過,如果對字體大小,顏色等進行一些調整,會不會看起來更舒服點呢?

欧美性久久久久