iptables 完全指南

在了解iptables之前我们先了解一下 防火墙 的概念
防火墙是由Check Point创立者Gil Shwed于1993年发明并引入国际互联网,防火墙也是一种位于内部网络与外部网络之间的网络安全系统,能有效地保护系统的安全。
在Windows系统中有一个我们熟悉的防火墙,在控制面板里,但一般情况下我们会把它给关闭了,使用第三方的杀毒软件自带的防火墙功能,在Linux中也有类似杀毒软件的防火墙的东西,而且是系统自带过来的,那就是iptables,很多在初学Linux的时候对它很陌生,所以为了实验的顺利进行都是会先关掉防火墙。

简单理解就是当其他用户想要发送数据包给服务器,就必须经过防火墙才能到达主机,如果没有这一层防火墙保护,那可想而知服务器直接暴露在公网上是多么危险的,当然防火墙也是包含有硬件防火墙和软件防火墙两种,其实有很多硬件防火墙也是利用Linux最小化使用iptables再去加上图形化web界面集成硬件,就成了硬件防火墙。

在Linux系统中其实有两个防火墙,一个是四层防火墙:iptables,另外一个是七层防火墙(tcp-wrapper)

1 应用层-----------------tcp-wrapper  (七层防火墙)
2 表示层
3 会话层
4 传输层-----------------iptables     (四层防火墙)
5 网络层
6 数据链路层
7 物理层

iptables近期历史版本:
2.0.X内核:ipfwadm
2.2.X内核:ipchains
2.4.X内核:iptables

iptables结构

在RHEL7+的系统中除了有iptables,还有一个firewall

iptable 默认的表、链结构示意图

结合上图的表结构,我们可以单独将每个表里的链结合下面这张图知道数据包在iptable中的具体流向

以上就是iptables的结构

iptables 语法及选项

 1 iptables -t [表名:raw、mangle、nat、filter,默认不加为filter] -<A/I/D/R> [链名] [规则号] -<i/o> [网卡名称] -p [协议] -s [源IP/掩码] --sport [源端口] -d [目标IP/掩码]  --dport [目标端口] -j [动作]
 2 # 选项表
 3 -t<表>       :指定操作的表
 4 -A           :向规则中添加
 5 -D           :从规则中删除
 6 -i           :向规则中插入
 7 -R           :替换规则
 8 -L           :显示表中的规则
 9 -F           :清空表中规则
10 -Z           :清空表中规则及数据包计算器和字节计数器
11 -N           :创建新的用户自定义规则链
12 -P           :自定义规则链中的默认目标
13 -h           :帮助
14 -p           :指定匹配的数据包协议类型
15 -s           :指定匹配的数据包源ip地址
16 -j<动作>      :指定要操作的动作(见下面动作表)
17 -i<网卡名称>  :指定数据包进入本机的入口网卡
18 -o<网卡名称>  :指定数据包离开本机的出口网卡
19 # 表名称
20 raw        :连接跟踪表
21 mangle     :标记表
22 net        :地址转换表
23 filter     :过滤表
24 # 规则链名
25 INPUT       :处理输入的数据包
26 OUTPUT      :处理输出的数据包
27 PORWARD     :处理转发的数据包
28 PREROUTING  :目标地址转换
29 POSTOUTING  :源地址转换
30 # 动作
31 accept      :接收数据包
32 DROP        :丢弃数据包
33 REDIRECT    :重定向
34 SNAT        :源地址转换
35 DNAT        :目标地址转换
36 MASQUERADE  :IP伪装
37 LOG         :日志

启动/停止/重启/配置永久生效

启动iptables

1 systemctl start iptables

停止iptables

1 systemctl stop iptables

重启iptables

1 systemctl restart iptables

将规则写入配置文件永久生效

1 ervice iptables save

(注意在RHEL7+无法使用systemctl将规则写入配置文件,无需做软链,直接使用下面的命令写入配置文件即可)

实例:

注意:在写入规则的时候如果不加-t指定某张表时,默认是写到filter表里

查看iptables已有规则(参数:-t、-L、—line-numbers、-n)

