在 Linux 中阻止/允许 icmp ping 请求

在本文中,将介绍在 Linux 服务器中阻止或者允许传入和传出 icmp ping 请求的不同方法。

阻止来自网络 192.168.1.0/24 中所有服务器对 本地主机 192.168.1.6 的 ICMP ping 请求

[root@test1 ~]# iptables -I INPUT -s 192.168.1.0/24 -p icmp -j DROP

尝试从同一网络的其他机器ping

[root@test ~]# ping 192.168.1.6
PING 192.168.1.6 (192.168.1.6) 56(84) bytes of data.
^C
--- 192.168.1.6 ping statistics --
3 packets transmitted, 0 received, 100% packet loss, time 2949ms

在上面的测试中,发送了 3 个数据包,但没有收到任何数据包。
正如我们在下面看到的,本地主机中的 INPUT 链阻止了 3 个数据包

[root@test1 ~]# iptables -L -v
Chain INPUT (policy ACCEPT 28 packets, 2004 bytes)
 pkts bytes target     prot opt in     out     source               destination
    3   252 DROP       icmp --  any    any     192.168.1.0/24       anywhere
	
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 18 packets, 1688 bytes)
 pkts bytes target     prot opt in     out     source               destination

阻止所有传出的 ping 请求

[root@test1 ~]# iptables -I OUTPUT -d 192.168.1.0/24 -p icmp -j DROP
[root@test1 ~]# ping 192.168.1.2
PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.
ping: sendmsg: Operation not permitted
ping: sendmsg: Operation not permitted
ping: sendmsg: Operation not permitted
^C
--- 192.168.1.2 ping statistics --
3 packets transmitted, 0 received, 100% packet loss, time 2183ms

正如我们在上面看到的,3 个数据包尝试向 192.168.1.0/24 发送 icmp 请求,但正如它所说的“不允许操作”

[root@test1 ~]# iptables -v -L
Chain INPUT (policy ACCEPT 38 packets, 2548 bytes)
 pkts bytes target     prot opt in     out     source               destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
Chain OUTPUT (policy ACCEPT 25 packets, 2688 bytes)
 pkts bytes target     prot opt in     out     source               destination
    3   252 DROP       icmp --  any    any     anywhere             192.168.1.0/24

我们还可以为 icmp 请求类型使用另外的参数

  1. echo-request (ping)
  2. echo-reply (pong)

当我们尝试 ping 服务器时,基本上我们是在向该服务器发送一个回显请求,该服务器一旦收到该请求,它就会回复回显回复。

仅阻止 ESTABLISHED 连接的所有 ICMP 传入流量

# iptables -I INPUT -s 192.168.1.0/24 -p icmp -m state --state ESTABLISHED -j DROP

尝试从同一网络的其他机器ping

[root@test ~]# ping 192.168.1.6
PING 192.168.1.6 (192.168.1.6) 56(84) bytes of data.
64 bytes from 192.168.1.6: icmp_seq=1 ttl=64 time=2.00 ms
^C
--- 192.168.1.6 ping statistics --
4 packets transmitted, 1 received, 75% packet loss, time 3092ms

看到上面有什么不同了吗?

第一个数据包被正确发送,这意味着节点 1 发送了 SYN 信号,节点 2 接受了 SYN/ACK 信号,并将 ACK 信号发送回节点 1(TCP 3 方式握手)。
因此,一旦建立连接,就不允许进一步的 icmp 请求,因为我们看到 4 个数据包中有 1 个成功传输,3 个被丢弃。

[root@test1 ~]# iptables -L -v
Chain INPUT (policy ACCEPT 28 packets, 2121 bytes)
 pkts bytes target     prot opt in     out     source               destination
    3   252 DROP       icmp --  any    any     192.168.1.0/24       anywhere            state ESTABLISHED
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
Chain OUTPUT (policy ACCEPT 18 packets, 1620 bytes)
 pkts bytes target     prot opt in     out     source               destination

我们也可以对其他状态条件执行相同操作

# iptables -I INPUT -s 192.168.1.0/24 -p icmp -m state --state NEW,ESTABLISHED,RELATED -j DROP
状态描述
NEW意味着数据包已启动新连接,或者与两个方向上没有看到数据包的连接相关联
ESTABLISHED意思是数据包与连接在两个方向上的数据包相关联
RELATED意味着数据包正在启动新连接,但与现有连接相关联,例如FTP数据传输或者ICMP错误

使用 OUTPUT 链阻止来自 192.168.1.0/24 的回声请求的所有传出echo-reply

# iptables -I OUTPUT -s 192.168.1.0/24 -p icmp --icmp-type echo-reply -j DROP

现在尝试从同一网络的任何机器ping

[root@test ~]# ping 192.168.1.6
PING 192.168.1.6 (192.168.1.6) 56(84) bytes of data.
^C
--- 192.168.1.6 ping statistics --
3 packets transmitted, 0 received, 100% packet loss, time 2521ms

如我们所见,在 OUTPUT 链中阻止了 3 个数据包

[root@test1 ~]# iptables -L -v
Chain INPUT (policy ACCEPT 26 packets, 1952 bytes)
 pkts bytes target     prot opt in     out     source    destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source    destination
Chain OUTPUT (policy ACCEPT 16 packets, 1504 bytes)
 pkts bytes target   prot opt in   out  source           destination
    3   252 DROP     icmp --  any  any  192.168.1.0/24   anywhere   icmp echo-reply
日期:2020-06-02 22:16:54 来源:oir作者:oir