Linux学习Day11:iptables防火墙管理工具

  防火墙作为公网与内网之间的保护屏障,在保障数据安全性方面起着至关重要的作用。RHEL 6和之前的系统都采用iptables作为配置防火墙策略的工具,但是在RHEL7系统中,friewalld防火墙取代了iptables防火墙。其实,iptables和firewalld都不算是真正的防火墙,它们都只是用来定义防火墙策略的管理工具而已。区别就是iptables服务会把配置好的防火墙策略交由内核层面的netfilter网络过滤器来处理,而firewalld服务则是交由内核层面的nftables包过滤框架来处理。

 

一、iptables


  考虑到大量的企业在生产环境中依然使用iptables作为防火墙管理工具,所以还是要好好掌握iptables的相关知识,在学习其他防火墙知识的时候也有借鉴意义。

  1、策略与规则

  首先,把用于处理和过滤流量的策略条目称之为规则,多条规则可以组成一个规则链。防火墙会按照从上至下的顺序来读取配置的策略条目,在找到了匹配项后就立即结束匹配工作,并去执行匹配策略中定义的行为(即放行或阻止)。如果在读取完所有定义的规则之后,仍然没有找到匹配项,就去执行默认的策略。

  一般来说,默认的策略规则有两种:一种是""(即放行),另一种是""(即阻止)。当防火墙的默认规则设置为"堵"时,就要设置放行的规则,否则谁也进不来;当默认规则设置为"通"时,就要设置阻止的规则,否则谁都能进来。规则根据功能的不同也分为几大类:

  •   在进行路由选择之前处理数据包(PREROUTING);
  •   处理流入的数据包(INPUT);
  •   处理流出的数据包(OUTPUT);
  •   处理转发的数据包(FORWARD);
  •   在进行路由选择后处理数据包(POSTROUTING)。

  一般来说,企业都是对流入的数据包进行处理,而从内网向外网发送的流量一般是可控且良性的,因此我们使用最多的是INPUT规则链。光有规则还不够,还需要对成功匹配这些规则的流量作进一步的处理,比如ACCEPT(允许流量通过)、REJECT(拒绝流量通过)、LOG(记录日志信息)、DROP(拒绝流量通过)。这里讲解一下REJECT和DROP的区别,REJECT会在拒绝流量的同时回复一条"您的信息收到了,但是被拒绝了"信息,DROP则会将流量直接丢弃而且不响应。

[root@linuxprobe ~]# ping -c 4 192.168.10.10
PING 192.168.10.10 (192.168.10.10) 56(84) bytes of data.
From 192.168.10.10 icmp_seq=1 Destination Port Unreachable     //流量被拒绝(REJECT)
From 192.168.10.10 icmp_seq=2 Destination Port Unreachable
From 192.168.10.10 icmp_seq=3 Destination Port Unreachable
From 192.168.10.10 icmp_seq=4 Destination Port Unreachable
--- 192.168.10.10 ping statistics ---
4 packets transmitted, 0 received, +4 errors, 100% packet loss, time 3002ms
-------------------------------------分割线-----------------------------------------------
[root@linuxprobe ~]# ping -c 4 192.168.10.10
PING 192.168.10.10 (192.168.10.10) 56(84) bytes of data.
                                                              //流量被丢弃(DROP)或者主机不在线
--- 192.168.10.10 ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 3000ms

   2、基本的命令参数

   iptables命令可以根据流量的源地址、目的地址、传输协议、服务类型等信息来进行匹配,一旦匹配成功,iptables就会根据策略规则所预设的动作来处理这些流量。另外,防火墙的策略规则是按照从上至下的顺序,因此要把较为严格、优先级高的策略规则放在最前面。iptables命令常用的参数以及作用如下所示:

