分析SELinux的安全策略: sepolicy

一、未正確載入SELinux Policy (sepolicy)

SELinux是Linux的一個強制訪問控制(MAC)系統。然而,有時候管理員會禁用SELinux機制,因為這需要更多的系統配置和管理工作以確保可靠性和安全性。當系統未正確載入SELinux Policy時,會導致各種問題,尤其是在運行需要特權(如root)用戶的應用程序時。在這種情況下,應用程序可能會發現它無法訪問必要的資源,例如文件、設備以及其他系統對象。

下面給出一個示例:

[root@localhost ~]# id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[root@localhost ~]# getenforce
Permissive

上述示例中的”Permissive”意味著SELinux並沒有實施策略。 在這種情況下,可以使用以下命令複製默認的SELinux策略並載入它:

[root@localhost ~]# cp -p /etc/selinux/targeted/policy/policy.24 /etc/selinux/targeted/policy/policy.24_ORG
[root@localhost ~]# setenforce 1

使用”setenforce 1″可以重新實施SELinux政策。

二、分析SELinux Policy (sepolicy) 中的文件結構

在SELinux policy文件中,規則採用不同的格式來表示。 這些規則是由文件 (type), 別名 (alias), 能力 (capability), 內核對象 (class), 和動作 (permission)組成的。其中,類型(type)是策略中最重要的組成部分之一。它們定義了一個對象的安全上下文。

類型(type)關係的例子:

# 我們需要保護foo.conf的以下幾個屬性
# - 文件內容 (content)
# - 文件屬性 (attributes)
# - 文件目錄 (directory)
# - 文件執行 (execute)
# - 文件寫入 (write)

# SELinux 文件類型是 「httpd_sys_content_t」
type httpd_sys_content_t;
# 定義httpd_sys_content_t規則之間的關係
allow httpd_sys_content_t content_t:file { read getattr open };
allow httpd_sys_content_t attributes_t:file { getattr };
allow httpd_sys_content_t directory_t:dir { search read getattr open };
allow httpd_sys_content_t exec_t:file { execute };
allow httpd_sys_content_t file_t:file { read create write getattr setattr append unlink link rename };

類型(type)用於定義進程的安全上下文。 它還定義了允許進程訪問哪些對象;都是哪些操作。這裡展示了一個更具體的類型定義,包括了若干個SELinux規則:

# SELinux 文件類型是 「httpd_sys_script_exec_t」
type httpd_sys_script_exec_t;
# 執行許可權
type_transition httpd_t httpd_sys_exec_t : httpd_sys_script_exec_t;

# 定義 「httpd_sys_script_exec_t」 對象允許操作的文件類型。
# httpd_sys_script_exec_t類型可以讀取系統包含PHP腳本程序
allow httpd_sys_script_exec_t httpd_sys_content_t:file { execute_no_trans };
allow httpd_sys_script_exec_t usr_t:file execute;

三、測試並調試SELinux Policy (sepolicy) 文件

SELinux Policy通常由多個模塊組成,每個模塊代表一組規則或指令。 在Linux中,我們可以使用「semodule」命令創建,安裝和卸載這些模塊。 但在該模塊安裝之前,建議使用SETools庫中的semodule_package命令在離線系統上構建模塊。

yum install policycoreutils-python-utils
semodule_package -o example.pp -m example.te
semodule -i example.pp

一旦成功安裝SELinux Policy,我們可以使用「semanage」工具查看並更改策略信息。以下是一些示例命令:

# 顯示指定類型的SELinux規則信息
semanage fcontext -l -t httpd_sys_content_t | more

# 獲取SELinux用戶的默認上下文
semanage login -l

# 設定默認上下文
semanage login -m -s user_u test

# 設定本地啟動策略
semanage boolean -l | grep audit
semanage boolean -D enableaudit off
semanage boolean -D enableaudit on

四、調試SELinux Policy (sepolicy) 文件

在調試SELinux Policy文件時,有兩個主要的方法:audit2allow和audit2why。

