使用 Scapy 时 ICMP Ping 数据包不会生成回复

发布于 2024-10-04 11:30:35 字数 470 浏览 4 评论 0 原文

我最近开始探索 Scapy。确实是一个很棒的工具!

我有一个问题...当我使用 Wireshark 监控我的网卡并使用标准 PING 安装从系统命令提示符执行常规 ping 操作时,wireshark 会弹出“Ping 请求”,然后显示“Ping 回复”指示它已发送一个答复。但是当我在 Scapy 中手动执行此操作时,它没有发送任何回复。这是怎么回事?我花了很多时间试图解决这个问题,所以我真的希望有人能够阐明我的这个问题...

这是我使用的代码:

>>> from scapy.all import IP, ICMP, send
>>> IP = IP(dst="127.0.0.1")
>>> Ping = ICMP()
>>> send(IP/Ping)

数据包已成功发送,Wireshark 显示收到了 Ping 请求,但不是那个它已发回回复。

I recently began exploring Scapy. A wonderful tool indeed!

I have a problem... When I monitor my network card using Wireshark and I do a regular ping from the systems command prompt with the standard PING installation, wireshark pops up with "Ping request" and then "Ping reply" indication that it sent a reply. But when i do it manually in Scapy, it sends no reply back.. How can this be? I spent alot of time trying to figure this out so i really hope someone can shed some light on this issue of mine...

Here is the code i used:

>>> from scapy.all import IP, ICMP, send
>>> IP = IP(dst="127.0.0.1")
>>> Ping = ICMP()
>>> send(IP/Ping)

The packet is sent successfully and Wireshark shows a Ping request received, but not that it has sent a reply back.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手心的温暖 2024-10-11 11:30:35

这是常见问题解答项

我无法 ping 通 127.0.0.1。 Scapy 不适用于 127.0.0.1 或环回接口

loopback接口是一个非常特殊的接口。通过它的数据包并没有真正被组装和分解。内核将数据包路由到其目的地,同时数据包仍存储在内部结构中。您使用 tcpdump -i lo 看到的只是一个假象,让您认为一切正常。内核并不知道Scapy在背后做了什么,所以你在loopback接口上看到的也是假的。只不过这个不是来自当地的建筑。因此内核永远不会收到它。

为了与本地应用程序通信,您需要使用 PF_INET/SOCK_RAW 套接字而不是 PF_PACKET/SOCK_RAW(或 Linux 其他系统上的等效项)在上一层构建数据包:

<前><代码>>>>配置L3socket
<0xb7bdf5fc 处的类 __main__.L3PacketSocket>
>>>>> conf.L3socket=L3RawSocket
>>>>> sr1(IP(dst="127.0.0.1")/ICMP())
>>

This is an FAQ item:

I can't ping 127.0.0.1. Scapy does not work with 127.0.0.1 or on the loopback interface

The loopback interface is a very special interface. Packets going through it are not really assembled and dissassembled. The kernel routes the packet to its destination while it is still stored an internal structure. What you see with tcpdump -i lo is only a fake to make you think everything is normal. The kernel is not aware of what Scapy is doing behind his back, so what you see on the loopback interface is also a fake. Except this one did not come from a local structure. Thus the kernel will never receive it.

In order to speak to local applications, you need to build your packets one layer upper, using a PF_INET/SOCK_RAW socket instead of a PF_PACKET/SOCK_RAW (or its equivalent on other systems that Linux) :

>>> conf.L3socket
<class __main__.L3PacketSocket at 0xb7bdf5fc>
>>> conf.L3socket=L3RawSocket
>>> sr1(IP(dst="127.0.0.1")/ICMP())
<IP  version=4L ihl=5L tos=0x0 len=28 id=40953 flags= frag=0L ttl=64 proto=ICMP chksum=0xdce5 src=127.0.0.1 dst=127.0.0.1 options='' |<ICMP  type=echo-reply code=0 chksum=0xffff id=0x0 seq=0x0 |>>
烟花肆意 2024-10-11 11:30:35

试试这个

def ping(host, repeat=3):
    packet = IP(dst=host)/ICMP()
    for x in range(repeat):
        response = sr1(packet)
        response.show2()

你没有正确存储回复

Try this

def ping(host, repeat=3):
    packet = IP(dst=host)/ICMP()
    for x in range(repeat):
        response = sr1(packet)
        response.show2()

Your not storing the reply properly

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文