一、基本 SELINUX 安全性概念

1.SELINUX( 安全增强型 Linux) 是可保护你系统安全性的额外机制

2.在某种程度上 , 它可以被看作是与标准权限系统并行的权限系统。在常规模式中 , 以用户身份运行进程 ,并且系统上的文件和其他资源都设置了权限 ( 控制哪些用户对哪些文件具有哪些访问权 SELINUX 的另一个不同之处在于 , 若要访问文件 , 你必须具有普通访问权限和 SELINUX 访问权限。因此 , 即使以超级用户身份 root 运行进程 , 根据进程以及文件或资源的 SELinux 安全性上下文可能拒绝访问文件或资源限 ) 标签

3.示例:在无selinux 保护时 , 恶意人员可以尝试利用 web 服务器中的安全漏洞强行进入系统。如果成功 , 将会控制以用户apache 身份运行的进程 , 这时再由一个本地安全漏洞就可能使攻击者获得超级用户的访问权限

二、selinux 安全上下文访问规则

1.WEB 服务器的 HTTPD进程设置了SELINUX 上下文system_u:system_r:httpd_t 标签。该上下文的重要部分是第三个用冒号分隔的字段SELINUX 类型 : httpd_t

2.系统上的文件和资源也设置了 SELINUX 上下文标签 , 并且重要的部分是 SELINUX 类型。例如 , /var/www/html 中的文件具有类型 httpd_sys_content_t 。 /tmp 和/var/tmp中的文件通常具有类型 tmp_t

3.Seliux策略具有允许以 httpd_t 身份运行的进程访问标记为 httpd_sys_content_t 的文件的规则。没有规则允许这些进程访问标记有 tmp_t的文件 , 因此将拒绝这些访问 , 即使常规文件权限指出应该允许这些访问

三、SELINUX模式

1.强制模式 :SELINUX 主动拒绝访问尝试读取类型上下文为tmp_t的web服务器。在强制模式中 ,SELINXU 既记录冲突 , 也强制执行规则

2.许可模式 : 通常用于对问题进行故障排除。在许可模式下 , 即使没有明确规则 , SELINUX 也允许所有交互 , 并且记录所有被拒绝的交互。此模式可以用于确定你是否有 SELINUX问题。无需重新引导即可从强制模式转为许可模式 , 或再从许可模式转回强制模式

四、显示及更改SELINUX模式

1.getenforce

2.setenforce0|1

–0 表示permissive # 警告

–1 表示enforcing # 强制

五、更改 selinux 的开机状态

1.vim/etc/sysconfig/selinux

#SELINUX=disabled

PS:disable表示关闭,enforcing表示强制,permissive表示警告,disable状态切换到permissive状态或enforcing状态需要重启系统

六、显示 SELinux 文件上下文

1.什么确定文件的初始 SELinux 上下文通常是父目录。将父目录的上下文指定给新创建的文件。这对 vimcp 和 touch 等命令其作用 , 但是 , 如果文件是在其他位置创建的并且保留了权

限 ( 与 mv 或 cp -a 一样 ) 则还将保留 SELinux 上下文

2.许多处理文件的命令具有一个用于显示或设置 SELinux 上下文的选项 ( 通常是 -Z ) 。例如 , ps 、 ls 、 cp 和 mkdir 都使用 -Z 选项显示或设置 SELinux 上下文

3.显示上下文

– ps axZ

– ps-ZC

– ls-Z

七、修改 selinux 安全上下文

1.chcon-t

一次性定制安全上下文,执行 restorecon 刷新后还原

2.semanagefcontext

永久更改文件的上下文

八、semanage 命令

1.restorecon是policycoreutil 软件包的一部分

2.semanage是policycoreutil-python 软件包的一部分

3.semanagefcontext 可用与显示或修改 restorrecon 用来设置默认文件上下文的规则

