带你了解iptables防火墙

Linux包过滤防火墙

netfilter
1、位于Linux内核中的包过滤功能体系
2、称为Linux防火墙的“内核态”
iptables
1、位于/sbin/iptables,用来管理防火墙规则的工具
2、称为Linux防火墙的“用户态”

包过滤的工作层次
1、主要是网络层,针对IP数据包
2、体现在对包内的IP地址、端口等信息的处理上

iptables的表、链结构

规则表
1、表的作用:容纳各种规则链
2、表的划分依据:防火墙规则的作用相似

默认包括4个规则表
**raw表:**确定是否对该数据包进行状态跟踪
**mangle表:**为数据包设置标记
**nat表:**修改数据包中的源、目标IP地址或端口
**filter表:**确定是否放行该数据包(过滤)

规则链
1、规则的作用:对数据包进行过滤或处理
2、链的作用:容纳各种防火墙规则
3、链的分类依据:处理数据包的不同时机

默认包括5种规则链
INPUT:处理入站数据包
OUTPUT:处理出站数据包
FORWARD:处理转发数据包
POSTROUTING链:在进行路由选择后处理数据包
PREROUTING链:在进行路由选择前处理数据包

数据包过滤的匹配流程

规则表之间的顺序
raw→mangle→nat→filter

规则链之间的顺序
入站:PREROUTING→INPUT
出站:OUTPUT→POSTROUTING
转发:PREOUTING→FORWARD→POSTROUTING

规则链内的匹配顺序
1、按顺序依次检查,匹配即停止(LOG策略例外)
2、若找不到相匹配的规则,则按该链的默认策略处理

编写防火墙规则

iptables安装

关闭firewalld防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service

安装iptables防火墙
yum -y install iptables iptables-services

设置iptables开机启动
systemctl start iptables.service
systemctl enable iptables.service

iptables的基本语法

语法构成
iptables [-t 表名] 选项 [链名] [条件] [-j 控制类型]

注意事项
1、不指定表名时,默认指filter表
2、不指定链名时,默认指表内的所有链
3、除非设置链的默认策略,否则必须指定匹配条件
4、选项、链名、控制类型使用大写字母,其余均为小写字母

数据包的常见控制类型

ACCEPT:允许通过
DROP:直接丢弃,不给出任何回应
REJECT:拒绝通过,必要时会给出提示
LOG:记录日志信息,然后传给下一条规则继续匹配

添加新规则:


-A:末尾追加规则
例:在filter表INPUT链的末尾添加一条防火墙(“-p 协议名” 作为匹配条件)
[root@client1 ~]# iptables -t filter -A INPUT -p tcp -j ACCEPT

-I:指定序号追加新规则,若无指定,默认为第一条
例:给filter表追加第一条规则(省略了“-t filter”,默认使用filter表)
                     追加第二条规则
[root@client1 ~]# iptables -I INPUT -p udp -j ACCEPT
[root@client1 ~]# iptables -I INPUT 2 -p udp -j ACCEPT

查看规则列表:

-L结合--line-numbers:指定链内的各条规则,并显示序列号
[root@client1 ~]# iptables -L INPUT --line-numbers 
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     udp  --  anywhere             anywhere            
2    ACCEPT     udp  --  anywhere             anywhere            
3    ACCEPT     tcp  --  anywhere             anywhere
-n结合-L:以数字显示显示链内的各条规则
[root@client1 ~]# iptables -nL INPUT --line-numbers 
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           
2    ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           
3    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0

删除、清空规则:

-D:删除一条规则
[root@client1 ~]# iptables -D INPUT 3
[root@client1 ~]# iptables -nL INPUT --line-numbers 
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           
2    ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0 
-F:清空规则(不指定表,默认是filter表)
[root@client1 ~]# iptables -F INPUT(清空input链)
[root@client1 ~]# iptables -nL INPUT --line-numbers 
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
[root@client1 ~]# iptables -F (清空filter表)
[root@client1 ~]# iptables -t nat -F(清空nat表)

设置默认策略

-P:没有其他匹配数据包的规则,执行默认策略(ACCEPTDROP)。
使用“-F”清空链的时候,默认策略不受影响。
例:将filter表里的FORWARD链的默认策略设为丢弃;
      将filter表里的OUTPUT链的默认策略设为允许;
[root@client1 ~]# iptables -t filter -P FORWARD DROP
[root@client1 ~]# iptables -P OUTPUT ACCEPT

规则匹配条件

通用匹配

1、可直接使用,不依赖于其他条件或扩展
2、包括网络协议、IP地址、网络接口等条件
3、常见的通用匹配条件:

