Top Banner
Security Enhanced Linux 酷酷酷 study-area 2010 年 8
93

Selinux twnic 201008-sa

Dec 05, 2014

Download

Documents

CYJ

http://phorum.study-area.org/index.php/topic,62220.0.html
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Selinux twnic 201008-sa

Security Enhanced Linux

酷學園 study-area

2010 年 8 月

Page 2: Selinux twnic 201008-sa

2

關於我關於我…… ....

小州 - Kenduest < [email protected]>

現任於聯成電腦 Linux 課程講師歷經神通電腦、資策會單位之 Linux 課程訓練講師9 年的 Linux 教育訓練經歷

專長 –Linux Sytem and Networking Administration

Linux System and Socket Programming (C/C++)

認證 –LPIC Level I, II / Novell NCLP / Red hat RHCE

Page 3: Selinux twnic 201008-sa

3

大綱大綱

SELinux 簡介傳統 Linux 權限概觀SELinux 架構概觀SELinux 模式切換與啟用SELinux 安全本文與布林數值SELinux 問題診斷SELinux 常見服務問題設定SELinux 自訂規則配置

Page 4: Selinux twnic 201008-sa

SELinux 簡介

Page 5: Selinux twnic 201008-sa

5

何謂 SELinux ?全名為 Security-Enhanced Linux

為美國國家安全局 (National Security Agency , NSA) 所開發的安全功能

提供別於傳統 unix 系統以使用者權限控管的作法,可提供更詳盡逐一的授權控管

SELinux SELinux 簡介簡介

Page 6: Selinux twnic 201008-sa

6

SELinux SELinux 簡介簡介

為何需要 SELinux ?傳統的 unix 權限控管過於單純

系統以程式的運作身份提供權限判斷root 身份啟動的程式帶有特權身份程式具備該身份環境下所有的權限功能

Page 7: Selinux twnic 201008-sa

7

SELinux SELinux 簡介簡介

引入 SELinux 好處 ?可以限制個別行程所具備權限

就算是 root 所執行的程式可以進行存取限制

Page 8: Selinux twnic 201008-sa

8

SELinux SELinux 簡介簡介

SElinux 應用層面初步應用,規範系統與網路服務對系統存取權限

終極應用,規範使用者、每一個程式執行期所具備的權限

Page 9: Selinux twnic 201008-sa

9

SELinux SELinux 簡介簡介

目前 SELinux 政策資料庫可以選擇不同的 Policy database 針對不同的系統層級進行控管與保護

現階段 Policy database 使用以 targeted daemon 為主,僅針對系統的 daemon 服務進行控管限制

Page 10: Selinux twnic 201008-sa

10

SELinux SELinux 簡介簡介

現階段使用 SELinux 優點可以把服務程式限制於特定環境運作

限制所能夠使用的資源 ( 網路、檔案目錄 )

減少程式漏洞帶來的系統危害

Page 11: Selinux twnic 201008-sa

11

SELinux SELinux 簡介簡介

使用 SELinux 的 Linux 版本建議 ?Fedora Linux 5 與 RHEL 5 版本的開始已經逐漸穩健,大多服務可以於該啟用環境正常工作使用

SuSE Linux 本身提供 Apparmor 替代 SELinux ,設計與操作上比較單純與易用

Page 12: Selinux twnic 201008-sa

傳統 Linux 權限概觀

Page 13: Selinux twnic 201008-sa

13

傳統 傳統 Linux Linux 權限問題權限問題

只有區分 root 與非 root 權限等級以 root 身份執行的程式表示有最大權限,不受到權限規範的範圍

需要 root 身份啟動與運作的程式可能有意外的風險

Page 14: Selinux twnic 201008-sa

14

傳統 傳統 Linux Linux 權限問題權限問題

行程是允許修改檔案目錄權限使用者為檔案的 owner ,即可變更檔案權限

使用者執行的程式,都可以完整讀取、寫入等存取自己的檔案目錄,甚至可以異動權限

預設都開放允許下,程式運作時有機會修改非自己範圍的檔案目錄項目

Page 15: Selinux twnic 201008-sa

15

傳統 傳統 Linux Linux 權限問題權限問題

