1.什麼時selinux
selinux,核心級加強型防火牆
基本 SELINUX 安全性概念
SELINUX ( 安全增強型 Linux ) 是可保護你系統安全性的額外機制在某種程度上 , 它可以被看作是與标準權限系統行的權限系統。在正常模式中 , 以使用者身份運作程序,并且系統上的檔案和其他資源都設定了權限 ( 控制哪些使用者對哪些檔案具有哪些通路權 SELINUX 的另一個不同之處在于 , 若要通路檔案 , 你必須具有普通通路權限和 SELINUX 通路權限。是以 , 即使以超級使用者身份 root 運作程序 , 根據程序以及檔案或資源的 SELinux 安全性上下文可能拒絕通路檔案或資源限 ) 标簽
2.如何管理selinux級别
selinux開啟或者關閉)
vim /etc/sysconfig/selinux
selinux=disabled ##關閉狀态
selinux=Enforcing ##強制狀态
selinux=Permissive ##警告狀态
getenforce ##檢視狀态
當selinux開啟時
setenforce 0|1 ##更改selinux運作
– 0 表示 permissive# 警告
– 1 表示 enforcing # 強制
3.如何更改檔案安全上下文
(1)selinux 安全上下文通路規則
WEB 伺服器的 HTTPD 程序設定了 SELINUX 上下文system_u:system_r:httpd_t 标簽。該上下文的重要部分是第三個用冒号分隔的字段 SELINUX 類型 :httpd_t系統上的檔案和資源也設定了 SELINUX 上下文标簽 ,并且重要的部分是 SELINUX 類型。例如 ,/var/www/html 中的檔案具有類型httpd_sys_content_t 。/tmp 和/var/tmp 中的檔案通常具有類型 tmp_tSeliux 政策具有允許以httpd_t 身份運作的程序通路标記為 httpd_sys_content_t 的檔案的規則。沒有規則允許這些程序通路标記有 tmp_t 的檔案 , 是以将拒絕這些通路 , 即使正常檔案權限指出應該允許這些通路
臨時更改)
(2)顯示 SELinux 檔案上下文
什麼确定檔案的初始 SELinux 上下文 ? 通常是父目錄。将父目錄的上下文指定給新建立的檔案。這對 vimcp 和touch 等指令其作用 , 但是 , 如果檔案是在其他位置建立的并且保留了權限 ( 與 mv 或 cp -a 一樣 ) 則還将保留SELinux 上下文許多處理檔案的指令具有一個用于顯示或設定 SELinux 上下文的選項 ( 通常是 -Z ) 。例如 , ps 、 ls 、 cp 和 mkdir 都使用 -Z 選項顯示或設定SELinux 上下文
顯示上下文
– ps axZ
– ps -ZC
– ls -Z
chcon -t
一次性定制安全上下文,執行 restorecon 重新整理後還原
chcon -t 安全上下文檔案
chcon -t public_content_t /publicftp -R
254 touch /mnt/westos
260 mv /mnt/westos /var/ftp/pub/
261 ls -Z /var/ftp/pub/
262 ps auxZ | grep vsftpd
263 chcon -t public_content_t /var/ftp/pub/westos
264 ls -Z /var/ftp/pub/

