Selinux学习笔记

第一:Selinux概述

自主访问控制DAC
    ugo/rwx基本权限
    文件系统的特殊位
    访问控制列表ACL

强制访问控制MAC(MAC在目前的开源是实现中,redheat系列是Selinux,suse,ubuntu系列中的实现是AppArmor)
    NSA开发的安全增强型Linux(目的是把权力关在笼子里)(主体 对象 动作)

第二:修改selinux的策略

    永久修改
        cat /etc/selinux/config
    临时修改
        setenforce [ 0|1 ]

Selinux的三种状态,及如何查看是否有挂载虚拟的selinux文件系统

    enforcing:  1   强制模式
    permissive: 0   宽容模式,不阻止违规行为,只是记录日志()
    disabled:       关闭模式

查看虚拟的selinux文件系统挂载情况
[root@CDN1 ~]# mount | grep selinuxfs
selinuxfs on /sys/fs/selinux type selinuxfs (rw,relatime)

第四:Selinux中"一切都得有一个标签" label=context

    所有用户,文件,进程,网络端口资源都具有与其关联的Selinux标签/上下文
    Selinux策略决策是基于资源的标签/上下文进行的(标签和上下文是一个东西)
    普通文件对象的Selinux标签/上下文存储在文件系统的扩展属性(可以理解为存在inode)中

使用-Z可以看到
[root@CDN1 ~]# id -Z
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

[root@CDN1 ~]# ls -Z /etc/ssh/sshd_config   #查看文件的selinux上下文
-rw-------. root root system_u(表示系统创建的):object_r:etc_t:s0       /etc/ssh/sshd_config
[root@CDN1 ~]# ls -Z test
-rw-r--r--. root root unconfined_u(一般都是用户创建的):object_r:admin_home_t:s0 test

[root@CDN1 ~]# ls -dZ /var/www/html         #查看目录的selinux上下文
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html

[root@CDN1 ~]# ps -eZ | grep sshd
system_u:system_r:sshd_t:s0-s0:c0.c1023 3815 ? 00:00:00 sshd
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 4079 ? 00:00:00 sshd

注意:
    新创建文件,他会继承父目录的上下文
    复制文件,他会继承父目录的上下文
    移动文件,他会保留父目录的上下文(这就是 bind软件zone你要移动而不是在外面新建好移动的原因)

第五:Selinux标签包含四部分信息(注意下划线后面的标识),第三个字段是最重要的

    selinux用户user:selinux角色role:类型type:敏感度级别level
    system_u(表示系统创建的):object_r:etc_t:s0

selinux的用户管理

安装semanage命令工具
    yum install policycoreutils-python -y

管理映射关系
    查看映射关系:semanage login -l
    创建新的映射:semanage login -a -s staff_u tom
    修改新的映射:semanage login -m -s sysadm_u tom    (如何修改默认关系)
    删除新的映射:semanage login -d tmo
管理selinux用户
    查看用户:semanage user -l
    创建自定义用户:semanage user -a -R staff_r swift_u
    修改自定义用户:semanage user -m -R "staff_r sysadm_r" swift_u
    删除自定义用户:semanage user -d swift_u

第六:Selinux的布尔值

A:查看有哪些可设置的Boolean选项
[root@CDN1 ~]# semanage boolean -l
SELinux boolean                State  Default Description
privoxy_connect_any            (on   ,   on)  Allow privoxy to connect any
smartmon_3ware                 (off  ,  off)  Allow smartmon to 3ware
mpd_enable_homedirs            (off  ,  off)  Allow mpd to enable homedirs
xdm_sysadm_login               (off  ,  off)  Allow xdm to sysadm login
xen_use_nfs                    (off  ,  off)  Allow xen to use nfs
mozilla_read_content           (off  ,  off)  Allow mozilla to read content
ssh_chroot_rw_homedirs         (off  ,  off)  Allow ssh to chroot rw homedirs
*************************************省略***********************************

B:在selinxfs中查看有关http的相关策略设置
[root@CDN1 ~]# ls /sys/fs/selinux/booleans/ | grep ^http
httpd_anon_write
httpd_builtin_scripting
httpd_can_check_spam
httpd_can_connect_ftp
httpd_can_connect_ldap
httpd_can_connect_mythtv
httpd_can_connect_zabbix
httpd_can_network_connect
httpd_can_network_connect_cobbler
httpd_can_network_connect_db
httpd_can_network_memcache
httpd_can_network_relay

C:修改默认的Boolean值
    读取当前 user_exec_content 的值   #表示允许用户执行 home 及 /tmp目录下的脚本,这里的用户是指 selinux中的user_u 用户
[root@CDN1 ~]# getsebool user_exec_content
user_exec_content --> on

    把 user_exec_content 的值修改为 off   #表示不允许用户执行 home 及 /tmp目录下的脚本
[root@CDN1 ~]# setsebool user_exec_content off     #-p 添加-p 表示永久生效

    把tom映射为user_u(tom用户需要重新登录才会生效)
[root@CDN1 ~]# semanage login -a -s user_u tom      #后期还原,删除用户映射  semanage login -d tom
[root@CDN1 ~]# semanage login -l
Login Name           SELinux User         MLS/MCS Range        Service
__default__          unconfined_u         s0-s0:c0.c1023       *
root                 unconfined_u         s0-s0:c0.c1023       *
system_u             system_u             s0-s0:c0.c1023       *
tom                  user_u               s0                   *

D:验证:tom执行home及/tmp目录中的文件就会失败
    id -Z   #第一列表示在selinux中的 用户角色
    /home/tom/1.sh  #失败
    /tmp/1.sh   #失败
声明:本文为原创,作者为 辣条①号,转载时请保留本声明及附带文章链接:https://boke.wsfnk.com/archives/476.html
谢谢你请我吃辣条谢谢你请我吃辣条

如果文章对你有帮助,欢迎点击上方按钮打赏作者

最后编辑于:2022/12/18作者: 辣条①号

现在在做什么? 接下来打算做什么? 你的目标什么? 期限还有多少? 进度如何? 不负遇见,不谈亏欠!

暂无评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

arrow grin ! ? cool roll eek evil razz mrgreen smile oops lol mad twisted wink idea cry shock neutral sad ???

文章目录