行程允許進行系統大部分呼叫無限制 可以透過 signal 架構發送信號給行程 可以透過 socket 有網路存取功能 可以使用 ioctl 進行比較低階系統溝通 可以使用 fork, exec 呼叫產生與建立行程 … ( 大部分都可以呼叫執行任意使用 )

Page 16: Selinux twnic 201008-sa

16

傳統 傳統 Linux Linux 權限問題權限問題

傳統 Linux 權限規範問題總結基於使用者身份決定權限,非個別行程無法針對 行程 本身提供細部更別的權限規範

root 身份運作的程式具備最高權限而無限制

Page 17: Selinux twnic 201008-sa

SELinux 架構概觀

Page 18: Selinux twnic 201008-sa

18

SELinux SELinux 架構概觀架構概觀

權限控管機制模式 DAC ( Discretionary Access Control)

自主存取控制 依據當時候運作的身份決定存取權限

MAC ( Mandatory Access Control ) 強制存取控制 依據指定條件決定是否具備可存取權限

Page 19: Selinux twnic 201008-sa

19

SELinux SELinux 架構概觀架構概觀

SELinux 權限控管模式本身為 MAC 層級提供存取控制

實際應用,程式執行期需要滿足 MAC 與 DAC 規範環境限制才允許進行存取

Page 20: Selinux twnic 201008-sa

20

SELinux SELinux 架構概觀架構概觀

SELinux 規範項目MAC - Mandatory Access ControlRBAC - Role Based Access ControlTE - Type EnforcementDomain Translation

Page 21: Selinux twnic 201008-sa

21

SELinux SELinux 架構概觀架構概觀

MAC (Mandarory Access Control)提供完整徹底的規範機制

檔案系統面: read, write, unlink, rename,

mount...

網路通訊面: listen, accept, bind... 等

行程管理面: fork, signal... 等

Page 22: Selinux twnic 201008-sa

22

SELinux SELinux 架構概觀架構概觀

RBAC (Role Based Access Control)透過 Role (角色 ) 來規範權限

可以規範每個使用者具備的角色身份切換到特定角色身份才賦予特定存取權限

Page 23: Selinux twnic 201008-sa

23

SELinux SELinux 架構概觀架構概觀

TE (Type Enforcement)只會給行程最小所需的運作環境

制定許多 Type 項目,套用於指定檔案與網路項目

規範行程以特定 Domain 運作最後限制特定的 Domain 能夠存取特定的 Type

Page 24: Selinux twnic 201008-sa

24

SELinux SELinux 架構概觀架構概觀

Domain Translation規範不同行程的 Domain 轉移方式

A Process 規範使用 A-Domain 環境執行B Process 規範使用 B-Domain 環境執行於 A-Domain 環境執行 B ,是否要沿用還是轉移

Page 25: Selinux twnic 201008-sa

SELinux 模式切換與啟用

Page 26: Selinux twnic 201008-sa

26

SELinux SELinux 狀態檢視與切換狀態檢視與切換

檢視 selinux 配置與啟動狀態使用 sestatus 命令執行輸出訊息

SELinux status: enabledSELinuxfs mount: /selinuxCurrent mode: enforcingMode from config file: enforcingPolicy version: 21Policy from config file: targeted

Page 27: Selinux twnic 201008-sa

27

SELinux SELinux 狀態檢視與切換狀態檢視與切換

切換 selinux 目前運作模式使用 setenforce 命令

setenforce [ Enforcing | Permissive | 1 | 0 ]

Enforcing 或 1 ,表示強制模式 Permissive 或 0 ,表示寬容模式

Page 28: Selinux twnic 201008-sa

28

SELinux SELinux 啟用啟用

確認事宜決定與安裝所需 Policy database

GRUB 啟動核心參數設定設定預設啟用模式filesystem 進行 security context 重新標記

Page 29: Selinux twnic 201008-sa

29

SELinux SELinux 啟用啟用

SELinux Policy Database 套件RHEL/CentOS 提供套件包項目

selinux-policy-targeted

selinux-policy-strict

selinux-policy-mls

使用 yum 程式進行安裝 yum install …..

Page 30: Selinux twnic 201008-sa

30

SELinux SELinux 啟用啟用

檢視 Policy Database 相關規則可以透過 sesearch 命令

