From b16c1994524bcb8c6e86ac5c586b8be0edd6a2b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konrad=20Go=C5=82awski?= Date: Mon, 1 Jul 2024 13:10:09 +0200 Subject: [PATCH] WIP --- scapy/layers/inet.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/scapy/layers/inet.py b/scapy/layers/inet.py index ed8ce7e2ae9..76e2edd1a3c 100644 --- a/scapy/layers/inet.py +++ b/scapy/layers/inet.py @@ -1003,10 +1003,12 @@ def __init__(self): def getfield(self, pkt, s): # RFC4884 section 5.2 says if the ICMP packet length - # is >144 then ICMP extensions start at byte 137. + # is >144 then ICMP extensions start at least at byte 137. if len(pkt.original) < 144: return s, None - offset = 136 + len(s) - len(pkt.original) + if pkt.length == 0: + return s, None + offset = pkt.length * 4 # original padded datagram len data = s[offset:] # Validate checksum if checksum(data) == data[3:5]: @@ -1014,17 +1016,6 @@ def getfield(self, pkt, s): # Dissect return s[:offset], ICMPExtension_Header(data) - def addfield(self, pkt, s, val): - if val is None: - return s - data = bytes(val) - # Calc how much padding we need, not how much we deserve - pad = 136 - len(pkt.payload) - len(s) - if pad < 0: - warning("ICMPExtension_Header is after the 136th octet of ICMP.") - return data - return super(_ICMPExtensionField, self).addfield(pkt, s, b"\x00" * pad + data) - class _ICMPExtensionPadField(TrailerField): def __init__(self): @@ -1201,6 +1192,18 @@ class ICMP(Packet): post_dissection = _ICMP_extpad_post_dissection def post_build(self, p, pay): + if self.ext is not None and self.extpad in [None, b""]: + padding_index = pay.rindex(bytes(self.ext)) + payload_len = len(pay[:padding_index]) + padding_len = (4 - payload_len % 4) % 4 + if payload_len + padding_len < 128: + padding_len = 128 - payload_len + padding = b"\x00" * padding_len + pay = pay[:padding_index] + padding + pay[padding_index:] + if self.ext is not None and self.length in [None, 0]: + ext_index = pay.rindex(bytes(self.ext)) + length = len(pay[:ext_index]) // 4 + p = p[:5] + chb(length) + p[6:] p += pay if self.chksum is None: ck = checksum(p)