From 51f634b653840464bf3760fa1f7b3133868a92b3 Mon Sep 17 00:00:00 2001 From: generaltony007 <191973953+hakarrr@users.noreply.github.com> Date: Fri, 13 Nov 2026 14:20:47 +0530 Subject: [PATCH 1/2] fix(dns): reject compression pointers that target the fixed header Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com> --- impacket/dns.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/impacket/dns.py b/impacket/dns.py index 8220f11bd7..fb82ad3637 100644 --- a/impacket/dns.py +++ b/impacket/dns.py @@ -329,6 +329,13 @@ def parseCompressedMessage(self, buf, offset=0, visited=None): visited.add(offset) pointer = struct.unpack("!H", buf[offset:offset+2])[0] # network unsigned short pointer = (pointer & 0x3FFF) - self.__HEADER_BASE_SIZE + if pointer < 0: + # RFC 1035 4.1.4 pointers are absolute offsets from the start of the DNS + # message; values below the header size point into the fixed header and + # are invalid. Without this check the negative offset is silently + # interpreted from the end of the buffer (Python negative indexing), + # parsing garbage bytes as a name. + raise Exception("Invalid DNS compression pointer: offset %d points inside the DNS header." % (pointer + self.__HEADER_BASE_SIZE)) if offset == pointer: raise Exception("The infinite loop is in DNS decompression. Encountered pointer points to the current offset.") offset += 2 From 35bdbb68e049a5ab4794cb11e65405c18e454b13 Mon Sep 17 00:00:00 2001 From: generaltony007 <191973953+hakarrr@users.noreply.github.com> Date: Fri, 13 Nov 2026 14:21:28 +0530 Subject: [PATCH 2/2] test(dns): regression tests for header-bound compression pointers Co-authored-by: generaltony007 <144252170+GeneralTony007@users.noreply.github.com> --- tests/misc/test_dns.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/misc/test_dns.py b/tests/misc/test_dns.py index 9b0d9b8d7b..6094de6a9a 100644 --- a/tests/misc/test_dns.py +++ b/tests/misc/test_dns.py @@ -19,6 +19,34 @@ def test_parse_compressed_message_rejects_cyclic_pointers(self): with self.assertRaisesRegex(Exception, "infinite loop"): DNS().parseCompressedMessage(b"\xc0\x0e\xc0\x0c") + def test_parse_compressed_message_rejects_pointer_into_header(self): + # RFC 1035 4.1.4 compression pointers are absolute offsets from the start + # of the DNS message. Values below the fixed header size (12) point inside + # the header and are invalid. Before this fix, subtracting + # HEADER_BASE_SIZE produced a negative offset that Python negative + # indexing wrapped to the end of the buffer, silently parsing unrelated + # tail bytes as a name. Verified pre-fix results on master @ 5648c37 with + # the exact 17-byte buffers used below (2 pointer bytes + 15-byte tail): + # 0xC000 -> returns (2, '') (buf[-12] is 0x00, read as root label: empty name) + # 0xC007 -> returns (2, b'ab') (buf[-5] is 0x02, read as label "ab") + # 0xC003 -> returns (2, '') (buf[-9] is 0x00, read as root label: empty name) + # i.e. no exception at all and a wrong name in every case. + tail = b"\x00" * 10 + b"\x02ab" + b"\x00\x00" + for pointer in (b"\xc0\x00", b"\xc0\x07", b"\xc0\x03"): + with self.subTest(pointer=pointer): + with self.assertRaisesRegex(Exception, "Invalid DNS compression pointer"): + DNS().parseCompressedMessage(pointer + tail) + + def test_get_questions_rejects_pointer_into_header(self): + # Full packet: qname is a pointer to absolute offset 0 (inside the header). + # Verified pre-fix on master @ 5648c37: returns [('', 1, 1)] (empty name), no exception. + pkt = (b"\x12\x34\x81\x80\x00\x01\x00\x01\x00\x00\x00\x00" + b"\xc0\x00\x00\x01\x00\x01" + b"\x03www\x07example\x03com\x00" + b"\x00\x01\x00\x01\x00\x00\x00\x3c\x00\x04\x01\x02\x03\x04") + with self.assertRaisesRegex(Exception, "Invalid DNS compression pointer"): + DNS(pkt).get_questions() + def test_str(self): def chk(b, t): self.assertEqual(str(DNS(b)), t)