sesearch -a

sesearch -a -s ftpd_t

Page 31: Selinux twnic 201008-sa

31

SELinux SELinux 啟用啟用

GRUB 配置GRUB 啟動核心參數

預設是否支援,於編譯核心時可以指定可以於載入 kernel 決定是否啟用 selinux 支援配置參數: selinux=[0|1]

GRUB 的 /boot/grub/menu.lst 配置 kernel /boot/vmlinuz root=/dev/sda1 selinux=1

Page 32: Selinux twnic 201008-sa

32

SELinux SELinux 啟用啟用

SELinux 啟動配置設定檔案檔案清單

/etc/sysconfig/selinux 或 /etc/selinux/config

內容組成SELINUX=[ enforcing | permissive | disabled ]

SELINUXTYPE=[ targeted | strict | mls ]

Page 33: Selinux twnic 201008-sa

33

SELinux SELinux 啟用啟用

SELinux 啟用設定說明組成項目SELINUX=[ enforcing | permissive | disabled ]

enforcing ) 強制模式,並提供限制存取機制permissive ) 寬容模式,以警告代替強制規範disabled ) 關閉

指定預設 SELinux 啟用的模式環境

Page 34: Selinux twnic 201008-sa

34

SELinux SELinux 啟用啟用

SELinux 啟用設定說明組成項目

SELINUXTYPE=[ targeted | strict | mls ]

targeted ) 僅保護特定有策略規則的服務程式strict ) 完整保護受限模式mls ) mls 保護受限模式

指定要載入使用的政策資料庫項目

Page 35: Selinux twnic 201008-sa

35

SELinux SELinux 啟用啟用

針對檔案系統重新標記安全本文每個檔案目錄都有一份安全本文指定開機時重新標記安全本文方式

touch /.autorelabel

reboot

Page 36: Selinux twnic 201008-sa

SELinux 安全本文與布林值

Page 37: Selinux twnic 201008-sa

37

SELinux SELinux 安全本文與布林值安全本文與布林值

SELinux 存取規範透過 “安全本文” 與 “布林值” 決定相關程式是否可以存取相關資源

Page 38: Selinux twnic 201008-sa

38

SELinux SELinux 安全本文安全本文

SELinux 安全本文系統相關行程、檔案目錄與網路資源都具備一個標籤資訊,稱呼安全本文

劃分成為 Subject 與 Object 來進行規範

Page 39: Selinux twnic 201008-sa

39

SELinux SELinux 安全本文安全本文

SELinux 安全本文Subject

表示要進行存取操作的行程項目比方提供服務的 ftpd 程式本身

Object

表示要存取的項目包含檔案系統、網路、行程溝通存取等項目

Page 40: Selinux twnic 201008-sa

40

SELinux SELinux 安全本文安全本文

SELinux 安全本文SELinux 依據 Subject 與 Object 內的安全本文的規則,決定相關的行為是否可以存取

Page 41: Selinux twnic 201008-sa

41

SELinux SELinux 安全本文安全本文

SELinux 運作示意

Page 42: Selinux twnic 201008-sa

42

SELinux SELinux 安全本文檢視安全本文檢視

檢視 Security Context 相關程式指令傳入 -Z 參數

id -Z

ls -Z | ls --scontext

ps Z | ps -Z | ps axZ | ps auxZ

netstat -Z | netstat -ntuZ

Page 43: Selinux twnic 201008-sa

43

SELinux SELinux 安全本文組成安全本文組成

Security Context 組成一般格式組成

identity:role:type

格式組成說明 identity ( 身份識別 ) role (角色 ) type (類型 )

Page 44: Selinux twnic 201008-sa

44

SELinux SELinux 安全本文組成安全本文組成

Security Context 組成其他擴增特殊的格式

MLS ( Multi Level System )

user:role:type:sensitivity:compartments

MCS ( Multi Category System )

user:role:type:sensitivity:category

其中 sensitivity 固定為 s0

Page 45: Selinux twnic 201008-sa

45

SELinux SELinux 安全本文組成安全本文組成

Security Context 組成說明Identity ( 身份 )

