终端管理工具
firewall-cmd命令中使用的参数以及作用

查看firewalld服务当前所使用的区域
[root@localhost ~]# firewall-cmd --get-default-zonepublic
查询ens33网卡在firewalld服务中的区域
[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33public
把firewalld服务中ens33网卡的默认区域修改为external,并在系统重启后生效。分别查看当前与 永久模式下的区域名称
[root@localhost ~]# firewall-cmd --permanent --zone=external --changeinterface=ens33The interface is under control of NetworkManager, setting zone to 'external'.success[root@localhost ~]# firewall-cmd --permanent --get-zone-of-interface=ens33no zone[root@localhost ~]# firewall-cmd --get-zone-of-interface=ens33external
把firewalld服务的当前默认区域设置为public
[root@localhost ~]# firewall-cmd --set-default-zone=publicWarning: ZONE_ALREADY_SET: publicsuccess[root@localhost ~]# firewall-cmd --get-default-zonepublic
启动/关闭firewalld防火墙服务的应急状况模式,阻断一切网络连接(当远程控制服务器时请慎 用)
[root@localhost ~]# firewall-cmd --panic-on[root@localhost ~]# firewall-cmd --panic-off
查询public区域是否允许请求SSH和HTTPS协议的流量
[root@localhost ~]# firewall-cmd --zone=public --query-service=sshyes[root@localhost ~]# firewall-cmd --zone=public --query-service=httpsno
把firewalld服务中请求HTTPS协议的流量设置为永久允许,并立即生效
[root@localhost ~]# firewall-cmd --zone=public --add-service=httpssuccess[root@localhost ~]# firewall-cmd --permanent --zone=public --addservice=httpssuccess[root@localhost ~]# firewall-cmd --reloadsuccess[root@localhost ~]# firewall-cmd --zone=public --query-service=httpsyes
把firewalld服务中请求HTTP协议的流量设置为永久拒绝,并立即生效
[root@localhost ~]# firewall-cmd --zone=public --remove-service=https --permanentsuccess[root@localhost ~]# firewall-cmd --reloadsuccess
把在firewalld服务中访问8080和8081端口的流量策略设置为允许,但仅限当前生效
[root@localhost ~]# firewall-cmd --zone=public --add-port=8080-8081/tcpsuccess[root@localhost ~]# firewall-cmd --zone=public --list-ports8080-8081/tcp
把原本访问本机888端口的流量转发到22端口,要且求当前和长期均有效
firewall-cmd —permanent —zone=<区域> —add-forward-port=port=<源端口号> :proto=<协议>:toport=<目标端口>:toaddr=<目标IP地址>
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-forwardport=port=888:proto=tcp:toport=22:toaddr=192.168.91.128success
[c:\~]$ ssh root@192.168.91.128 888Connecting to 192.168.91.128:888...Connection established.To escape to local shell, press 'Ctrl+Alt+]'.Last login: Thu Apr 18 16:20:23 2019 from 192.168.91.1[root@localhost ~]#
firewalld中的富规则表示更细致、更详细的防火墙策略配置,它可以针对系统服务、端口号、源地址和 目标地址等诸多信息进行更有针对性的策略配置。它的优先级在所有的防火墙策略中也是最高的。比 如,我们可以在firewalld服务中配置一条富规则,使其拒绝192.168.91.0/24网段的所有用户访问本机的 ssh服务(22端口)
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-richrule="rule \family="ipv4" \source address="192.168.91.0/24" \service name="ssh" \reject"success[root@localhost ~]# firewall-cmd --reloadsuccess
[c:\~]$ ssh root@192.168.91.128Connecting to 192.168.91.128:22...Could not connect to '192.168.91.128' (port 22): Connection failed.
服务的访问控制列表
TCP Wrappers服务的控制列表文件中常用的参数

在配置TCP Wrappers服务时需要遵循两个原则:
编写拒绝策略规则时,填写的是服务名称,而非协议名称;
建议先编写拒绝策略规则,再编写允许策略规则,以便直观地看到相应的效果。
下面编写拒绝策略规则文件,禁止访问本机sshd服务的所有流量
[root@localhost ~]# vim /etc/hosts.deny## hosts.deny This file contains access rules which are used to# deny connections to network services that either use# the tcp_wrappers library or that have been# started through a tcp_wrappers-enabled xinetd.## The rules in this file can also be set up in# /etc/hosts.allow with a 'deny' option instead.## See 'man 5 hosts_options' and 'man 5 hosts_access'# for information on rule syntax.# See 'man tcpd' for information on tcp_wrappers##sshd:*
[c:\~]$ ssh root@192.168.91.128Connecting to 192.168.91.128:22...Connection established.To escape to local shell, press 'Ctrl+Alt+]'.Connection closing...Socket close.Connection closed by foreign host.Disconnected from remote host(192.168.91.128:22) at 16:35:31.
在允许策略规则文件中添加一条规则,使其放行源自192.168.10.0/24网段,访问本机sshd服务的所有 流量
[root@localhost ~]# vim /etc/hosts.allow## hosts.allow This file contains access rules which are used to# allow or deny connections to network services that# either use the tcp_wrappers library or that have been# started through a tcp_wrappers-enabled xinetd.## See 'man 5 hosts_options' and 'man 5 hosts_access'# for information on rule syntax.# See 'man tcpd' for information on tcp_wrappers#sshd:192.168.91.
SELinux安全子系统
SELinux(Security-Enhanced Linux)是美国国家安全局在Linux开源社区的帮助下开发的一个强制访问 控制(MAC,Mandatory Access Control)的安全子系统。RHEL 7系统使用SELinux技术的目的是为了 让各个服务进程都受到约束,使其仅获取到本应获取的资源。
SELinux服务有三种配置模式,具体如下。
enforcing:强制启用安全策略模式,将拦截服务的不合法请求。
permissive:遇到服务越权访问时,只发出警告而不强制拦截。
disabled:对于越权的行为不警告也不拦截。
然在禁用SELinux服务后确实能够减少报错几率,但这在生产环境中相当不推荐。
[root@localhost ~]# vim /etc/selinux/config# This file controls the state of SELinux on the system.# SELINUX= can take one of these three values:# enforcing - SELinux security policy is enforced.# permissive - SELinux prints warnings instead of enforcing.# disabled - No SELinux policy is loaded.SELINUX=enforcing# SELINUXTYPE= can take one of three values:# targeted - Targeted processes are protected,# minimum - Modification of targeted policy. Only selected processes areprotected.# mls - Multi Level Security protection.SELINUXTYPE=targeted
SELinux服务的主配置文件中,定义的是SELinux的默认运行状态,可以将其理解为系统重启后的状态, 因此它不会在更改后立即生效。可以使用getenforce命令获得当前SELinux服务的运行模式
[root@localhost ~]# getenforceEnforcing
可以用setenforce [0|1]命令修改SELinux当前的运行模式(0为禁用,1为启用)。注意,这种修改只是 临时的,在系统重启后就会失效
我们比较一下自己创建的acpache目录和原本的目录的安全上下文
[root@localhost ~]# ls -Zd /var/www/htmldrwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html[root@localhost ~]# ls -Zd /home/wwwroot/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服务程序所访问到
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t/home/wwwroot/[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t/home/wwwroot/*[root@localhost ~]# restorecon -Rv /home/wwwroot/restorecon reset /home/wwwroot context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:user_home_dir_t:s0restorecon reset /home/wwwroot/index.html contextunconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
注意,执行上述设置之后,还无法立即访问网站,还需要使用restorecon命令将设置好的SELinux安全 上下文立即生效。在使用restorecon命令时,可以加上-Rv参数对指定的目录进行递归操作,以及显示 SELinux安全上下文的修改过程。最后,再次刷新页面,就可以正常看到网页内容了
使用getsebool命令查询并过滤出所有与HTTP协议相关的安全策略。其中,off为禁止状态,on为允许状 态 。
[root@localhost ~]# getsebool -a | grep httphttpd_anon_write --> offhttpd_builtin_scripting --> onhttpd_can_check_spam --> offhttpd_can_connect_ftp --> offhttpd_can_connect_ldap --> offhttpd_can_connect_mythtv --> offhttpd_can_connect_zabbix --> offhttpd_can_network_connect --> offhttpd_can_network_connect_cobbler --> offhttpd_can_network_connect_db --> offhttpd_can_network_memcache --> offhttpd_can_network_relay --> offhttpd_can_sendmail --> offhttpd_dbus_avahi --> offhttpd_dbus_sssd --> offhttpd_dontaudit_search_dirs --> offhttpd_enable_cgi --> onhttpd_enable_ftp_server --> offhttpd_enable_homedirs --> offhttpd_execmem --> offhttpd_graceful_shutdown --> onhttpd_manage_ipa --> offhttpd_mod_auth_ntlm_winbind --> offhttpd_mod_auth_pam --> offhttpd_read_user_content --> offhttpd_run_ipa --> offhttpd_run_preupgrade --> offhttpd_run_stickshift --> offhttpd_serve_cobbler_files --> offhttpd_setrlimit --> offhttpd_ssi_exec --> offhttpd_sys_script_anon_write --> offhttpd_tmp_exec --> offhttpd_tty_comm --> offhttpd_unified --> offhttpd_use_cifs --> offhttpd_use_fusefs --> offhttpd_use_gpg --> offhttpd_use_nfs --> offhttpd_use_openstack --> offhttpd_use_sasl --> offhttpd_verify_dns --> offnamed_tcp_bind_http_port --> offprosody_bind_http_port --> off[root@localhost ~]# setsebool -P httpd_enable_homedirs=on
面对如此多的 SELinux 域安全策略规则,实在没有必要逐个理解它们,我们只要能通过名字大致猜测出 相关的策略用途就足够了。比如,想要开启 httpd 服务的个人用户主页功能,那么用到的 SELinux 域安 全策略应该是 httpd_enable_homedir