1 # 参数
2 -t:指定表
3 -L:查看所有规则
4 --line-numbers:查看规则编号
5 -n:取消IP解析成主机名称
1 iptables -t <表名> -L
 1 # iptables -L 没有指定查看某张表时,默认会显示filter表
 2 [root@Server ~]# iptables -L
 3 Chain INPUT (policy ACCEPT)
 4 target     prot opt source               destination
 5 ACCEPT     all  --  anywhere             anywhere
 6 ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
 7 ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh
 8 ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http
 9 ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:https
10 DROP       tcp  --  anywhere             anywhere             tcp dpt:mysql
11 ACCEPT     icmp --  anywhere             anywhere             icmp echo-request
12 Chain FORWARD (policy ACCEPT)
13 target     prot opt source               destination
14 Chain OUTPUT (policy ACCEPT)
15 target     prot opt source               destination
16 # 如果想查看其他表,则使用-t参数指定表
17 [root@Server ~]# iptables -t nat -L
18 Chain PREROUTING (policy ACCEPT)
19 target     prot opt source               destination
20 Chain INPUT (policy ACCEPT)
21 target     prot opt source               destination
22 Chain OUTPUT (policy ACCEPT)
23 target     prot opt source               destination
24 Chain POSTROUTING (policy ACCEPT)
25 target     prot opt source               destination
26 # 查看规则编号
27 [root@Server ~]# iptables -L --line-numbers
28 Chain INPUT (policy ACCEPT)
29 num  target     prot opt source               destination
30 1    ACCEPT     all  --  anywhere             anywhere
31 2    ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
32 3    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh
33 4    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http
34 5    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:https
35 6    DROP       tcp  --  anywhere             anywhere             tcp dpt:mysql
36 7    ACCEPT     icmp --  anywhere             anywhere             icmp echo-request
37 8    ACCEPT     all  --  anywhere             anywhere
38 Chain FORWARD (policy ACCEPT)
39 num  target     prot opt source               destination
40 Chain OUTPUT (policy ACCEPT)
41 num  target     prot opt source               destination
42 # 当规则多的时候查看规则很慢时,是因为iptables要将规则里的IP从hosts文件里解析成主机名称所以会慢,可以加-n参数取消解析
43 iptables -L -n

添加规则(参数:-A、-j、-I、-N)

1 -A:添加
2 -j:动作
3 -I:插入
4 -N:添加用户自定义规则链
1 # 用法
2 iptables -t <表> -A <链> -j <动作>
3 # 再次强调没有加-t参数默认是filter表(四表见上面结构说明)
4 # 指定表添加规则:iptables -t filter -A INPUT -j ACCEPT
5 ACCEPT  :接受
6 DROP    :拒绝
 1 # 添加允许所有人访问
 2 [root@Server ~]# iptables -A INPUT -j ACCEPT
 3 # 查看规则
 4 [root@Server ~]# iptables -L
 5 Chain INPUT (policy ACCEPT)
 6 target     prot opt source               destination
 7 ACCEPT     all  --  anywhere             anywhere        # 刚刚添加的规则
 8 
 9 Chain FORWARD (policy ACCEPT)
10 target     prot opt source               destination
11 
12 Chain OUTPUT (policy ACCEPT)
13 target     prot opt source               destination
14 
15 # 在filter表中的INPUT链第5行中插入规则
16 [root@Server ~]# iptables -I INPUT 5 -j ACCEPT
17 # 添加用户自定义规则链
18 [root@Server ~]# iptables -N userflood

顺序匹配(权重)

在原有规则里再添加规则时,第一条规则权重比第二条高

注意:这里ACCEPT权重优先级比DROP高,所以DROP不生效,如果反过来则DROP生效而ACCEPT不生效
 1 [root@Server ~]# iptables -A INPUT -j ACCEPT
 2 [root@Server ~]# iptables -A INPUT -j DROP
 3 [root@Server ~]# iptables -L
 4 Chain INPUT (policy ACCEPT)
 5 target     prot opt source               destination
 6 ACCEPT     all  --  anywhere             anywhere
 7 DROP       all  --  anywhere             anywhere
 8 Chain FORWARD (policy ACCEPT)
 9 target     prot opt source               destination
10 Chain OUTPUT (policy ACCEPT)
11 target     prot opt source               destination

清除规则(参数:-F、-D、-X、-Z)