参数 作用
-P 设置默认策略
-F 清空规则链
-L 查看规则链
-A 在规则链的末尾加入新规则
-I num 在规则链的头部加入新规则
-D num 删除某一条规则
-s 匹配来源地址IP/MASK,加叹号“!”表示除这个IP外
-d 匹配目标地址
-i 网卡名称 匹配从这块网卡流入的数据
-o 网卡名称 匹配从这块网卡流出的数据
-p 匹配协议,如TCP、UDP、ICMP
--dport num 匹配目标端口号
--sport num 匹配来源端口号

   3、配置防火墙策略

   (1)查看已有的防火墙规则链

[root@linuxprobe ~]# iptables -L
Chain INPUT (policy ACCEPT)        //处理输入流量的规则链,默认策略为"放行"
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
ACCEPT     all  --  anywhere             anywhere            
INPUT_direct  all  --  anywhere             anywhere            
INPUT_ZONES_SOURCE  all  --  anywhere             anywhere            
INPUT_ZONES  all  --  anywhere             anywhere            
ACCEPT     icmp --  anywhere             anywhere            
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited
-----------------------省略部分输出内容----------------------------------------

   (2)清空已有的防火墙规则链

[root@linuxprobe ~]# iptables -F      //清空所有规则链
[root@linuxprobe ~]# iptables -L        //再次查看防火墙规则链
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
                                                         //所有的规则链都被清空
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 
-----------------------省略部分输出内容-----------------------

  (3)把INPUT规则链的默认策略改为"拒绝"  

[root@linuxprobe Desktop]# iptables -P INPUT DROP       //注意:不能写成drop,严格区分大小写
[root@linuxprobe Desktop]# iptables -L
Chain INPUT (policy DROP)     //默认策略变更为DROP
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 
-------------------------省略部分输出内容-----------------------

  知识补充:规则链的默认拒绝动作只能是DROP,而不能是REJECT。

  (4)向INPUT规则链中添加允许ICMP流量通过的策略规则

[root@linuxprobe ~]# ping 192.168.134.10
PING 192.168.134.10 (192.168.134.10) 56(84) bytes of data.
^C
--- 192.168.134.10 ping statistics ---
32 packets transmitted, 0 received, 100% packet loss, time 31012ms

[root@linuxprobe ~]# iptables -I INPUT -p icmp -j ACCEPT      //-I INPUT表示在INPUT规则链头部添加一条新的规则,-j参数后面接处理的动作
[root@linuxprobe ~]# ping 192.168.134.10
PING 192.168.134.10 (192.168.134.10) 56(84) bytes of data.
64 bytes from 192.168.134.10: icmp_seq=1 ttl=64 time=0.032 ms
64 bytes from 192.168.134.10: icmp_seq=2 ttl=64 time=0.116 ms
64 bytes from 192.168.134.10: icmp_seq=3 ttl=64 time=0.088 ms
^C
--- 192.168.134.10 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.032/0.078/0.116/0.036 ms

  (5)删除一条INPUT规则链中的规则,并把默认策略设置为ACCEPT

[root@linuxprobe ~]# iptables -L --line-number      //--line-number参数显示规则的序号
Chain INPUT (policy DROP)
num  target     prot opt source               destination         
1    ACCEPT     icmp --  anywhere             anywhere      //该规则的序号为1       

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination  
------------------------------省略部分输出内容------------------------
[root@linuxprobe ~]# iptables -D INPUT 1       //删除序号为1的规则
[root@linuxprobe ~]# iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination         
                                                     //规则删除成功
Chain FORWARD (policy ACCEPT)
target     prot opt source               destination   
 ---------------------------省略部分输出内容--------------------------     
