When looking a packet byte code, how would you identify a dns packet.
The IP header’s protocol field would tell that a UDP frame follows, but inside the UDP frame no protocol field exists to specify what comes next and, from what I can see, there is nothing inside the frame that would uniquely identify it as a dns packet.
When looking a packet byte code, how would you identify a dns packet. The
Share
Other than it being on port 53, there’s a few things you can look out for which might give a hint that you’re looking at DNS traffic.
I will refer to the field names used in §4.1 of RFC 1035 a lot here:
As you can see above the header is 12 bytes long – a 2 byte ID, 2 bytes of flags, and 4 x 2 bytes of counts.
In any DNS packet the
QDCOUNTfield will be exactly one (0x0001). Technically other values are allowed by the protocol, but in practise they are never used.In a query (
QR == 0) theANCOUNTandNSCOUNTvalues will be exactly zero (0x0000), and theARCOUNTwill typically be 0, 1, or 2, depending on whetherEDNS0(RFC 2671)andTSIG(RFC 2845) are being used.RCODEwill also be zero in a query.Responses are somewhat harder to identify, unless you’re observing both sides of the conversation and can correlate each response to the query that triggered it.
Obviously the
QRbit will be set, and as above theQDCOUNTshould still be one. The remaining counters however will have many and varied permutations. However it’s exceedingly unlikely that any of the counters will be greater than 255, so you should be able to rely on bytes 4, 6, 8 and 10 all being zero.Following the headers you’ll start to find resource records, the first one being the actual question that was asked (§4.1.2). The unfortunate part here is that the designers of the protocol saw fit to include a variable length label field (
QNAME) in front of two fixed fields (QTYPEandQCLASS).[To further complicate matters labels can be compressed, using a backwards pointer to somewhere else in the packet. Fortunately you will almost never see a compressed label in the Question Section, since by definition you can’t go backwards from there. Technically a perverse implementor could send a compression pointer back into the header, but that shouldn’t happen].
So, start reading each length byte and then skip that many bytes until you reach a null byte, and then the next two 16 bit words will be
QTYPEandQCLASS. There are very few legal values forQCLASS, and almost all packets will contain the value1forIN("Internet"). You may occasionally see3forCH(Chaos).That’s it for now – if I think of anything else I’ll add it later.