0. 序言
linux防火墙有多种多样,CentOS 有firewalld, Ubuntu有ufw,…等。他们各有各的使用方法,但他们底层都使用的是iptables,所以还是有必要学习下的。
由于firewalld、ufw等都是基于iptables,他们都会操作iptables,所以,使用iptables时要先将firewalld、ufw等服务停掉。
sudo systemctl stop firewalld // 停止firewalld服务
sudo systemctl disable firewalld // 关闭开机自启
1. 概念
-
iptables是linux下的防火墙工具,它可以帮我们基于规则进行网络流量控制。
1.1 允许/拒绝某种协议的链接建立,比如TCP,UDP
1.2 允许/拒绝来自某个IP的访问
1.3 允许/拒绝来袭某个端口的访问
1.4 基于某个规则进行流量转发 -
规则、链、表
2.1 将来自某个ip/端口/协议的流量拒绝/接受/转发,都是一条规则。
2.2 多条规则构成一条链
2.3 多个链构成一个表
-
四表五链
3.1 filter表,可以决定一个数据包是否可以到达目标进程端口
3.2 mangle表,可以修改数据包的内容
3.3 nat表,可以修改源和目标的ip地址,从而进行包路由
3.4 raw表,能基于数据包的状态进行规则设定
3.5 PREROUTING链 数据包到达网口时进行规则匹配
3.6 INPUT链 对路由策略分派过来的包到达目标进程端口之前进行匹配并处理
3.7 FORWARD链 对路由策略分派过来的包进行转发
3.8 OUTPUT链 判断从本地的目标进程端口处理好的包是否返回/如何返回给请求方
3.9 POSTROUTING链 数据包离开网口时的匹配
表优先顺序 raw —> mangle —> nat —> filter
链优先顺序 PREROUTING —> INPUT —> FORWARD —> OUTPUT —> POSTROUTING -
执行流程
2. 命令规则
[root@root ~]# iptables -h
iptables v1.4.21
Usage: iptables -[ACD] chain rule-specification [options]
iptables -I chain [rulenum] rule-specification [options]
iptables -R chain rulenum rule-specification [options]
iptables -D chain rulenum [options]
iptables -[LS] [chain [rulenum]] [options]
iptables -[FZ] [chain] [options]
iptables -[NX] chain
iptables -E old-chain-name new-chain-name
iptables -P chain target [options]
iptables -h (print this help information)
Commands:
Either long or short options are allowed.
--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
Change chain name, (moving any references)
Options:
--ipv4 -4 Nothing (line is ignored by ip6tables-restore)
--ipv6 -6 Error (line is ignored by iptables-restore)
[!] --protocol -p proto protocol: by number or name, eg. `tcp'
[!] --source -s address[/mask][...]
source specification
[!] --destination -d address[/mask][...]
destination specification
[!] --in-interface -i input name[+]
network interface name ([+] for wildcard)
--jump -j target
target for rule (may load target extension)
--goto -g chain
jump to chain with no return
--match -m match
extended match (may load extension)
--numeric -n numeric output of addresses and ports
[!] --out-interface -o output name[+]
network interface name ([+] for wildcard)
--table -t table table to manipulate (default: `filter')
--verbose -v verbose mode
--wait -w [seconds] maximum wait to acquire xtables lock before give up
--wait-interval -W [usecs] wait time to try to acquire xtables lock
default is 1 second
--line-numbers print line numbers when listing
--exact -x expand numbers (display exact values)
[!] --fragment -f match second or further fragments only
--modprobe=<command> try to insert modules using this command
--set-counters PKTS BYTES set the counter during insert/append
[!] --version -V print package version.
2.1 查看iptables规则
iptables –L –n 或者 iptables –L –n –t filter 或者 iptables –L –n –x –v
中文说明:
-L :列出一个或所有链的规则
-v:显示详细信息、包括每条规则匹配包数量和匹配字节数
-x:在v的基础上、进制自动单位换算(K,M)
-n: 只显示IP地址和端口号码。不显示域名和服务名称
-t : 接表名、如果不加-t,默认就是 –t filter
2.2 清空默认规则
iptables –F ###清除所有规则
iptables –X ####删除用户自定义的链
iptables –Z ####链的计数器清零
2.3 规则持久化
对规则的操作通常不是持久的,机器重启后规则将丢失,因此,需要对规则 进行持久化
centos
- 第一步,在修改iptables配置后,将其导出到某个文件,比如: /etc/iptables.conf 。命令为:
iptables-save > /etc/iptables.conf
这一步,每次修改后都要做- 第二步,在/etc/rc.local中添加命令
iptables-restore < /etc/iptables.conf
。从此之后,每次重启,系统会自动从/etc/iptables.conf恢复对应的iptables配置。
ubuntu
安装iptables-persistent,它会在系统启动时,从/etc/iptables/rules.v4 和 /etc/iptables/rules.v6分别加载ipv4 和ipv6的iptables 规则sudo apt install iptables-persistent
所以,每次我们对iptables进行了任何改动,使用下面的命令,将当前生效的iptables配置,导出到/etc/iptables/rules.v4 和 /etc/iptables/rules.v6即可
sudo iptables-save > /etc/iptables/rules.v4 // 如果添加了ipv4规则,执行这步 sudo iptables-svae > /etc/iptables/rules.v6 // 如果添加了ipv6规则,执行这步
我们在配置规则时,需要知道一条规则分为两大部分
-
匹配规则 即哪条链会命中该条规则,比如一个指定的ip,即为一条规则
匹配规则:
1.1-t
指定表 默认filter表
1.2-p
指定协议
1.3-i
指定输入网口 --in-interface
1.4-o
指定输出网口 --out-interface
1.5-s
指定源ip,支持单个ip,多个ip(逗号分隔),网段格式
1.6-d
指定目标ip,支持单个ip,多个ip(逗号分隔),网段格式
1.7-sport[s]
指定源端口, 多个端口逗号分隔,需指定-m multiport
1.8-dport[s]
指定目标端口, 多个端口逗号分隔,需指定-m multiport
1.9-m
match 指定规则匹配匹配指定链接状态的数据包
iptables -I INPUT -m contrack --ctstate RELATED,ESTABLISHED -j ACCEPT
有些版本的linux,对应的module不是conntrack,而是state。对应的指定状态的参数不是--ctstate
而是--state
.上述命令需替换为:
iptables -I INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
状态有:- NEW: 新创建的链接
- ESTABLISHED: 已经建立的链接
- RELATED: 跟已经创建的链接相关的链接
- INVALID: 非正常状态
- DNAT: 如果一个链接其目标地址被nat表PREROUTING链中的规则修改了,就是这个状态
- SNAT: 如果一个链接其源地址被nat表中的规则修改了,就是这个状态
1.10
!
取反,以上除了-t -m
外,其他都支持取反, 例如-m multiport ! --dports 22,3306,8080 -j ACCEPT
-
动作 即匹配到规则后,需要做哪些动作,接受还是拒绝
2.1 ACCEPT: 直接接受该数据包,不会走其他链和规则。比如filter中的input表中某个规则命中后,动作是ACCEPT,那么该数据包将被直接送达目标进程端口。
2.2 DROP: 直接抛弃该数据包,并且没有任何返回。且不会走其他链和规则。
2.3 REJECT: 跟DROP类似,但会跟请求方返回一些拒绝信息,比如我们拒绝掉ICMP协议后,ping该主机,会返回“destination host unreachable”。
2.4 RETURN: 当前规则不做任何处理就返回
2.5 LOG: 和RETURN类似,但会将请求信息记录到系统日志中,记录路径为:/var/log/syslog 或 /var/log/messages