Linux_SElinux 安全上下文的理解

selinux
安全上下文是一个访问控制属性。在SELinux中,类型标识符是安全上下文中决定访问的主要的部分。
为了简便,这里我就使用一台机器既作客户端又作服务端来进行实验


 1. getenforce #查看他的selinux是否为强制状态
 2. touch /mnt/file1 #在mnt下创建file1 文件 因为mnt的安全上下文为mnt_t 
 3. mv /mnt/file1 /var/ftp/pub
 4. lftp 192.168.144.84 
 5. ls /var/ftp/pub/ #因为安全上下文不同是看不到移动过去的文件的

这里说明为什么使用mv而不是cp
cp:会重新生成符合目录本身的安全上下文,当然可以使用-p参数
mv:只是单纯将原有文件移动过去 不改变文件本身的属性
这里写图片描述


  1 [root@84 ftp]# touch /westos/file #建立/westos并在里面创建文件 作为匿名用户的家目录
  2 [root@84 ftp]# vim /etc/vsftpd/vsftpd.conf #更改配置文件 指定家目录
 11 [root@84 ftp]# vim /etc/vsftpd/vsftpd.conf
 12 [root@84 ftp]# systemctl restart vsftpd.service
 13 [root@84 ftp]# lftp 192.168.122.84
 14 lftp 192.168.122.84:~> ls #可以看到由于安全上下文的关系看不到里面的文件
 15 lftp 192.168.122.84:/> exit
 16 [root@84 ftp]# semanage fcontext -l | grep /westos/
 17 [root@84 ftp]# semanage fcontext -l | grep /westos
 18 [root@84 ftp]# semanage fcontext -l | grep /var/ftp #这步是为了查看安全上下文的类型 
 19 /var/ftp(/.*)?                                     all files          system    _u:object_r:public_content_t:s0
 20 /var/ftp/bin(/.*)?                                 all files          system    _u:object_r:bin_t:s0
 21 /var/ftp/etc(/.*)?                                 all files          system    _u:object_r:etc_t:s0
 22 /var/ftp/lib(/.*)?                                 all files          system    _u:object_r:lib_t:s0
 23 /var/ftp/lib/ld[^/]*\.so(\.[^/]*)*                 regular file       system    _u:object_r:ld_so_t:s0
 24 [root@84 ftp]# semanage fcontext -a -t public_content_t '/westos(/.*)?' #永久性设定
 25 [root@84 ftp]# semanage fcontext -l | grep /westos 
 26 /westos(/.*)?                                      all files          system    _u:object_r:public_content_t:s0
 27 restorecon -FvvR /westos #恢复文件的安全上下文
 28 ot@84 Desktop]# lftp 192.168.122.84
 29 lftp 192.168.122.84:~> ls 
 30 -rw-r--r--    1 0        0               0 May 15 07:46 file
 31 lftp 192.168.122.84:/> exit
 touch /.autorelabel  #让系统重新扫描所有文件


semanage fcontext -a -t public_content_t(安全上下文的格式) /westos() -a 添加 t 类型 永久性的改变 将其写入列表中
‘‘将代表动作的语句改变为名词形式
/westos(/.)==/westos///
在selinux开启的情况下 虽然本地用户可以上传
但sebool值为off便不能操作 sebool 相当于给程序添加开关

 1  lftp 192.168.122.84
  2 lftp 192.168.122.84:~> ls
  3 -rw-r--r--    1 0        0               0 May 15 07:46 file
  4 lftp 192.168.122.84:/> exit
  5 [root@84 Desktop]# lftp 192.168.122.84 -u student #本地用户登陆
  6 Password:
  7 lftp student@192.168.122.84:~> ls
  8 lftp student@192.168.122.84:~> put /etc/passwd
  9 put: Access failed: 553 Could not create file. (passwd)
 10 lftp student@192.168.122.84:~> exit
 11 [root@84 Desktop]# getsebool -a | grep ftp  #查看ftp相关的sebool值
 12 ftp_home_dir --> off
 13 ftpd_anon_write --> off
 14 ftpd_connect_all_unreserved --> off
 15 ftpd_connect_db --> off
 16 ftpd_full_access --> off
 17 ftpd_use_cifs --> off
 18 ftpd_use_fusefs --> off
 19 ftpd_use_nfs --> off
 20 ftpd_use_passive_mode --> off
 21 httpd_can_connect_ftp --> off
 22 httpd_enable_ftp_server --> off
 23 sftpd_anon_write --> off
 24 sftpd_enable_homedirs --> off
 25 sftpd_full_access --> off
 26 sftpd_write_ssh_home --> off
 27 tftp_anon_write --> off
 28 tftp_home_dir --> off
 29 [root@84 Desktop]# setsebool ftp_home_dir 1  #将他改为开的状态  1 或者 on 均可
 30 [root@84 Desktop]# lftp 192.168.122.84 -u student
 31 Password:
 32 lftp student@192.168.122.84:~> ls
 33 ls: Login failed: 530 Login incorrect.
 34 lftp student@192.168.122.84:~> exit
 35 [root@84 Desktop]# lftp 192.168.122.84 -u student
 36 Password:
 37 lftp student@192.168.122.84:~> ls
 38 lftp student@192.168.122.84:~> put /etc/passwd
 39 2005 bytes transferred
 40 lftp student@192.168.122.84:~> exit

改变 http端口号
vim /etc/httpdconf/httpd.conf 查看配置文件并改变他的端口号码
semange port -l | grep http 找该文件的端口号
semanage port -a -t … -p tcp 7777 selinux允许http使用列表以外的端口
这里写图片描述

 1  vim /etc/httpd/conf/httpd.conf
  2 [root@84 Desktop]# systemctl start httpd
  3 [root@84 Desktop]# systemctl restart httpd
  4 Job for httpd.service failed. See 'systemctl status httpd.se    rvice' and 'journalctl -xn' for details. #selinux强制状态无法启动
  5 [root@84 Desktop]# setenforce 0 #改为警告
  6 [root@84 Desktop]# systemctl restart httpd
  7 [root@84 Desktop]# semanage port -l | grep http  #查看httpd的端口号
  8 http_cache_port_t              tcp      8080, 8118, 8123, 10    001-10010
  9 http_cache_port_t              udp      3130
 10 http_port_t                    tcp      80, 81, 443, 488, 80    08, 8009, 8443, 9000
 11 pegasus_http_port_t            tcp      5988
 12 pegasus_https_port_t           tcp      5989
 14 [root@84 Desktop]# semanage port -a -t http_port_t -p tcp 7777 
 15 [root@84 Desktop]# semanage port -l | grep http
 16 http_cache_port_t              tcp      8080, 8118, 8123, 10    001-10010
 17 http_cache_port_t              udp      3130
 18 http_port_t                    tcp      7777, 80, 81, 443, 4    88, 8008, 8009, 8443, 9000
 19 pegasus_http_port_t            tcp      5988
 20 pegasus_https_port_t           tcp      5989
 21 [root@84 Desktop]# setenforce 1
 22 [root@84 Desktop]# systemctl restart httpd

/var/log/message 清空日志
message 会生成解决的方法 为 setroubleshoot这个软件
当真正访问他是会在日志里记录,并生成解决方案
这里写图片描述
如图所示
为生成的解决方案
但此方案并不能保证安全性,只是单纯的能够让你浏览该文件

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值