audit2allow 命令可以處理selinux audit日誌,它分析日誌文件並提供用於修復訪問拒絕問題的 SELinux許可權修補程序。下面給出一個示例:

# 查看/目錄下是否存在文件 touch
[root@localhost /]# touch /temp/hello.txt

# /var/log/audit/audit.log現在肯定現實的是「type = AVC 「和「denied」的日誌消息
[root@localhost /]# ausearch -m avc -ts recent | audit2allow

audit2why 命令用於分析SELinux內核日誌,以確定哪些規則阻止 SELinux 在處理特定動作時工作。以下是一個示例:

audit2why < /var/log/audit/audit.log

五、總結

本文討論了在未正確載入SELinux Policy時會面臨哪些問題,以及分析SELinux Policy文件時的文件結構和配置方法。此外,我們還介紹了如何測試和調試你的SELinux策略。穀粒提醒,必須正確設置SELinux Policy文件,以保護系統免受來自外部攻擊的威脅。

原創文章,作者:VKAMV,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/329321.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
VKAMV的頭像VKAMV
上一篇 2025-01-14 18:55
下一篇 2025-01-14 18:55

相關推薦

  • Selinux Permissive

    Selinux是Linux中的一個強制訪問控制(MAC)系統,它通過強制使用安全策略來保護系統免受攻擊和入侵,防止非授權的進程和用戶進行惡意活動。Permissive模式是Seli…

    編程 2025-04-12
  • Android SELinux的詳細闡述

    一、SELinux許可權定義 SELinux(Security Enhanced Linux)是一種Linux內核安全模塊,可以通過定義強制訪問控制規則對用戶、進程、文件、網路等進行…

    編程 2025-02-25
  • 關閉SELinux的命令詳解

    SELinux(Security-Enhanced Linux)是一種Linux內核安全模塊,它為每個進程分配安全上下文,規範了進程之間的交互,防止惡意代碼利用漏洞進行攻擊。但是在…

    編程 2024-12-19
  • 編寫php的安全策略,php技術網php安全技術

    本文目錄一覽: 1、怎麼解決php 代碼安全 2、如何實現php的安全最大化?怎樣避免sql注入漏洞和xss跨站腳本攻擊漏洞 3、編寫php代碼時需要注意些什麼 4、php安全配置…

    編程 2024-12-16
  • 如何禁用SELinux

    一、禁用SELinux的好處 SELinux是一種強制訪問控制安全機制,在保證伺服器安全的同時,也可能造成一些不必要的限制。禁用SELinux的好處主要有以下幾點: 1、減少不必要…

    編程 2024-12-15
  • CentOS 7 關閉 SELinux

    一、什麼是 SELinux SELinux(Security-Enhanced Linux)是一個在 Linux 內核中運行的安全子系統,它基於強制訪問控制(MAC)的原理來實現系…

    編程 2024-12-12
  • Selinux Permissive詳解

    一、Selinux Permissive的基本概念 Selinux Permissive指的是Selinux的一種狀態,即安全標籤與策略的實際執行狀態僅記錄在日誌文件中,不會對系統…

    編程 2024-12-12
  • 詳解臨時關閉SELinux

    一、什麼是SELinux SELinux,全名為安全增強式 Linux(Security-Enhanced Linux),是一個基於 Linux 內核的訪問控制安全機制,具有基於角…

    編程 2024-11-19
  • Selinux enforcing詳解

    一、Selinux enforcing優勢 Selinux是一種Mandatory Access Control系統,通過對系統資源、進程和用戶進行細粒度的訪問控制,能夠大大增加系…

    編程 2024-10-04
  • 永久關閉Selinux

    一、永久關閉Selinux安全機制 Selinux是Linux系統中的一個安全機制,它的作用是提高系統的安全性,限制進程的許可權,保護系統不受到惡意攻擊。但是,有時候我們需要關閉Se…

    編程 2024-10-03

發表回復

登錄後才能評論