4.semanagefcontext 使用扩展正则表达式来指定路径和文件名。 fcontext 规则中最常用的扩展正则表达式是(/.*)?, 表示随意地匹配 / 后跟任何数量的字符

5.semanagefcontext 将递归地与在表达式前面列出的目录以及该目录中的所有内容相匹配

6.示例:

[root@serverX~]# mkdir /virtual

[root@serverX~]# touch /virtual/index.html

[root@serverX~]# ls -Zd /virtual/

drwxr-xr-x.root root unconfined_u:object_r:default_t:s0 /virtual/

[root@serverX~]# ls -Z /virtual/

-rw-r--r--.root root unconfined_u:object_r:default_t:s0 index.html

[root@serverX~]# semanage fcontext -a -f "" -t httpd_sys_content_t

'/virtual(/.*)?'

[root@serverX~]# restorecon -RFvv /virtual/

restoreconreset /virtual context system_u:object_r:default_t:s0-

>system_u:object_r:httpd_sys_content_t:s0

restoreconreset /virtual/index.html context system_u:object_r:default_t:s0-

>system_u:object_r:httpd_sys_content_t:s0

[root@serverX~]# ls -Zd /virtual/

drwxr-xr-x.root root system_u:object_r:httpd_sys_content_t:s0 /virtual/

[root@serverX~]# ls -Z /virtual/

-rw-r--r--.root root system_u:object_r:httpd_sys_content_t:s0 index.html

九、管理 SELinux 布尔值

1.SELinux布尔值是更改 SELinux 策略行为的开关。SELinux 布尔值是可以启用或禁用的规则。安全管理员可以使用 SELinux 布尔值来调整策略 , 以有选择地进行调整

2.许多软件包都具有 man page *_selinux(8), 其中详细说明了所使用的一些布尔值 ; man -k ‘_selinux’ 可以轻松地找到这些手册

3.getsebool用于显示布尔值 , setsebool 用于修改布尔值

4.setsebool-P 修改 SELinux 策略 , 以永久保留修改。

5.semanageboolean -l 将显示布尔值是否永久

6.示例

[root@serverX~]# getsebool -a

abrt_anon_write--> off

.....

[root@serverX~]# getsebool httpd_enable_homedirs

httpd_enable_homedirs--> off

[root@serverX~]# setsebool httpd_enable_homedirs on

[root@serverX~]# semanage boolean -l |grep httpd_enable_homedirs

httpd_enable_homedirs

->off Allow httpd to read home directories

[root@serverX~]# getsebool httpd_enable_homedirs

httpd_enable_homedirs--> on

[root@serverX~]# setsebool -P httpd_enable_homedirs on

[root@serverX~]# semanage boolean -l |grep httpd_enable_homedirs

httpd_enable_homedirs

->on

Allowhttpd to read home directories

十、监控 SELinux 冲突

1.必须安装setroubleshoot-server 软件包 , 才能将SELinux 消息发送至 /var/log/messages

2.etroubleshoot-server侦听/var/log/audit/audit.log中的审核信息并将简短摘要发送至 /var/log/messages

3.摘要包括SELinux 冲突的唯一标识符 ( UUIDs ),可用于收集更多信息。 Sealert -l UUID 用于生成特定事件的报告。 Sealert -a/var/log/audit/audit.log 用于在该文件中生成所有事件的报告

4.示例

[root@demo~]# echo www.westos.org /root/file1

[root@demo~]# mv /root/file1 /var/www/html/

[root@demo~]# service httpd start

[root@demo~]# elinks -dump http://localhost/file1

Forbidden

Youdon't have permission to access /file1 on this server.

[root@demo~]# tail -1 /var/log/audit/audit.log

....

type=AVCmsg=audit(1312818090.800:25721): avc: denied { getattr } for

pid=29653comm="httpd"

Path="/var/www/html/file1"dev=dm-1 ino=54309

scontext=unconfined_u:system_r:httpd_t:s0

