Skip to content

1.文档

https://www.netfilter.org/

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/security_guide/index

2.iptables简介

  • 来自netfilter

iptables is the userspace command line program used to configure the Linux 2.4.x and later packet filtering ruleset. It is targeted towards system administrators.

Since Network Address Translation is also configured from the packet filter ruleset, iptables is used for this, too.

The iptables package also includes ip6tables. ip6tables is used for configuring the IPv6 packet filter.

shell
[root@kube-master yaml]# modinfo ip_tables
filename:       /lib/modules/4.19.12-1.el7.elrepo.x86_64/kernel/net/ipv4/netfilter/ip_tables.ko
alias:          ipt_icmp
description:    IPv4 packet filter
author:         Netfilter Core Team <coreteam@netfilter.org>
license:        GPL
srcversion:     3967C875058C2EE2475C9C2
depends:
intree:         Y
name:           ip_tables
vermagic:       4.19.12-1.el7.elrepo.x86_64 SMP mod_unload modversions
[root@kube-master yaml]# modinfo ip_tables
filename:       /lib/modules/4.19.12-1.el7.elrepo.x86_64/kernel/net/ipv4/netfilter/ip_tables.ko
alias:          ipt_icmp
description:    IPv4 packet filter
author:         Netfilter Core Team <coreteam@netfilter.org>
license:        GPL
srcversion:     3967C875058C2EE2475C9C2
depends:
intree:         Y
name:           ip_tables
vermagic:       4.19.12-1.el7.elrepo.x86_64 SMP mod_unload modversions
shell
[root@kube-master yaml]# lsmod |grep ip_tables
ip_tables              24576  4 iptable_filter,iptable_raw,iptable_nat,iptable_mangle
[root@kube-master yaml]# lsmod |grep ip_tables
ip_tables              24576  4 iptable_filter,iptable_raw,iptable_nat,iptable_mangle

3.iptables的四表五链

表的处理优先级:raw>mangle>nat>filter

3.1 四表

iptables的四个表iptable_filteriptable_mangleiptable_natiptable_raw,默认表是filter(没有指定表的时候就是filter表)

filter 表

用来对数据包进行过滤,具体的规则要求决定如何处理一个数据包。

对应的内核模块为:iptable_filter,其表内包括三个链:inputforwardoutput;

nat 表

nat 全称:network address translation 网络地址转换,主要用来修改数据包的 IP 地址、端口号信息。

对应的内核模块为:iptable_nat,其表内包括三个链:preroutingpostroutingoutput;

mangle 表

主要用来修改数据包的服务类型,生存周期,为数据包设置标记,实现流量整形、策略路由等。

对应的内核模块为:iptable_mangle,其表内包括五个链:preroutingpostroutinginputoutputforward;

raw 表

主要用来决定是否对数据包进行状态跟踪

对应的内核模块为:iptable_raw,其表内包括两个链:outputprerouting;

❌ 注意

raw表只使用在PREROUTING链和OUTPUT链上,因为优先级最高,从而可以对收到的数据包在系统进行ip_conntrack(连接跟踪)前进行处理。一但用户使用了raw表,在某个链上,raw表处理完后,将跳过NAT表和ip_conntrack处理,即不再做地址转换和数据包的链接跟踪处理了。RAW表可以应用在那些不需要做nat的情况下,以提高性能

3.2 五链

iptables的五个链PREROUTING,INPUTFORWARDOUTPUTPOSTROUTING

input 链

当收到访问防火墙本机地址的数据包时,将应用此链中的规则

output 链

当防火墙本机向外发送数据包时,将应用此链中的规则;

forward 链

当收到需要通过防火中转发给其他地址的数据包时,将应用此链中的规则,注意如果需要实现forward转发需要开启Linux内核中的ip_forward功能

prerouting 链

在对数据包做路由选择之前,将应用此链中的规则

postrouting 链

在对数据包做路由选择之后,将应用此链中的规则

4.iptables语法

4.1基本语法

iptables [-t 表名] 管理选项 [链名] [匹配条件] [-j 控制类型]
iptables [-t 表名] 管理选项 [链名] [匹配条件] [-j 控制类型]
  • 表名链名:指定iptables命令所操作的,未指定表名时将默认使用filter表;
  • 管理选项:表示iptables规则的操作方式,比如:插入增加删除查看等;
  • 匹配条件:指定要处理的数据包的特征,不符合指定条件的数据包不处理;
  • 控制类型:指数据包的处理方式,比如:允许accept拒绝reject丢弃drop日志LOG等;

4.2常用参数

iptables 命令的常用管理选项
-A:在指定链的末尾添加一条新的规则
-D:删除指定链中的某一条规则,可删除指定序号或具体内容
-I:在指定链中插入一条新规则,未指定序号时默认作为第一条规则
-R:修改、替换指定链中的某一条规则,可指定规则序号或具体内容
-L:列出指定链中所有的规则,未指定链名,则列出表中的所有链
-F:清空指定链中所有的规则,未指定链名,则清空表中的所有链
-P:设置指定链的默认策略
-n:使用数字形式显示输出结果
-v:查看规则列表时显示详细的信息
-h:查看命令帮助信息
--line-numbers:查看规则列表时,同时显示规则在链中的顺序号
iptables 命令的常用管理选项
-A:在指定链的末尾添加一条新的规则
-D:删除指定链中的某一条规则,可删除指定序号或具体内容
-I:在指定链中插入一条新规则,未指定序号时默认作为第一条规则
-R:修改、替换指定链中的某一条规则,可指定规则序号或具体内容
-L:列出指定链中所有的规则,未指定链名,则列出表中的所有链
-F:清空指定链中所有的规则,未指定链名,则清空表中的所有链
-P:设置指定链的默认策略
-n:使用数字形式显示输出结果
-v:查看规则列表时显示详细的信息
-h:查看命令帮助信息
--line-numbers:查看规则列表时,同时显示规则在链中的顺序号

参考

https://tuxknight-notes.readthedocs.io/en/latest/iptables/iptables.html

https://www.360blogs.top/iptables-详解/

https://tonydeng.github.io/sdn-handbook/basic/tcpip.html

视频

https://www.bilibili.com/video/BV1Yt4y1Z7so/?spm_id_from=333.788.recommend_more_video.11&vd_source=271cfb4bb43eae8c9b0543f4ae14ec31