Open selfboot opened 7 years ago
Following shows a simple demo which build QNAME for domain name google.com
:
google = asyncdns.build_address("google.com")
print(binascii.hexlify(google))
It prints (just as what we wish: each label consists of a length octet followed by that number of octets.)
06676f6f676c6503636f6d00
As #39 has showed, the dns request to google.com is
Then we can build the request as follows:
dns_request = asyncdns.build_request("google.com", 1, 0x9118)
print(binascii.hexlify(dns_request))
It prints as what we analysis from the data package which is captured from wireshark when dig google.com
.
91180100000100000000000006676f6f676c6503636f6d0000010001
Function build_address use common.chr(l)
to convert int to bytes.
def compat_chr(d):
if bytes == str:
return _chr(d)
return bytes([d])
_chr = chr
chr = compat_chr
>>> type(chr(34))
<type 'str'>
>>> type(chr(34)) is bytes
True
>>> import binascii
>>> binascii.hexlify(chr(34))
'22'
>>> type(chr(34))
<class 'str'>
>>> binascii.hexlify(chr(34))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'
>>> bytes([34])
b'"'
>>> binascii.hexlify(bytes([34]))
b'22'
As issue #38 mentioned, the top level format of message is divided into 5 sections (some of which are empty in certain cases) shown below:
So, we need to fill these five section to make a DNS request message.
Header
The format string pack the DNS request header section as followers:
Details:
In
asyncdns.build_request
, there are following snippet:Format string
!HBBHHHH
specify that the byte order is network(= big-endian), and:Question
The question section is used to carry the "question" in most queries, i.e., the parameters that define what is being asked. The section contains QDCOUNT (usually 1) entries, each of the following format:
The following snippet packs the QTYPE and QCLASS para in question section.
QNAME is created by the following snippet:
Ref
#37 Struct: working with binary data #38 Data format of message used in DNS #39 View DNS data datagram via wireshark