**协议匹配:-p 协议名**
例:丢弃通过icmp协议访问防火墙本机的数据包
[root@client1 ~]# iptables -I INPUT -p icmp -j DROP 
允许转发经过防火墙的除icmp协议之外的数据包
[root@client1 ~]# iptables -A FORWARD ! -p icmp -j ACCEPT #“!”表示取反 

**地址匹配:-s 源地址、-d 目的地址**
拒绝转发源地址为192.168.1.11的数据:
[root@client1 ~]# iptables -A FORWARD -s 192.168.1.11 -j REJECT 
允许转发源地址位于192.168.7.0/24网段的数据:
[root@client1 ~]# iptables -A FORWARD -s 192.168.7.0/24 -j ACCEPT 
封锁来自10.20.30/24网段的数据
[root@client1 ~]# iptables -I INPUT -s 10.20.30.0/24 -j DROP

**接口匹配:-i入站网卡、-o 出战网卡**
丢弃从外网接口(eth1)访问防火墙且源地址为私有地址的数据包
[root@client1 ~]# iptables -A INPUT -i eth1 -s 192.168.0.0/16 -j DROP
[root@client1 ~]# iptables -A INPUT -i eth1 -s 10.0.0.0/8 -j DROP
[root@client1 ~]# iptables -A INPUT -i eth1 -s 172.16.0.0/12 -j DROP

隐含匹配

1、要求以特定的协议匹配作为前提
2、包括端口、TCP标记、ICMP类型等条件
3、常用的隐含匹配条件
端口匹配:–sport源端口、–dport目的端口
允许网段为192.168.4.0//24转发DNS查询数据包
[root@client1 ~]# iptables -A FORWARD -s 192.168.4.0/24 -p udp --dport 53 -j ACCEPT
[root@client1 ~]# iptables -A FORWARD -d 192.168.4.0/24 -p udp --sport 53 -j ACCEPT
构建vsftpd服务器时,开放20、21端口
[root@client1 ~]# iptables -A INPUT -p tcp --dport 20:21 -j ACCEPT
ICMP类型匹配:–icmp-type ICMP类型
“Echo-Request”(代码为8)——ICMP协议的请求
“Echo-Reply”(代码为0)——ICMP协议的回显
“Destinnation-Unreachable”(代码为3)——ICMP协议的目标不可达
例:禁止从其他主机ping本机,允许本机ping其他主机:
[root@client1 ~]# iptables -A INPUT -p icmp --icmp-type 8 -j DROP
[root@client1 ~]# iptables -A INPUT -p icmp --icmp-type 0 -j ACCEPT
[root@client1 ~]# iptables -A INPUT -p icmp --icmp-type 3 -j ACCEPT
[root@client1 ~]# iptables -A INPUT -p icmp -j DROP

显式匹配

1、要求以“-m 扩展模块”的形式明确指出类型
2、包括多端口、MAC地址、IP范围、数据包状态等条件
3、常用的显式匹配条件
多端口匹配
-m multiport --sports 源端口列表
-m multiport -dports 目的端口列表

例:允许本机开放2580110143端口,以便提供电子邮件服务:
 [root@client1 ~]# iptables -A INPUT -p tcp -m multiport --dport 25,80,110,143 -j ACCEPT 

IP范围匹配:-m iprange --src-range IP范围

例:禁止转发源IP地址位于192.168.4.21192.168.4.28之间的TCP数据包
[root@client1 ~]# iptables -A FORWARD -p tcp -m iprange --src-range 192.168.4.21-192.168.4.28 -j ACCEPT 

MAC地址匹配: -m mac --mac-source MAC地址

例:根据MAC地址封锁主机,禁止其访问本机的任何应用
[root@client1 ~]# iptables -A INPUT -m mac --mac-source 00:0c:c0:51:3f:56 -j DROP

状态匹配: -m state --state 连接状态
连接状态:NEW(与任何连接无关的)、ESTABLISHED(已建立连接的或响应请求)、RELATED(与已有连接有相关性的,如FTP数据连接)

1:禁止准发与正常TCP连接无关的非--syn请求数据包
[root@client1 ~]# iptables -A FORWARD -m state --state NEW -p tcp ! --syn -j DROP2:只开放本机的web服务(80端口),但对发给本机的TCP应答数据予以放行,其他入站数据均丢弃:
[root@client1 ~]# iptables -I INPUT -p tcp -m multiport --dport 80 -j ACCEPT 
[root@client1 ~]# iptables -I INPUT -p tcp -m state --state ESTABLISHED -j ACCEPT 
[root@client1 ~]# iptables -P INPUT DROP
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值