简体   繁体   English

如何使用 Scapy 提取 TCP 数据包的原始数据

[英]How to extract Raw of TCP packet using Scapy

I use the sniff function of scapy module.我使用 scapy 模块的sniff功能。 My filter and prn function are doing a great job.我的filterprn功能做得很好。 But now, I would like to extract the Raw of the TCP packet and handle it using hexadecimal or binary format.但是现在,我想提取TCP packetRaw并使用十六进制或二进制格式处理它。

Here is the documentation of Packet Class in scapy.这是 scapy 中 Packet Class 的文档

How can I do that ?我怎样才能做到这一点 ?

I tried print packet[Raw] but it seems to be converted as ASCII or something like that.我尝试了print packet[Raw]但它似乎被转换为 ASCII 或类似的东西。 I want to keep it in hexadecimal or binary.我想将其保存为十六进制或二进制。

You can get the raw bytes of the packet using scapy.compat.raw 1 :您可以使用scapy.compat.raw 1获取数据包的原始字节:

from scapy.all import raw
raw(packet)

The former is cross-version compatible, but if you are guaranteed to run with Python 3 and support for Python 2 is not needed, you can simply invoke bytes , which doesn't require an ad-hoc import statement (and is actually how scapy.compat.raw is implemented behind the scenes):前者是跨版本兼容的,但如果你保证使用Python 3运行并且不需要对Python 2的支持,你可以简单地调用bytes ,这不需要临时导入语句(实际上是多么scapy.compat.raw在幕后实现):

bytes(packet)

You can print the raw bytes of the packet in a readable format using scapy.compat.bytes_hex 2 :您可以使用scapy.compat.bytes_hex 2以可读格式打印数据包的原始字节:

from scapy.all import bytes_hex
print(bytes_hex(packet))

1 scapy.compat.raw 's implementation can be found here . 1 scapy.compat.raw的实现可以在这里找到。
2 scapy.compat.bytes_hex 's implementation can be found here . 2 scapy.compat.bytes_hex的实现可以在这里找到。

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

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