[root@linuxprobe ~]# iptables -P INPUT ACCEPT
[root@linuxprobe ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination 
-----------------------省略部分输出信息----------------------

  (6)将INPUT规则链设置为只允许指定网段的主机访问本机的22端口,并拒绝其他所有主机的流量

[root@linuxprobe ~]# iptables -I INPUT -s 192.168.134.0/24 -p tcp --dport 22 -j ACCEPT
[root@linuxprobe ~]# 
[root@linuxprobe ~]# iptables -A INPUT -p tcp --dport 22 -j REJECT      //注意这里用的-A参数,在规则链末尾添加规则
[root@linuxprobe ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  192.168.134.0/24     anywhere             tcp dpt:ssh       //允许动作在前
REJECT     tcp  --  anywhere             anywhere             tcp dpt:ssh reject-with icmp-port-unreachable     //拒绝动作在后
-------------------------省略部分输出信息------------------------------------

   知识补充:再次重申,允许动作一定要放到拒绝动作的前面,否则所有流量都会被拒绝掉。

   (7)向INPUT规则链中添加拒绝所有人访问本机的12345端口的策略规则

[root@linuxprobe ~]# iptables -I INPUT -p tcp --dport 12345 -j REJECT     //拒绝TCP协议
[root@linuxprobe ~]# iptables -I INPUT -p udp --dport 12345 -j REJECT     //拒绝UDP协议
[root@linuxprobe ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
REJECT     udp  --  anywhere             anywhere             udp dpt:italk reject-with icmp-port-unreachable
REJECT     tcp  --  anywhere             anywhere             tcp dpt:italk reject-with icmp-port-unreachable
ACCEPT     tcp  --  192.168.134.0/24     anywhere             tcp dpt:ssh
REJECT     tcp  --  anywhere             anywhere             tcp dpt:ssh reject-with icmp-port-unreachable
--------------------------省略部分输出内容---------------------------------------------

  (8)向INPUT规则链中添加拒绝192.168.10.5主机访问本机80端口的策略规则

[root@linuxprobe ~]# iptables -I INPUT -s 192.168.10.5 -p tcp --dport 80 -j REJECT
[root@linuxprobe ~]# iptables -L --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     tcp  --  192.168.10.5         anywhere             tcp dpt:http reject-with icmp-port-unreachable
2    REJECT     tcp  --  192.168.10.5         anywhere             tcp dpts:ftp-data:ssh reject-with icmp-port-unreachable
3    REJECT     udp  --  anywhere             anywhere             udp dpt:italk reject-with icmp-port-unreachable
4    REJECT     tcp  --  anywhere             anywhere             tcp dpt:italk reject-with icmp-port-unreachable
5    ACCEPT     tcp  --  192.168.134.0/24     anywhere             tcp dpt:ssh
6    REJECT     tcp  --  anywhere             anywhere             tcp dpt:ssh reject-with icmp-port-unreachable
-----------------------------省略部分输出内容----------------------------------------

 

   (9)向INPUT规则链中添加拒绝所有主机访问本机1000~1024端口的策略规则

[root@linuxprobe ~]# iptables -I INPUT -p tcp --dport 1000:1024 -j REJECT      //连续的端口号可以用冒号
[root@linuxprobe ~]# iptables -I INPUT -p udp --dport 1000:1024 -j REJECT
[root@linuxprobe ~]# iptables -L --line-number
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     udp  --  anywhere             anywhere             udp dpts:cadlock2:1024 reject-with icmp-port-unreachable
2    REJECT     tcp  --  anywhere             anywhere             tcp dpts:cadlock2:1024 reject-with icmp-port-unreachable
3    REJECT     tcp  --  192.168.10.5         anywhere             tcp dpt:ssh reject-with icmp-port-unreachable
4    REJECT     tcp  --  192.168.10.5         anywhere             tcp dpt:http reject-with icmp-port-unreachable
---------------------------------------------省略部分输出内容----------------------------------

 二、总结


  iptables命令的知识就介绍到这里了,特别要注意的是,使用iptables命令配置的防火墙策略默认会在系统重启后失效,如果想让配置的防火墙策略永久生效,还需要执行下面的保存命令:

[root@linuxprobe ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]

   不过我的实验结果是:即使执行了上述命令,配置的防火墙策略依然重启后失效

 

 

posted @ 2020-03-15 14:40  黑色幽默2020  阅读(305)  评论(0编辑  收藏  举报