近於於系統使用者帳號,提供給 selinux 識別帳號登入主機後,不管程式切換到那個身份帳號,該 identity 都固定相同,一般搭配 role 使用

root 表示 root 帳號身份

system_u 表示系統行程

user_u 表示一般使用者帳號

Page 46: Selinux twnic 201008-sa

46

SELinux SELinux 安全本文組成安全本文組成

Security Context 組成說明Role (角色 )

可以確認所屬類型是使用者、行程或檔案等項目

檔案目錄,一般為 object_r

行程資訊,一般為 system_r

使用者 targeted polcy 下為 system_r

strict policy 下細分 sysadm_r, staff_r , user_r

Page 47: Selinux twnic 201008-sa

47

SELinux SELinux 安全本文組成安全本文組成

Security Context 組成說明Type (類型 )

SELinux 內的 type 又可以稱呼為 domain

檔案目錄 , 行程皆有一個 type 標示規範項目一般檔案目錄主要稱呼為 type

執行中的行程主要稱呼為 domain

可以依據 type 相關組合來限制可以存取資源項目

Page 48: Selinux twnic 201008-sa

48

系統服務程式與安全本文系統服務程式與安全本文

系統服務程式與安全本文一般預設 targeted policy 設定,服務程式需要由 /etc/init.d/ 目錄執行才會有轉移進而啟用 selinux 的存取規範功能

Page 49: Selinux twnic 201008-sa

SELinux 安全本文與布林值修改

Page 50: Selinux twnic 201008-sa

50

SELinux SELinux 安全本文修改安全本文修改

變更檔案目錄 Security Context程式項目

chcon

restorecon

fixfiles

Page 51: Selinux twnic 201008-sa

51

SELinux SELinux 安全本文修改安全本文修改

chcon

說明變更檔案目錄的 security context

使用方式chcon -t var_t /etc/vsftpd/vsftpd.conf

chcon --reference=/var/www/html index.html

Page 52: Selinux twnic 201008-sa

52

SELinux SELinux 安全本文修改安全本文修改

restorecon

說明依據設定錄恢復檔案目錄安全本文

使用方式restorecon filename

附註依據 /etc/selinux/<POLICY>/contexts/files/ 內

file_contexts 與 file_contexts.local 恢復

Page 53: Selinux twnic 201008-sa

53

SELinux SELinux 安全本文修改安全本文修改

fixfiles

說明依據設定錄恢復檔案目錄安全本文

使用方式 fixfiles { check | restore|[-F] relabel } [[dir] ... ]

fixfiles -R rpmpackage[,rpmpackage...] { check |

restore }

Page 54: Selinux twnic 201008-sa

54

SELinux Boolean SELinux Boolean 項目項目

SELinux Boolean 功能 SELinux 歸範了許多規則項目,可以透過調整 boolean value 達成開啟與關閉功能

實際 boolean value 檔案於 /selinux/bootleans 目錄

可以透過 getsebool 與 setsebool 管理

Page 55: Selinux twnic 201008-sa

55

檢視 檢視 SELinux BooleanSELinux Boolean

getsebool說明:列出所有 selinux bool 值清單列表與內容使用方式: getsebool [ -a ]

使用範例 getsebool ftpd_disable_trans

getsebool -a

Page 56: Selinux twnic 201008-sa

56

修改 修改 SELinux BooleanSELinux Boolean

setsebool說明:修改指定 selinux bool 數值內容使用方式: setsesebool [-P] name=value

使用範例 setsebool ftpd_disable_trans=on

Page 57: Selinux twnic 201008-sa

57

GUI GUI 介面工具介面工具

GUI 環境檢視與修改工具安裝 policycoreutils-gui 套件使用 system-config-selinux 程式

Page 58: Selinux twnic 201008-sa

SELinux 問題診斷

Page 59: Selinux twnic 201008-sa

59

SELinux SELinux 問題診斷問題診斷

服務問題診斷方式使用 auditd ,配合 audit2way

使用 setroubleshoot , 配合 sealert

Page 60: Selinux twnic 201008-sa

60

SELinux SELinux 問題診斷問題診斷

使用 auditd 服務診斷問題啟用 auditd 服務,配合與 audit2way 檢視問題點

服務管理 /etc/init.d/auditd { start | stop | restart }

