防护墙介绍
众所周知,相较于企业内网,外部的公网环境更加恶劣,罪恶丛生。在公网与企业内网之间充当保护屏障的防火墙。虽然有软件或硬件之分,但主要功能都是依据策略对穿越防火墙自身的流量进行过滤。就像家里安装的防盗门一样,目的是保护亲人和财产安全。防火墙策略可以基于流量的源目地址、端口号、协议、应用等信息来定制,然后防火墙使用预先定制的策略规则监控出入的流量,若流量与某一条策略规则相匹配,则执行相应的处理,反之则丢弃。这样一来,就能够保证仅有合法的流量在企业内网和外部公网之间流动了。

从RHEL 7系统开始,firewalld防火墙正式取代了iptables防火墙。对于接触Linux系统比较早或学习过RHEL 5/6系统的读者来说,当他们发现曾经掌握的知识在RHEL 7/8中不再适用,需要全新学习firewalld时,难免会有抵触心理。其实,iptables与firewalld都不是真正的防火墙,它们都只是用来定义防火墙策略的防火墙管理工具而已;或者说,它们只是一种服务。iptables服务会把配置好的防火墙策略交由内核层面的netfilter网络过滤器来处理,而firewalld服务则是把配置好的防火墙策略交由内核层面的nftables包过滤框架来处理。换句话说,当前在Linux系统中其实存在多个防火墙管理工具,旨在方便运维人员管理Linux系统中的防火墙策略,我们只需要配置妥当其中的一个就足够了。
Iptables简介
在早期的Linux系统中,默认使用的是iptables防火墙管理服务来配置防火墙。尽管新型的firewalld防火墙管理服务已经被投入使用多年,但是大量的企业在生产环境中依然出于各种原因而继续使用iptables。考虑到iptables在当前生产环境中还具有顽强的生命力,以及为了使大家在求职面试过程中被问到iptables的相关知识时能胸有成竹,刘遄老师觉得还是有必要在本书中好好地讲解一下这项技术。更何况前文也提到,各个防火墙管理工具的配置思路是一致的,在掌握了iptables后再学习其他防火墙管理工具时,也有借鉴意义。
策略与规则
防火墙会按照从上到下的顺序来读取配置的策略规则,在找到匹配项后就立即结束匹配工作并去执行匹配项中定义的行为(即放行或阻止)。如果在读取完所有的策略规则之后没有匹配项,就去执行默认的策略。一般而言,防火墙策略规则的设置有两种:“通”(即放行)和“堵”(即阻止)。当防火墙的默认策略为拒绝时(堵),就要设置允许规则(通),否则谁都进不来;如果防火墙的默认策略为允许,就要设置拒绝规则,否则谁都能进来,防火墙也就失去了防范的作用。
iptables服务把用于处理或过滤流量的策略条目称之为规则,多条规则可以组成一个规则链,而规则链则依据数据包处理位置的不同进行分类,具体如下:
- 在进行路由选择前处理数据包(PREROUTING);
- 处理流入的数据包(INPUT);
- 处理流出的数据包(OUTPUT);
- 处理转发的数据包(FORWARD);
- 在进行路由选择后处理数据包(POSTROUTING);
基本命令参数
iptables是一款基于命令行的防火墙策略管理工具,具有大量的参数。
根据OSI七层模型的定义,iptables属于数据链路层的服务,所以可以根据流量的源地址、目的地址、传输协议、服务类型等信息进行匹配;一旦匹配成功,iptables就会根据策略规则所预设的动作来处理这些流量。另外,再次提醒一下,防火墙策略规则的匹配顺序是从上到下的,因此要把较为严格、优先级较高的策略规则放到前面,以免发生错误。下表总结归纳了常用的iptables命令参数。再次强调,无须死记硬背这些参数,只需借助下面的实验来理解掌握即可。
iptables中常用的参数以及作用
参数 | 作用 |
---|---|
-P | 设置默认策略 |
-F | 清空规则链 |
-L | 查看规则链 |
-A | 在规则链的末尾加入新规则 |
-I num | 在规则链的头部加入新规则 |
-D num | 删除某一条规则 |
-s | 匹配来源地址IP/MASK,加叹号“!”表示除这个IP外 |
-d | 匹配目标地址 |
-i 网卡名称 | 匹配从这块网卡流入的数据 |
-o 网卡名称 | 匹配从这块网卡流出的数据 |
-p | 匹配协议,如TCP、UDP、ICMP |
--dport num | 匹配目标端口号 |
--sport num | 匹配来源端口号 |
示例
1.在iptables命令后添加-L参数查看已有的防火墙规则链。
[root@53 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- anywhere anywhere udp dpt:domain
ACCEPT tcp -- anywhere anywhere tcp dpt:domain
ACCEPT udp -- anywhere anywhere udp dpt:bootps
ACCEPT tcp -- anywhere anywhere tcp dpt:bootps
Chain FORWARD (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere 192.168.122.0/24 ctstate RELATED,ESTABLISHED
ACCEPT all -- 192.168.122.0/24 anywhere
ACCEPT all -- anywhere anywhere
REJECT all -- anywhere anywhere reject-with icmp-port-unreachable
REJECT all -- anywhere anywhere reject-with icmp-port-unreachable
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- anywhere anywhere udp dpt:bootpc
2.在iptables命令后添加-F参数清空已有的防火墙规则链。
[root@53 ~]# iptables -F
[root@53 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD_IN_ZONES (0 references)
target prot opt source destination
Chain FORWARD_IN_ZONES_SOURCE (0 references)
target prot opt source destination
Chain FORWARD_OUT_ZONES (0 references)
...
3.把INPUT规则链的默认策略设置为拒绝
[root@53 ~]# iptables -P INPUT DROP
[root@53 ~]# iptables -L
Chain INPUT (policy DROP)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
...
防火墙策略规则的设置无非有两种方式:“通”和“堵”。当把INPUT链设置为默认拒绝后,就要往里面写入允许策略了,否则所有流入的数据包都会被默认拒绝掉。
4.向INPUT链中添加允许ICMP流量进入的策略规则。
在日常运维工作中,经常会使用ping命令来检查对方主机是否在线,而向防火墙的INPUT规则链中添加一条允许ICMP流量进入的策略规则就默认允许了这种ping命令检测行为。
[root@53 ~]# iptables -I INPUT -p icmp -j ACCEPT
[root@53 ~]# ping -c 192.168.1.102
[root@53 ~]# ping -c 4 192.168.1.102
PING 192.168.1.102 (192.168.1.102) 56(84) bytes of data.
64 bytes from 192.168.1.102: icmp_seq=1 ttl=64 time=0.151 ms
64 bytes from 192.168.1.102: icmp_seq=2 ttl=64 time=0.375 ms
...
5.删除INPUT规则链中刚刚加入的那条策略(允许ICMP流量),并把默认策略设置为允许。
使用-F参数会清空已有的所有防火墙策略;使用-D参数可以删除某一条指定的策略,因此更加安全和准确。
[root@53 ~]# iptables -D INPUT 1
[root@53 ~]# iptables -P INPUT ACCEPT
[root@53 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD_IN_ZONES (0 references)
target prot opt source destination
6.将INPUT规则链设置为只允许指定网段的主机访问本机的22端口,拒绝来自其他所有主机的流量。
要对某台主机进行匹配,可直接写出它的IP地址;如需对网段进行匹配,则需要写为子网掩码的形式(比如192.168.1.0/24)。
[root@53 ~]# iptables -I INPUT -s 192.168.1.0/24 -p tcp --dport 22 -j ACCEPT
[root@53 ~]# iptables -A INPUT -p tcp --dport 22 -j REJECT
[root@53 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- 192.168.1.0/24 anywhere tcp dpt:ssh
REJECT tcp -- anywhere anywhere tcp dpt:ssh reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT)
target prot opt source destination
...
防火墙策略规则是按照从上到下的顺序匹配的,因此一定要把允许动作放到拒绝动作前面,否则所有的流量就将被拒绝掉,从而导致任何主机都无法访问我们的服务。
7.向INPUT规则链中添加拒绝所有人访问本机12345端口的策略规则。
[root@53 ~]# iptables -I INPUT -p tcp --dport 12345 -j REJECT
[root@53 ~]# iptables -I INPUT -p udp --dport 12345 -j REJECT
[root@53 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
REJECT udp -- anywhere anywhere udp dpt:italk reject-with icmp-port-unreachable
REJECT tcp -- anywhere anywhere tcp dpt:italk reject-with icmp-port-unreachable
ACCEPT tcp -- 192.168.1.0/24 anywhere tcp dpt:ssh
REJECT tcp -- anywhere anywhere tcp dpt:ssh reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
...
8.向INPUT规则链中添加拒绝192.168.10.5主机访问本机80端口(Web服务)的策略规则。
[root@53 ~]# iptables -I INPUT -p tcp -s 192.168.1.111 --dport 80 -j REJECT
[root@53 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
REJECT tcp -- 192.168.1.111 anywhere tcp dpt:http reject-with icmp-port-unreachable
REJECT udp -- anywhere anywhere udp dpt:italk reject-with icmp-port-unreachable
REJECT tcp -- anywhere anywhere tcp dpt:italk reject-with icmp-port-unreachable
ACCEPT tcp -- 192.168.1.0/24 anywhere tcp dpt:ssh
REJECT tcp -- anywhere anywhere tcp dpt:ssh reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT)
target prot opt source destination
...
9.向INPUT规则链中添加拒绝所有主机访问本机1000~1024端口的策略规则。
前面在添加防火墙策略时,使用的是-I参数,它默认会把规则添加到最上面的位置,因此优先级是最高的。如果工作中需要添加一条最后“兜底”的规则,那就用-A参数吧。这两个参数的效果差别还是很大的:
[root@53 ~]# iptables -A INPUT -p tcp --dport 1000:1024 -j REJECT
[root@53 ~]# iptables -A INPUT -p udp --dport 1000:1024 -j REJECT
[root@53 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
REJECT tcp -- 192.168.1.111 anywhere tcp dpt:http reject-with icmp-port-unreachable
REJECT udp -- anywhere anywhere udp dpt:italk reject-with icmp-port-unreachable
REJECT tcp -- anywhere anywhere tcp dpt:italk reject-with icmp-port-unreachable
ACCEPT tcp -- 192.168.1.0/24 anywhere tcp dpt:ssh
REJECT tcp -- anywhere anywhere tcp dpt:ssh reject-with icmp-port-unreachable
REJECT tcp -- anywhere anywhere tcp dpts:cadlock2:1024 reject-with icmp-port-unreachable
REJECT udp -- anywhere anywhere udp dpts:cadlock2:1024 reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
...
特别注意,使用iptables命令配置的防火墙规则默认会在系统下一次重启时失效,如果想让配置的防火墙策略永久生效,还要执行保存命令
[root@53 ~]# iptables-save
# Generated by iptables-save v1.4.21 on Thu Jul 28 04:53:36 2022
*mangle
:PREROUTING ACCEPT [786186:45785318]
:INPUT ACCEPT [786186:45785318]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [1091585:180099335]
:POSTROUTING ACCEPT [1091585:180099335]
:FORWARD_direct - [0:0]
:INPUT_direct - [0:0]
:OUTPUT_direct - [0:0]
...
10.删除指定规则
[root@53 ~]# iptables -L -n --line-number #查看每个规则chain的序列号。
[root@53 ~]# iptables -D INPUT 4 #删除序列号为4的规则
11.iptables 开启关闭状态
[root@53 ~]# service iptables status #查看状态
Redirecting to /bin/systemctl status iptables.service
Unit iptables.service could not be found. #报错的话 需要安装下iptables-services
[root@53 ~]# yum install iptables-services -y
[root@53 ~]# service iptables stop #关闭iptables服务
Redirecting to /bin/systemctl stop iptables.service
[root@53 ~]# service iptables start # 开启iptables服务
Redirecting to /bin/systemctl start iptables.service
[root@53 ~]# service iptables status #查看iptables服务状态
Redirecting to /bin/systemctl status iptables.service
如果公司服务器是5/6/7版本的话,对应的保存命令应该是:
[root@53 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables: [ OK ]