清除所有规则(参数:-F)

1 # 清空指定表所有链的规则
2 [root@Server ~]# iptables -F
3 # 清空指定表指定链的规则
4 [root@Server ~]# iptables -F OUTPUT

再次强调,-t指定表

删除指定规则(参数:-D)

1 # 删除filter表中INPUT链的第8行(行编号上面有讲)
2 [root@Server ~]# iptables -t filter -D INPUT 8

删除用户自定义链

1 [root@Server ~]# iptables -X userflood

清除链的计数器(参数:-Z)

1 [root@Server ~]# iptables -Z

修改规则/链名(参数:-R、-E)

1 # 修改filter表中的INPUT链第6条改为ACCEPT
2 # 如规则多的话修改某条规则还不如删掉重新添加
3 [root@Server ~]# iptables -t filter -R INPUT 6 -j ACCEPT
4 # 重命名用户自定义链名
5 [root@Server ~]# iptables -E userflood floodname

默认规则

1 Chain INPUT (policy ACCEPT)  # 其中的polucy是默认规则
2 ACCEPT  允许
3 DROP    拒绝
4 修改指定表指定链默认规则
5 [root@Server ~]# iptables -t filter -P OUTPUT DROP

匹配规则条件(参数:-p、-d、-s、-i、-o、—dport、time、state、connlimit、mac、iprang)

 1 -p:指定协议
 2 -d:指定原IP
 3 -s:指定目标IP
 4 -i:指定入口网卡
 5 -o:指定出口网卡
 6 -m:指定模块
 7 --dport:指定端口
 8 time:时间段
 9 state:
10 connlimit:
11 mac:
12 iprang:
 1 # 指定协议指定IP指定端口拒绝访问
 2 iptables -t filter -A INPUT -p tcp  -d 202.96.146.12/24 --dport 22 -j DROP
 3 
 4 # 只允许访问本机80端口(其他端口都禁止访问)
 5 [root@Server ~]# iptables -A INPUT -p tcp ! --dport 80 -j DROP
 6 
 7 # 拒绝所有人访问80端口
 8 [root@Server- ~]# iptables -A INPUT -p tcp --dport 80 -j DROP
 9 
10 # 单独允许访问指定端口,其他端口拒绝连接(主动模式)
11 [root@Server ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
12 [root@Server ~]# iptables -A INPUT -j DROP

被动模式:当发现某一端口被攻击时才去禁止相应的端口链接

1 # 根据协议禁止访问
2 [root@Server ~]# iptables -A INPUT --dport -j DROP
1 # 根据网卡禁止访问
2 # 入口网卡
3 [root@Server ~]# iptables -A INPUT -i eth0 -j DROP
4 # 出口网卡
5 [root@Server ~]# iptables -A INPUT -o eth0 -j DROP
-m 加载模块
 1 # multiport 匹配多个端口
 2 # 匹配多个端口
 3 [root@Server-188 ~]# iptables -A INPUT -p tcp -m multiport --dport 22,80 -j ACCEPT
 4 # 匹配端口范围
 5 [root@Server-188 ~]# iptables -A INPUT -p tcp -m multiport --dport 1024:2048 -j ACCEPT
 6 
 7 # iprange 匹配IP地址
 8 # 匹配IP范围 # --src-range来源IP
 9 [root@Server-188 ~]# iptables -A INPUT -p tcp -m iprange --src-range 10.0.8.100-10.0.8.200 -j ACCEPT
10 # 匹配整个网段 # --dst-range目标IP
11 [root@Server-188 ~]# iptables -A INPUT -p tcp -m iprange --dst-range 10.0.8.0 -j ACCEPT
12 
13 # string 匹配字符串
14 # 匹配算法:kmp、bm
15 [root@Server-188 ~]# iptables -A OUTPUT -p tcp -m string --algo kmp --string "baidu" -j DROP
16 # 备注:“baidu”为匹配的对象
17 
18 # connlimit 单个IP并发链接数限制
19 [root@Server-188 ~]# iptables -A INPUT -p tcp --dport 80 -m connlimit ! --connlimit-above 3 -j ACCEPT
20 
21 # limit 限速

猜你喜欢

转载自www.cnblogs.com/zhoul/p/9926514.html