参考文献

iptables

  • iptables有一个很重要的设置概念就是5表5链

  • 一个filter表,一个nat表,一个mangle表,一个raw表,一个security.它们分别作用于不同的方向,

    • filter表: 此表用于数据包过滤.它有三个内置链: INPUT(用于本地传递的数据包),FORWARD(用于通过系统路由的数据包)和 OUTPUT(用于本地生成的数据包)

      • 内核模块: iptables_filter
    • natnetwork address translation,网络地址转换功能;

      • 此表用于网络地址转换(nat).它有三个内置链: PREROUTING(用于到达网络接口的数据包),INPUT(用于本地生成的数据包)和 OUTPUT(用于离开网络接口的数据包)
      • 内核模块: iptable_nat
    • mangle表拆解报文,做出修改,并重新封装的功能;

      • 此表用于专门的数据包修改.它有五个内置链: PREROUTING(用于到达网络接口的数据包),INPUT(用于本地传递的数据包),FORWARD(用于通过系统路由的数据包),OUTPUT(用于本地生成的数据包)和 POSTROUTING(用于离开网络接口的数据包)
      • iptable_mangle
    • raw表关闭nat表上启用的连接追踪机制;

      • 此表处理在其他任何表之前处理的数据包.它有两个内置链: PREROUTING(用于到达网络接口的数据包)和 OUTPUT(用于本地生成的数据包)
      • iptable_raw
    • security表用于强制访问控制网络规则

      • INPUT(用于本地传递的数据包),FORWARD(用于通过系统路由的数据包),OUTPUT(用于本地生成的数据包)
  • 5链分别是INPUT链,OUTPUT链,FORWARD链,PREROUTING链和POSTROUTING链.这5条链分别也对应不同的一些设置规则.

    • INPUT链: 进来的数据包应用此规则链中的规则;
      • INPUT的规则可以存在于: mangle表,filter表
    • OUTPUT链: 外出的数据包应用此规则链中的规则;
      • OUTPUT的规则可以存在于: raw表,mangle表,nat表,filter表.
    • FORWARD链: 转发数据包时应用此规则链中的规则;
      • FORWARD的规则可以存在于: mangle表,filter表.
    • PREROUTING链: 对数据包作路由选择前应用此链中的规则;
      • PREROUTING的规则可以存在于: raw表,mangle表,nat表
    • POSTROUTING链: 对数据包作路由选择后应用此链中的规则.
      • POSTROUTING的规则可以存在于: mangle表,nat表.
  • 报文的流向

    • 到本机某进程的报文: PREROUTING –> INPUT
    • 由本机转发的报文: PREROUTING –> FORWARD –> POSTROUTING
    • 由本机的某进程发出报文(通常为响应报文): OUTPUT –> POSTROUTING
raw PREROUTING,OUTPUT
mangle PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING
nat PREROUTING,OUTPUT,POSTROUTING
filter INPUT,FORWARD,OUTPUT
security INPUT,FORWARD,OUTPUT
PREROUTING raw,mangle,nat
INPUT mangle,filter
FORWARD mangle,filter
OUTPUT raw,mangle,nat,filter
POSTROUTING mangle,nat
  • iptables定义了4张”表”,当他们处于同一条”链”时,执行的优先级如下

    1
    raw –> mangle –> nat –> filter

iptables简化网络数据包流程图

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
                               XXXXXXXXXXXXXXXXXX
XXX Network XXX
XXXXXXXXXXXXXXXXXX
+
|
v
+-------------+ +------------------+
|table: filter| <---+ | table: nat |
|chain: INPUT | | | chain: PREROUTING|
+-----+-------+ | +--------+---------+
| | |
v | v
[local process] | **************** +--------------+
| +---------+ Routing decision +------> |table: filter |
v **************** |chain: FORWARD|
**************** +------+-------+
Routing decision |
**************** |
| |
v **************** |
+-------------+ +------> Routing decision <---------------+
|table: nat | | ****************
|chain: OUTPUT| | +
+-----+-------+ | |
| | v
v | +-------------------+
+--------------+ | | table: nat |
|table: filter | +----+ | chain: POSTROUTING|
|chain: OUTPUT | +--------+----------+
+--------------+ |
v
XXXXXXXXXXXXXXXXXX
XXX Network XXX
XXXXXXXXXXXXXXXXXX

