简体   繁体   English

在linux中处理ping的是什么?

[英]What handles ping in linux?

I want to override/change how linux handles ping icmp echo request packets. 我想覆盖/更改linux处理ping icmp echo请求数据包的方式。 Meaning I want to run my own server to reply to incoming icmp (echo request or other) packets. 意思是我想运行自己的服务器来回复传入的icmp(echo请求或其他)数据包。

But for this to work properly, I guess I need to disable the default ping icmp packets handling from linux. 但为了使其正常工作,我想我需要禁用从linux处理的默认ping icmp数据包。 But I can't figure out how to do this (I don't even know what handles icmp requests... The kernel ? some userspace code ?)... All I find is about dropping icmp echo requests with iptables. 但我无法弄清楚如何做到这一点(我甚至不知道是什么处理icmp请求......内核?一些用户空间代码?)...我发现只是用iptables删除icmp echo请求。

To help understand, let me explain my goal: I want to be able to send some data with ping. 为了帮助理解,让我解释一下我的目标:我希望能够通过ping发送一些数据。 (easy) but I need to be able to read and extract that data. (简单)但我需要能够读取和提取该数据。 also, I want to be able to answer with a special echo-reply (with some data embedded) 另外,我希望能够回答一个特殊的echo-r​​eply(嵌入了一些数据)

To override the default kernel behaviour for a ICMP ECHO request ( ping ) you can do the following without having to poke into the kernel or writting a filter. 要覆盖ICMP ECHO请求( ping )的默认内核行为,您可以执行以下操作,而无需戳入内核或编写过滤器。

  • First: instruct iptables to drop ICMP ECHO requests. 第一:指示iptables删除ICMP ECHO请求。 They will however come to your host and enter your network card, but they won't be answered by the kernel: 然而,他们会来到您的主机并输入您的网卡,但内核无法回答:

    iptables -A INPUT- p icmp --icmp-type 8 -j DROP

  • Second: use tcpdump to sniff over ICMP packets (or write a program that uses libcap to do yourself the capture). 第二:使用tcpdump来嗅探ICMP数据包(或者编写一个使用libcap来自己捕获的程序)。 tcpdump has options to display the payload data, or to write dunmped packets to a file. tcpdump具有显示有效负载数据或将dunmped数据包写入文件的选项。 You can use this last feature to open tcpdump with -w option from your program, connect its output to a pipe and read the pipe. 您可以使用此最后一项功能从程序中打开带有-w选项的tcpdump,将其输出连接到管道并读取管道。 This way, you can access to incoming ICMP echo requests even if they are going to be discarded by iptables. 这样,即使它们将被iptables丢弃,您也可以访问传入的ICMP回应请求。 From your program, you will be able to parse the payload data. 从您的程序中,您将能够解析有效负载数据。

    tcpdump -p icmp -i eth0 -s 0 -Xnlt

    (This is for displaying data in readable human hexadecimal and ASCII on the standard output, change the -X -l options according to write raw data to a file/socket) (这是用于在标准输出上以可读的人类十六进制和ASCII显示数据,根据将原始数据写入文件/套接字来更改-X -l选项)

  • Third: using raw sockets, your program can send a customized packet pretending to be a response to a previous ICMP echo request, with the payload you desire. 第三:使用原始套接字,您的程序可以发送一个自定义数据包,假装是对先前ICMP回应请求的响应,具有您想要的有效负载。 This SO question may have more clues for you in this field: How to receive ICMP request in C with raw sockets 这个问题可能在这个领域有更多线索: 如何在C中使用原始套接字接收ICMP请求

Sorry for commenting on an already-answered question, but I'd like to add (for googlers maybe) that blocking ICMP packets with iptables won't allow you to read them with RAW IP sockets, because iptables works at IP layer. 很抱歉对已经回答的问题发表评论,但我想补充(对于googlers来说)阻止使用iptables的ICMP数据包将不允许您使用RAW IP套接字读取它们,因为iptables在IP层工作。 You might be able to do it with RAW ethernet sockets tho, but it's an objectively worse approach. 您可以使用RAW以太网套接字来实现它,但这是客观上更糟糕的方法。

The recommended way to disable the icmp packet handling by the kernel is via the /proc/sys/net/ipv4/icmp_echo_ignore_all interface: 禁用内核处理icmp数据包的推荐方法是通过/proc/sys/net/ipv4/icmp_echo_ignore_all接口:

echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all

This will allow ICMP packets to pass and be captured, but won't be answered automatically. 这将允许ICMP数据包通过并被捕获,但不会自动应答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM