Akemi

Linux软件防火墙-arptables、ebtables、nftables介绍与使用

2026/02/11

arptables介绍

arptables是linux中用于管理arp包过滤规则的工具,也基于netfilter框架

arp协议本身非常简单,缺乏安全验证机制,因此容易遭受攻击

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# arptables应用场景
1.防御arp欺骗
攻击者伪造arp应答包,导致受害者的arp缓存被污染,将本应发送给合法主机的流量发送给了攻击者
arptables可以桂枝规则,只允许来自可信MAC地址的arp应答包

2.防御arp泛洪
攻击者发送大量伪造的arp请求包,耗尽交换机的mac地址表或资源
arptables可以限制单位时间内接收到的arp包的数量

3.网络访问控制
可以根据源目MAC来允许或拒绝主机之间的arp通信,从而实现一个基于mac地址的简单访问控制列表

4.arp包过滤和记录
可以LOG异常的arp请求或应答,用于网络监控和故障排查

#
filter表
INPUT、OUTPUT链,分别处理发往本机和发出的包

# arptables操作
(1)默认规则
arptables -P INPUT ACCEPT
arptables -P OUTPUT ACCEPT

(2)拒绝特定mac地址的arp回复
arptables -A INPUT --source-mac 00:11:22:33:44:55 -j DROP

(3)只允许特定mac的arp回复
arptables -A INPUT --source-mac 00:11:22:33:44:55 --src-ip 192.168.1.100 -j ACCEPT
arptables -A INPUT -j DROPs

# ip地址过滤
(1)绑定IP和ARP地址
arptables -A INPUT --src-ip 192.168.1.1 --source-mac ! 00:11:22:33:44:55 -j DROP

(2)限制特定IP段的arp请求
arptables -A INPUT --src-ip 192.168.1.0/24 -j ACCEPT
arptables -A INPUT --src-ip 192.168.2.0/24 -j DROP

(3)只允许eth0接口的arp请求
arptables -A INPUT -i eth0 -j ACCEPT
arptables -A INPUT -j DROP

(4)限制从wlan0发出的arp包
arptables -A OUTPUT -o wlan0 --dst-ip 192.168.1.1 -j ACCEPT
arptables -A OUTPUT -o wlan0 -j DROP

ebtables介绍

etbtables是一个用于在linux内核中设置和维护以太网帧规则的工具,工作在数据链路层,比较少用

主要用于控制网桥上的流量,比如brctl或ip link创建的网桥

所有经过桥接设备的流量,无论是转发还是发往本机,都会先经过ebtables,只有目标是本机上层协议栈的IP包才会继续传递给iptables

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# mac地址过滤
(1)阻止特定mac地址通信
ebtables -A INPUT -s 00:11:22:33:44:55 -j DROP
ebtables -A FORWARD -s 00:11:22:33:44:55 -j DROP

(2)只允许特定mac地址通信
ebtables -A INPUT -s 00:11:22:33:44:55 -j ACCEPT
ebtables -A INPUT -j DROP

# IP层过滤
(1)过滤IP流量
ebtables -A FORWARD -p IPv4 --ip-proto tcp --ip-dport 80 -j DROP
ebtables -A FORWARD -p IPv4 --ip-src 192.168.1.0/24 -j ACCEPT

(2)阻止特定arp请求
ebtables -A FORWARD -p arp --arp-ip-dst 192.168.1.1 -j DROP

新一代tables-nftables

nftables对iptables ip6tables arptables等的功能进行了融合

为了降低迁移成本,提供了iptables-nft等兼容工具,允许用户继续使用iptables语法,规则会自动转换为nftables格式

1
2
3
4
5
6
ls -l /sbin/iptables
lrwxrwxrwx. 1 root root 26 Mar 2 2024 /sbin/iptables -> /etc/alternatives/iptables
ls -l /etc/alternatives/iptables
lrwxrwxrwx. 1 root root 22 Mar 2 2024 /etc/alternatives/iptables -> /usr/sbin/iptables-nft

可以看到现在使用的iptables其实就是使用iptables-nft

nftables的组件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
表、链、规则、地址簇、优先级

1.表
和iptables不同,nftables没有默认的表,需要手动创建

2.链
3.规则

