Skip to content

Commit

Permalink
DNS: add the DNS COOKIE EDNS(0) option
Browse files Browse the repository at this point in the history
https://datatracker.ietf.org/doc/html/rfc7873#section-4

The patch was cross-checked with Wireshark:
```
tdecode(Ether()/IPv6()/UDP()/DNS(qd=[], ar=[DNSRROPT(rdata=[EDNS0COOKIE(client_cookie=b'\x01'*8, server_cookie=b'\x02'*16)])]))
...
Data length: 28
Option: COOKIE
    Option Code: COOKIE (10)
    Option Length: 24
    Option Data: 010101010101010102020202020202020202020202020202
    Client Cookie: 0101010101010101
    Server Cookie: 02020202020202020202020202020202
```
  • Loading branch information
evverx authored and gpotter2 committed Jan 1, 2024
1 parent ba7ff8c commit 084400f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
13 changes: 13 additions & 0 deletions scapy/layers/dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
StrField,
StrLenField,
UTCTimeField,
XStrFixedLenField,
XStrLenField,
)
from scapy.sendrecv import sr1
from scapy.supersocket import StreamSocket
Expand Down Expand Up @@ -559,6 +561,16 @@ class EDNS0ClientSubnet(_EDNS0Dummy):
length_from=lambda p: p.source_plen))]


class EDNS0COOKIE(_EDNS0Dummy):
name = "DNS EDNS0 COOKIE"
fields_desc = [ShortEnumField("optcode", 10, edns0types),
FieldLenField("optlen", None, length_of="server_cookie", fmt="!H",
adjust=lambda pkt, x: x + 8),
XStrFixedLenField("client_cookie", b"\x00" * 8, length=8),
XStrLenField("server_cookie", "",
length_from=lambda pkt: max(0, pkt.optlen - 8))]


# RFC 8914 - Extended DNS Errors

# https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#extended-dns-error-codes
Expand Down Expand Up @@ -612,6 +624,7 @@ class EDNS0ExtendedDNSError(_EDNS0Dummy):
6: EDNS0DHU,
7: EDNS0N3U,
8: EDNS0ClientSubnet,
10: EDNS0COOKIE,
15: EDNS0ExtendedDNSError,
}

Expand Down
27 changes: 27 additions & 0 deletions test/scapy/layers/dns_edns0.uts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,33 @@ d = DNSRROPT(raw_d)
assert EDNS0ClientSubnet in d.rdata[0] and d.rdata[0].family == 2 and d.rdata[0].address == "2001:db8::"


+ EDNS0 - Cookie

= Basic instantiation & dissection

b = b'\x00\n\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00'

p = EDNS0COOKIE()
assert raw(p) == b

p = EDNS0COOKIE(b)
assert p.optcode == 10
assert p.optlen == 8
assert p.client_cookie == b'\x00' * 8
assert p.server_cookie == b''

b = b'\x00\n\x00\x18\x01\x01\x01\x01\x01\x01\x01\x01\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02'

p = EDNS0COOKIE(client_cookie=b'\x01' * 8, server_cookie=b'\x02' * 16)
assert raw(p) == b

p = EDNS0COOKIE(b)
assert p.optcode == 10
assert p.optlen == 24
assert p.client_cookie == b'\x01' * 8
assert p.server_cookie == b'\x02' * 16


+ EDNS0 - Extended DNS Error

= Basic instantiation & dissection
Expand Down

0 comments on commit 084400f

Please sign in to comment.