问题
I'm using scapy to read and pretty print a trace file. The PDUs I'm reading (My_Packet) have the format:
| header | payload | crc |
--------------------------
The payload field contains another PDU inside. I'm using the following code to strip the crc part, but unfortunately something does not work as expected (I cannot figure out what's wrong).
#!/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")))
The runtime error is:
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 version: Version git-archive.dev9fc949739
Python version: Python 2.7.12
来源:https://stackoverflow.com/questions/48763072/scapy-getting-trailer-field-in-the-dissector