4.地址簇
nftables的每个表只有一个地址簇,并且只适用于该簇的数据包:
- ip
- ip6
- inet # 同时处理ipv4和ipv6
- arp
- bridge

5.优先级
nftbales的优先级priority的作用是定义链的执行顺序,数据包经过内核时,会按优先级从低到高触发不同的链
优先级一般范围-200~200
prerouting -100
input forward output 0
postrouting 100

nftables基本语法与命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
nft [选项] 命令 [对象] [规则]

# 列出所有当前规则
nft list ruleset
# 清空所有规则
nft flush rulest

# 创建表
nft add table <地址簇> <表名>
# 创建链
nft add chain <地址簇> <表名> <链名> { type <类型> hook {钩子} priority <优先级> ;policy <默认策略>;}
# 在链末尾添加规则
nft add chain <地址簇> <表名> <链名> <表达式> <动作>
# 在链开头添加规则
nft insert rule <地址簇> <表名> <链名> <表达式> <动作>
# 删除规则
nft delete rule <地址簇> <表名> <链名> <句柄>

# iptables与nftables对比添加规则
iptables -t nat -A PREROUTING -p tcp --dport 1000 -j DNAT --to-destination 1.1.1.1:1234

nft add tables ip nat
nft add chain ip nat prerouting '{type nat hook prerouting priority -100;policy accpet;}'
nft add rule ip nat prerouting tcp dport 1000 dnat to 1.1.1.1:1234

nftables配置规则示例-filter表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 创建表
nft add tables inet my_table
nft list ruleset

# 链的类型
nft没有内置链,分为两种,一种是基本链,附加到网络栈钩子的链,钩子点包括input output forward prerouting postrouting
另一种是常规链,是用户自定义的链,用以复用规则,将多个链中重复的规则集中管理,通过跳转机制被其他链调用

nft add chain inet my_table my_utility_chain
nft add chain inet my_table my_filter_chain {type fiilter hook input priority 0;}

# nftables配置实例,filter表
1.创建ipv4 filter表
nft add table ip filter
nft list tables

2.创建基本链
nft add chain ip filter input '{ type filter hook input priority 0;policy accept;}'
nft add chain ip filter forward '{ type filter hook forward priority 0; policy accept;}'
nft add chain ip filter output '{ type filter hook output priority 0;policy accept;}'
# 白名单
nft add chain ip filter input '{ type filter hook input priority 0;policy drop;}'
nft add chain ip filter forward '{ type filter hook forward priority 0; policy drop;}'
nft add chain ip filter output '{ type filter hook output priority 0;policy drop;}'

3.基础过滤规则
(1)环回接口
nft add rule ip filter input iif 'lo' accept
nft add rule ip filter output oif 'lo' accept

(2)允许已建立的连接和相关流量
nft add rule ip filter input ct state established,related accept

(3)允许ssh
nft add rule ip filter input tcp dport 22 accept

(4)允许http https
nft add rule ip filter input tcp dport {80,443} accept

(5)允许ICMP
nft add rule ip filter input ip protocol icmp type echo-request accept

nftables配置规则示例-nat表

1
2
3
4
5
6
7
8
9
10
1.配置nat表和链(
nft add table ip nat
nft add chain ip nat prerouting '{ type nat hook prerouting priority -100;policy accept;}'
nft add chain ip nat postrouting '{ type nat hook postrouting priority -100;policy accept;}'

2.端口转发
nft add rule ip nat prerouting tcp dport 8080 dnat to 192.168.1.10:80

3.源地址转换
nft add rule ip nat postrouting ip saddr 192.168.1.0/24 snat to 203.0.113.1

nftable配置文件导出导入

1
2
3
4
5
6
7
8
9
10
11
12
# 导出
nft list ruleset > nftrule

# 清空规则
nft flush ruleset

# 导入规则
nft -f nftrule

# 导入特定表的规则
nft list table ip filter > nftrule_filter
nft -f nftrule_filter
CATALOG
  1. 1. arptables介绍
  2. 2. ebtables介绍
  3. 3. 新一代tables-nftables
    1. 3.1. nftables的组件
    2. 3.2. nftables基本语法与命令
    3. 3.3. nftables配置规则示例-filter表
    4. 3.4. nftables配置规则示例-nat表
    5. 3.5. nftable配置文件导出导入