selinux

############selinux的管理############


学习目标
•基本 SELINUX 安全概念
•SELINUX 模式
•更改 SELINUX 模式
•显示进程和文件的 SELINUX 上下文
•管理文件的 selinux 上下文
•管理服务的 bool 值


tips:做此实验之前,要首先配置yum源仓库,然后安装vsftpd 和lftp。
     并且 systemctl start vsftpd
         systemctl stop firewalld.service
         systemctl enable vsftpd




基本 SELINUX 安全性概念
• SELINUX ( 安全增强型 Linux ) 是可保护你系统安全性的额外机制
• 在某种程度上 , 它可以被看作是与标准权限系统并行的权限系统。在常规模式中 , 以用户身份运行进程 ,并且系统上的文件和其他资源都设置了权限 ( 控制哪些用户对哪些文件具有哪些访问权 SELINUX 的另一个不同之处在于 , 若要访问文件 , 你必须具有普通访
• 问权限和 SELINUX 访问权限。因此 , 即使以超级用户身份 root 运行进程 , 根据进程以及文件或资源的 SELinux 安全性上下文可能拒绝访问文件或资源限 ) 标签




原因:
• 在无 selinux 保护时 , 恶意人员可以尝试利用 web 服务器中的安全漏洞强行进入系统。如果成功 , 将会控制以用户apache 身份运行的进程 , 这时再由一个本地安全漏洞就可能使攻击者获得超级用户的访问权限




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_t
• Seliux 策略具有允许以 httpd_t 身份运行的进程访问标记为 httpd_sys_content_t 的文件的规则。没有规则允许这些进程访问标记有 tmp_t 的文件 , 因此将拒绝这些访问 , 即使常规文件权限指出应该允许这些访问






SELINUX 模式
强制模式 : SELINUX 主动拒绝访问尝试读取类型上下文为 tmp_t的 web 服务器。在强制模式中 ,SELINXU 既记录冲突 , 也强制执行规则。
许可模式 : 通常用于对问题进行故障排除。在许可模式下 , 即使没有明确规则 , SELINUX 也允许所有交互 , 并且记录所有被拒绝的交互。此模式可以用于确定你是否有 SELINUX
问题。无需重新引导即可从强制模式转为许可模式 , 或再从许可模式转回强制模式。






显示及更改 SELINUX 模式
• getenforce
• setenforce 0|1
– 0 表示 permissive
# 警告
– 1 表示 enforcing # 强制


更改 selinux 的开机状态
• vim /etc/sysconfig/selinux
注:disable表示关闭,enforcing表示强制,permissive表示警告,disable状态切换到permissive状态或者enforcing状态需要重启系统。




  108  vim /etc/vsftpd/vsftpd.conf
  109  systemctl restart vsftpd.service 
  110  lftp 172.25.254.244
  111  setenforce 1 ##再链接就不可以了
  112  getenforce 
  113  lftp 172.25.254.244
