564 lines
16 KiB
Markdown
564 lines
16 KiB
Markdown
<h2><center>Firewalld</center></h2>
|
||
|
||
------
|
||
|
||
## 一:Firewalld 简介
|
||
|
||
`centos 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. 相关命令
|
||
|
||
查看服务状态
|
||
|
||
```bash
|
||
[root@wxin ~]# systemctl status firewalld
|
||
[root@wxin ~]# systemctl status -l firewalld
|
||
```
|
||
|
||
启动关闭服务
|
||
|
||
```bash
|
||
[root@wxin ~]# systemctl start firewalld
|
||
[root@wxin ~]# systemctl stop firewalld
|
||
[root@wxin ~]# systemctl reload firewalld
|
||
[root@wxin ~]# 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. 命令行方式
|
||
|
||
#### 1. 查看
|
||
|
||
**查看当前默认区间**
|
||
|
||
```bash
|
||
[root@wxin ~]# firewall-cmd --get-default-zone
|
||
public
|
||
```
|
||
|
||
**显示当前正在使用的区域及其对应的网卡接口**
|
||
|
||
```bash
|
||
[root@wxin ~]# firewall-cmd --get-active-zones
|
||
public
|
||
interfaces: ens33
|
||
```
|
||
|
||
**显示所有可用的区域**
|
||
|
||
```bash
|
||
[root@wxin ~]# firewall-cmd --get-zones
|
||
block dmz drop external home internal public trusted work
|
||
```
|
||
|
||
**显示所有区域及其规则**
|
||
|
||
```bash
|
||
[root@wxin ~]# firewall-cmd --list-all-zones
|
||
block
|
||
target: %%REJECT%%
|
||
icmp-block-inversion: no
|
||
interfaces:
|
||
sources:
|
||
services:
|
||
ports:
|
||
protocols:
|
||
masquerade: no
|
||
forward-ports:
|
||
source-ports:
|
||
icmp-blocks:
|
||
rich rules:
|
||
|
||
|
||
dmz
|
||
target: default
|
||
icmp-block-inversion: no
|
||
interfaces:
|
||
sources:
|
||
services: ssh
|
||
ports:
|
||
protocols:
|
||
masquerade: no
|
||
forward-ports:
|
||
source-ports:
|
||
icmp-blocks:
|
||
rich rules:
|
||
|
||
|
||
drop
|
||
target: DROP
|
||
icmp-block-inversion: no
|
||
interfaces:
|
||
sources:
|
||
services:
|
||
ports:
|
||
protocols:
|
||
masquerade: no
|
||
forward-ports:
|
||
source-ports:
|
||
icmp-blocks:
|
||
rich rules:
|
||
|
||
|
||
external
|
||
target: default
|
||
icmp-block-inversion: no
|
||
interfaces:
|
||
sources:
|
||
services: ssh
|
||
ports:
|
||
protocols:
|
||
masquerade: yes
|
||
forward-ports:
|
||
source-ports:
|
||
icmp-blocks:
|
||
rich rules:
|
||
|
||
|
||
home
|
||
target: default
|
||
icmp-block-inversion: no
|
||
interfaces:
|
||
sources:
|
||
services: dhcpv6-client mdns samba-client ssh
|
||
ports:
|
||
protocols:
|
||
masquerade: no
|
||
forward-ports:
|
||
source-ports:
|
||
icmp-blocks:
|
||
rich rules:
|
||
|
||
|
||
internal
|
||
target: default
|
||
icmp-block-inversion: no
|
||
interfaces:
|
||
sources:
|
||
services: dhcpv6-client mdns samba-client ssh
|
||
ports:
|
||
protocols:
|
||
masquerade: no
|
||
forward-ports:
|
||
source-ports:
|
||
icmp-blocks:
|
||
rich rules:
|
||
|
||
|
||
public (active)
|
||
target: default
|
||
icmp-block-inversion: no
|
||
interfaces: ens33
|
||
sources:
|
||
services: dhcpv6-client ssh
|
||
ports:
|
||
protocols:
|
||
masquerade: no
|
||
forward-ports:
|
||
source-ports:
|
||
icmp-blocks:
|
||
rich rules:
|
||
|
||
|
||
trusted
|
||
target: ACCEPT
|
||
icmp-block-inversion: no
|
||
interfaces:
|
||
sources:
|
||
services:
|
||
ports:
|
||
protocols:
|
||
masquerade: no
|
||
forward-ports:
|
||
source-ports:
|
||
icmp-blocks:
|
||
rich rules:
|
||
|
||
|
||
work
|
||
target: default
|
||
icmp-block-inversion: no
|
||
interfaces:
|
||
sources:
|
||
services: dhcpv6-client ssh
|
||
ports:
|
||
protocols:
|
||
masquerade: no
|
||
forward-ports:
|
||
source-ports:
|
||
icmp-blocks:
|
||
rich rules:
|
||
```
|
||
|
||
**查看默认区域内允许访问的所有服务**
|
||
|
||
```bash
|
||
[root@wxin ~]# firewall-cmd --list-service
|
||
dhcpv6-client ssh
|
||
```
|
||
|
||
**查看指定区域允许访问的服务列表**
|
||
|
||
```bash
|
||
firewall-cmd --list-service --zone=区域名
|
||
|
||
[root@wxin ~]# firewall-cmd --list-service --zone=work
|
||
dhcpv6-client ssh
|
||
```
|
||
|
||
**查看指定区域允许访问的端口列表**
|
||
|
||
```bash
|
||
firewall-cmd --list-ports --zone=区域名
|
||
|
||
[root@wxin ~]# firewall-cmd --list-ports --zone=work
|
||
|
||
```
|
||
|
||
**查看与网卡绑定的区域**
|
||
|
||
```bash
|
||
firewall-cmd --get-zone-of-interface=网卡名
|
||
|
||
[root@wxin ~]# firewall-cmd --get-zone-of-interface=ens33
|
||
public
|
||
```
|
||
|
||
**查看所有icmp类型**
|
||
|
||
```bash
|
||
[root@wxin ~]# firewall-cmd --get-icmptypes
|
||
address-unreachable bad-header communication-prohibited destination-unreachable echo-reply echo-request fragmentation-needed host-precedence-violation host-prohibited host-redirect host-unknown host-unreachable ip-header-bad neighbour-advertisement neighbour-solicitation network-prohibited network-redirect network-unknown network-unreachable no-route packet-too-big parameter-problem port-unreachable precedence-cutoff protocol-unreachable redirect required-option-missing router-advertisement router-solicitation source-quench source-route-failed time-exceeded timestamp-reply timestamp-request tos-host-redirect tos-host-unreachable tos-network-redirect tos-network-unreachable ttl-zero-during-reassembly ttl-zero-during-transit unknown-header-type unknown-option
|
||
```
|
||
|
||
#### 2. 添加
|
||
|
||
**给指定区域添加绑定的网卡**
|
||
|
||
```bash
|
||
firewall-cmd --add-interface=网卡名 --zone=区域名
|
||
```
|
||
|
||
**给指定区域添加源地址**
|
||
|
||
```bash
|
||
firewall-cmd --add-source=源地址 --zone=区域名
|
||
```
|
||
|
||
**给指定区域添加允许访问的服务**
|
||
|
||
```bash
|
||
firewall-cmd --add-service=服务名 --zone=区域名
|
||
```
|
||
|
||
**给指定区域添加允许访问的服务列表**
|
||
|
||
```bash
|
||
firewall-cmd --add-service={服务名1,服务名2,...} --zone=区域名
|
||
```
|
||
|
||
**给指定区域添加允许访问的端口**
|
||
|
||
```bash
|
||
firewall-cmd --add-port=端口/协议 --zone=区域名
|
||
```
|
||
|
||
**给指定区域添加允许访问的连续的端口列表**
|
||
|
||
```bash
|
||
firewall-cmd --add-port=端口1-端口2/协议 --zone=区域名
|
||
```
|
||
|
||
**给指定区域添加允许访问的不连续的端口**
|
||
|
||
```bash
|
||
firewall-cmd --add-port={端口1,端口2,...}/协议 --zone=区域名
|
||
```
|
||
|
||
**给指定区域添加拒绝访问的icmp类型**
|
||
|
||
```bash
|
||
firewall-cmd --add-icmp-block=icmp类型 --zone=区域名
|
||
```
|
||
|
||
#### 3. 删除
|
||
|
||
**根据服务名删除**
|
||
|
||
```bash
|
||
firewall-cmd --remove-service=服务名 --zone=区域名
|
||
```
|
||
|
||
**根据端口/协议删除**
|
||
|
||
```bash
|
||
firewall-cmd --remove-port=端口/协议 --zone=区域名
|
||
```
|
||
|
||
**根据icmp类型删除**
|
||
|
||
```bash
|
||
firewall-cmd --remove-icmp-block=icmp类型 --zone=区域名
|
||
```
|
||
|
||
**从指定区域里删除绑定的网卡**
|
||
|
||
```bash
|
||
firewall-cmd --remove-interface=网卡名 --zone=区域名
|
||
```
|
||
|
||
**从指定区域里删除绑定的源地址**
|
||
|
||
```bash
|
||
firewall-cmd --remove-source=源地址 --zone=区域名
|
||
```
|
||
|
||
#### 4. 修改
|
||
|
||
**修改当前默认区域**
|
||
|
||
```bash
|
||
firewall-cmd --set-default-zone=区域名
|
||
|
||
[root@wxin ~]# firewall-cmd --set-default-zone=work
|
||
success
|
||
[root@wxin ~]# firewall-cmd --get-default-zone
|
||
work
|
||
```
|
||
|
||
**修改/添加网卡 绑定给指定区域**
|
||
|
||
```bash
|
||
firewall-cmd --change-interface=网卡名 --zone=区域名
|
||
```
|
||
|
||
**修改/添加源地址 绑定给指定区域**
|
||
|
||
```bash
|
||
firewall-cmd --change-source=源地址 --zone=区域名
|
||
```
|
||
|
||
#### 5.案例
|
||
|
||
比如我当前的默认zone是public,需要开放80端口对外访问,则执行如下命令:
|
||
|
||
```bash
|
||
[root@wxin ~]# firewall-cmd --zone=public --permanent --add-port=80/tcp
|
||
success
|
||
[root@wxin ~]# firewall-cmd --reload
|
||
success
|
||
```
|
||
|
||
### 3. 设置配置方法
|
||
|
||
#### 1. 运行时配置
|
||
|
||
- 实时生效,并持续至Firewalld重新启动或重新加载配置
|
||
- 不中断现有连接
|
||
- 不能修改服务配置
|
||
|
||
```bash
|
||
[root@wxin ~]# firewall-cmd ...
|
||
|
||
将之前的运行时配置都转换成永久配置
|
||
[root@wxin ~]# firewall-cmd --runtime-to-permanent
|
||
```
|
||
|
||
#### 2. 永久配置
|
||
|
||
- 不立即生效,除非Firewalld重新启动或重新加载配置
|
||
- 中断现有连接
|
||
- 可以修改服务配置
|
||
|
||
```bash
|
||
[root@wxin ~]# firewall-cmd .... --permanent
|
||
[root@wxin ~]# firewall-cmd --reload 或 systemctl restart firewalld
|
||
```
|
||
|
||
### 4. 修改配置文件
|
||
|
||
```bash
|
||
以public zone为例,对应的配置文件是/etc/firewalld/zones/public.xml,像我们刚刚添加80端口后,体现在public.xml 中的内容为:
|
||
[root@wxin ~]# vim /etc/firewalld/zones/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="ssh"/>
|
||
<service name="dhcpv6-client"/>
|
||
<port protocol="tcp" port="80"/>
|
||
</zone>
|
||
|
||
注意在修改配置文件后 --reload 或重启 firewall 服务。
|
||
```
|
||
|
||
## 四:高级规则
|
||
|
||
### 1. Direct Rules 概述
|
||
|
||
通过`firewall-cmd`工具,可以使用`--direct`选项在运行时间里增加和删除链。如果不熟悉`iptables`,使用直接接口非常危险,因为可能无意间导致防火墙被入侵。直接端口模式适用于服务或者程序,以便于在运行时间内增加特定的防火墙规则。直接端口模式添加的规则优先应用。
|
||
|
||
```bash
|
||
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
|
||
```
|
||
|
||
查看防火墙上设置的规则
|
||
|
||
```bash
|
||
[root@wxin ~]# firewall-cmd --direct --get-all-rules
|
||
```
|
||
|
||
添加高级规则
|
||
|
||
```bash
|
||
[root@wxin ~]# 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 服务端口。
|
||
|
||
删除防火墙上的规则
|
||
|
||
```bash
|
||
[root@wxin ~]# 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
|
||
|
||
案例
|
||
|
||
```bash
|
||
# 将80端口的流量转发至8080
|
||
[root@wxin ~]# firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080
|
||
success
|
||
|
||
# 将80端口的流量转发至192.168.0.1
|
||
[root@wxin ~]# firewall-cmd --add-forward-port=port=80:proto=tcp:toaddr=192.168.0.1
|
||
success
|
||
|
||
# 将80端口的流量转发至192.168.0.1的8080端口
|
||
[root@wxin ~]# firewall-cmd --add-forward-port=port=80:proto=tcp:toaddr=192.168.0.1:toport=8080
|
||
success
|
||
```
|
||
|
||
开启`IP`伪装
|
||
|
||
```bash
|
||
查看:
|
||
[root@wxin ~]# firewall-cmd --query-masquerade
|
||
|
||
开启:
|
||
[root@wxin ~]# firewall-cmd --add-masquerade
|
||
success
|
||
|
||
关闭:
|
||
[root@wxin ~]# firewall-cmd --remove-masquerade
|
||
success
|
||
```
|
||
|
||
作用:
|
||
|
||
- 当我们想把某个端口隐藏起来的时候,就可以在防火墙上阻止那个端口访问,然后再开一个不规则的端口,之后配置防火墙的端口转发,将流量转发过去。
|
||
- 端口转发还可以做流量分发,一个防火墙拖着好多台运行着不同服务的机器,然后用防火墙将不同端口的流量转发至不同机器。 |