firewalld中常用的区域名称及策略规则
(NN@GBLU2`G)$8_JBQLRLXG.jpg

终端管理工具

firewall-cmd命令中使用的参数以及作用

_(%C%QGAA%Q7()Y%)60ZPEK.png
查看firewalld服务当前所使用的区域

  1. [root@localhost ~]# firewall-cmd --get-default-zone
  2. public

查询ens33网卡在firewalld服务中的区域

  1. [root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33
  2. public

把firewalld服务中ens33网卡的默认区域修改为external,并在系统重启后生效。分别查看当前与 永久模式下的区域名称

  1. [root@localhost ~]# firewall-cmd --permanent --zone=external --changeinterface=ens33
  2. The interface is under control of NetworkManager, setting zone to 'external'.
  3. success
  4. [root@localhost ~]# firewall-cmd --permanent --get-zone-of-interface=ens33
  5. no zone
  6. [root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33
  7. external

把firewalld服务的当前默认区域设置为public

  1. [root@localhost ~]# firewall-cmd --set-default-zone=public
  2. Warning: ZONE_ALREADY_SET: public
  3. success
  4. [root@localhost ~]# firewall-cmd --get-default-zone
  5. public

启动/关闭firewalld防火墙服务的应急状况模式,阻断一切网络连接(当远程控制服务器时请慎 用)

  1. [root@localhost ~]# firewall-cmd --panic-on
  2. [root@localhost ~]# firewall-cmd --panic-off

查询public区域是否允许请求SSH和HTTPS协议的流量

  1. [root@localhost ~]# firewall-cmd --zone=public --query-service=ssh
  2. yes
  3. [root@localhost ~]# firewall-cmd --zone=public --query-service=https
  4. no

把firewalld服务中请求HTTPS协议的流量设置为永久允许,并立即生效

  1. [root@localhost ~]# firewall-cmd --zone=public --add-service=https
  2. success
  3. [root@localhost ~]# firewall-cmd --permanent --zone=public --addservice=https
  4. success
  5. [root@localhost ~]# firewall-cmd --reload
  6. success
  7. [root@localhost ~]# firewall-cmd --zone=public --query-service=https
  8. yes

把firewalld服务中请求HTTP协议的流量设置为永久拒绝,并立即生效

  1. [root@localhost ~]# firewall-cmd --zone=public --remove-service=https --
  2. permanent
  3. success
  4. [root@localhost ~]# firewall-cmd --reload
  5. success

把在firewalld服务中访问8080和8081端口的流量策略设置为允许,但仅限当前生效

  1. [root@localhost ~]# firewall-cmd --zone=public --add-port=8080-8081/tcp
  2. success
  3. [root@localhost ~]# firewall-cmd --zone=public --list-ports
  4. 8080-8081/tcp

把原本访问本机888端口的流量转发到22端口,要且求当前和长期均有效
firewall-cmd —permanent —zone=<区域> —add-forward-port=port=<源端口号> :proto=<协议>:toport=<目标端口>:toaddr=<目标IP地址>

  1. [root@localhost ~]# firewall-cmd --permanent --zone=public --add-forwardport=port=888:proto=tcp:toport=22:toaddr=192.168.91.128
  2. success
  1. [c:\~]$ ssh root@192.168.91.128 888
  2. Connecting to 192.168.91.128:888...
  3. Connection established.
  4. To escape to local shell, press 'Ctrl+Alt+]'.
  5. Last login: Thu Apr 18 16:20:23 2019 from 192.168.91.1
  6. [root@localhost ~]#

firewalld中的富规则表示更细致、更详细的防火墙策略配置,它可以针对系统服务、端口号、源地址和 目标地址等诸多信息进行更有针对性的策略配置。它的优先级在所有的防火墙策略中也是最高的。比 如,我们可以在firewalld服务中配置一条富规则,使其拒绝192.168.91.0/24网段的所有用户访问本机的 ssh服务(22端口)

  1. [root@localhost ~]# firewall-cmd --permanent --zone=public --add-richrule="rule \
  2. family="ipv4" \
  3. source address="192.168.91.0/24" \
  4. service name="ssh" \
  5. reject"
  6. success
  7. [root@localhost ~]# firewall-cmd --reload
  8. success
  1. [c:\~]$ ssh root@192.168.91.128
  2. Connecting to 192.168.91.128:22...
  3. Could not connect to '192.168.91.128' (port 22): Connection failed.

服务的访问控制列表

TCP Wrappers服务的控制列表文件中常用的参数

H1S18SB%KH9IH}_T7$}2Q8Y.png
在配置TCP Wrappers服务时需要遵循两个原则:
编写拒绝策略规则时,填写的是服务名称,而非协议名称;
建议先编写拒绝策略规则,再编写允许策略规则,以便直观地看到相应的效果。
下面编写拒绝策略规则文件,禁止访问本机sshd服务的所有流量

  1. [root@localhost ~]# vim /etc/hosts.deny
  2. #
  3. # hosts.deny This file contains access rules which are used to
  4. # deny connections to network services that either use
  5. # the tcp_wrappers library or that have been
  6. # started through a tcp_wrappers-enabled xinetd.
  7. #
  8. # The rules in this file can also be set up in
  9. # /etc/hosts.allow with a 'deny' option instead.
  10. #
  11. # See 'man 5 hosts_options' and 'man 5 hosts_access'
  12. # for information on rule syntax.
  13. # See 'man tcpd' for information on tcp_wrappers
  14. #
  15. #
  16. sshd:*
  1. [c:\~]$ ssh root@192.168.91.128
  2. Connecting to 192.168.91.128:22...
  3. Connection established.
  4. To escape to local shell, press 'Ctrl+Alt+]'.
  5. Connection closing...Socket close.
  6. Connection closed by foreign host.
  7. Disconnected from remote host(192.168.91.128:22) at 16:35:31.

在允许策略规则文件中添加一条规则,使其放行源自192.168.10.0/24网段,访问本机sshd服务的所有 流量

  1. [root@localhost ~]# vim /etc/hosts.allow
  2. #
  3. # hosts.allow This file contains access rules which are used to
  4. # allow or deny connections to network services that
  5. # either use the tcp_wrappers library or that have been
  6. # started through a tcp_wrappers-enabled xinetd.
  7. #
  8. # See 'man 5 hosts_options' and 'man 5 hosts_access'
  9. # for information on rule syntax.
  10. # See 'man tcpd' for information on tcp_wrappers
  11. #
  12. sshd:192.168.91.

SELinux安全子系统

SELinux(Security-Enhanced Linux)是美国国家安全局在Linux开源社区的帮助下开发的一个强制访问 控制(MAC,Mandatory Access Control)的安全子系统。RHEL 7系统使用SELinux技术的目的是为了 让各个服务进程都受到约束,使其仅获取到本应获取的资源。

SELinux服务有三种配置模式,具体如下。
enforcing:强制启用安全策略模式,将拦截服务的不合法请求。
permissive:遇到服务越权访问时,只发出警告而不强制拦截。
disabled:对于越权的行为不警告也不拦截。
然在禁用SELinux服务后确实能够减少报错几率,但这在生产环境中相当不推荐。

  1. [root@localhost ~]# vim /etc/selinux/config
  2. # This file controls the state of SELinux on the system.
  3. # SELINUX= can take one of these three values:
  4. # enforcing - SELinux security policy is enforced.
  5. # permissive - SELinux prints warnings instead of enforcing.
  6. # disabled - No SELinux policy is loaded.
  7. SELINUX=enforcing
  8. # SELINUXTYPE= can take one of three values:
  9. # targeted - Targeted processes are protected,
  10. # minimum - Modification of targeted policy. Only selected processes are
  11. protected.
  12. # mls - Multi Level Security protection.
  13. SELINUXTYPE=targeted

SELinux服务的主配置文件中,定义的是SELinux的默认运行状态,可以将其理解为系统重启后的状态, 因此它不会在更改后立即生效。可以使用getenforce命令获得当前SELinux服务的运行模式

  1. [root@localhost ~]# getenforce
  2. Enforcing

可以用setenforce [0|1]命令修改SELinux当前的运行模式(0为禁用,1为启用)。注意,这种修改只是 临时的,在系统重启后就会失效
我们比较一下自己创建的acpache目录和原本的目录的安全上下文

  1. [root@localhost ~]# ls -Zd /var/www/html
  2. drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html
  3. [root@localhost ~]# ls -Zd /home/wwwroot/
  4. drwxr-xr-x. root root unconfined_u:object_r:home_root_t:s0 /home/wwwroot/

在文件上设置的SELinux安全上下文是由用户段、角色段以及类型段等多个信息项共同组成的。其中,
用户段system_u代表系统进程的身份,
角色段object_r代表文件目录的角色,
类型段httpd_sys_content_t代表网站服务的系统文件

semanage

用于管理SELinux的策略
semanage [选项] [文件]
使用semanage命令时,经常用到的几个参数及其功能如下所示:
-l参数用于查询;
-a参数用于添加;
-m参数用于修改;
-d参数用于删除。
可以向新的网站数据目录中新添加一条SELinux安全上下文,让这个目录以及里面的所有文件能够被 httpd服务程序所访问到

  1. [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t
  2. /home/wwwroot/
  3. [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t
  4. /home/wwwroot/*
  5. [root@localhost ~]# restorecon -Rv /home/wwwroot/
  6. restorecon reset /home/wwwroot context unconfined_u:object_r:home_root_t:s0-
  7. >unconfined_u:object_r:user_home_dir_t:s0
  8. restorecon reset /home/wwwroot/index.html context
  9. unconfined_u:object_r:home_root_t:s0-
  10. >unconfined_u:object_r:httpd_sys_content_t:s0

注意,执行上述设置之后,还无法立即访问网站,还需要使用restorecon命令将设置好的SELinux安全 上下文立即生效。在使用restorecon命令时,可以加上-Rv参数对指定的目录进行递归操作,以及显示 SELinux安全上下文的修改过程。最后,再次刷新页面,就可以正常看到网页内容了

使用getsebool命令查询并过滤出所有与HTTP协议相关的安全策略。其中,off为禁止状态,on为允许状 态 。

  1. [root@localhost ~]# getsebool -a | grep http
  2. httpd_anon_write --> off
  3. httpd_builtin_scripting --> on
  4. httpd_can_check_spam --> off
  5. httpd_can_connect_ftp --> off
  6. httpd_can_connect_ldap --> off
  7. httpd_can_connect_mythtv --> off
  8. httpd_can_connect_zabbix --> off
  9. httpd_can_network_connect --> off
  10. httpd_can_network_connect_cobbler --> off
  11. httpd_can_network_connect_db --> off
  12. httpd_can_network_memcache --> off
  13. httpd_can_network_relay --> off
  14. httpd_can_sendmail --> off
  15. httpd_dbus_avahi --> off
  16. httpd_dbus_sssd --> off
  17. httpd_dontaudit_search_dirs --> off
  18. httpd_enable_cgi --> on
  19. httpd_enable_ftp_server --> off
  20. httpd_enable_homedirs --> off
  21. httpd_execmem --> off
  22. httpd_graceful_shutdown --> on
  23. httpd_manage_ipa --> off
  24. httpd_mod_auth_ntlm_winbind --> off
  25. httpd_mod_auth_pam --> off
  26. httpd_read_user_content --> off
  27. httpd_run_ipa --> off
  28. httpd_run_preupgrade --> off
  29. httpd_run_stickshift --> off
  30. httpd_serve_cobbler_files --> off
  31. httpd_setrlimit --> off
  32. httpd_ssi_exec --> off
  33. httpd_sys_script_anon_write --> off
  34. httpd_tmp_exec --> off
  35. httpd_tty_comm --> off
  36. httpd_unified --> off
  37. httpd_use_cifs --> off
  38. httpd_use_fusefs --> off
  39. httpd_use_gpg --> off
  40. httpd_use_nfs --> off
  41. httpd_use_openstack --> off
  42. httpd_use_sasl --> off
  43. httpd_verify_dns --> off
  44. named_tcp_bind_http_port --> off
  45. prosody_bind_http_port --> off
  46. [root@localhost ~]# setsebool -P httpd_enable_homedirs=on

面对如此多的 SELinux 域安全策略规则,实在没有必要逐个理解它们,我们只要能通过名字大致猜测出 相关的策略用途就足够了。比如,想要开启 httpd 服务的个人用户主页功能,那么用到的 SELinux 域安 全策略应该是 httpd_enable_homedir