chkconfig --list auditd

chkconfig auditd on

訊息檢視 audit2way < /var/log/audit/audit.log

Page 61: Selinux twnic 201008-sa

61

SELinux SELinux 問題診斷問題診斷

使用 setroubleshoot 服務診斷問題啟用 setroubleshoot 服務,於

/var/log/messages 檔案提供訊息紀錄,後續可以提供 X 環境檢視,或搭配 sealert 檢視問題點

服務管理 /etc/init.d/setroubleshoot { start | stop | restart }

chkconfig --list setroubleshoot

chkconfig setroubleshoot on

Page 62: Selinux twnic 201008-sa

62

SELinux SELinux 問題診斷問題診斷

使用 setroubleshoot 服務診斷問題訊息檢視

sealert -l <msg-id>

Page 63: Selinux twnic 201008-sa

SELinux 於 Apache 項目

Page 64: Selinux twnic 201008-sa

64

SELinux SELinux 於 於 Apache Apache 項目項目

security context 與 boolean valueman httpd_selinux

網頁檔案目錄 security context一般網頁項目

httpd_sys_content_t ( 一般項目 )

public_content_t ( 與其他服務並用 )

Page 65: Selinux twnic 201008-sa

65

SELinux SELinux 於 於 Apache Apache 項目項目

selinux boolean是否可以執行 cgi 程式

setsebool [-P] httpd_enable_cgi=[on|off]

是否可以執行 phpsetsebool [-P] httpd_builtin_scripting=[on|off]

Page 66: Selinux twnic 201008-sa

66

SELinux SELinux 於 於 Apache Apache 項目項目

selinux boolean是否可以存取網路連結

setsebool [-P] httpd_can_network_connect=[on|off]

是否關閉 apache 保護限制setsebool [-P] httpd_disable_trans=[on|off]

Page 67: Selinux twnic 201008-sa

67

SELinux SELinux 於 於 Apache Apache 項目項目

非 port 80, 8080 listening 使用檢視可 listen 的 port

semanage port -l | grep http_port_t

新增可 listen 的 portsemanage port -a -t http_port -p tcp <port>

Page 68: Selinux twnic 201008-sa

68

SELinux SELinux 於 於 Apache Apache 項目項目

新增檔案目錄套用的 security

context檢視預設套用的安全本文

semanage fcontext -l | grep httpd_sys_content_t

新增預設套用的安全本文semanage fcontext -a -t httpd_sys_content_t

'/path(/.*)?‘

Page 69: Selinux twnic 201008-sa

SELinux 於 Samba 項目

Page 70: Selinux twnic 201008-sa

70

SELinux SELinux 於 於 Samba Samba 項目項目

security context 與 boolean valueman samba_selinux

Page 71: Selinux twnic 201008-sa

71

SELinux SELinux 於 於 Samba Samba 項目項目

檔案目錄 security context一般檔案目錄

public_content_t ( 提供一般存取 )

Page 72: Selinux twnic 201008-sa

72

SELinux SELinux 於 於 Samba Samba 項目項目

新增檔案目錄套用的 security

context檢視預設套用的安全本文

semanage fcontext -l | grep samba_share_t

新增預設套用的安全本文semanage fcontext -a -t \

samba_share_t '/path(/.*)?'

Page 73: Selinux twnic 201008-sa

73

SELinux SELinux 於 於 Samba Samba 項目項目

selinux boolean是否可以存取家目錄分享

setsebool [-P] samba_enable_home_dirs=[on|off]

是否關閉 samba 保護限制setsebool [-P] samba_disable_trans=[on|off]

Page 74: Selinux twnic 201008-sa

SELinux 於 FTP 項目

Page 75: Selinux twnic 201008-sa

75

SELinux SELinux 於 於 FTP FTP 項目項目

security context 與 boolean valueman ftpd_selinux

Page 76: Selinux twnic 201008-sa

76

SELinux SELinux 於 於 FTP FTP 項目項目

檔案目錄 security context匿名登入的一般檔案目錄

public_content_t ( 提供一般存取 )

public_content_rw_t ( 提供檔案寫入存取 )

Page 77: Selinux twnic 201008-sa

77

SELinux SELinux 於 於 FTP FTP 項目項目

