selinux

selinux是强制访问控制的一种策略,可以指明某一个进程访问哪些资源,在传统的linux中,一切皆文件,由用户、组和权限来控制访问,在selinux中,一切皆对象,由存放在扩展属性领域的安全元素控制访问,所有文件、端口、进程都具备安全上下文
selinux影响着:(1)程序访问文件(2)程序访问功能(微信),进程本身功能,开关

查询selinux状态

命令:getenforce #查询结果有3种状态
1、enforcing #如果违反了策略,无法继续操作,表示强制
2、disabled #禁止
3、permissive #selinux有效,即是违法策略,依旧可以继续操作,但是会有警告,查看警告信息:cat /var/log/audit/audit.log

更改selinux状态

vim /etc/sysconfig/selinux #文件编辑
reboot #重启
测试:

[root@localhost ~]# getenforce
Disabled
[root@localhost ~]#vim /etc/sysconfig/selinux  #修改selinux=Enforcing
[root@localhost ~]#reboot  #重启
[root@localhost ~]# getenforce
Enforcing

临时改变selinux状态

setenforce 0 | 1 #临时改变selinux状态,其中0表示警告模式;1表示强制模式
getenforce 查看状态
查询结果:
Permissive #警告模式,警告不被拒绝
Enforcing #强制模式,拒绝并且警告
测试:

[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]# setenforce 1
[root@localhost ~]# getenforce
Enforcing

selinux安全上下文管理

selinux之安全上下文介绍:https://blog.csdn.net/Ying_smile/article/details/109050734

做selinux安全上下文时首先需要开启vsftpd,关闭firewalld

[root@localhost ~]# systemctl restart vsftpd
[root@localhost ~]# systemctl enable vsftpd
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
rm '/etc/systemd/system/basic.target.wants/firewalld.service'
rm '/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service'

1.安全上下文

ls -Z #查看文件的安全上下文
semanage fcontext -l #内核指定的所有文件的安全上下文的列表 ; 查看文件安全上下文(man semanage fcontext)
测试:

服务端:
[root@localhost ~]# touch /mnt/haha1
[root@localhost ~]# mv /mnt/haha1 /var/ftp/haha1  #复制是新建的过程,移动是重命名的过程,文件系统权限和属性不会改变
[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# cd /var/ftp
[root@localhost ftp]# ls -Z
-rw-r--r--. root root unconfined_u:object_r:mnt_t:s0   haha1  #安全上下文为mnt_t
-rw-r--r--. root root system_u:object_r:public_content_t:s0 hellohello  #安全上下文为public_content_t
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 pub
[root@localhost ftp]# semanage fcontext -l | grep /var/ftp  #内核记录的/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

2.修改安全上下文

2.1.临时修改

chcon [-R] [-t type] 目录|文件 #临时修改安全上下文,在修改了selinux状态后重启系统,安全上下文会变回来
测试:

服务端:
[root@localhost ~]# mkdir /haha2  #建立目录haha2
[root@localhost ~]# touch /haha2/hahafile  #给haha2里面建立文件
[root@localhost ~]# ls -Zd /haha2  #查看haha2的安全上下文
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /haha2
[root@localhost ~]# ls -Zd /var/ftp  #查看ftp的安全上下文
drwxr-xr-x. root root system_u:object_r:public_content_t:s0 /var/ftp
[root@localhost ~]# semanage fcontext -l | grep /haha2  #内核记录过的/haha2的安全上下文
[root@localhost ~]# semanage fcontext -l | grep /var/ftp  #内核记录过的/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 ~]# chcon -t public_content_t /haha2 -R  #修改/haha2的安全上下文public_content_t
[root@localhost ~]# ls -Zd /haha2  #查看安全上下文
drwxr-xr-x. root root unconfined_u:object_r:public_content_t:s0 /haha2
客户端:
[root@localhost ~]# lftp 172.25.254.196
lftp 172.25.254.196:~> ls  #可以查看到haha1
-rw-r--r--    1 0        0               0 May 15 11:02 haha1
-rw-r--r--    1 0        0               0 May 07 12:30 hellohello
drwxr-xr-x    2 0        0              33 Mar 07  2014 pub
lftp 172.25.254.196:/> quit
服务端:
[root@localhost ~]# vim /etc/sysconfig/selinux  #修改selinux的状态为disabled
[root@localhost ~]# reboot  #重启
disabled
[root@localhost ~]# vim /etc/sysconfig/selinux  #修改selinux的状态为enforcing
[root@localhost ~]# reboot  #重启
Enforcing
[root@localhost ~]# ls -Zd /haha2  #/haha2的安全上下文又改变回来,说明chcon -t的修改是临时的
drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /haha2
客户端:
[root@localhost ~]# lftp 172.25.254.196
lftp 172.25.254.196:~> ls   #看不到文件haha1,因为haha1的安全上下文不符
-rw-r--r--    1 0        0               0 May 07 12:30 hellohello
drwxr-xr-x    2 0        0              33 Mar 07  2014 pub
lftp 172.25.254.196:/> quit

2.2.永久修改

semanage fcontext -a -t type 目录|文件 #永久修改安全上下文,-a 添加,-t 指定类型,重启之后不会改变修改
restorecon -Rv 目录 #刷新上下文,并显示过程
测试:

服务端:
[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf  #将匿名用户访问的家目录改成/haha2,anon_root=/haha2为文件中添加的内容
[root@localhost ~]# systemctl restart vsftpd
[root@localhost ~]# semanage fcontext -a -t public_content_t '/haha2(/.*)?'  #永久修改/haha2的安全上下文,(/.*)指目录的所有文件,其中/haha2(/.*)?==/haha2/*
[root@localhost ~]# semanage fcontext -l | grep /haha2  #内核记录过的/haha2安全上下文
/haha2(/.*)?                                       all files          system_u:object_r:public_content_t:s0
[root@localhost ~]# restorecon -FvvR /haha2/  #F刷新,vv显示列表
restorecon reset /haha2 context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /haha2/hahafile context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
[root@localhost ~]# vim /etc/sysconfig/selinux  #改变selinux状态
disabled
[root@localhost ~]# reboot  #重启
[root@localhost ~]# vim /etc/sysconfig/selinux  #改变selinux状态为enforcing
enforcing
[root@localhost ~]# reboot  #重启
[root@localhost ~]# ls -Z /haha2  #查看/haha2的安全上下文没有变回去,说明是永久修改
-rw-r--r--. root root system_u:object_r:public_content_t:s0 hahafile
客户端:
[root@localhost ~]# lftp 172.25.254.196
lftp 172.25.254.196:~> ls  可以查看到haha2里面的文件
-rw-r--r--    1 0        0               0 May 15 12:18 hahafile
lftp 172.25.254.196:/> quit

当seliunx出错时,执行touch /.autorelabel,在根底下建立.autorelabel文件(相当于selinux 自动初始化),然后重启系统,selinux恢复

selinux布尔值

开启selinux后,系统进程会默认加载布尔值,相当于开关,修改布尔值时,selinux在enforcing模式下
setsebool -p 需要修改的内容 布尔值on|off #修改selinux布尔值
getsebool -a | 名称 #查看当前系统所有布尔值
测试:

测试前提是ftp服务默认本地用户有可写功能
[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# getsebool -a | grep ftp  #查看ftp的selinux布尔值
ftp_home_dir --> off
ftpd_anon_write --> off
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> off
ftpd_use_cifs --> off
ftpd_use_fusefs --> off
ftpd_use_nfs --> off
ftpd_use_passive_mode --> off
httpd_can_connect_ftp --> off
httpd_enable_ftp_server --> off
sftpd_anon_write --> off
sftpd_enable_homedirs --> off
sftpd_full_access --> off
sftpd_write_ssh_home --> off
tftp_anon_write --> off
tftp_home_dir --> off
[root@localhost ~]# lftp 172.25.254.196 -u student  #本地用户连接
Password:
lftp student@172.25.254.196:~> put /etc/passwd   #因为ftp的用户家目录的布尔值是关闭状态,所以不能上传文件
put: Access failed: 553 Could not create file. (passwd)
lftp student@172.25.254.196:~> quit
[root@localhost ~]# setsebool -P ftp_home_dir on  #修改ftp_home_dir布尔值为on
[root@localhost ~]# getsebool -a | grep ftp  #查看是否修改
ftp_home_dir --> on
ftpd_anon_write --> off
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> off
ftpd_use_cifs --> off
ftpd_use_fusefs --> off
ftpd_use_nfs --> off
ftpd_use_passive_mode --> off
httpd_can_connect_ftp --> off
httpd_enable_ftp_server --> off
sftpd_anon_write --> off
sftpd_enable_homedirs --> off
sftpd_full_access --> off
sftpd_write_ssh_home --> off
tftp_anon_write --> off
tftp_home_dir --> off
[root@localhost ~]# lftp 172.25.254.196 -u student  #本地用户连接
Password:
lftp student@172.25.254.196:~> put /etc/passwd  #可以上传文件
2044 bytes transferred
lftp student@172.25.254.196:~> quit

监控selinux冲突

当新建文件|目录时,在浏览器查看ftp://ip,查看不到新建文件、目录,并且产生日志
/var/log/messages #查看报错后,程序提供解决方案
/var/log/audit/audit.log #查看报错信息
其中提供解决方案的不是系统而是一个软件程序,但此工具不安全,只是解决当前不能访问的问题;查找安装包:rpm -qa | grep setrouble ;如果删除此工具,在日志/var/log/messages下是不会提供方案,但是/var/log/audit/audit.log中可以看到日志
测试:

[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf  #关闭用户家目录修改
[root@localhost ~]# systemctl restart vsftpd.service
[root@localhost ~]# > /var/log/messages
[root@localhost ~]# cat /var/log/messages
[root@localhost ~]# touch /mnt/haha3
[root@localhost ~]# mv /mnt/haha3 /var/ftp/

浏览器测试:看不到文件haha3,并产生日志

[root@localhost ~]# cat /var/log/messages  #查看日志,有解决方案
Then execute:
restorecon -v '$FIX_TARGET_PATH'   #日志内容过多,这里只显示解决方案这一行
[root@localhost ~]# restorecon -v /var/ftp/*  #根据解决方案解决冲突
restorecon reset /var/ftp/haha3 context unconfined_u:object_r:mnt_t:s0->unconfined_u:object_r:public_content_t:s0

浏览器测试:可以看到文件haha3

删除根据日志生成解决方案的软件后
[root@localhost ~]# touch /mnt/haha4
[root@localhost ~]# mv /mnt/haha4 /var/ftp/
[root@localhost ~]# > /var/log/audit/audit.log
[root@localhost ~]# > /var/log/messages

浏览器测试:产生日志

[root@localhost ~]# cat /var/log/messages  #内容里面没有解决方案
[root@localhost ~]# cat /var/log/audit/audit.log  #可以查看到日志
type=AVC msg=audit(1526390706.734:180): avc:  denied  { getattr } for  pid=2601 comm="vsftpd" path="/haha4" dev="vda1" ino=8843659 scontext=system_u:system_r:ftpd_t:s0-s0:c0.c1023 tcontext=unconfined_u:object_r:mnt_t:s0 tclass=file
type=SYSCALL msg=audit(1526390706.734:180): arch=c000003e syscall=6 success=no exit=-13 a0=7f19f059a640 a1=7f19f05a27c0 a2=7f19f05a27c0 a3=2020202020203020 items=0 ppid=2599 pid=2601 auid=4294967295 uid=14 gid=50 euid=14 suid=14 fsuid=14 egid=50 sgid=50 fsgid=50 tty=(none) ses=4294967295 comm="vsftpd" exe="/usr/sbin/vsftpd" subj=system_u:system_r:ftpd_t:s0-s0:c0.c1023 key=(null)
type=AVC msg=audit(1526390712.816:181): avc:  denied  { getattr } for  pid=2601 comm="vsftpd" path="/haha4" dev="vda1" ino=8843659 scontext=system_u:system_r:ftpd_t:s0-s0:c0.c1023 tcontext=unconfined_u:object_r:mnt_t:s0 tclass=file
type=SYSCALL msg=audit(1526390712.816:181): arch=c000003e syscall=6 success=no exit=-13 a0=7f19f059a640 a1=7f19f05a27c0 a2=7f19f05a27c0 a3=2020202020203020 items=0 ppid=2599 pid=2601 auid=4294967295 uid=14 gid=50 euid=14 suid=14 fsuid=14 egid=50 sgid=50 fsgid=50 tty=(none) ses=4294967295 comm="vsftpd" exe="/usr/sbin/vsftpd" subj=system_u:system_r:ftpd_t:s0-s0:c0.c1023 key=(null)
[root@localhost ~]# yum install setroubleshoot-server-3.2.17-2.el7.x86_64  #安装该软件

修改http服务的端口标签

在/etc/httpd/conf/httpd.conf文件中可以修改http端口,修改时需要selinux在enforcing模式下
semanage port -l #查看端口标签
semanage port -a -t 类型 -p tcp 端口号 #添加端口
测试:

[root@localhost ~]# vim /etc/httpd/conf/httpd.conf

文件内容:

[root@localhost ~]# systemctl restart httpd  #因为新添加的端口不在默认端口里面所以重启失败
Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details.
[root@localhost ~]# setenforce 0  #将selinux改为警告模式
[root@localhost ~]# systemctl restart httpd  #可以重启httpd
[root@localhost ~]# semanage port -l | grep http  #查看端口
http_cache_port_t              tcp      8080, 8118, 8123, 10001-10010
http_cache_port_t              udp      3130
http_port_t                    tcp      80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t            tcp      5988
pegasus_https_port_t           tcp      5989
[root@localhost ~]# semanage port -a -t http_port_t -p tcp 666  #添加端口号
[root@localhost ~]# semanage port -l | grep http
http_cache_port_t              tcp      8080, 8118, 8123, 10001-10010
http_cache_port_t              udp      3130
http_port_t                    tcp      666, 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t            tcp      5988
pegasus_https_port_t           tcp      5989
[root@localhost ~]# setenforce 1  #修改selinux为强制模式
[root@localhost ~]# systemctl restart httpd  #可以重启httpd

linux之selinux强制访问控制相关推荐

  1. Linux桌面需要强制访问控制,Linux强制访问控制机制模块详细描述(1)

    原标题:Linux强制访问控制机制模块详细描述(1) 2 详细分析 2.1模块功能描述 对于SELinux中实现的MLS,其主要通过安全级别对系统资源的访问进行限制,相关操作定义在security/s ...

  2. Linux桌面需要强制访问控制,RHCSA 系列(十三): 在 RHEL 7 中使用 SELinux 进行强制访问控制...

    RHCSA 认证:SELinux 精要和控制文件系统的访问 尽管作为第一级别的权限和访问控制机制是必要的,但它们同样有一些局限,而这些局限则可以由安全增强 Linux(Security Enhance ...

  3. Linux系统selinux目录,SELinux深入理解

    1. 简介 SELinux带给Linux的主要价值是:提供了一个灵活的,可配置的MAC机制. Security-Enhanced Linux (SELinux)由以下两部分组成: Kernel SEL ...

  4. linux 敏感标记 权限,闲话Linux系统安全(二)——强制访问控制(MAC)

    安全秘笈第二式--不安全的特殊权限和强制访问控制(MAC) 在DAC的机制中,不管是所有权加权限的管理办法,还是文件系统访问控制列表(facl),都是非常强大的访问控制机制,均可以对文件资源进行比较有 ...

  5. linux——selinux强制访问,监控selinux冲突,修改http服务的端口标签

    一.selinux #内核级的加强型防火墙 #针对文件的时候,会对系统中每个文件添加安全上下文 #针对进程的时候,会对系统中每个进程添加安全上下文 selinux是强制访问控制的一种策略,可以指明某一 ...

  6. context linux,使用selinux contexts

    SELinux增强了Linux系统的安全,但是对于初学者来说很容易造成各种各样的错误,经常被搞的莫名其妙不知所措,最后只能祭出大招把SELinux一关了之.笔者也是初学者,也没有搞清楚其中的道理,只是 ...

  7. 安全的加强的linux:SELinux

    selinux:全称安全加强的linux secutity enhanced linux 上个世纪80年代美国国家安全属NSA研发出 MAC:强制访问控制 linux安全模型,每一个用户,他所拥有的文 ...

  8. Linux下selinux简单梳理

    在linux环境下执行某些程序时,偶尔会遇到来一个关于selinux的强制模式不可执行的情况,这种情况下需要关闭selinux或者将enforcing改为permissive模式后才能进行执行.sel ...

  9. Linux中SELinux理解

    SELinux是Security Enhaanced Linux 的英文缩写,字面上的意思就是安全强化的Linux 其实SELinux是在进行进程.文件等详细权限配置时依据的一个内核模块.由于启动网络 ...

最新文章

  1. [Ahoi2013]差异[后缀数组+单调栈]
  2. VMware vSphere4.1看图识HA
  3. 翻译: Waf 教程
  4. spring + angular 实现导出excel
  5. mysql中text格式化_mysql中char,varchar,text
  6. C/C++学习之路: 继承
  7. C++ 多态在异常中的使用
  8. webx学习(二)——Webx Framework
  9. 面试官问面向对象特点_最好的面试官有什么共同点?
  10. java面向对象程序课本,Java面向对象程序设计
  11. lambda表达式可以用来声明_lambda表达式可以用来创建包含多个表达式的匿名函数...
  12. 登陆SQL Server 2000数据库提示超时已过期的解决方法
  13. 人生难免有失意,还是个小姑娘的她...成功的转行测试岗拿到18k offer
  14. Python----进程之间共享数据(全局变量)
  15. srt乱码字幕中文显示解决办法
  16. PHP设计模式——原型模式
  17. 谷歌生物医学专用翻译_实用技能 | 知云文献翻译
  18. 嗅探器c语言源码,自己做的嗅探器
  19. IP地址中的网络地址和主机地址分别是什么意思?
  20. Microsemi Libero免费版License申请教程(2022年)

热门文章

  1. c++求一维数组标准差
  2. 大闹天竺里的机器人_大闹天竺的演员表和角色介绍_全部人物 - 漫漫看电影
  3. 获得淘宝商品详情原数据(商品主图、商品数据)
  4. Python将flv格式转化为avi
  5. Project2013 界面目录清单
  6. 了解Web项目前后端分离开发流程,这一篇就够了
  7. 虚拟机扩容,解决虚拟机磁盘根目录不足
  8. AVS之HPM3.2代码学习笔记1:TSCPM技术的原理和代码实现
  9. lol韩服服务器满了显示什么意思,LOL韩服再次回收超级账号,“rank分出现问题,针对LPL的选手吗”...
  10. 论文解读ViLBERT: Pretraining Task-Agnostic Visiolinguistic Representations for Vision-and-Language Tasks