图片来源于https://upload.wikimedia.org/wikipedia/commons/3/37/Netfilter-packet-flow.svg

iptables的规则

  • 规则: 根据指定的匹配条件来尝试匹配每个流经此处的报文,一旦匹配成功,则由规则后面指定的处理动作进行处理;
  • 规则由匹配条件和处理动作组成.

匹配条件

  • 匹配条件分为基本匹配条件与扩展匹配条件
基本匹配条件
  • 源地址Source IP,目标地址 Destination IP

处理动作

  • 处理动作在iptables中被称为target
  • 动作也可以分为基本动作和扩展动作
target 含义
ACCEPT 直接允许数据包通过
DROP 直接丢弃数据包,不给任何回应信息,这时候客户端会感觉自己的请求泥牛入海了,过了超时时间才会有反应
REJECT 拒绝数据包通过,必要时会给数据发送端一个响应的信息,客户端刚请求就会收到拒绝的信息
SNAT 源地址转换,解决内网用户用同一个公网地址上网的问题
MASQUERADE (伪装)将src ip改写为网卡 ip,和 SNAT 的区别是它会自动读取网卡 ip.路由设备必备
DNAT 目标地址转换
REDIRECT 在本机做端口映射
LOG /var/log/messages文件中记录日志信息,然后将数据包传递给下一条规则,也就是说除了记录以外不对数据包做任何其他操作,仍然让下一条规则去匹配

iptables命令规则设置的常见选项

1
2
iptables -t 表名 <-A/I/D/R> 规则链名 [规则号] <-i/o 网卡名> -p 协议名 <-s 源IP/源子网> --sport 源端口 <-d 目标IP/目标子网> --dport 目标端口 -j 动作

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
~$ iptables -h
iptables v1.8.4

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.

iptables配置文件

1
/etc/sysconfig/iptables

INPUT

1
2
3
# iptables -vL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
属性 含义
pkts 对应规则匹配到的报文的个数
bytes 对应匹配到的报文包的大小总和
target 规则对应的target,往往表示规则对应的”动作”,即规则匹配成功后需要采取的措施
prot 表示规则对应的协议,是否只针对某些协议应用此规则
opt 表示规则对应的选项
in 表示数据包由哪个接口(网卡)流入,即从哪个网卡来
out 表示数据包将由哪个接口(网卡)流出,即到哪个网卡去
source 表示规则对应的源头地址,可以是一个IP,也可以是一个网段
destination 表示规则对应的目标地址。可以是一个IP,也可以是一个网段

示例

服务管理

1
2
3
4
5
6
7
8
9
10
# service iptables start|stop|restart|status
# service iptables save //定义的所有内容,在重启时都会失效。调用save命令可以把规则保存到文件/etc/sysconfig/iptables中。
# iptables-save //保存规则
# iptables-restore //加载规则。开机的时候,会自动加载/etc/sysconfig/iptables
# iptables-restore < /etc/sysconfig/iptables2 //加载自定义的规则文件

//iptables服务配置文件: /etc/sysconfig/iptables-config
//iptables规则文件: /etc/sysconfig/iptables

# echo "1">/proc/sys/net/ipv4/ip_forward //打开iptables转发

显示当前规则

1
2
3
4
5
6
iptables -nvL

# 查看nat表
iptables -t nat -nL
# 查看filter表
iptables -t filter -nL

重置规则

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 不带参数的 -F 命令刷新其当前表中的所有链
# iptables -F
# -X 删除表中的所有空非默认链
# iptables -X
# iptables -t nat -F
# iptables -t nat -X
# iptables -t mangle -F
# iptables -t mangle -X
# iptables -t raw -F
# iptables -t raw -X
# iptables -t security -F
# iptables -t security -X
# iptables -P INPUT ACCEPT
# iptables -P FORWARD ACCEPT
# iptables -P OUTPUT ACCEPT

规则管理命令

1
2
3
4
iptables -A:在规则链的末尾加入新规则
iptables -D:删除某个规则
iptables -I:在规则链的头部加入新规则
iptables -R:替换规则链中的规则