新增檔案目錄套用的 security

context檢視預設套用的安全本文

semanage fcontext -l | grep public_content_t

新增預設套用的安全本文semanage fcontext -a -t \

public_content_t '/path(/.*)?'

Page 78: Selinux twnic 201008-sa

78

SELinux SELinux 於 於 FTP FTP 項目項目

selinux boolean是否用一般帳號登入時存取家目錄

setsebool [-P] ftp_home_dir=[on|off]

是否允許可以讀寫標示該類型的檔案 public_content_rw_tsetsebool [-P] allow_ftpd_anon_write=[on|off]

Page 79: Selinux twnic 201008-sa

79

SELinux SELinux 於 於 FTP FTP 項目項目

selinux boolean是否完全開放檔案的讀寫存取限制

setsebool [-P] allow_ftpd_full_access=[on|off]

是否存取存取 nfs 與 cifs 掛載存取分享setsebool [-P] allow_ftpd_use_nfs=[on|off]

setsebool [-P] allow_ftpd_use_cifs=[on|off]

Page 80: Selinux twnic 201008-sa

80

SELinux SELinux 於 於 FTP FTP 項目項目

selinux boolean是否關閉 ftpd 保護限制

setsebool [-P] ftpd_disable_trans=[on|off]

Page 81: Selinux twnic 201008-sa

81

SELinux SELinux 於 於 FTP FTP 項目項目

非 port 21 使用檢視可 listen 的 port

semanage port -l | grep ftp_port_t

新增可 listen 的 portsemanage port -a -t ftp_port_t -p tcp <port>

Page 82: Selinux twnic 201008-sa

SELinux 於 SSH 項目

Page 83: Selinux twnic 201008-sa

83

SELinux SELinux 於 於 SSH SSH 項目項目

檔案目錄 security context一般設定檔案

etc_t

private keysshd_key_t

Page 84: Selinux twnic 201008-sa

84

SELinux SELinux 於 於 SSH SSH 項目項目

非 port 22 使用檢視可 listen 的 port

semanage port -l | grep ssh_port_t

新增可 listen 的 portsemanage port -a -t ssh_port_t -p tcp <port>

Page 85: Selinux twnic 201008-sa

85

SELinux SELinux 於 於 SSH SSH 項目項目

selinux boolean是否關閉 ssh 保護限制

setsebool [-P] ftpd_disable_trans=[on|off]

Page 86: Selinux twnic 201008-sa

SELinux 於 Squid 項目

Page 87: Selinux twnic 201008-sa

87

SELinux SELinux 於 於 Squid Squid 項目項目

檔案目錄 security context一般設定檔案

squid_conf_t

cache 目錄結構與檔案squid_cache_t

Page 88: Selinux twnic 201008-sa

88

SELinux SELinux 於 於 Squid Squid 項目項目

非常見 port 3128,8080 使用檢視可 listen 的 port

semanage port -l | grep http_cache_port_t

新增可 listen 的 portsemanage port -a -t http_cache_port_t \

-p tcp <port>

Page 89: Selinux twnic 201008-sa

89

SELinux SELinux 於 於 Squid Squid 項目項目

selinux boolean是否關閉 ssh 保護限制

setsebool [-P] squid_disable_trans=[on|off]

Page 90: Selinux twnic 201008-sa

SELinux 自訂規則項目

Page 91: Selinux twnic 201008-sa

91

SELinux SELinux 自訂規則項目自訂規則項目

產生自訂相關服務允許規則由 auditd 的 AVC 訊息給 audit2allow 產生 .te 格式檔案

修改 .te 格式內容,使用 checkmodule 檢查語法正確度,最後編譯產生 .pp 格式的 binary 檔案

使用 semodule_package 建立一個 .mo 檔案使用 semodule 載入 .pp 內容放入 .mo 檔案提供使用

Page 92: Selinux twnic 201008-sa

92

SELinux SELinux 自訂規則項目自訂規則項目

流程步驟cat /var/log/audit/audit.log | audit2allow \

-m local > local.techeckmodule -M -m -o local.mod local.tesemodule_package -o local.pp -m local.modsemodule -i local.pp

Page 93: Selinux twnic 201008-sa

93

Q & AQ & A