Linux命令-sesearch(SELinux 策略规则搜索)

📅 2026/7/24 11:46:18
Linux命令-sesearch(SELinux 策略规则搜索)
Linux命令-sesearchSELinux 策略规则搜索快速参考基本语法安装 sesearch常用选项实际应用场景规则类型详解与 seinfo 配合使用高级搜索故障排查总结快速参考sesearch用于在 SELinux 策略中搜索允许的规则。它可以查找哪些类型可以访问哪些类型、哪些域可以执行哪些程序等是分析和调试 SELinux 策略的核心工具。与seinfo查询策略对象不同sesearch查询的是策略规则。应用场景分析 SELinux 拒绝原因、查找类型之间的允许关系、验证策略修改是否生效。基本语法# 基本语法sesearch[选项][策略文件]# 查找允许的规则sesearch--allow# 查找特定源类型的允许规则sesearch--allow-shttpd_t# 查找特定目标类型的允许规则sesearch--allow-thttpd_sys_content_t# 查找特定类别的规则sesearch--allow-cfile安装 sesearch# Debian/Ubuntusudoaptupdatesudoaptinstallsetools# RHEL/CentOSsudoyuminstallsetools-console# Fedorasudodnfinstallsetools-console# 验证安装whichsesearch sesearch--help常用选项# --allow 查找 allow 规则sesearch--allow-shttpd_t# --neverallow 查找 neverallow 规则sesearch--neverallow# --auditallow 查找 auditallow 规则sesearch--auditallow# --dontaudit 查找 dontaudit 规则sesearch--dontaudit# -s 源类型 指定源类型subjectsesearch--allow-shttpd_t# -t 目标类型 指定目标类型objectsesearch--allow-thttpd_sys_content_t# -c 类别 指定对象类别file, dir, lnk_file 等sesearch--allow-cfile# --source 仅显示源类型sesearch--allow-shttpd_t--source# --target 仅显示目标类型sesearch--allow-thttpd_sys_content_t--target# --count 仅统计数量sesearch--allow-shttpd_t--count实际应用场景# 场景1分析 HTTP 服务可以访问哪些文件 # 查找 httpd_t 域可以读取哪些文件类型sesearch--allow-shttpd_t-cfile-pread# 查找 httpd_t 域可以写入哪些文件类型sesearch--allow-shttpd_t-cfile-pwrite# 查看 httpd_t 域的所有允许规则sesearch--allow-shttpd_t|less# 场景2分析为什么被 SELinux 拒绝 # 1. 查看审计日志sudoausearch-mAVC-tsrecent# 2. 找出阻止的操作例如httpd 访问 user_home_t# 日志显示denied { read } for pid... commhttpd nameindex.html devsda2 ino... scontextsystem_u:system_r:httpd_t:s0 tcontextsystem_u:object_r:user_home_t:s0 tclassfile# 3. 查找是否允许 httpd_t 读取 user_home_tsesearch--allow-shttpd_t-tuser_home_t-cfile-pread# 如果无输出说明策略不允许# 4. 解决方案修改布尔值或策略sudosetsebool-Phttpd_enable_homedirs on# 场景3验证策略修改是否生效 # 1. 添加自定义策略规则需要编写 .te 文件并编译# ...略# 2. 验证规则已添加sesearch--allow-smyapp_t-tetc_t-cfile# 场景4列出所有域转换规则 # 查找哪些域可以转换到 httpd_tsesearch--type_trans-thttpd_t# 查找 httpd_t 可以转换到哪些域sesearch--type_trans-shttpd_t规则类型详解# allow 规则允许# 语法allow 源类型 目标类型:类别 权限;# 示例allow httpd_t httpd_sys_content_t:file read;# 查找所有 allow 规则sesearch--allow|head-20# neverallow 规则禁止# 语法neverallow 源类型 目标类型:类别 权限;# 示例neverallow ~admin_t ~security_t:security load_policy;# 查找所有 neverallow 规则sesearch--neverallow# auditallow 规则审计允许# 即使允许也记录审计日志sesearch--auditallow# dontaudit 规则不审计# 即使拒绝也不记录审计日志减少日志噪音sesearch--dontaudit与 seinfo 配合使用# seinfo 查询策略对象sesearch 查询策略规则# 1. 先用 seinfo 查找相关类型seinfo-t|grephttpd# 2. 再用 sesearch 查找相关规则sesearch--allow-shttpd_t# 3. 查找某个类型可以访问哪些文件类型sesearch--allow-shttpd_t-cfile|awk{print $3}|sort-u# 4. 查找某个文件类型可以被哪些域访问sesearch--allow-thttpd_sys_content_t-cfile|awk{print $1}|sort-u高级搜索# 搜索特定权限 # 查找所有可以读取文件的规则sesearch--allow-cfile-pread# 查找所有可以写入文件的规则sesearch--allow-cfile-pwrite# 查找所有可以执行文件的规则sesearch--allow-cfile-pexecute# 搜索特定类别 # 文件sesearch--allow-cfile# 目录sesearch--allow-cdir# 符号链接sesearch--allow-clnk_file# 套接字sesearch--allow-cunix_stream_socket# 搜索域转换 # 查找可以转换到 httpd_t 的规则sesearch--type_trans-thttpd_t# 查找 httpd_t 可以转换到哪些类型sesearch--type_trans-shttpd_t# 搜索角色允许 sesearch--role_allow故障排查# 问题1sesearch 命令找不到# 解决安装 setoolssudoaptinstallsetools# 问题2sesearch 报错 policy version mismatch# 原因策略版本不匹配# 解决更新 policycoreutilssudoaptinstallpolicycoreutils# 问题3搜索无结果# 原因类型名错误或确实无此规则# 解决先用 seinfo 确认类型存在seinfo-t|grep类型名总结核心价值sesearch是分析和调试 SELinux 策略的必备工具帮助理解为什么被拒绝和如何修改策略。常用组合sesearch --allow -s 源类型查找源类型的所有允许规则sesearch --allow -t 目标类型查找目标类型的所有允许规则sesearch --allow -s 源 -t 目标 -c 类别 -p 权限精确查找seinfo -t | grep 关键词先查找相关类型再用 sesearch 搜索规则注意修改 SELinux 策略需要谨慎错误的策略可能导致服务无法运行或安全漏洞。