SElinux简介:

SELinux(Secure Enhanced Linux)安全加强型LINUX; ,是一个安全体系结构。它通过LSM(Linux Security Modules)框架被集成到Linux Kernel 2.6.x中。它是NSA (United States National Security Agency)和SELinux社区的联合项目。SELinux提供了一种灵活的强制访问控制(MAC)系统,且内嵌于Linux Kernel中。


操作系统有两类访问控制:

    DAC:自主访问控制   Discretionary Access Control

标准的linux是DAC

    MAC:强制访问控制  Mandatory Access Control

SElinux 为linux引入MAC


selinux的运行机制:

一个进程对另外一个被操作资源的操作功能(受限于文件被操作资源和操作资源是否在同一个范围)

subject      operation     object

subject:  进程  

operation:操作

object:被存取的项目。例如进程 文件。


SElinux规则库:

用来哪种域能访问哪种或哪些种类型内的文件


SElinux工作机制:(判断程序是否能访问文件)

1.根据安全上下文判断程序是否可以访问文件

2.通过selinux规则库--->判断程序所属域是否能访问此文件的类型;如果接受就访问,否则就算违反了安全策略,并将此次操作写入日志中。

注意:如果在SElinux的规则库中没有对某进程访问某文件进行允许的话,那么是被禁止的!


selinux的配置文件: 

    /etc/sysconfig/cofig---->/etc/selinux/config.


一、SELINUX=enforcing|permissive|disabled         

enforcing:强制,每个受限的进程必然受限。不受限的未必受限

permissive:启用授权,每个受限的进程违规操作不会被禁止,但会被记录于审计日志中

disabled:关闭;所有进程任意访问。


注意:disable状态更改其他状态必须重启系统,让内核做重新打标签

二、SELINUXTYPE=targeted|strict              SELinux的工作策略

strict:每个进程都受到selinux的控制

targeted:仅有限个进程受到selinux控制

(只监控容易被***的进程)


日志文件:记录违反策略的行为

/var/log/audit/audit.log


SELinux的每个文件提供了安全标签,也为进程提供了安全标签

user:role:type 

    user:selinux的user 

    role:内核角色

    type:类型 


相关的命令:

ls -Z: 检查文件、目录的安全上下文

[root@localhost ~]# ll -Z
-rw-------. root root  system_u:object_r:admin_home_t:s0 anaconda-ks.cfg
drwxr-xr-x. root root  system_u:object_r:admin_home_t:s0 Desktop
drwxr-xr-x. root root  system_u:object_r:admin_home_t:s0 Documents

ps auZ:查看进程的安全上下文

-rw-r--r--. root root  system_u:object_r:admin_home_t:s0 zsh-5.0.2-7.el7_1.2.x86_64.rpm
[root@localhost ~]# ps auZ
LABEL                           USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
system_u:system_r:getty_t:s0-s0:c0.c1023 root 1217 0.0  0.0 110028 840 tty1     Ss+  11:37   0:00 /sbin/agetty --noclear tty1 linux
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 root 1325 0.0  0.3 116684 3496 pts/0 Ss 11:38   0:00 -bash
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 root 1925 0.0  0.1 144124 1724 pts/0 R+ 14:25   0:00 ps auZ

getenforce:获取selinux当前状态

[root@localhost ~]# getenforce
Enforcing

setenforce 0|1 修改当前selinux状态


0:设置为permissive

1: 设置为enforcing

注意:修改只能是当前状态处于二者之一,而不能是disable,如果是disable只能修改配置文件。            仅当前系统有效。


chcon:给文件重新达标

       chcon [OPTION]... CONTEXT FILE...

       chcon [OPTION]... [-u USER] [-r ROLE] [-l RANGE] [-t TYPE] FILE...

       chcon [OPTION]... --reference=RFILE FILE...

常用:

chcon [-R] -t  type filename

-R:递归

-t type:类型

[root@localhost ~]# ll -Z 111.sh 
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 111.sh
[root@localhost ~]# chcon -t root_t 111.sh
[root@localhost ~]# ll -Z 111.sh 
-rw-r--r--. root root unconfined_u:object_r:root_t:s0  111.sh

getsebool:查看布尔型设置

getsebool [-a] [boolean]  -a   boolean 

                -a:程序名称

                boolean:程序名称

[root@localhost ~]# getsebool -a
abrt_anon_write --> off
abrt_handle_event --> off
abrt_upload_watch_anon_write --> on
antivirus_can_scan_system --> off
[root@localhost ~]# getenforce boinc_execmem
Enforcing

setsebool:设置布尔型设置

setsebool [ -P] boolean value | bool1=val1 bool2=val2

-P表示永久有效,没有的话只是表示当次

setsebool -p name on|off

[root@localhost ~]# getsebool ssh_keysign 
ssh_keysign --> off
[root@localhost ~]# setsebool ssh_keysign 1
[root@localhost ~]# getsebool ssh_keysign 
ssh_keysign --> on