链管理命令

1
2
3
4
iptables -F:清空规则链
iptables -Z:清空规则链中的数据包计算器和字节计数器
iptables -N:创建新的用户自定义规则链
iptables -P:设置规则链中的默认策略

通用匹配参数

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
-t
对指定的表 table 进行操作
如果不指定此选项,默认的是 filter 表

-p 协议
指定规则的协议,如 tcp, udp, icmp 等,可以使用all来指定所有协议
如果不指定 -p 参数,默认的是 all 值

-s 源地址
指定数据包的源地址
参数可以使IP地址、网络地址、主机名
例如:-s 192.168.1.101 指定IP地址
例如:-s 192.168.1.10/24 指定网络地址

-d 目的地址
指定数据包的目的地址,规则和 -s 类似

-j 执行目标
指定规则匹配时如何处理数据包
可能的值是ACCEPT, DROP, QUEUE, RETURN 等

-i 输入接口
指定要处理来自哪个接口的数据包,这些数据包将进入 INPUT, FORWARD, PREROUTE 链
例如:-i eth0指定了要处理经由eth0进入的数据包
如果不指定 -i参数,那么将处理进入所有接口的数据包
如果指定 ! -i eth0,那么将处理所有经由eth0以外的接口进入的数据包
如果指定 -i eth+,那么将处理所有经由eth开头的接口进入的数据包

-o 输出
指定了数据包由哪个接口输出,这些数据包将进入 FORWARD, OUTPUT, POSTROUTING链
如果不指定-o选项,那么所有接口都可以作为输出接口
如果指定 ! -o eth0,那么将从eth0以外的接口输出
如果指定 -i eth+,那么将仅从eth开头的接口输出

扩展参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-sport 源端口
针对 -p tcp 或者 -p udp,默认情况下,将匹配所有端口
可以指定端口号或者端口名称、端口范围,例如 –sport 22, –sport ssh,–sport 22:100
从性能上讲,使用端口号更好, /etc/services 文件描述了映射关系

-dport 目的端口
规则和 –sport 类似

-tcp-flags TCP 标志
针对 -p tcp
可以指定由逗号分隔的多个参数
取值范围:SYN, ACK, FIN, RST, URG, PSH, ALL, NONE

-icmp-type ICMP 标志
针对 -p icmp
icmp-type 0 表示 Echo Reply
icmp-type 8 表示 Echo

添加规则

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 开启SSH服务端口
$ iptables -A INPUT -p tcp --dport 22 -j ACCEPT
$ iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT

# 开启Web服务端口
$ iptables -A OUTPUT -p tcp --sport 80 -j ACCEPT
$ iptables -A INPUT -p tcp --dport 80 -j ACCEPT

# 允许本地回环接口(即运行本机访问本机)
$ iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT

# 允许已建立的或相关连的通行
$ iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# 允许所有本机向外的访问
$ iptables -A OUTPUT -j ACCEPT

# 允许访问22端口
$ iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# 允许访问80端口
$ iptables -A INPUT -p tcp --dport 80 -j ACCEPT

# 允许ftp服务的21端口
$ iptables -A INPUT -p tcp --dport 21 -j ACCEPT

# 允许FTP服务的20端口
$ iptables -A INPUT -p tcp --dport 20 -j ACCEPT

# 禁止其他未允许的规则访问
$ iptables -A INPUT -j reject

# 禁止其他未允许的规则访问
$ iptables -A FORWARD -j REJECT

# 在指定表的指定链的尾部添加一条规则,-A选项表示在对应链的末尾添加规则,省略-t选项时,表示默认操作filter表中的规则
# 命令语法:iptables -t 表名 -A 链名 匹配条件 -j 动作
$ iptables -t filter -A INPUT -s 192.168.1.146 -j DROP

# 在指定表的指定链的指定位置添加一条规则
# 命令语法:iptables -t 表名 -I 链名 规则序号 匹配条件 -j 动作
$ iptables -t filter -I INPUT 5 -s 192.168.1.146 -j REJECT

