how do I generate dns wire format

387 views Asked by At

I am trying to understand the DNS wire format used in DNS over HTTP:

ref: https://developers.cloudflare.com/1.1.1.1/encryption/dns-over-https/make-api-requests/dns-wireformat/

This page mentions the query for www.example.com:

curl -D - -X GET -H "accept: application/dns-json" "http://127.0.0.1:8553/dns-query?dns=AAABAAABAAAAAAAAA3d3dwdleGFtcGxlA2NvbQAAAQAB"

I want to know how the AAABAAABAAAAAAAAA3d3dwdleGFtcGxlA2NvbQAAAQAB was generated for www.example.com ?

echo "www.example.com" | base64
d3d3LmV4YW1wbGUuY29tCg==


echo "AAABAAABAAAAAAAAA3d3dwdleGFtcGxlA2NvbQAAAQAB" | base64 -d
wwwexamplecom

Clearly, I am missing something here. It appears that the wire format is just not base64 encoded version of the domain name?

1

There are 1 answers

0
Bhakta Raghavan On

I built on the answer/links from Denver's response.

Here is a python snippet to generate the wire format:

import base64
import dns.message

w = "AAABAAABAAAAAAAAA3d3dwdleGFtcGxlA2NvbQAAAQAB"
b = base64.b64decode("AAABAAABAAAAAAAAA3d3dwdleGFtcGxlA2NvbQAAAQAB")
m = dns.message.from_wire(b)
print(str(m))

##The above should print:
#
# id 0
# opcode QUERY
# rcode NOERROR
# flags RD
# ;QUESTION
# www.example.com. IN A
# ;ANSWER
# ;AUTHORITY
# ;ADDITIONAL

# Now generate back the wire format
data = str(m)
wireBytes = dns.message.from_text(data).to_wire()
wire = base64.b64encode(wireBytes)
print(wire.decode("utf-8"))
print(w)