selinux

来源:互联网 发布:淘宝虚假交易处罚2016 编辑:程序博客网 时间:2024/06/05 04:04

1. 简介

  SELinux(Security-Enhanced Linux) 是美国国家安全局「NSA=The National Security Agency」 和SCC(Secure Computing Corporation)开发的 linux的一个扩张强制访问控制(MAC)安全模块,是 Linux® 上最杰出的新安全子系统。NSA是在Linux社区的帮助下开发了一种访问控制体系,在这种访问控制体系的限制下,进程只能访问那些在他的任务中所需要文件。  SELinux是一种基于 域-类型 模型(domain-type)的强制访问控制(MAC)安全系统,它由NSA编写并设计成内核模块包含到内核中,相应的某些安全相关的应用也被打了SELinux的补丁,最后还有一个相应的安全策略。 众所周知,标准的UNIX安全模型是"任意的访问控制"DAC。就是说,任何程序对其资源享有完全的控制权。假设某个程序打算把含有潜在重要信息的文件扔到/tmp目录下,那么在DAC情况下没人能阻止他! 而MAC情况下的安全策略完全控制着对所有资源的访问。这是MAC和DAC本质的区别。 SELinux提供了比传统的UNIX权限更好的访问控制。

2. 基本概念
2.1 自主访问控制DAC
管理的方式不同就形成不同的访问控制方式。一种方式是由客体的属主对自己的客体进行管理,由属主自己决定是否将自己客体的访问权或部分访问权授予其他主体,这种控制方式是自主的,我们把它称为自主访问控制(Discretionary Access Control——DAC)。在自主访问控制下,一个用户可以自主选择哪些用户可以共享他的文件。Linux系统中有两种自主访问控制策略,一种是9位权限码(User-Group-Other),另一种是访问控制列表ACL(Access Control List)。
2.2 强制访问控制MAC
强制访问控制(Mandatory Access Control——MAC),用于将系统中的信息分密级和类进行管理,以保证每个用户只能访问到那些被标明可以由他访问的信息的一种访问约束机制。通俗的来说,在强制访问控制下,用户(或其他主体)与文件(或其他客体)都被标记了固定的安全属性(如安全级、访问权限等),在每次访问发生时,系统检测安全属性以便确定一个用户是否有权访问该文件。其中多级安全(MultiLevel Secure, MLS)就是一种强制访问控制策略。

  1. 控制切换
    /etc/sysconfig/selinux标准设置如下:
    1. # This file controls the state of SELinux on the system.      2. # SELINUX= can take one of these three values:      3. # enforcing - SELinux security policy is enforced.      4. # permissive - SELinux prints warnings instead of enforcing.      5. # disabled - SELinux is fully disabled.      6. SELINUX=enforcing      7.       8. # SELINUX=disabled      9. # SELINUXTYPE= type of policy in use. Possible values are:      10. # targeted - Only targeted network daemons are protected.      11. # strict - Full SELinux protection.      12. SELINUXTYPE=targeted

3.1 SELINUX
SELINUX 有【disabled】、【permissive】、【enforcing】3种选择。
• disabled:不启用SELINUX功能
• permissive:SELINUX有效,但是即使你违反了策略,它让你继续操作,但是把你的违反的内容记录下来。在我们开发策略的时候非常的有用。相当于Debug模式。
• enforcing:当你违反了策略,你就无法继续操作下去。
3.2 SELINUXTYPE
目前主要有2大类,【targeted】和【strict】。
• targeted:它是红帽子开发的targeted,它只是对于主要的网络服务进行保护,比如apache, sendmail, bind, postgresql等,不属于那些domain的就都让他们在unconfined_t里,可导入性高,可用性好但是不能对整个系统进行保护。
• strict:是由NAS开发的,能对整个系统进行保护,但是设定复杂,但是只要掌握一些基本的知识,还是可以玩得动的。
我们除了在/etc/sysconfig/selinux设它有效无效外,在启动的时候,也可以通过传递参数selinux给内核来控制它。(Fedora 5默认是有效)
3.3 对文件系统赋予标签
[cpp]

1. [root@python sysconfig]# getenforce  2. Enforcing  3. #确认有效后重新对文件系统赋予标签:  4. [root@python sysconfig]# /sbin/fixfiles relabel  5. #或者  6. [root@python /]# touch /.autorelabel  7. #然后 reboot,你就在secure的Linux环境下工作了。  

4.1 临时修改标签
[root@ftp-server ~]# ls -Z /var/ftp/pub/

-rw-------. student ftp  system_u:object_r:public_content_t:s0 group-rw-------. ftp     ftp  system_u:object_r:public_content_t:s0 passwd-rw-r--r--. root    root unconfined_u:object_r:mnt_t:s0   westos      ##/mnt下创建的文件
public_content_t /var/ftp/pub/public_content_t /var/ftp/pub/westos                  //也可用restorecon -v /var/ftp/pub/*

ls -Z /var/ftp/pub/
lftp 172.25.254.138
4.2 永久修改标签
mkdir /westos/linux{1..3} -p
vim /etc/vsftpd/vsftpd.conf ##修改访问目录

anon_root=/westos

systemctl restart vsftpd.service
vim /etc/sysconfig/selinux
Selinux=Enforcing
Reboot

semanage fcontext -l | grep /westos          ##看不到semanage fcontext -l | grep /var/ftp/semanage fcontext -a -t public_content_t '/westos(/.*)?'semanage fcontext -l | grep /westos         ##只能看到/westosresrestorecon  -RvvF /westos/               ##全部修改restorecon reset /westos context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0restorecon reset /westos/linux1 context system_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0restorecon reset /westos/linux2 context system_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0restorecon reset /westos/linux3 context system_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0

lftp 172.25.254.138 -u westos ##用户权限限制,不能上传删除
getsebool -a | grep ftp

ftp_home_dir --> offftpd_anon_write --> offftpd_connect_all_unreserved --> offftpd_connect_db --> offftpd_full_access --> offftpd_use_cifs --> offftpd_use_fusefs --> offftpd_use_nfs --> off......

setsebool -P ftp_home_dir on ##再连接就可以上传了

那么怎么用匿名用户上传?

semanage fcontext -a -t public_context_rw_t '/var/ftp/pub(/.*?)'restorecon -RvvF /var/ftp/pubsetsebool -P ftpd_anon_write on

vim /etc/vsftpd/vsftpd.conf
anon_upload_enable=YES
**注意权限,若没有则修改
[root@ftp-server ~]# ls -ldZ /var/ftp/pub/

drwxrwxrwx. root root system_u:object_r:public_content_rw_t:s0 /var/ftp/pub/

可以在/var/log/message生成日志的软件
setroubleshoot-server-3.2.17-2.el7.x86_64
解决方法Then execute:

原创粉丝点击