Python 中的数据包嗅探 (Windows)
使用 Python 嗅探网络数据包的最佳方法是什么?
我从几个地方听说,最好的模块是一个名为 Scapy 的模块,不幸的是,它使 python.exe 在我的系统上崩溃。 我认为这只是我安装方式的问题,除了许多其他人告诉我它在 Windows 上运行得不太好。 (如果有人感兴趣,我正在运行 Windows Vista,这可能会影响一些事情)。
有谁知道更好的解决方案?
UPD:
在阅读了告诉我安装 PyPcap 的答案后,我稍微搞了一下,发现我尝试使用的 Scapy 也告诉我安装 PyPcap,只不过它是一个针对其使用的修改版本。 显然,正是这个修改后的 PyPcap 导致了问题,因为答案中的示例也导致了挂起。
我安装了 PyPcap 的原始版本(来自 Google 的网站),并且 Scapy 开始正常工作(我没有尝试很多东西,但至少在我开始嗅探时它没有崩溃)。 我向 Scapy 开发人员发送了一张新的缺陷票: http://trac.secdev.org/scapy/ Ticket/166,希望他们能用它做点什么。
不管怎样,我只是想让你们知道。
What is the best way to sniff network packets using Python?
I've heard from several places that the best module for this is a module called Scapy, unfortunately, it makes python.exe crash on my system. I would assume that it's just a problem with how I installed it, except that many other people have told me that it doesn't work particularly well on Windows. (If anyone is interested, I'm running Windows Vista, which might affect things).
Does anyone know of a better solution?
UPD:
After reading the answer telling me to install PyPcap, I messed around with it a bit and found out that Scapy, which I had tried using, was telling me to install PyPcap as well, except that it's a modified version for it's use. It was this modified PyPcap that was causing the problem, apparently, since the example in the answer also caused a hang.
I installed the original version of PyPcap (from Google's site), and Scapy started working fine (I didn't try many things, but at least it didn't crash as soon as I started sniffing). I sent a new defect ticket to the Scapy developers: http://trac.secdev.org/scapy/ticket/166, hope they can do something with it.
Anyways, just thought I'd let y'all know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
困难的方法
您可以使用原始套接字嗅探所有 IP 数据包。
原始套接字是一种以二进制形式发送和接收数据的套接字。
python 中的二进制表示为一个字符串,如下所示
\x00\xff
...每个\x..
都是一个字节。要读取IP数据包,您需要根据IP协议分析接收到的二进制数据包。
这是 IP 协议格式的图像,其中包含每个标头的大小(以位为单位)。
本教程可能会帮助您了解理解原始数据包并将其拆分为标头的过程:http://www.binarytides.com/python-packet-sniffer-code-linux/
简单方法
另一种非常轻松地嗅探 IP 数据包的方法是使用 scapy 模块。
此代码将为您打印每个 IP 数据包的源 IP 和目标 IP。
您可以通过阅读此处的文档来使用 scapy 做更多事情:http://www.scapy. secdev.org/projects/scapy/doc/usage.html
这取决于您想要实现的目标,但如果您需要构建一个项目,其功能是嗅探 IP 数据包,那么我建议使用 scapy更稳定的脚本。
The hard way
You can sniff all of the IP packets using a raw socket.
Raw socket is a socket the sends and receives data in binary.
Binary in python is represented in a string which looks like this
\x00\xff
... every\x..
is a byte.To read an IP packet you need to analyze the received packet in binary according to the IP protocol.
This is and image of the format of the IP protocol with the sized in bits of every header.
This tutorial might help you understand the proccess of understanding a raw packet and splitting it to headers: http://www.binarytides.com/python-packet-sniffer-code-linux/
The easy way
Another method to sniff IP packets very easily is to use the scapy module.
This code will print for you the source IP and the destination IP for every IP packet.
You can do much more with scapy by reading it's documentation here: http://www.secdev.org/projects/scapy/doc/usage.html
It depends on the goal you are trying to achieve but if you need to build a project the one it's features is sniffing IP packets then I recommend to use scapy for more stable scripts.
使用 pypcap:
输出示例:
Using pypcap:
output sample:
使用 python-libpcap。
Use python-libpcap.
您可以使用原始套接字和您的接口 IP 地址(在管理模式下),
you can use raw sockets, with your interface ip address (in admin mode),
如果是scapy,请尝试以下方法。 (适用于 Windows 10)
If scapy, pleae try the following method. (It works on Windows 10)
另一种选择是 pypcap。
要解析结果,Construct 非常流畅。
Another option is pypcap.
To parse the results, Construct is very slick.