tcontext=unconfined_u:object_r:admin_home_t:s0tclass=file

....

[root@demo~]# tail /var/log/messages

5.示例

[root@demo~]# tail /var/log/messages

.....

Aug 823:41:30 demo setroubleshoot: SELinux is preventing /usr/sbin/httpd

"getattr"access to /var/www/html/file1.

Forcomplete SELinux messages. run sealert -l bbb10235-7683-4387-977d-

a34c141bba86

[root@demo~]# sealert -l bbb10235-7683-4387-977d-a34c141bba86

Summary:

SELinuxis preventing /usr/sbin/httpd "getattr" access to /var/www/html/file1.

DetailedDescription:

......

Youcan restore the default system context to this file by executing the

restoreconcommand. restorecon

'/var/www/html/file1',if this file is a directory, you can recursively restore using

restorecon-R

'/var/www/html/file1'.

FixCommand:

/sbin/restorecon'/var/www/html/file1'

6.示例

执行事件报告中给出的解决命令 :

[root@demo~]# restorecon /var/www/html/file1

[root@demo~]# elinks -dump http://localhost/file1

eg:[root@localhostDesktop]# vim /etc/sysconfig/selinux   ###修改selinux的开机状态,关闭

#SELINUX=disabled

[root@localhostDesktop]# reboot    ###每次修改selinux的状态后,只有重启虚拟机,才能发挥作用

Connectionto 172.25.254.137 closed by remote host.

Connectionto 172.25.254.137 closed.

[kiosk@foundation37Desktop]$ ssh root@172.25.254.137 -X

root@172.25.254.137'spassword:

Lastfailed login: Mon Apr 10 21:47:46 EDT 2017 from 172.25.254.37 on ssh:notty

Therewas 1 failed login attempt since the last successful login.

Lastlogin: Mon Apr 10 21:41:21 2017 from 172.25.254.37

[root@localhost~]# systemctl stop firewalld.service

[root@localhost~]# touch /mnt/file

[root@localhost~]# mv /mnt/file /var/ftp/pub/   ###匿名用户的家目录/var/ftp/

[root@localhost~]# lftp 172.25.254.137

lftp 172.25.254.137:~>ls

drwxr-xr-x   2 0        0             17Apr 11 11:04 pub

lftp 172.25.254.137:/>cd pub/

lftp 172.25.254.137:/pub>ls

-rw-r--r--   1 0        0              0Apr 11 11:03 file   ###selinux关闭,新建的文件安全上下文与系统安全上下文不匹配的文件显示

lftp 172.25.254.137:/pub>quit

[root@localhost~]# cd /var/ftp/pub/

[root@localhostpub]# ls

file

[root@localhostpub]# ls -Z file

-rw-r--r--root root ?                               file

[root@localhostpub]# vim /etc/sysconfig/selinux   ###修改selinux的开机状态开启

#SELINUX=enforcing

[root@localhostpub]# reboot   ###重启虚拟机

Connectionto 172.25.254.137 closed by remote host.

Connectionto 172.25.254.137 closed.

[kiosk@foundation37Desktop]$ ssh root@172.25.254.137 -X

root@172.25.254.137'spassword:

Lastlogin: Mon Apr 10 22:02:33 2017 from 172.25.254.37

[root@localhost~]# systemctl stop firewalld.service

[root@localhost~]# lftp 172.25.254.137

lftp 172.25.254.137:~>ls

drwxr-xr-x   2 0        0             17Apr 11 11:04 pub

lftp 172.25.254.137:/>cd pub/

lftp 172.25.254.137:/pub>ls

-rw-r--r--   1 0        0              0Apr 11 11:03 file

lftp 172.25.254.137:/pub>quit

[root@localhost~]# touch /mnt/file1

[root@localhost~]# mv /mnt/file1 /var/ftp/pub/   ###新建文件的文件安全上下文与系统安全上下文不匹配

