Skip to main content

Simple library to encode/decode DNS wire-format packets

Project description

dnslib
------

A simple library to encode/decode DNS wire-format packets. This was originally
written for a custom nameserver.

The key classes are:

* DNSRecord (contains a DNSHeader and one or more DNSQuestion/DNSRR records)
* DNSHeader
* DNSQuestion
* RR (resource records)
* RD (resource data - superclass for TXT,A,MX,CNAME,PRT,SOA)

To decode a DNS packet:

>>> packet = 'd5ad818000010005000000000377777706676f6f676c6503636f6d0000010001c00c0005000100000005000803777777016cc010c02c0001000100000005000442f95b68c02c0001000100000005000442f95b63c02c0001000100000005000442f95b67c02c0001000100000005000442f95b93'.decode('hex')
>>> d = DNSRecord.parse(packet)
>>> print d
<DNS Header: id=0xd5ad type=RESPONSE opcode=QUERY flags=RD,RA rcode=None q=1 a=5 ns=0 ar=0>
<DNS Question: 'www.google.com' qtype=A qclass=IN>
<DNS RR: 'www.google.com' rtype=CNAME rclass=IN ttl=5 rdata='www.l.google.com'>
<DNS RR: 'www.l.google.com' rtype=A rclass=IN ttl=5 rdata='66.249.91.104'>
<DNS RR: 'www.l.google.com' rtype=A rclass=IN ttl=5 rdata='66.249.91.99'>
<DNS RR: 'www.l.google.com' rtype=A rclass=IN ttl=5 rdata='66.249.91.103'>
<DNS RR: 'www.l.google.com' rtype=A rclass=IN ttl=5 rdata='66.249.91.147'>

To create a DNS Request Packet:

>>> d = DNSRecord(q=DNSQuestion("google.com"))
>>> print d
<DNS Header: id=... type=QUERY opcode=QUERY flags=RD rcode=None q=1 a=0 ns=0 ar=0>
<DNS Question: 'google.com' qtype=A qclass=IN>
>>> d.pack()
'...'

>>> d = DNSRecord(q=DNSQuestion("google.com",QTYPE.MX))
>>> print d
<DNS Header: id=... type=QUERY opcode=QUERY flags=RD rcode=None q=1 a=0 ns=0 ar=0>
<DNS Question: 'google.com' qtype=MX qclass=IN>
>>> d.pack()
'...'

To create a DNS Response Packet:

>>> d = DNSRecord(DNSHeader(qr=1,aa=1,ra=1),
... q=DNSQuestion("abc.com"),
... a=RR("abc.com",rdata=A("1.2.3.4")))
>>> print d
<DNS Header: id=... type=RESPONSE opcode=QUERY flags=AA,RD,RA rcode=None q=1 a=1 ns=0 ar=0>
<DNS Question: 'abc.com' qtype=A qclass=IN>
<DNS RR: 'abc.com' rtype=A rclass=IN ttl=0 rdata='1.2.3.4'>
>>> d.pack()
'...'

To create a skeleton reply to a DNS query:

>>> q = DNSRecord(q=DNSQuestion("abc.com",QTYPE.CNAME))
>>> a = q.reply(data="xxx.abc.com")
>>> print a
<DNS Header: id=... type=RESPONSE opcode=QUERY flags=AA,RD,RA rcode=None q=1 a=1 ns=0 ar=0>
<DNS Question: 'abc.com' qtype=CNAME qclass=IN>
<DNS RR: 'abc.com' rtype=CNAME rclass=IN ttl=0 rdata='xxx.abc.com'>
>>> a.pack()
'...'

Add additional RRs:

>>> a.add_answer(RR('xxx.abc.com',QTYPE.A,rdata=A("1.2.3.4")))
>>> print a
<DNS Header: id=... type=RESPONSE opcode=QUERY flags=AA,RD,RA rcode=None q=1 a=2 ns=0 ar=0>
<DNS Question: 'abc.com' qtype=CNAME qclass=IN>
<DNS RR: 'abc.com' rtype=CNAME rclass=IN ttl=0 rdata='xxx.abc.com'>
<DNS RR: 'xxx.abc.com' rtype=A rclass=IN ttl=0 rdata='1.2.3.4'>
>>> a.pack()
'...'

Changelog:

0.1 2010-09-19 Initial Release
0.2 2010-09-22 Minor fixes
0.3 2010-10-02 Add DNSLabel class to supportt arbitrary labels (embedded '.')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dnslib-0.3.tar.gz (6.7 kB view details)

Uploaded Source

File details

Details for the file dnslib-0.3.tar.gz.

File metadata

  • Download URL: dnslib-0.3.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for dnslib-0.3.tar.gz
Algorithm Hash digest
SHA256 6a59b70ba5d0288aebfadf145e547abe988c01ee97fd0ef2c57723ac0ab5a577
MD5 7bedb4336e0c5a01a247f8bfd8719a1b
BLAKE2b-256 278014baa3a19f40644309f4483156db2bd9ae1b6990237c6a8d20673dab8572

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page