Struct 将嗅探到的scapy数据包转换为字节

Struct 将嗅探到的scapy数据包转换为字节,struct,packet,scapy,unpack,Struct,Packet,Scapy,Unpack,当使用scapy嗅探数据包时,我可以将它们保存到变量中 sniffed = sniff(count=1) 现在我想看看包里有什么 print sniffed 或 但这一切给了我如下的启示: ������0� E4h@@����������� l�� 这不是我所需要的。那么,我如何才能将嗅探到的数据包转换成人类可读的二进制文件、字节数组或其他更有用的东西,以便能够看到里面的内容呢?我已经尝试过使用struct.unpack(format,packet)和“!B”等格式,但这似乎不是正确

当使用scapy嗅探数据包时,我可以将它们保存到变量中

sniffed = sniff(count=1)
现在我想看看包里有什么

print sniffed

但这一切给了我如下的启示:

������0�    E4h@@����������� l��
这不是我所需要的。那么,我如何才能将嗅探到的数据包转换成人类可读的二进制文件、字节数组或其他更有用的东西,以便能够看到里面的内容呢?我已经尝试过使用
struct.unpack(format,packet)
“!B”
等格式,但这似乎不是正确的解决方案,因为数据包可能长于一个字节、短于一个字节或整数


我正在尝试的例子

>>> packet = sniff(count=1)[0]
>>> hexdump(packet)
0000   00 50 56 8E 00 0D 14 CC  20 16 E7 59 08 00 45 00   .PV..... ..Y..E.
0010   00 34 6B AB 40 00 40 06  C6 48 AC 11 8A E2 68 10   .4k.@.@..H....h.
0020   69 CC B5 47 00 50 E9 85  17 B0 BA EF 29 B2 80 10   i..G.P......)...
0030   01 DD 8D 58 00 00 01 01  08 0A 00 0E A2 C0 03 5D   ...X...........]
0040   9D 1C 
>>> packetByteArray = bytearray(repr(str(packet)))
>>> hex(packetByteArray[0])
'0x27'
>>>

但是在hextump中,我可以看到第一个字节实际上是
0x00
,而不是
0x27

,您可能正在搜索或
repr(str(pkt))
,以获得字符串编码的输出。注意,sniff返回一个列表,而不是一个pkt

如果您想逐个访问序列化的数据包字节,只需序列化层
str(pkt)
即可获得python(char/byte)-字符串


如果您已将数据包读取为pkt,则可能会按时间看到字节:

pktBytes=[]
pktTimes=[]
from datetime import datetime
#Read each packet and append to the lists.
for p in pkt:
    if IP in p:
        try:
            pktBytes.append(p[IP].len)
            pktTime=datetime.fromtimestamp(p.time)
            pktTimes.append(pktTime.strftime("%Y-%m-%d %H:%M:%S.%f"))
        except:
            pass

# Convert list to series
bytes = pd.Series(pktBytes).astype(int)

# Convert the timestamp list to a pd date_time with the option “errors=coerce” to handle errors.
times = pd.to_datetime(pd.Series(pktTimes).astype(str),  errors='coerce')

# Build the dataframe, set time as index
df  = pd.DataFrame({'Bytes': bytes, 'Times':times})
df = df.set_index('Times')

# See how it looks in 2 seconds sums
df.resample('2S').sum().plot()

hextump
提供了我所需的信息,但如何轻松访问数据包?比如
firstByteOfPacket=packet[0]
?。我目前正试图通过
packetByteArray=bytearray(repr(str(packet))
实现这一点,然后
firstByteOfPacket=packetByteArray[0]
期望收到数据包的第一个字节。然而,这给了我一些明显不是第一个字节的东西,正如我从
hextump
中看到的那样。我将在问题中添加一个示例。请参阅更新的答案。只需序列化层/数据包并访问任何字节作为
str(pkt)[1]
获取ascii字符或
ord(str(pkt)[1])
获取值。其他一切都只是代表性的问题。是的,这就是我需要的。
for b in str(pkt):
    print "char: %s ord/value: %d hex: %x"%(b,ord(b),ord(b))
pktBytes=[]
pktTimes=[]
from datetime import datetime
#Read each packet and append to the lists.
for p in pkt:
    if IP in p:
        try:
            pktBytes.append(p[IP].len)
            pktTime=datetime.fromtimestamp(p.time)
            pktTimes.append(pktTime.strftime("%Y-%m-%d %H:%M:%S.%f"))
        except:
            pass

# Convert list to series
bytes = pd.Series(pktBytes).astype(int)

# Convert the timestamp list to a pd date_time with the option “errors=coerce” to handle errors.
times = pd.to_datetime(pd.Series(pktTimes).astype(str),  errors='coerce')

# Build the dataframe, set time as index
df  = pd.DataFrame({'Bytes': bytes, 'Times':times})
df = df.set_index('Times')

# See how it looks in 2 seconds sums
df.resample('2S').sum().plot()