我正在使用scapy来阅读和打印跟踪文件。我正在阅读的PDU(My_Packet)具有以下格式:
| header | payload | crc |
--------------------------
有效负载字段包含另一个PDU。我正在使用以下代码去除crc部分,但遗憾的是有些东西没有按预期工作(我无法弄清楚是什么问题)。
#!/usr/bin/env python
from scapy.all import *
class My_Packet(Packet):
'''My_Packet PDU'''
name = "My Packet"
CRC_SIZE = 4
fields_desc=[ LEShortField("len", 0) ]
def __init__(self, *args, **kargs):
"""Build My_Packet"""
self.crc = []
super(My_Packet, self).__init__(*args, **kargs)
def get_crc(self):
"""Get the CRC"""
return self.crc
def post_dissect(self, p):
"""Remove trailer/CRC"""
self.crc = p[-self.CRC_SIZE:]
return p[:self.len - self.CRC_SIZE]
def extract_padding(self, p):
"""Remove trailer for the upper layer"""
return p[:self.len - self.CRC_SIZE], p[self.len - self.CRC_SIZE:]
if __name__ == '__main__':
bind_layers(UDP, My_Packet, sport=5001, dport=5001)
packets = rdpcap("./tcpdump_5501.bin")
# iterate through PDUs
for pkt in packets:
if pkt.haslayer(UDP):
pld = pkt[My_Packet].load
print("PDU: 0x{}".format(pld.encode("hex")))
crc = pkt[My_Packet].get_crc()
print("CRC: 0x{}".format(crc.encode("hex")))
运行时错误是:
Traceback (most recent call last):
File "./so.py", line 41, in <module>
pld = pkt[My_Packet].load
File "/usr/local/lib/python2.7/dist-packages/scapy/packet.py", line 964, in __getitem__
raise IndexError("Layer [%s] not found" % lname)
IndexError: Layer [My_Packet] not found
Scapy版本:版本git-archive.dev9fc949739
Python版本:Python 2.7.12