防火墙超详配置

Centos7中使用firewalld来作为防火墙,其底层调用的命令仍然是iptables等命令,但是在配置上发生了较大的变化。
Centos7中有两个位置存放了firewall的配置文件,一个是/etc/firewalld,一个是/usr/lib/firewalld,前者是用户配置目录,后者是系统配置目录。/usr/lib/firewalld目录中存放的是firewalld提供的一些默认和备份的配置文件,一般不随意改变,/etc/firewalld目录下才是用户配置的真正生效的配置文件,只有在/etc/firewalld目录不存在或该目录下不存在配置文件的情况下/usr/lib/firewalld目录下的配置文件才会生效。

一. zone的概念
zone定义了防火墙对某个连接、网口(interface)或源地址的信任等级,我们可以把他理解为防火墙对不同的连接(connection)、网口(interface)或源地址(source address)划分到不同的zone当中,而不同的zone定义了不同的规则,因此防火墙可以针对不同的连接、网口(interface)或源地址做出不同的行为。例如,我们将10.12.18.201这个地址划分到zone1中,将10.12.18.202这个地址划分到zone2中,然后zone1中定义的规则为:允许访问3306端口,其余的端口都拒绝访问;zone2中定义的规则为:拒绝访问3306端口,其余的端口都允许访问。那么10.12.18.201就仅能访问本机的3306端口,10.12.18.202就仅不能访问本机的3306端口。每个zone的防火墙规则是通过/etc/firewalld/zones目录下的xml配置文件来配置的。
zone和connection、interface、source address之间是一对多的关系,即一个connection、interface或source address仅能划分到一个zone中,而一个zone中可以包含多个connection、interface或source address。

1.1 预定义的zone
Centos7中firewalld为用户预定义了9个zone,分别为drop,block,public,external,dmz,work,home,internal,trusted。这9个zone的配置文件在/usr/lib/firewalld/zones目录下,通过查看他们的配置文件可以得知这9个zone的规则是怎么样的。
drop:任何传入本机的网络数据包都会被丢弃,并且不会回复,只允许本机对外访问其他服务器。
block:任何传入本机的网络连接请求都会被拒绝,并且会回复一条拒绝访问的消息。
public:用于本机处于公共网络环境的场景下,仅接受特定的连接请求,如仅接受某些特定的IP的连接请求,或仅对外部开放特定的某些端口。Centos 7 默认的public.xml文件中仅开放用于ssh连接请求的22端口和dhcpv6-client服务的546端口。
external:与public类似,Centos 7 默认仅开放用于ssh连接请求的22端口。
dmz:与external一样,Centos 7 默认也是仅开放用于ssh连接请求的22端口。
work:用于工作网络环境场景下,信任大部分的其他的计算机不会对本机进行攻击。Centos 7 默认开放用于ssh连接请求的22端口,dhcpv6-client服务的546端口,以及IPP协议的631端口。
home:用于家庭网络环境,信任网络上的其他计算机不会攻击本机。Centos 7默认开放用于ssh连接请求的22端口,dhcpv6-client服务的546端口,IPP协议的631端口,samba服务的137、138端口,mDNS服务的5353端口。
internal:与home一样,Centos 7默认开放用于ssh连接请求的22端口,dhcpv6-client服务的546端口,IPP协议的631端口,samba服务的137、138端口,mDNS服务的5353端口。
trusted:所有对本机的网络连接请求都会被接受。

1.2 将interface和source划分到某个zone
将某个source划分到某个zone的命令如下:

firewall-cmd [--permanent] [--zone=zone] --add-source=source

例如:

firewall-cmd --permanent --zone=trusted --add-source=192.168.5.112

这条命令会将192.168.5.112这个网口划分到trusted这个zone,–permanent参数会将该配置写入trusted这个zone的配置文件trusted.xml中,使其永久生效,如果不加–permanent,则只会临时生效,重启防火墙或者调用firewall-cmd --reload重新加载配置文件会使得该项配置失效。

将某个网口(interface)划分到某个zone的命令如下:

 firewall-cmd [--permanent] [--zone=zone] --add-interface=interface

例如:

 firewall-cmd --permanent --zone=block --add-interface=ens33

这条命令会将ens33这个网口划分到block这个zone,这样其他机器访问本机的ens33网口时就会默认走这个zone。
如果某个连接请求没有划分到任何一个zone,那么就会走默认的zone,Centos7 默认情况下,默认zone为public。可以通过以下命令查看或者修改默认zone。

获取默认的zone

firewall-cmd --get-default-zone

修改默认的zone:

firewall-cmd --permanent --set-default-zone=[zone]

其他的一些相关的命令:

列出该zone下绑定了哪些interface:
firewall-cmd [--zone=zone] --list-interfaces
 
将该interface绑定到另一个zone上:
firewall-cmd [--permanent] [--zone=zone] --change-interface=interface
 
如果该interface之前绑定到了某个zone,则取消绑定,这样就会走默认zone
firewall-cmd [--permanent] --remove-interface=interface
 
与source相关的:
firewall-cmd [--permanent] --remove-source=source
firewall-cmd [--permanent] [--zone=zone] --list-sources
firewall-cmd [--permanent] [--zone=zone] --add-source=source

1.3 zone配置文件

一个zone的xml配置文件的示例如下:

<?xml version="1.0" encoding="utf-8"?>
<zone target="DEFAULT">
  <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"/>
  <port protocol="tcp" port="8080"/>
  <source address="192.168.5.112">
  <source address="10.12.18.0/24"/>
  <rule family="ipv4">
    <source address="10.12.18.0/24"/>
    <port protocol="tcp" port="7180-7190"/>
    <accept/>
  </rule>
</zone>

每个标签的含义:
zone:给一个zone指定target,

target=“ACCEPT|%%REJECT%%|DROP”

可用于接受(ACCEPT)、拒绝(%%REJECT%%)或丢弃(DROP)与任何规则(端口、服务等)都不匹配的每个数据包,即指定该zone的默认的对连接(connection)、网口(interface)或源地址(source address)的行为,如果不指定target,则默认为default,default的行为与REJECT类似。
short:给该zone指定一个名字。
description:对该zone的描述
service:该zone开启的服务(service),service下一小节会讲
port:该zone打开的端口,protocol属性可以指定协议,通常为tcp或udp,port属性指定端口号
source:绑定到该zone的source,其效果等同于 firewall-cmd [–permanent] [–zone=zone] --add-source=source 指令
rule:为该zone添加的富语言规则(rich language rule)。rich language rule的写法可以参照官网

点击这里!!!

上述示例的富语言规则的含义为对10.12.18.X网段的source开放7180至7190所有端口的tcp连接请求。

二. Service的概念
service是预定义的一系列的本机的协议、端口、目标ip等,service可以让我们更加方便的让防火墙限制外部对本机的某些端口的访问。service的配置文件在/etc/firewalld/services或/usr/lib/firewalld/services文件夹下,每个service的配置都是一个xml文件。

2.1 service配置文件
系统在/usr/lib/firewalld/services文件夹下为我们预定义了一些列的service配置文件,我们也可以在/etc/firewalld/services定义自己的service。例如,ftp.xml的内容如下:

<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>FTP</short>
  <description>FTP is a protocol used for remote file transfer. If you plan to make your FTP server publicly available, enable this option. You need the vsftpd package installed for this option to be useful.</description>
  <port protocol="tcp" port="21"/>
  <module name="nf_conntrack_ftp"/>
</service>

 这样,在某个zone的配置文件中可以引用这个service,例如,在public.xml中引入ftp service:

<?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"/>
  <service name="ftp"/>
</zone>

等价于:

<?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"/>
  <port protocol="tcp" port="21"/>
</zone>

2.2 service相关的指令

打印所有预定义的service(/usr/lib/firewalld/services或/etc/firewalld/services下每个xml配置文件就是一个预定义的service):

firewall-cmd [--permanent] --get-services

列出此zone开启的服务列表:

firewall-cmd [--permanent] [--zone=zone] [--permanent] [--policy=policy] --list-services

将一个service添加到一个zone,timeout可以为这个zone设置这个service的生效时间,过了这个生效时间,此service将从该zone中被移除。–timeout参数和–permanent参数是不兼容的

firewall-cmd [--permanent] [--zone=zone] [--permanent] [--policy=policy] --add-service=service [--timeout=timeval]

 三. ipset的概念
ipset,顾名思义,就是可用于将多个IP或MAC地址分组在一起。通过使用ipset,可以将不同的ip地址进行分组,简化ip地址的管理和zone的配置。ipset的配置文件在/etc/firewalld/ipsets目录下,该目录下一个xml配置文件对应一个ipset。
例如,将以下ip地址组合为一个ipset,配置文件命名为ipset1.xml:

<?xml version="1.0" encoding="utf-8"?>
<ipset type="hash:net">
  <entry>10.12.18.201</entry>
  <entry>192.168.5.201</entry>
</ipset>

行啦,这就是我找到的一些基础的防火墙的知识,更深刻一点的,我一不太会······哈哈哈,好了,快收藏吧

猜你喜欢

转载自blog.csdn.net/m0_64122244/article/details/128439849