[root@localhost~]# lftp 172.25.254.137

lftp 172.25.254.137:~>ls

drwxr-xr-x   2 0        0             29Apr 11 11:12 pub

lftp 172.25.254.137:/>cd pub/

lftp 172.25.254.137:/pub>ls

-rw-r--r--   1 0        0              0Apr 11 11:03 file   ###新建file1不显示

lftp 172.25.254.137:/pub>quit

[root@localhost~]# ls /var/ftp/pub/

file file1

[root@localhost~]# ls -lZ /var/ftp/pub/

-rw-r--r--.root root system_u:object_r:public_content_t:s0 file    ###文件上下文public_content_t

-rw-r--r--.root root unconfined_u:object_r:mnt_t:s0   file1   ###文件上下文mnt_t,与file的不一致

[root@localhost~]# ps auxZ | grep vsftpd   ###系统上下文ftpd_t,匹配的文件上下文是public_content_t

system_u:system_r:ftpd_t:s0-s0:c0.c1023root 1322 0.0  0.0 52756  564 ?       Ss   22:10   0:00/usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf

unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023root 1928 0.0  0.0 112656 988 pts/0 S+ 22:14   0:00 grep--color=auto vsftpd

[root@localhost~]# chcon -t public_content_t /var/ftp/pub/file1   ###一次性定制安全上下文,临时的

[root@localhost~]# ls -lZ /var/ftp/pub/

-rw-r--r--.root root system_u:object_r:public_content_t:s0 file

-rw-r--r--.root root unconfined_u:object_r:public_content_t:s0 file1   ###文件上下文被修改为public_content_t

[root@localhost~]# lftp 172.25.254.137

lftp 172.25.254.137:~>cd pub/

lftp 172.25.254.137:/pub>ls

-rw-r--r--   1 0        0              0Apr 11 11:03 file

-rw-r--r--   1 0        0              0Apr 11 11:12 file1   ###因文件安全上下文变一致,显示

lftp 172.25.254.137:/pub>quit

[root@localhost~]#

[root@localhostvsftpd]# lftp 172.25.254.137 -u student

password:

lftpstudent@172.25.254.137:~> ls

lftpstudent@172.25.254.137:~> put /etc/passwd

put:Access failed: 553 Could not create file. (passwd)

lftpstudent@172.25.254.137:~> quit

[root@localhostvsftpd]# setenforce 0

[root@localhostvsftpd]# lftp 172.25.254.137 -u student

password:

lftpstudent@172.25.254.137:~> ls

lftpstudent@172.25.254.137:~> put /etc/passwd

2042bytes transferred

lftpstudent@172.25.254.137:~> put /etc/group

862bytes transferred

lftpstudent@172.25.254.137:~> ls

-rw-r--r--   1 1000     1000         862 Apr 11 11:27 group

-rw-r--r--   1 1000     1000        2042 Apr 11 11:25 passwd

lftpstudent@172.25.254.137:~> quit

[root@localhostvsftpd]# getsebool -a | grep ftp    ###显示布尔值

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@localhostvsftpd]# setsebool -P ftp_home_dir on   ###修改布尔值

[root@localhostvsftpd]# 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@localhostvsftpd]# mkdir /student

[root@localhostvsftpd]# touch /student/file{1..5}

[root@localhostvsftpd]# ls /student/

file1 file2  file3  file4  file5

[root@localhostvsftpd]# vim /etc/vsftpd/vsftpd.conf

#local_root=/student  ###修改本地用户家目录为/student

[root@localhostvsftpd]# systemctl restart vsftpd.service

[root@localhostvsftpd]# lftp 172.25.254.137 -u student

password:

lftpstudent@172.25.254.137:~> ls    ###本地用户家目录中有五个文件

-rw-r--r--   1 0        0              0Apr 11 11:23file1

-rw-r--r--   1 0        0              0Apr 11 11:23file2