永久更改)
semanage fcontext 可用與顯示或修改
restorrecon 用來設定預設檔案上下文的規則
semanage fcontext -l ##列出核心安全上下文清單内容
semanage fcontext -a -t public_content_t '/publicftp(/.*)?'
semanage fcontext 使用擴充正規表達式來指定路徑和檔案名。 fcontext 規則中最常用的擴充正規表達式是(/.*)?, 表示随意地比對 / 後跟任何數量的字元
restorecon -FvvR /publicftp/
semanage fcontext -d ##删除核心安全上下文清單内容
232 systemctl status vsftpd ##檢視ftp檔案共享服務狀态,要是運作狀态
233 firewall-cmd --list-all ##檢視火牆權限,允許ftp通過火牆
248 getenforce ##selinux的狀态,Enforcing強制狀态
249 vim /etc/vsftpd/vsftpd.conf ##ftp配置檔案,匿名家目錄為/westos
250 systemctl restart vsftpd ##重置ftp檔案共享
251 mkdir /westos ##建立/westos
252 ls /westos ##檢視目錄檔案
253 ps auxZ | grep vsftpd ##顯示selinux檔案上下文
254 ls -lZ /var/ftp
255 ls -Zd /westos/
256 semanage fcontext -l | grep westos ##列出安全上下文westos内容,
257 semanage fcontext -l | grep var/ftp##列出安全上下文/var/ftp
内容
258 semanage fcontext -a -t public_content_t '/westos(/.*)?'
##永久更改檔案安全上下文 -a 表示添加 -t 安全上下文類型 因為*,?等符号都有其特定含義是以要加‘’括起來。
259 semanage fcontext -l | grep var/ftp
260 semanage fcontext -l | grep westos
261 ls -Zd /westos/ 檢視 /westos的資訊,沒有因為沒有更新
262 restorecon -RvvF /westos/ ## 更新預設檔案上下文是public_content_t
263 ls -Zd /westos/ 檢視 /westos的資訊
264 touch /westos/file11 在/westos下建立檔案
265 ls -Z /westos/ 檢視/westos下檔案的資訊
4.如何控制selinux對服務功能的開關
getsebool -a | grep 服務名稱
getsebool -a | grep ftp ##顯示布爾值
setsebool -P 功能bool值 on|off 或者0|1
setsebool -P ftpd_anon_write on ##修改功能布爾值
265 setenforce 0 ##更改selinux運作級别Permissive警告狀态
266 setenforce 1 ##更改selinux運作級别Enforcing強制狀态
267 getsebool -a | grep ftp ##顯示ftp布爾值
268 setsebool -P ftp_home_dir on ##修改ftp_home_dir功能布爾值
269 getsebool -a | grep ftp##顯示ftp布爾值
272 semanage boolean -l ##顯示布爾值是否永久
初次上傳檔案時,上傳限制,
[kiosk@foundation44 ~]$ lftp 172.25.254.144 -u student
Password:
lftp [email protected]:~> ls
-rw-r--r-- 1 1000 1000 2453 Apr 29 15:41 passwd
drwxr-xr-x 2 1000 1000 6 Apr 26 06:59 zhang
lftp [email protected]:~> put /etc/passwd
put: Access failed: 553 Could not create file. (passwd)
lftp [email protected]:~> quit
(1)setenforce 0 ##更改selinux運作級别Permissive警告狀态
2453 bytes transferred in 10 econds (243b/s)
(2)setenforce 1 ##更改selinux運作級别Enforcing強制狀态
(3)setsebool -P ftp_home_dir on ##修改ftp_home_dir功能布爾值
2453 bytes transferred in 10 seconds (244b/s)
5.監控selinux的錯誤資訊
監控 SELinux 沖突
必須安裝 setroubleshoot-server 軟體包 , 才能将SELinux 消息發送至/var/log/messages
etroubleshoot-server 偵聽/var/log/audit/audit.log 中的稽核資訊并将簡短摘
要發送至 /var/log/messages
摘要包括 SELinux 沖突的唯一辨別符 ( UUIDs),可用于收集更多資訊。
Sealert -l UUID 用于生成特定事件的報告。
Sealert -a /var/log/audit/audit.log 用于在該檔案中生成所有事件的報告
26 yum search setroubleshoot
27 yum install setroubleshoot.x86_64 -y ##安裝軟體包
28 > /var/log/messages ## 将SELinux 消息發送至 /var/log/message
29 cat /var/log/messages ##檢視日志
31 history
15 touch /mnt/hellowestos1 ##建立檔案
16 mv /mnt/hellowestos1 /westos/ ##将檔案移動到/westos
17 cd /westos/ ##切換到/westos
18 ls ##檢視
19 cd
20 ls -Z /westos/ ##檢視/westos/下檔案的安全上下文資訊
21 semanage fcontext -l | grep /westos/ ##列出安全上下文westos内容,
22 semanage fcontext -a -t public_content_t '/westos(/.*)?'##永久更改檔案安全上下文
23 restorecon -RvvF /westos/ ## 更新預設檔案上下文是public_content_t
24 ls -Z /westos/ ##檢視/westos/下檔案的安全上下文資訊
25 history
(1)當建立的檔案hellowestos1,沒有更改安全上下文時檢視不到hellowestos1
[kiosk@foundation44 ~]$ lftp 172.25.254.144
lftp 172.25.254.144:~> ls
-rw-r--r-- 1 0 0 0 Apr 26 06:07 file1
-rw-r--r-- 1 0 0 0 Apr 29 14:57 file11
-rw-r--r-- 1 0 0 0 Apr 26 06:07 file2
-rw-r--r-- 1 0 0 0 Apr 26 06:07 file3
-rw-r--r-- 1 0 0 0 Apr 26 06:07 file4
-rw-r--r-- 1 0 0 0 Apr 26 15:32 file{1}
-rw-r--r-- 1 0 0 0 May 02 09:49 hellowestos
lftp 172.25.254.144:/> quit
(2)當建立的檔案hellowestos1,更改安全上下文時可以看到hellowestos
-rw-r--r-- 1 0 0 0 Apr 29 14:57 file11
-rw-r--r-- 1 0 0 0 Apr 26 06:07 file2
-rw-r--r-- 1 0 0 0 Apr 26 15:32file{1}
-rw-r--r-- 1 0 0 0 May 02 10:32hellowestos1