-----------------------------------------------------------------------------
[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf
[root@localhost ~]# systemctl restart vsftpd.service 
[root@localhost ~]# lftp 172.25.254.244
lftp 172.25.254.244:~> ls
drwxrwxr-x    2 0        50             31 May 03 20:00 pub
lftp 172.25.254.244:/> pub
Unknown command `pub'.
lftp 172.25.254.244:/> cd pub
lftp 172.25.254.244:/pub> ls
-rw-------    1 14       50            850 May 03 20:00 group
-rw-------    1 14       50           2005 May 03 19:24 passwd
lftp 172.25.254.244:/pub> rm -fr group 
rm ok, `group' removed
lftp 172.25.254.244:/pub> ls
-rw-------    1 14       50           2005 May 03 19:24 passwd
lftp 172.25.254.244:/pub> touch westos
Unknown command `touch'.
lftp 172.25.254.244:/pub> mkdir westos
mkdir ok, `westos' created
lftp 172.25.254.244:/pub> ls
-rw-------    1 14       50           2005 May 03 19:24 passwd
drwx------    2 14       50              6 May 03 20:10 westos
lftp 172.25.254.244:/pub> rm -fr westos/ passwd 
rm ok, 2 files removed                 
lftp 172.25.254.244:/pub> exit
[root@localhost ~]# 
[root@localhost ~]# lftp 172.25.254.244
lftp 172.25.254.244:~> ls
drwxrwxr-x    2 0        50              6 May 03 20:10 pub
lftp 172.25.254.244:/> cd pub
lftp 172.25.254.244:/pub> ls
lftp 172.25.254.244:/pub> put /etc/passwd
2005 bytes transferred
lftp 172.25.254.244:/pub> ls
-rw-------    1 14       50           2005 May 03 20:16 passwd
lftp 172.25.254.244:/pub> rm -fr passwd 
rm ok, `passwd' removed
lftp 172.25.254.244:/pub> ls
lftp 172.25.254.244:/pub> exit
[root@localhost ~]# setenforce 1
[root@localhost ~]# getenforce 
Enforcing
[root@localhost ~]# lftp 172.25.254.244
lftp 172.25.254.244:~> ls
drwxrwxr-x    2 0        50              6 May 03 20:16 pub
lftp 172.25.254.244:/> cd pub
lftp 172.25.254.244:/pub> ls
lftp 172.25.254.244:/pub> put /etc/passwd
put: Access failed: 553 Could not create file. (passwd)
lftp 172.25.254.244:/pub> exit
[root@localhost ~]# 
-----------------------------------------------------------------------------




显示 SELinux 文件上下文
• 什么确定文件的初始 SELinux 上下文 ? 通常是父目录。将父目录的上下文指定给新创建的文件。这对 vimcp 和 touch 等命令其作用 , 但是 , 如果文件是在其他位置创建的并且保留了权限 ( 与 mv 或 cp -a 一样 ) 则还将保留 SELinux 上下文
• 许多处理文件的命令具有一个用于显示或设置 SELinux 上下文的选项 ( 通常是 -Z ) 。例如 , ps 、 ls 、 cp 和 mkdir 都使用 -Z 选项显示或设置 SELinux 上下文
• 显示上下文
– ps axZ
– ps -ZC
– ls -Z




修改 selinux 安全上下文
• chcon -t
– 一次性定制安全上下文,执行 restorecon 刷新后还原
• semanage fcontext
– 永久更改文件的上下文


    9  ps auxZ | grep vsftpd
   10  ls -lZ /var/ftp
   11  mkdir /westos
   12  ls -Zd /westos
   13  semanage fcontext -l | grep westos
   14  semanage fcontext -l | grep var/ftp
   15  semanage fcontext -a -t public_content_t '/westos(/.*)?'
   16  semanage fcontext -l | grep var/ftp
   17  semanage fcontext -l | grep westos
   18  touch /westos/file{1..5}
   19  cd /westos
   20  ls
   21  cd 
   22  lftp 172.25.254.244
   23  vim /etc/vsftpd/vsftpd.conf ##更改匿名用户家目录为/westos
   24  systemctl restart vsftpd.service 
   25  lftp 172.25.254.244 ##此时登陆后ls,看不到家目录下的内容
   26  restorecon -RvvF /westos/
   27  lftp 172.25.254.244 ##此时可以看到家目录下的内容
   28  history
-----------------------------------------------------------------------------
[root@localhost ~]# ps auxZ | grep vsftpd
system_u:system_r:ftpd_t:s0-s0:c0.c1023 root 4987 0.0  0.0 52760  688 ?        Ss   16:09   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 root 8717 0.0  0.0 112640 932 pts/0 S+ 16:39   0:00 grep --color=auto vsftpd
[root@localhost ~]# ls -lZ /var/ftp
drwxrwxr-x. root ftp system_u:object_r:public_content_t:s0 pub
[root@localhost ~]# mkdir /westos
[root@localhost ~]# ls -Zd /westos
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /westos
[root@localhost ~]# semanage fcontext -l | grep westos
[root@localhost ~]# semanage fcontext -l | grep var/ftp
/var/ftp(/.*)?                                     all files          system_u:object_r:public_content_t:s0 
/var/ftp/bin(/.*)?                                 all files          system_u:object_r:bin_t:s0 
/var/ftp/etc(/.*)?                                 all files          system_u:object_r:etc_t:s0 
/var/ftp/lib(/.*)?                                 all files          system_u:object_r:lib_t:s0 
/var/ftp/lib/ld[^/]*\.so(\.[^/]*)*                 regular file       system_u:object_r:ld_so_t:s0 
[root@localhost ~]# semanage fcontext -a -t public_content_t '/westos(/.*)?'
[root@localhost ~]# semanage fcontext -l | grep var/ftp
/var/ftp(/.*)?                                     all files          system_u:object_r:public_content_t:s0 
/var/ftp/bin(/.*)?                                 all files          system_u:object_r:bin_t:s0 
/var/ftp/etc(/.*)?                                 all files          system_u:object_r:etc_t:s0 
/var/ftp/lib(/.*)?                                 all files          system_u:object_r:lib_t:s0 
/var/ftp/lib/ld[^/]*\.so(\.[^/]*)*                 regular file       system_u:object_r:ld_so_t:s0 
[root@localhost ~]# semanage fcontext -l | grep westos
/westos(/.*)?                                      all files          system_u:object_r:public_content_t:s0 
[root@localhost ~]# touch /westos/file{1..5}
[root@localhost ~]# cd /westos
[root@localhost westos]# ls
file1  file2  file3  file4  file5
[root@localhost westos]# cd 
[root@localhost ~]# lftp 172.25.254.244
lftp 172.25.254.244:~> ls
drwxrwxr-x    2 0        50              6 May 03 20:16 pub
lftp 172.25.254.244:/> cd /westos
cd: Access failed: 550 Failed to change directory. (/westos)
lftp 172.25.254.244:/> exit
[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf
[root@localhost ~]# systemctl restart vsftpd.service 
[root@localhost ~]# lftp 172.25.254.244
lftp 172.25.254.244:~> ls
lftp 172.25.254.244:/> exit          
[root@localhost ~]# restorecon -RvvF /westos/
restorecon reset /westos context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/file1 context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/file2 context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/file3 context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/file4 context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/file5 context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
[root@localhost ~]# lftp 172.25.254.244
lftp 172.25.254.244:~> ls
-rw-r--r--    1 0        0               0 May 03 20:51 file1
-rw-r--r--    1 0        0               0 May 03 20:51 file2
-rw-r--r--    1 0        0               0 May 03 20:51 file3
-rw-r--r--    1 0        0               0 May 03 20:51 file4
-rw-r--r--    1 0        0               0 May 03 20:51 file5
lftp 172.25.254.244:/> exit
[root@localhost ~]# ls -Z /westos
-rw-r--r--. root root system_u:object_r:public_content_t:s0 file1
-rw-r--r--. root root system_u:object_r:public_content_t:s0 file2
-rw-r--r--. root root system_u:object_r:public_content_t:s0 file3
-rw-r--r--. root root system_u:object_r:public_content_t:s0 file4
-rw-r--r--. root root system_u:object_r:public_content_t:s0 file5
-----------------------------------------------------------------------------








semanage 命令
• restorecon 是 policycoreutil 软件包的一部分
• semanage 是 policycoreutil-python 软件包的一部分
• semanage fcontext 可用与显示或修改 restorrecon 用来设置默认文件上下文的规则
• semanage fcontext 使用扩展正则表达式来指定路径和文件名。 fcontext 规则中最常用的扩展正则表达式是(/.*)?, 表示随意地匹配 / 后跟任何数量的字符
• semanage fcontext 将递归地与在表达式前面列出的目录以及该目录中的所有内容相匹配
示例:
    1  lftp 172.25.254.244
    2  getenforce 
    3  mkdir /westos
    4  vin /etc/vsftpd/vsftpd.conf 
    5  vim /etc/vsftpd/vsftpd.conf 
    6  systemctl restart vsftpd.service 
    7  lftp 172.25.254.244  
   17  touch /westos/file{1..5}
   18  cd /westos
   19  ls
   20  semanage fcontext -a -t public_content_t '/westos(/.*)?'
   21  restorecon -RvvF /westos/
   22  lftp 172.25.254.244
   23  touch /westos/file7
   24  lftp 172.25.254.244
   25  ls -Z /westos/
----------------------------------------------------------------------------
[root@localhost ~]# lftp 172.25.254.244
lftp 172.25.254.244:~> ls
drwxr-xr-x    2 0        0               6 Aug 03  2015 pub
lftp 172.25.254.244:/> exit
[root@localhost ~]# getenforce 
Enforcing
[root@localhost ~]# mkdir /westos
[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf 
[root@localhost ~]# systemctl restart vsftpd.service 
[root@localhost ~]# lftp 172.25.254.244
lftp 172.25.254.244:~> ls
lftp 172.25.254.244:/> exit
[root@localhost ~]# touch /westos/file{1..5}
[root@localhost ~]# cd /westos
[root@localhost westos]# ls
[root@localhost westos]# semanage fcontext -a -t public_content_t '/westos(/.*)?'
[root@localhost westos]# restorecon -RvvF /westos/
restorecon reset /westos context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/file1 context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/file2 context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/file3 context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/file4 context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/file5 context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
[root@localhost westos]# lftp 172.25.254.244
lftp 172.25.254.244:~> ls
-rw-r--r--    1 0        0               0 May 01 01:49 file1
-rw-r--r--    1 0        0               0 May 01 01:49 file2
-rw-r--r--    1 0        0               0 May 01 01:49 file3
-rw-r--r--    1 0        0               0 May 01 01:49 file4
-rw-r--r--    1 0        0               0 May 01 01:49 file5
lftp 172.25.254.244:/> ^C[root@localhost westos]# 
[root@localhost westos]# touch /westos/file7
[root@localhost westos]# lftp 172.25.254.244
lftp 172.25.254.244:~> ls
-rw-r--r--    1 0        0               0 May 01 01:49 file1
-rw-r--r--    1 0        0               0 May 01 01:49 file2
-rw-r--r--    1 0        0               0 May 01 01:49 file3
-rw-r--r--    1 0        0               0 May 01 01:49 file4
-rw-r--r--    1 0        0               0 May 01 01:49 file5
-rw-r--r--    1 0        0               0 May 01 01:56 file7
lftp 172.25.254.244:/> exit
[root@localhost westos]# ls -Z /westos/
-rw-r--r--. root root system_u:object_r:public_content_t:s0 file1
-rw-r--r--. root root system_u:object_r:public_content_t:s0 file2
-rw-r--r--. root root system_u:object_r:public_content_t:s0 file3
-rw-r--r--. root root system_u:object_r:public_content_t:s0 file4
-rw-r--r--. root root system_u:object_r:public_content_t:s0 file5
-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 file7
[root@localhost westos]# history 
----------------------------------------------------------------------------






管理 SELinux 布尔值
• SELinux 布尔值是更改 SELinux 策略行为的开关。SELinux 布尔值是可以启用或禁用的规则。安全管理员可
以使用 SELinux 布尔值来调整策略 , 以有选择地进行调整
• 许多软件包都具有 man page *_selinux(8), 其中详细说明了所使用的一些布尔值 ; man -k‘_selinux’ 可以轻松地找到这些手册
• getsebool 用于显示布尔值 , setsebool 用于修改布尔值
• setsebool -P 修改 SELinux 策略 , 以永久保留修改。
  semanage boolean -l 将显示布尔值是否永久
示例:
   前提:文件中没有打开可上传权限








监控 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 用于在该文件中生成所有事件的报告



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值