-rw-r--r--   1 0        0              0Apr 11 11:23file3

-rw-r--r--   1 0        0              0Apr 11 11:23file4

-rw-r--r--   1 0        0              0Apr 11 11:23file5

lftpstudent@172.25.254.137:~> quit

[root@localhostvsftpd]# ls -Zd /student/

drwxr-xr-x.root root unconfined_u:object_r:default_t:s0 /student/   ###安全上下文default_t,不一致,重启后文件将不再显示

[root@localhostvsftpd]# vim /etc/sysconfig/selinux   ###重启selinux

#SELINUX=disabled

[root@localhostvsftpd]# reboot

Connectionto 172.25.254.137 closed by remote host.

Connectionto 172.25.254.137 closed.

[kiosk@foundation37Desktop]$ ssh root@172.25.254.137 -X

root@172.25.254.137'spassword:

Lastlogin: Mon Apr 10 22:11:30 2017 from 172.25.254.37

[root@localhost~]# vim /etc/sysconfig/selinux

#SELINUX=enforcing

[root@localhost~]# reboot

Connectionto 172.25.254.137 closed by remote host.

Connectionto 172.25.254.137 closed.

[kiosk@foundation37Desktop]$ ssh root@172.25.254.137 -X

root@172.25.254.137'spassword:

Lastlogin: Mon Apr 10 22:39:06 2017 from 172.25.254.37

[root@localhost~]# systemctl stop firewalld.service

[root@localhost~]# lftp 172.25.254.137 -u student

password:

lftpstudent@172.25.254.137:~> ls     ###用户家目录的五个文件不显示,安全上下文不一致

lftpstudent@172.25.254.137:~> quit

[root@localhost~]# ls /student/

file1 file2  file3  file4  file5

[root@localhost~]# semanage fcontext -l | grep /var/ftp/   ###显示文件上下文的规则

/var/ftp/bin(/.*)?                                allfiles          system_u:object_r:bin_t:s0

/var/ftp/etc(/.*)?                                allfiles          system_u:object_r:etc_t:s0

/var/ftp/lib(/.*)?                                allfiles          system_u:object_r:lib_t:s0

/var/ftp/lib/ld[^/]*\.so(\.[^/]*)*                regularfile       system_u:object_r:ld_so_t:s0

[root@localhost~]# semanage fcontext -l | grep /student   ###显示文件上下文的规则

[root@localhost~]# semanage fcontext -a -t public_content_t '/student(/.*)?'   ###修改文件上下文的规则

[root@localhost~]# semanage fcontext -l | grep /student    ###文件上下文被修改,永久性的

/student(/.*)?                                    allfiles          system_u:object_r:public_content_t:s0

[root@localhost~]# restorecon /student/ -FvvR   ###文件上下文都被修改为public_content_t

restoreconreset /student contextunconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0

restoreconreset /student/file1 contextunconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0

restoreconreset /student/file2 contextunconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0

restoreconreset /student/file3 contextunconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0

restoreconreset /student/file4 context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0

restoreconreset /student/file5 contextunconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0

[root@localhost~]# lftp 172.25.254.137 -u student

password:

lftpstudent@172.25.254.137:~> ls   ###文件可以显示出来

-rw-r--r--   1 0        0              023file1

-rw-r--r--   1 0        0              0Apr 11 11:23file2

-rw-r--r--   1 0        0              0Apr 11 11:23file3

-rw-r--r--   1 0        0              0Apr 11 11:23file4

-rw-r--r--   1 0        0              0Apr 11 11:23file5

lftpstudent@172.25.254.137:~> quit

