如何在不发送数据包的情况下计算数据包校验和?
我正在使用 scapy,我想创建一个数据包并计算其校验和而不发送它。有办法做到吗?
谢谢。
I'm using scapy, and I want to create a packet and calculate its' checksum without sending it. Is there a way to do it?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我还尝试避免 show2() 因为它打印数据包。
我在源代码中找到了一个更好的解决方案:
此代码使用正确的校验和重新生成数据包,无需任何打印,实际上是 show2() 在打印之前在后台运行的内容。
I've also tried to avoid show2() because it print the packet.
I've found in the source a better solution:
This code regenerate the packet with the correct checksum without any print and actually is what show2() run in the background before printing.
创建数据包后,您需要从数据包中删除
.chksum
值;然后调用.show2()
You need to delete the
.chksum
value from the packet after you create it; then call.show2()
将此补丁添加到 scapy/packet.py:
然后,只需调用此函数,而不是调用
pkt.show2()
pkt.checksum_silent()
。 (记得先做del pkt[IP].chksum
和del pkt[UDP].chksum
等)如上图所示回答。这个功能应该更快并且安静。 (可能还有其他东西需要修剪;我将这段代码拼凑在一起,仅进行测试以确保它在正确的校验和下保持沉默。)
Add this patch to scapy/packet.py:
Then instead of calling
pkt.show2()
, just call this functionpkt.checksum_silent()
. (Remember to first dodel pkt[IP].chksum
anddel pkt[UDP].chksum
, etc.) as shown in the previous answer.This function should be faster and be silent. (There may be additional things to trim as well; I hacked this code together and only tested to make sure it was silent with correct checksum.)
事实上,show2() 函数会为您计算校验和,但它也会在完成工作后打印数据包的内容。不过,
show2()
有一个有用的小参数,名为dump
。消息来源是这样描述的:因此,通过设置 dump=True,您可以避免该函数默认提供的令人讨厌的输出,并且仍然可以获得您想要的计算。
Indeed, the
show2()
function calculates the checksum for you, but it also prints the contents of the packet once it is finished with its work. However,show2()
has a helpful little parameter nameddump
. The source describes it as such:So by setting
dump=True
, you can avoid the pesky output that the function provides by default, and still get the calculations that you want.您还可以使用
packet.build()
返回带有正确校验和的原始字节。然后将字节转换为数据包。You can also use
packet.build()
which returns raw bytes with correct checksum. Then convert the bytes to a packet.