文章目录
基础概念
四张表:
- raw
- mangle
- nat
- filter
当表位于同一个链时,优先级:raw --> mangle --> nat --> filter
五条链:
- PREROUTING
- INPUT
- FORWARD
- OUTPUT
- POSTROUTING
对应关系:
-
链与表
- PREROUTING:raw、mangle、nat
- INPUT:mangle、nat、filter
- FORWARD:mangle、filter
- OUTPUT:raw、mangle、nat、filter
- POSTROUTING:mangle、nat
-
表与链
- raw:PREROUTING、OUTPUT
- mangle:PREROUTING、INPUT、FORWARD、OUTPUT、POSTROUTING
- nat:PREROUTING、INPUT、POSTROUTING
- filter:INPUT、FORWARD、OUTPUT
数据处理过程:

处理动作(-j target)
- ACCEPT:允许数据包通过
- DROP:丢弃数据包,无任何回应,客户端会超时
- REJECT:拒绝数据包通过,回应给客户端无法访问
- SNAT:源地址转换
- MASQUERADE:SNAT的一种特殊形式,与端口绑定,适用于动态公网IP的场景
- DNAT:目标地址转换
- REDIRECT:本机做端口映射
- LOG:在 var/log/messages 中记录日志,然后匹配下一条规则(仅记录)
- 自定义链,匹配到的数据交给自定义链中的规则来处理
- RETURN:子链 return 后,回到触发 jump 的那条规则,从那条规则的下一条继续匹配;主链 return 后,执行默认策略
基本命令
操作 iptables 就是对四张表的增删改查
–help 参数
--append -A chain
Append to chain
--check -C chain
Check for the existence of a rule
--delete -D chain
Delete matching rule from chain
--delete -D chain rulenum
Delete rule rulenum (1 = first) from chain
--insert -I chain [rulenum]
Insert in chain as rulenum (default 1=first)
--replace -R chain rulenum
Replace rule rulenum (1 = first) in chain
--list -L [chain [rulenum]]
List the rules in a chain or all chains
--list-rules -S [chain [rulenum]]
Print the rules in a chain or all chains
--flush -F [chain] Delete all rules in chain or all chains
--zero -Z [chain [rulenum]]
Zero counters in chain or all chains
--new -N chain Create a new user-defined chain
--delete-chain
-X [chain] Delete a user-defined chain
--policy -P chain target
Change policy on chain to target
--rename-chain
-E old-chain new-chain
查看
查看表中的规则(-L --list)
iptables -t filter -L
# 默认操作 filter 表,所以上面等于 iptables -L
# 查看其他表
iptables -t raw -L
iptables -t mangle -L
iptables -t nat -L
# 直接 -L 会解析规则中的 IP 地址
-L 显示出来的内容:
Chain INPUT (policy ACCEPT) # 第一行 policy 表示这张表的默认动作(target)是ACCEPT
target prot opt source destination # 第二行开始是规则
DROP all -- anywhere anywhere
Chain FORWARD (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
ACCEPT all -- anywhere anywhere
ACCEPT all -- anywhere anywhere
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
查看表中的规则,-n 表示不解析 IP
# iptables -nL
# 可以看到 anywhere 都变成了 0.0.0.0/0 ,这样查表速度快
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all -- 0.0.0.0/0 0.0.0.0/0
LIBVIRT_INP all -- 0.0.0.0/0 0.0.0.0/0
Chain FORWARD (policy ACCEPT)
target prot opt source destination
DOCKER-USER all -- 0.0.0.0/0 0.0.0.0/0
DOCKER-ISOLATION-STAGE-1 all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
DOCKER all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
-v 选项会显示出计数器,就是每条链每个规则匹配到的包数
# iptables -nvL
# 例如:
Chain OUTPUT (policy ACCEPT 1173 packets, 94739 bytes)
pkts bytes target prot opt in out source destination
2134 195K LIBVIRT_OUT all -- * * 0.0.0.0/0 0.0.0.0/0
-x 选项,表示显示计数器的精确值,包数过多时, -v 会显示为 KB、MB人类可读形式,-x 就会精确显示字节数
–line-number 可以简写为 --line,显示规则的行号,增删改的时候会用到
可以在查看命令后面加指定的链名来只查看该链中的规则
# iptables --line -t filter -nxvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 1997 196482 DROP all -- * * 0.0.0.0/0 0.0.0.0/0
2 1307 981564 LIBVIRT_INP all -- * * 0.0.0.0/0 0.0.0.0/0
增加
插入一条规则(默认插入到首部):
# 拒绝来自 10.1.1.1 的所有流量
iptables -t filter -I INPUT -s 10.1.1.1 -j DROP
增加一条规则(追加到末尾)
# 拒绝所有的 icmp 报文
iptables -t filter -I INPUT -p icmp -j DROP
# DROP显示的是
请求超时。
# REJECT 显示的是
来自 10.1.1.133 的回复: 无法连到端口。(Destination Port Unreachable)
删除
方法一:根据规则编号进行删除
# 先查看规则
# iptables --line -t filter -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 6 360 DROP icmp -- * * 0.0.0.0/0 0.0.0.0/0
2 1613 1008K LIBVIRT_INP all -- * * 0.0.0.0/0 0.0.0.0/0
# 删除 filter 表中 INPUT 链的第一条规则
iptables -t filter -D INPUT 1
方法二:根据匹配条件与动作进行删除
# 删除 filter 表中 INPUT 链的拒绝 icmp 的规则
iptables -t filter -D INPUT -p icmp -j DROP
删除整张表的规则 -F flush
iptables -t filter -F
删除指定表中某条链的全部规则 -F flush
iptables -t filter -F OUTPUT
修改
# 添加一条规则
# iptables -t filter -I INPUT -p icmp -j DROP
# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP icmp -- 0.0.0.0/0 0.0.0.0/0
# 修改规则
# iptables -t filter -R INPUT 1 -p icmp -j REJECT
-R 选项表示修改指定的链,使用-R INPUT 1 表示修改INPUT链的第 1 条规则,但是在使用-R选项修改某个规则时,必须指定规则对应的原本的匹配条件(如果有多个匹配条件,都需要指定)。
例如:
# 有一条规则:
iptables -I INPUT -s 10.0.0.0/8 -p icmp -j REJECT
# 如果 -R 修改时没有写全匹配条件
iptables -R INPUT 1 -p icmp -j DROP
# 那么规则就会使用默认的配置,如下:10.0.0.0/8 修改后变成了 0.0.0.0/0
# 查看规则 iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP icmp -- 0.0.0.0/0 0.0.0.0/0
修改默认动作
当报文没有被链中的任何规则匹配到时,或者,当链中没有任何规则时,iptables 会按照默认动作处理报文,可以使用 -P 选项修改指定链的默认策略:
# 查看规则 iptables -nL
Chain INPUT (policy ACCEPT) # 默认规则是 ACCEPT
target prot opt source destination
DROP icmp -- 0.0.0.0/0 0.0.0.0/0
# 修改默认策略
iptables -t filter -P INPUT DROP
保存规则
默认情况下,做出的修改时临时的,当重启服务后改动会丢失。
CentOS6 使用service iptables save命令保存规则,规则默认保存在/etc/sysconfig/iptables文件中。
CentOS7 默认没有这个命令,可以通过 yum install -y iptables-services
#配置好yum源以后安装iptables-service
yum install -y iptables-services
#停止firewalld
systemctl stop firewalld
#禁止firewalld自动启动
systemctl disable firewalld
#启动iptables
systemctl start iptables
#将iptables设置为开机自动启动,以后即可通过iptables-service控制iptables服务
systemctl enable iptables
另外一种方法:使用iptables-save可以查看当前的规则,然后将输出重定向到配置文件中即可
iptables-save > /etc/sysconfig/iptables
恢复配置:可以将 iptables 的配置文件重新加载
iptables-restore < /etc/sysconfig/iptables
匹配条件
基本匹配条件
-s 用于匹配报文的源地址,可以同时指定多个源地址,每个 IP 之间用逗号隔开,也可以指定为一个网段。! 用于取反
-d 用于匹配报文的目标地址
iptables -t filter -I INPUT -s 1.1.1.1,2.2.2.2 -j DROP
iptables -t nat -I INPUT -s 192.168.0.0/24 -j DRIO
iptables -t filter -I OUTPUT ! -d 10.0.0.0/8 -j DROP
-p 用于匹配报文的协议类型,可以匹配的协议类型tcp、udp、udplite、icmp、esp、ah、sctp、icmpv6、mh等
iptables -t filter -I INPUT -p tcp -s 12.1.1.1/8 -j ACCEPT
iptables -t filter -I INPUT ! -p udp -s 12.1.1.1/8 -j ACCEPT
-i 用于匹配报文是从哪个网络流入的,所以 OUTPUT 和 POSTROUTING 不能使用该选项
iptables -t filter -I INPUT -p icmp -i ens33 -j DROP
iptables -t filter -I INPUT -p icmp ! -i ens33 -j ACCEPT
-i 用于匹配报文是从哪个网络流入的,所以 INPUT 和 PREROUTING 不能使用该选项
iptables -t filter -I OUTPUT -p icmp -o ens33 -j DROP
iptables -t filter -I OUTPUT -p icmp ! -o ens33 -j ACCEPT
扩展匹配条件
tcp 扩展模块
- -p tcp -m tcp --sport 用于匹配tcp协议报文的源端口,可以使用冒号指定一个连续的端口范围
- -p tcp -m tcp --dport 用于匹配tcp协议报文的目标端口,可以使用冒号指定一个连续的端口范围
iptables -t filter -I INPUT -d 192.168.1.1 -p tcp -m tcp --dport 22:80 -j ACCEPT
iptables -t filter -I INPUT -d 192.168.1.1 -p tcp -m tcp --dport :200 -j ACCEPT
iptables -t filter -I OUTPUT -d 10.1.1.1 -p tcp -m tcp ! --sport 1024: -j REJECT
- -p tcp -m multiport --sports 用于匹配报文的源端口,可以指定离散的多个端口号,端口之间用"逗号"隔开
- -p tcp -m multiport --dports 用于匹配报文的目标端口,可以指定离散的多个端口号,端口之间用"逗号"隔开
iptables -t filter -I OUTPUT -d 192.168.1.1 -p tcp -m multiport --sports 22,80,443 -j ACCEPT
iptables -t filter -I OUTPUT -s 1.1.1.0/24 -p tcp -m multiport --dports 22:80,443 -j DROP
iprange 扩展模块
两个匹配条件:--src-range 和 --dst-range,多个连续 IP 地址之间用 - 分隔。
iptables -t filter -I OUTPUT -m iprange --src-range 10.1.1.2-10.1.1.10 --dst-range 20.1.1.2-20.1.1.20 -j DROP
string 扩展模块
用来报文中的字符串,有两个匹配条件:
–algo:用于指定匹配算法,可选的算法有bm与kmp,此选项为必须选项,不用纠结于选择哪个算法,但是必须指定
–string:用于指定需要匹配的字符串
# 拒绝访问含有 Welcome 的内容
iptables -t filter -I INPUT -m string --algo bm --string "Welcome" -J REJECT
time 扩展模块
根据时间段来使规则生效,
--timestart指定开始时间,--timestop指定结束时间,不能取反--weekdays指定星期几,可以 ! 取反--monthdays指定每月的时间,可以 ! 取反--datestart指定开始日期,--datestop指定结束日期,不能取反
当一条规则中同时存在多个条件时,多个条件之间默认存在"与"的关系
# 每周一到周五的 9 点到 18 点不能访问 80 端口
iptables -t filter -I INPUT -p tcp --dport 80 -m time --weekdays 1,2,3,4,5 --timestart 9:00:00 --timestop 18:00:00 -j REJECT
# 每个月的第四个周五
iptables -t filter -I INPUT -m time --weekdays -monthdays 22,23,24,26,26,27,28 -j REJECT
state
五种状态的官方解释
对于 state 模块来说,tcp、udp、icmp 等报文都是有连接状态的,只要两台机器在通信,就算建立起来了连接;这个连接和 tcp 中的连接不一样。
state 中的连接分为五种状态:NEW、ESTABLISHED、RELATED、INVALID、UNTRACKED。
- NEW:新连接中的第一个包
- ESTABLISHED:NEW 包之后的包都是 ESTABLISHED 状态,表示连接已经建立
- RElATED:表示该数据包正在启动新连接,但与现有连接相关联,例如FTP数据传输或ICMP错误
- INVALID:不能识别或没有状态的包
- UNTRACKED:报文未被追踪,无法找到相关的连接
# 常用的规则:放行回包
iptables -I FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
自定义链
可以将一类规则自定义为一个链,方便管理。如 web 相关的规则,定义一个 WEB_CHAIN
# 在 filter 表中添加一个自定义链
iptables -t filter -N WEB_CHAIN
# 在 INPUT 链中引用自定义链
iptables -t filter -I INPUT -p tcp --dport 80 -j WEB_CHAIN
# 在自定义链中添加规则
iptables -t filter -I WEB_CHAIN -s 192.168.1.0/24 -j DROP
# 重命名自定义连
iptables -t filter -E WEB_CHAIN WEB
# 删除自定义链,必需满足:未被引用,规则为空
# 取消引用(删除相关规则)
iptables -t filter -D 1
# 清空链
iptables -t filter -F WEB
# 删除自定义链
iptables -t filter -X WEB
动作
REJECT
REJECT动作的常用选项为–reject-with,设置提示信息,提示对方为什么被拒绝
- icmp-net-unreachable
- icmp-host-unreachable
- icnp-port-unreachable
- icmp-proto-unreachable
- icmp-net-prohibited
- icmp-host-prohibited
- icmp-admin-prohibited
默认值为 icmp-port-unreachable
# 例如:
iptables -I INPUT -p icmp -j REJECT --reject-with icmp-admin-prohibited
LOG
LOG动作会将报文的相关信息记录在 /var/log/syslog 中
–log-level选项可以指定记录日志的日志级别,可用级别有emerg,alert,crit,error,warning,notice,info,debug。
–log-prefix选项可以给记录到的相关信息添加"标签"之类的信息,以便区分各种记录到的报文信息,方便在分析时进行过滤。
修改 log 文件保存的地方,方便区分
# vim /etc/rsyslog.conf
kern.* /var/log/iptables.log
# 重启服务
systemctl restart rsyslog
SNAT
iptables -t nat POSTROUTING -s 私网网段 -j SNAT --to-source 公网IP
DNAT
# 将内网服务器 192.168.1.100:80 映射到公网 2333 端口,如果没生效的话,加上 SNAT 规则
iptables -t nat PREROUTING -d 公网IP -p tcp --dport 2333 -j DNAT --to-destination 192.168.1.100:80
iptables -t nat POSTROUTING -s 192.168.1.100 -j SNAT --to-source 公网IP
MASQUERADE
如果公网地址是动态获取的,如果怕配置 SNAT 规则,地址发生变化时,就需要重新配置规则,MASQUERADE 会动态的将源地址转换为可用的IP地址。
# ens33 是公网口,转换内网网段
iptables -t nat -A POSTROUTING -s 192.168.0.0/16 -o ens33 -j MASQUERADE
REDIRECT
使用REDIRECT动作可以在本机上进行端口映射
比如,将本机的80端口映射到本机的8080端口上
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080
经过上述规则映射后,当别的机器访问本机的80端口时,报文会被重定向到本机的8080端口上。
REDIRECT规则只能定义在PREROUTING链或者OUTPUT链中。
本文详细介绍了Iptables防火墙的基础概念、基本命令、规则匹配条件及自定义链动作,包括四张表、五条链的对应关系,以及如何进行规则的增删改查。同时,讲解了如何使用扩展匹配条件、自定义链和不同动作如REJECT、LOG、SNAT、MASQUERADE、DNAT、REDIRECT等。

854

被折叠的 条评论
为什么被折叠?



