Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure that a valid TCP over DNS message can be decoded #2408

Merged
merged 2 commits into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions scapy/layers/dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,23 @@ def compress(self):
"""Return the compressed DNS packet (using `dns_compress()`"""
return dns_compress(self)

def pre_dissect(self, s):
"""
Check that a valid DNS over TCP message can be decoded
"""
if isinstance(self.underlayer, TCP):
# Compute the length of the DNS packet
dns_len = 0
if len(s) >= 2:
dns_len = struct.unpack("!H", s[:2])[0]
guedou marked this conversation as resolved.
Show resolved Hide resolved

# Check if the length is valid
if dns_len < 14 or len(s) < dns_len:
message = "Malformed DNS message: invalid length!"
warning(message)
raise Scapy_Exception(message)
return s


# https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-4
dnstypes = {
Expand Down
9 changes: 9 additions & 0 deletions test/regression.uts
Original file line number Diff line number Diff line change
Expand Up @@ -7886,6 +7886,15 @@ p = DNS(raw(DNS(id=1,ra=1,an=DNSRR(rrname='secdev', type='TXT', rdata=["sweet",
assert p[DNS].an.rdata == [b"sweet", b"celestia"]
assert raw(p) == b'\x00\x01\x01\x80\x00\x00\x00\x01\x00\x00\x00\x00\x06secdev\x00\x00\x10\x00\x01\x00\x00\x00\x01\x00\x0f\x05sweet\x08celestia'

= DNS - Malformed DNS over TCP message

try:
p = IP(raw(IP()/TCP()/DNS(length=24, qdcount=1)))
assert False
except Scapy_Exception as e:
assert str(e) == "Malformed DNS message: invalid length!"


= Layer binding

* Test DestMACField & DestIPField
Expand Down