|
|
|
@ -0,0 +1,352 @@
|
|
|
|
|
<h1><center>企业级防火墙技术Firewalld</center></h1>
|
|
|
|
|
|
|
|
|
|
------
|
|
|
|
|
|
|
|
|
|
**作者:行癫(盗版必究)**
|
|
|
|
|
|
|
|
|
|
## 一:Firewalld简介
|
|
|
|
|
|
|
|
|
|
7中默认将原来的防火墙iptables升级为了firewalld,firewalld跟iptables比起来至少有两大好处;
|
|
|
|
|
|
|
|
|
|
firewalld可以动态修改单条规则,而不需要像iptables那样,在修改了规则后必须得全部刷新才可以生效
|
|
|
|
|
|
|
|
|
|
firewalld在使用上要比iptables人性化很多,即使不明白“四张表五条链”而且对TCP/IP协议也不理解也可以实现大部分功能
|
|
|
|
|
|
|
|
|
|
#### 1.目录结构
|
|
|
|
|
|
|
|
|
|
firewalld的配置文件以xml格式为主(主配置文件firewalld.conf例外),有两个存储位置
|
|
|
|
|
|
|
|
|
|
/etc/firewalld/
|
|
|
|
|
|
|
|
|
|
/usr/lib/firewalld/
|
|
|
|
|
|
|
|
|
|
使用规则
|
|
|
|
|
|
|
|
|
|
当需要一个文件时firewalld会首先到第一个目录中去查找,如果可以找到,那么就直接使用,否则会继续到第二个目录中查找
|
|
|
|
|
|
|
|
|
|
注意
|
|
|
|
|
|
|
|
|
|
两个目录下的同名配置文件第一个目录内的生效
|
|
|
|
|
|
|
|
|
|
第二个目录中存放的是firewalld给提供的通用配置文件,如果我们想修改配置, 那么可以copy一份到第一个目录中
|
|
|
|
|
|
|
|
|
|
当然也可以直接修改第二个目录下的文件
|
|
|
|
|
|
|
|
|
|
#### 2.相关命令
|
|
|
|
|
|
|
|
|
|
查看服务状态
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
[root@xingdian~ ]# systemctl status firewalld
|
|
|
|
|
[root@xingdian~ ]# systemctl status -l firewalld
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
启动关闭服务
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
[root@xingdian~ ]# systemctl start firewalld
|
|
|
|
|
[root@xingdian~ ]# systemctl stop firewalld
|
|
|
|
|
[root@xingdian~ ]# systemctl reload firewalld
|
|
|
|
|
[root@xingdian~ ]# systemctl disable firewalld
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 二:Firewalld之Zone
|
|
|
|
|
|
|
|
|
|
firewalld默认提供了九个zone配置文件:block.xml、dmz.xml、drop.xml、external.xml、 home.xml、internal.xml、public.xml、trusted.xml、work.xml,他们都保存在“/usr/lib /firewalld/zones/”目录下;九个zone其实就是九种方案,而且起决定作用的其实是每个xml文件所包含的内容,而不是文件名,所以不需要对每种zone(每个文件名)的含义花费过多的精力,比如trusted这个zone会信任所有的数据包,也就是说所有数据包都会放行,但是public这个zone只会放行其中所配置的服务,其他的一律不予放行,其实我们如果将这两个文件中的内容互换一下他们的规则就换过来了,也就是public这个zone会放行所有的数据包
|
|
|
|
|
|
|
|
|
|
注意:
|
|
|
|
|
|
|
|
|
|
生效的只有默认zone里面的规则
|
|
|
|
|
|
|
|
|
|
trusted.xml中zone的target,就是因为他设置为了ACCEPT,所以才会放行所有的数据包
|
|
|
|
|
|
|
|
|
|
而 public.xml中的zone没有target属性,这样就会默认拒绝通过,所以public这个zone只有其中配置过的服务才可以通过
|
|
|
|
|
|
|
|
|
|
#### 1.drop(丢弃)
|
|
|
|
|
|
|
|
|
|
任何接收的网络数据包都被丢弃,没有任何回复。仅能有发送出去的网络连接
|
|
|
|
|
|
|
|
|
|
#### 2.block(限制)
|
|
|
|
|
|
|
|
|
|
任何接收的网络连接都被 IPv4 的 icmp-host-prohibited 信息和 IPv6 的 icmp6-adm-prohibited 信息所拒绝
|
|
|
|
|
|
|
|
|
|
#### 3.public(公共)
|
|
|
|
|
|
|
|
|
|
在公共区域内使用,不能相信网络内的其他计算机不会对您的计算机造成危害,只能接收经过选取的连接
|
|
|
|
|
|
|
|
|
|
#### 4.external(外部)
|
|
|
|
|
|
|
|
|
|
是为路由器启用了伪装功能的外部网。不信任来自网络的其他计算,不相信它们不会对您的计算机造成危害,只接收经过选择的连接
|
|
|
|
|
|
|
|
|
|
#### 5.dmz(非军事区)
|
|
|
|
|
|
|
|
|
|
用于您的非军事区内的电脑,此区域内可公开访问,可以有限地进入您的内部网络,仅仅接收经过选择的连接
|
|
|
|
|
|
|
|
|
|
#### 6.work(工作)
|
|
|
|
|
|
|
|
|
|
用于工作区。您可以基本相信网络内的其他电脑不会危害您的电脑。仅仅接收经过选择的连接
|
|
|
|
|
|
|
|
|
|
#### 7.home(家庭)
|
|
|
|
|
|
|
|
|
|
用于家庭网络。您可以基本信任网络内的其他计算机不会危害您的计算机。仅仅接收经过选择的连接
|
|
|
|
|
|
|
|
|
|
#### 8.internal(内部)
|
|
|
|
|
|
|
|
|
|
用于内部网络。您可以基本上信任网络内的其他计算机不会威胁您的计算机。仅仅接受经过选择的连接
|
|
|
|
|
|
|
|
|
|
#### 9.trusted(信任)
|
|
|
|
|
|
|
|
|
|
可接受所有的网络连接
|
|
|
|
|
|
|
|
|
|
## 三:规则配置
|
|
|
|
|
|
|
|
|
|
#### 1.方式
|
|
|
|
|
|
|
|
|
|
firewall-config(图形方式)
|
|
|
|
|
|
|
|
|
|
firewall-cmd(命令行方式)
|
|
|
|
|
|
|
|
|
|
直接编辑xml文件
|
|
|
|
|
|
|
|
|
|
#### 2.命令行方式
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
显示防火墙状态:
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --state
|
|
|
|
|
running
|
|
|
|
|
列出当前有几个zone:
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --get-zones
|
|
|
|
|
block dmz drop external home internal public trusted work
|
|
|
|
|
取得当前活动的zones:
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --get-active-zones
|
|
|
|
|
public
|
|
|
|
|
interfaces: ens33
|
|
|
|
|
|
|
|
|
|
设置当前区域的接口:
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --get-zone-of-interface=enp03s
|
|
|
|
|
临时修改网络接口(enp0s3)为内部区域(internal):
|
|
|
|
|
[root@xingdian~ ]#firewall-cmd --zone=internal --change-interface=enp03s
|
|
|
|
|
取得默认的zone:
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --get-default-zone
|
|
|
|
|
public
|
|
|
|
|
设置默认zone:
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --set-default-zone=public
|
|
|
|
|
|
|
|
|
|
取得当前支持的service(跟当前服务器是否已经安装某服务无关):
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --get-services
|
|
|
|
|
RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client ceph ceph-mon dhcp dhcpv6 dhcpv6-client dns docker-registry dropbox-lansync freeipa-ldap freeipa-ldaps freeipa-replication ftp high-availability http https imap imaps ipp ipp-client ipsec iscsi-target kadmin kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mosh mountd ms-wbt mysql nfs ntp openvpn pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster radius rpc-bind rsyncd samba samba-client sane smtp smtps snmp snmptrap squid ssh synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client vdsm vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server
|
|
|
|
|
|
|
|
|
|
检查下一次重载后将激活的服务:
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --get-service --permanent
|
|
|
|
|
RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client ceph ceph-mon dhcp dhcpv6 dhcpv6-client dns docker-registry dropbox-lansync freeipa-ldap freeipa-ldaps freeipa-replication ftp high-availability http https imap imaps ipp ipp-client ipsec iscsi-target kadmin kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mosh mountd ms-wbt mysql nfs ntp openvpn pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster radius rpc-bind rsyncd samba samba-client sane smtp smtps snmp snmptrap squid ssh synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client vdsm vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server
|
|
|
|
|
|
|
|
|
|
列出zone public 端口:
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --zone=public --list-ports
|
|
|
|
|
|
|
|
|
|
列出zone public当前设置:
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --zone=public --list-all
|
|
|
|
|
public (default, active)
|
|
|
|
|
interfaces: eno16777736
|
|
|
|
|
sources:
|
|
|
|
|
services: dhcpv6-client ssh
|
|
|
|
|
ports:
|
|
|
|
|
masquerade: no 伪装功能
|
|
|
|
|
forward-ports:
|
|
|
|
|
icmp-blocks:
|
|
|
|
|
rich rules:
|
|
|
|
|
|
|
|
|
|
增加zone public开放http service:
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --zone=public --add-service=http
|
|
|
|
|
success
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --permanent --zone=internal --add-service=http
|
|
|
|
|
success
|
|
|
|
|
--permanent 永久生效
|
|
|
|
|
重新加载配置:
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --reload
|
|
|
|
|
success
|
|
|
|
|
增加zone internal开放443/tcp协议端口:
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --zone=internal --add-port=443/tcp
|
|
|
|
|
success
|
|
|
|
|
删除zone public 中ssh服务
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --zone=public --remove-service=ssh
|
|
|
|
|
列出zone internal的所有service:
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --zone=internal --list-services
|
|
|
|
|
dhcpv6-client ipp-client mdns samba-client ssh
|
|
|
|
|
|
|
|
|
|
设置黑/白名单: (hosts.allow hosts.deny)
|
|
|
|
|
增加172.28.129.0/24网段到zone trusted(信任)
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --permanent --zone=trusted --add-source=172.28.129.0/24
|
|
|
|
|
success
|
|
|
|
|
add-sourc
|
|
|
|
|
列出zone truste的白名单:
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --permanent --zone=trusted --list-sources
|
|
|
|
|
172.28.129.0/24
|
|
|
|
|
|
|
|
|
|
活动的zone:
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --get-active-zones
|
|
|
|
|
public
|
|
|
|
|
interfaces: eno16777736
|
|
|
|
|
|
|
|
|
|
添加zone truste后重新加载,然后查看--get-active-zones:
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --reload
|
|
|
|
|
success
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --get-active-zones
|
|
|
|
|
public
|
|
|
|
|
interfaces: ens32 veth4103622
|
|
|
|
|
trusted
|
|
|
|
|
sources: 172.28.129.0/24
|
|
|
|
|
|
|
|
|
|
列出zone drop所有规则:
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --zone=drop --list-all
|
|
|
|
|
drop
|
|
|
|
|
interfaces:
|
|
|
|
|
sources:
|
|
|
|
|
services:
|
|
|
|
|
ports:
|
|
|
|
|
masquerade: no
|
|
|
|
|
forward-ports:
|
|
|
|
|
icmp-blocks:
|
|
|
|
|
rich rules:
|
|
|
|
|
|
|
|
|
|
添加172.28.13.0/24到zone drop:
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --permanent --zone=drop --add-source=172.28.13.0/24
|
|
|
|
|
success
|
|
|
|
|
|
|
|
|
|
添加后需要重新加载:
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --reload
|
|
|
|
|
success
|
|
|
|
|
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --zone=drop --list-all
|
|
|
|
|
drop
|
|
|
|
|
interfaces:
|
|
|
|
|
sources: 172.28.13.0/24
|
|
|
|
|
services:
|
|
|
|
|
ports:
|
|
|
|
|
masquerade: no
|
|
|
|
|
forward-ports:
|
|
|
|
|
icmp-blocks:
|
|
|
|
|
rich rules:
|
|
|
|
|
|
|
|
|
|
[root@xingdian~ ]#firewall-cmd --reload
|
|
|
|
|
success
|
|
|
|
|
|
|
|
|
|
从zone drop中删除172.28.13.0/24:
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --permanent --zone=drop --remove-source=172.28.13.0/24
|
|
|
|
|
success
|
|
|
|
|
|
|
|
|
|
查看所有的zones规则:
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --list-all-zones
|
|
|
|
|
|
|
|
|
|
案例:
|
|
|
|
|
比如我当前的默认zone是public,需要开放80端口对外访问,则执行如下命令:
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --zone=public --permanent --add-port=80/tcp
|
|
|
|
|
success
|
|
|
|
|
[root@xingdian~ ]# firewall-cmd --reload
|
|
|
|
|
success
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 3.修改配置文件
|
|
|
|
|
|
|
|
|
|
```xml
|
|
|
|
|
以public zone为例,对应的配置文件是/etc/firewalld/zones/public.xml,像我们刚刚添加80端口后,体现在public.xml 中的内容为:
|
|
|
|
|
[root@xingdian~ ]# cat public.xml
|
|
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|
|
|
|
<zone>
|
|
|
|
|
<short>Public</short>
|
|
|
|
|
<description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
|
|
|
|
|
<service name="dhcpv6-client"/>
|
|
|
|
|
<service name="ssh"/>--reload 或重
|
|
|
|
|
<port protocol="tcp" port="80"/>
|
|
|
|
|
</zone>
|
|
|
|
|
注意在修改配置文件后 --reload 或重启 firewall 服务。
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 四:高级规则
|
|
|
|
|
|
|
|
|
|
#### 1.Direct Rules概述
|
|
|
|
|
|
|
|
|
|
通过firewall-cmd工具,可以使用 --direct选项在运行时间里增加和删除链。如果不熟悉iptables,使用直接接口非常危险,因为可能无意间导致防火墙被入侵。直接端口模式适用于服务或者程序,以便于在运行时间内增加特定的防火墙规则。直接端口模式添加的规则优先应用
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
Direct Options
|
|
|
|
|
The direct options give a more direct access to the firewall. These options
|
|
|
|
|
require user to know basic iptables concepts, i.e. table
|
|
|
|
|
(filter/mangle/nat/...), chain (INPUT/OUTPUT/FORWARD/...), commands
|
|
|
|
|
(-A/-D/-I/...), parameters (-p/-s/-d/-j/...) and targets
|
|
|
|
|
(ACCEPT/DROP/REJECT/...).
|
|
|
|
|
|
|
|
|
|
Direct options should be used only as a last resort when it's not possible to
|
|
|
|
|
use for example --add-service=service or --add-rich-rule='rule'.
|
|
|
|
|
|
|
|
|
|
The first argument of each option has to be ipv4 or ipv6 or eb. With ipv4 it
|
|
|
|
|
will be for IPv4 (iptables(8)), with ipv6 for IPv6 (ip6tables(8)) and with eb
|
|
|
|
|
for ethernet bridges (ebtables(8)).
|
|
|
|
|
|
|
|
|
|
[--permanent] --direct --get-all-chains
|
|
|
|
|
Get all chains added to all tables. This option concerns only chains
|
|
|
|
|
previously added with --direct --add-chain.
|
|
|
|
|
|
|
|
|
|
[--permanent] --direct --get-chains { ipv4 | ipv6 | eb } table
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
查看防火墙上设置的规则
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
[root@xingdian zones]# firewall-cmd --direct --get-all-rules
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
添加高级规则
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
[root@xingdian zones]# firewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 1 -p tcp --dport 80 -s 172.25.254.77 -j ACCEPT
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
注意:
|
|
|
|
|
|
|
|
|
|
只允许172.25.254.77通过80端口访问主机位为110的http服务。因为访问的是主机位为110的http服务,需要主机位为110的内核同意开启http服务,需要在表filter中设置INPUT;-p 数据包类型;--dport 服务端口
|
|
|
|
|
|
|
|
|
|
删除防火墙上的规则
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
[root@xingdian zones]# firewall-cmd --permanent --direct --remove-rule ipv4 filter INPUT 1 -p tcp --dport 22 -s 172.25.254.217 -j REJECT
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
注意:
|
|
|
|
|
|
|
|
|
|
移除:不允许172.25.254.217通过22端口的访问ssh,连接172.25.254.110这条规则
|
|
|
|
|
|
|
|
|
|
#### 2.端口转发
|
|
|
|
|
|
|
|
|
|
端口转发可以将指定地址访问指定的端口时,将流量转发至指定地址的指定端口。转发的目的如果不指定 ip 的话就默认为本机,如果指定了 ip 却没指定端口,则默认使用来源端口。 如果配置好端口转发之后不能用,可以检查下面两个问题
|
|
|
|
|
|
|
|
|
|
比如我将 80 端口转发至 8080 端口,首先检查本地的 80 端口和目标的 8080 端口是否开放监听了
|
|
|
|
|
|
|
|
|
|
其次检查是否允许伪装 IP,没允许的话要开启伪装 IP
|
|
|
|
|
|
|
|
|
|
案例
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
1. firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080
|
|
|
|
|
# 将80端口的流量转发至8080
|
|
|
|
|
2. firewall-cmd --add-forward-port=port=80:proto=tcp:toaddr=192.168.0.1
|
|
|
|
|
# 将80端口的流量转发至192.168.0.1
|
|
|
|
|
3. firewall-cmd --add-forward-port=port=80:proto=tcp:toaddr=192.168.0.1:toport=8080
|
|
|
|
|
# 将80端口的流量转发至192.168.0.1的8080端口
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
开启IP伪装
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
查看:
|
|
|
|
|
[root@xingdian zones]# firewall-cmd --query-masquerade yes no
|
|
|
|
|
开启:
|
|
|
|
|
[root@xingdian zones]# firewall-cmd --add-masquerade
|
|
|
|
|
关闭:
|
|
|
|
|
[root@xingdian zones]# firewall-cmd --remove-masquerade
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
作用
|
|
|
|
|
|
|
|
|
|
当我们想把某个端口隐藏起来的时候,就可以在防火墙上阻止那个端口访问,然后再开一个不规则的端口,之后配置防火墙的端口转发,将流量转发过去
|
|
|
|
|
|
|
|
|
|
端口转发还可以做流量分发,一个防火墙拖着好多台运行着不同服务的机器,然后用防火墙将不同端口的流量转发至不同机器
|