seLinux的管理相关推荐

  1. selinux 的管理

    第十单元 selinux 的管理 一 显示及更改 SELINUX 模式 getenforce     ###显示selinux模式 setenforce 0|1               ##0指p ...

  2. linux selinux策略管理与标签

    文章目录 1. selinux 策略 2. selinux标签协议 1. selinux 策略 #打开80/tcp .443/tcp端口 firewall-cmd --permanent --add- ...

  3. Linux的基本学习(十四)——进程管理(下)与SELinux

    Linux的基本学习(十四)--进程管理(下)与SELinux 前言 进程这部分内容真是不少,来,我们继续跟着鸟哥学习. 特殊文件与进程 具有SUID/SGID权限的命令执行状态 SUID的权限其实与 ...

  4. Linux之强大的selinux

    简单点说,SELinux就是用来加强系统安全性的.它给一些特定程序(这些程序也在不断增加)做了一个沙箱,它将文件打上了一个安全标签,这些标签属于不同的类,也只能执行特定的操作,也就是规定了某个应用程序 ...

  5. selinux= 为 disabled_Selinux安全加固

    Selinux介绍 SELinux:Security-Enhanced Linux, 是美国国家安全局(NSA=The National Security Agency)和SCC(Secure Com ...

  6. 技术债务管理_管理技术债务

    技术债务管理 DevOps Essentials DevOps基础 介绍 (Introduction) Technical debt is one of the most insidious and ...

  7. gradle依赖管理_依赖管理

    gradle依赖管理 Why Bother 何必呢 Writing software is a very expensive process, and most systems we interact ...

  8. Linux中SELinux理解

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

  9. linux 文件添加标签,SELinux——有趣的标签

    ·配置SELinux: SELinux是否启用 给文件重新打标 给端口设置安全标签 设定某些操作的布尔型特性 SELinux的日志管理 1.SELinux的状态: ·enforcing:强制,每个受限 ...

最新文章

  1. 解决 /var/run/nginx.pid failed
  2. php与js的关系,Php与Js的交互
  3. jvm理论-字节码指令
  4. 天合公司 TRW Inc.
  5. c语言 去掉双引号_技术分享|浅谈C语言陷阱和缺陷
  6. 2020-5-13从0在mac上搭载SSM新闻网站项目
  7. MySQL数据库架构相关笔记(二)
  8. 调研AutoGluon数据处理与Tabular-NN
  9. windows 图片和传真查看器不能旋转
  10. python数据预处理 样本分布不均(过采样和欠采样)
  11. python下载图片到文件夹_Python批量下载网页图片详细教程
  12. mysql rds云数据库_云数据库 RDS MySQL快速入门手册
  13. 固态硬盘坏了的表现是什么?从哪里可以看出来?
  14. 想要创业,却没货源?答应我,别只在阿里妈妈上找了好吗
  15. Maven打包自定义MANIFEST.MF键值对
  16. 关于含税单价和不含税单价的关系记录
  17. BMI值(Body Mass Index,简称BMI),是指身高体重指数,是国际上常用的衡量人体肥胖程度和是否健康的重要标准,BMI的计算公式是:体重指数(BMI)=体重(kg)÷身高2(m)。
  18. u盘打开提示格式化怎么办?有用的方法来了
  19. python制作有道翻译软件
  20. mysql连接数据了的dep_MySQL数据库基础

热门文章

  1. 《马丁伊登》读后感范文(下)2000字
  2. xp系统显示无打印机服务器,怎样解决WinXP系统使用打印机提示无法设置默认打印机...
  3. 华硕的电脑装linux系统安装教程,华硕笔记本电脑一键重装系统详细教程
  4. 439、Java框架93 -【SpringMVC - 拦截器】 2020.12.14
  5. win10无限蓝屏_windows10系统蓝屏无限重启命令解决教程
  6. 完整的OpenDDS的发布订阅编写及源码(Windows)
  7. 滴滴-2019+快手2020(A)-校园招聘算法工程师笔试题
  8. Windows 10出现0xc0000225错误代码如何解决?
  9. FI MM CO T-CODE
  10. virtualBox虚拟机之间网络互通设置