如何在 Linux 机器上启用跟踪路由

如何在 Linux 机器上启用跟踪路由

我正在传输层上做一些工作,在我运行了我们的自定义策略来保护策略之后,我无法traceroute从 Linux 机器上执行这些策略。

root@keystone-evm:~# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     udp  --  anywhere             10.222.4.212         udp dpt:echo
ACCEPT     udp  --  anywhere             10.222.4.212         udp dpt:isakmp
ACCEPT     udp  --  anywhere             10.222.4.212         udp dpt:radius
ACCEPT     udp  --  anywhere             10.222.4.212         udp dpt:ntp
ACCEPT     icmp --  anywhere             10.222.4.212
ACCEPT     udp  --  anywhere             10.222.4.212         udp dpt:domain
ACCEPT     udp  --  anywhere             10.222.4.212         udp dpt:bootpc
ACCEPT     udp  --  anywhere             10.222.4.212         udp dpt:bootps
ACCEPT     123  --  anywhere             10.222.4.212
DROP       all  --  anywhere             anywhere
ACCEPT     udp  --  anywhere             anywhere             udp spts:33434:33524 state NEW,RELATED,ESTABLISHED

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     udp  --  10.222.4.212         anywhere             udp dpt:echo
ACCEPT     udp  --  10.222.4.212         anywhere             udp dpt:isakmp
ACCEPT     udp  --  10.222.4.212         anywhere             udp dpt:radius
ACCEPT     udp  --  10.222.4.212         anywhere             udp dpt:ntp
ACCEPT     icmp --  10.222.4.212         anywhere
ACCEPT     udp  --  10.222.4.212         anywhere             udp dpt:domain
ACCEPT     udp  --  10.222.4.212         anywhere             udp dpt:bootpc
ACCEPT     udp  --  10.222.4.212         anywhere             udp dpt:bootps
ACCEPT     123  --  10.222.4.212         anywhere
DROP       all  --  anywhere             anywhere
ACCEPT     udp  --  anywhere             anywhere             udp dpts:33434:33524 state NEW
root@keystone-evm:~# traceroute 10.222.4.100
traceroute to 10.222.4.100 (10.222.4.100), 30 hops max, 38 byte packets
 1traceroute: sendto: Operation not permitted

下面给出的是启用跟踪路由时我发出的命令:

  • iptables -A OUTPUT -o eth0 -p udp --dport 33434:33524 -m state --state NEW -j ACCEPT
  • iptables -A INPUT -p udp --sport 33434:33524 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT

答案1

我们可以看到man 8 traceroute那:

  • UDP 是 Linux 上的默认跟踪路由机制
  • traceroute期望收到“ICMP 不可达”消息作为查询的响应
  • 跟踪从端口 33434 开始,每跳增加一

与此同时,微软确认Windows 使用“ICMP 回显请求”在其实施过程中。

因此,以下是允许主机正确处理入站和执行出站跟踪路由的答案。附加一条规则以拒绝(而不是丢弃)UDP 端口 33434-33474 上的流量,并回复回显请求,并且如果您限制出站流量,则也允许匹配的出站数据包。

# reject (not drop) packets for inbound traceroutes from Linux boxes
iptables -I INPUT -p udp --dport 33434:33474 -j REJECT

# accept ping requests for Windows-style traceroutes
iptables -I INPUT -p ICMP --icmp-type echo-request -j ACCEPT

# allow ping responses for Windows-style traceroutes
iptables -I OUTPUT -p ICMP --icmp-type echo-reply -j ACCEPT

# allow the server to perform its own traceroutes
iptables -I OUTPUT -p udp --dport 33434:33474 -j ACCEPT

作为记录,以下是手册页的摘录:

LIST OF AVAILABLE METHODS
       In  general,  a  particular traceroute method may have to be chosen by -M name, but
       most of the methods have their simple cmdline switches (you can see them after  the
       method name, if present).

   default
       The traditional, ancient method of tracerouting. Used by default.

       Probe  packets  are udp datagrams with so-called "unlikely" destination ports.  The
       "unlikely" port of the first probe is 33434, then for each next probe it is  incre-
       mented by one. Since the ports are expected to be unused, the destination host nor-
       mally returns "icmp unreach port" as a final response.  (Nobody knows what  happens
       when some application listens for such ports, though).

       This method is allowed for unprivileged users.

   icmp       -I
       Most usual method for now, which uses icmp echo packets for probes.
       If you can ping(8) the destination host, icmp tracerouting is applicable as well.

   tcp        -T
       Well-known modern method, intended to bypass firewalls.
       Uses the constant destination port (default is 80, http).

答案2

感谢大家的意见。

我想出了一个 shell 脚本来帮我完成这项工作。我相信这对其他用户执行任务也会有帮助。请注意本地机器 IP。请相应地进行必要的更改。

#!/bin/sh
echo "Enabling Traceroute..."

#Outbound UDP traffic Policy

iptables -I OUTPUT -o eth0 -p udp --dport 33434:33524 -m state --state NEW -j ACCEPT

iptables -I INPUT -p udp --sport 33434:33524 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT

#Inbound ICMP traffic Policy


iptables -I INPUT -p icmp --icmp-type 3/3 -d 10.222.4.212 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT

iptables -I INPUT -p icmp --icmp-type 11  -d 10.222.4.212 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT

答案3

首先:iptables -A命令添加新规则实际链的末尾。它们仅在链中的最后一条规则之后被处理。但这不会发生,因为最后一条规则已经过滤掉了所有内容!你需要把这些命令您的最后一条规则,可以使用-I <n>iptables 标志来完成。

第二:Traceroute 的工作方式与 ping 类似,都是通过发送 ICMP 数据包。本质上,它是一种 ping,通过发送数据包 TTL 字段较低但不断增长的数据包,尝试获取通往目标计算机的远程网络节点列表。

我不知道您从哪里得到这个 udp/33434 东西。如果您想要跟踪路由,请启用没有任何端口的 ICMP。

第三:(反应通信)看来,有时 traceroute 不仅使用简单的 icmp 数据包,还使用 ​​udp 甚至 tcp 数据包。甚至有一个名为的工具tcptraceroute,它可以以非常好的可配置方式完成最后一件事。如果您不确定,请使用strace或 进行检查tcpdump,您的跟踪路由实际上想要通信的位置,并至少启用此端口。

相关内容