# -s用于匹配报文的源地址,可以同时指定多个源地址,每个IP之间用逗号隔开,也可以指定为一个网段。
$ iptables -t filter -I INPUT -s 192.168.1.111,192.168.1.118 -j DROP
$ iptables -t filter -I INPUT -s 192.168.1.0/24 -j ACCEPT
$ iptables -t filter -I INPUT ! -s 192.168.1.0/24 -j ACCEPT

# -d用于匹配报文的目标地址,可以同时指定多个目标地址,每个IP之间用逗号隔开,也可以指定为一个网段。
$ iptables -t filter -I OUTPUT -d 192.168.1.111,192.168.1.118 -j DROP
$ iptables -t filter -I INPUT -d 192.168.1.0/24 -j ACCEPT
$ iptables -t filter -I INPUT ! -d 192.168.1.0/24 -j ACCEPT

# -p用于匹配报文的协议类型,可以匹配的协议类型tcp、udp、udplite、icmp、esp、ah、sctp等(centos7中还支持icmpv6、mh)。
$ iptables -t filter -I INPUT -p tcp -s 192.168.1.146 -j ACCEPT
$ iptables -t filter -I INPUT ! -p udp -s 192.168.1.146 -j ACCEPT

# -i用于匹配报文是从哪个网卡接口流入本机的,由于匹配条件只是用于匹配报文流入的网卡,所以在OUTPUT链与POSTROUTING链中不能使用此选项。
$ iptables -t filter -I INPUT -p icmp -i eth4 -j DROP
$ iptables -t filter -I INPUT -p icmp ! -i eth4 -j DROP

# -o用于匹配报文将要从哪个网卡接口流出本机,于匹配条件只是用于匹配报文流出的网卡,所以在INPUT链与PREROUTING链中不能使用此选项。
$ iptables -t filter -I OUTPUT -p icmp -o eth4 -j DROP
$ iptables -t filter -I OUTPUT -p icmp ! -o eth4 -j DROP

删除规则

1
2
3
4
# 显示INPUT链规则的行号
iptables -L INPUT --line-numbers

iptables -D INPUT <line-number>

只允许某台主机或某个网段进行SSH连接

1
2
3
4
5
# 只允许192.168.11.60的机器进行SSH连接
[root@tp ~]# iptables -A INPUT -s 192.168.11.60 -p tcp --dport 22 -j ACCEPT

# 如果允许或限制一段IP地址可用192.168.0.0/24表示192.168.0.1-255端的所有IP, 24表示子网掩码数
[root@tp ~]# iptables -A INPUT -s 192.168.0.0/24 -p tcp --dport 22 -j ACCEPT

开启转发功能

  • 在做NAT网络配置时,FORWARD默认规则是DROP时,必须开启数据包转发功能
1
2
[root@tp ~]# iptables -A FORWARD -i eth0 -o eth1 -m state --state RELATED,ESTABLISHED -j ACCEPT
[root@tp ~]# iptables -A FORWARD -i eth1 -o eh0 -j ACCEPT

白名单

1
2
3
4
5
6
7
8
# 允许机房内网机器可以访问
$ iptables -A INPUT -p all -s 192.168.1.0/24 -j ACCEPT

# 允许机房内网机器可以访问
$ iptables -A INPUT -p all -s 192.168.140.0/24 -j ACCEPT

# 允许 183.121.3.7 访问本机的 3380 端口
$ iptables -A INPUT -p tcp -s 183.121.3.7 --dport 3380 -j ACCEPT

黑名单

1
2
3
4
5
6
7
8
9
10
11
 # 屏蔽单个 IP
$ iptables -I INPUT -s 123.45.6.7 -j DROP

# 屏蔽 IP 网段 从 123.0.0.1 到 123.255.255.254
$ iptables -I INPUT -s 123.0.0.0/8 -j DROP

# 屏蔽 IP 网段 从 123.45.0.1 到 123.45.255.254
$ iptables -I INPUT -s 124.45.0.0/16 -j DROP

# 屏蔽 IP 网段 从 123.45.6.1 到 123.45.6.254
$ iptables -I INPUT -s 123.45.6.0/24 -j DROP

防止 SYN 洪水攻击

1
$ iptables -A INPUT -p tcp --syn -m limit --limit 5/second -j ACCEPT