1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
# -*- coding: utf-8 -*-
"""
digparser
---------
Encode/decode DNS packets from DiG textual representation. Parses
question (if present: +qr flag) & answer sections and returns list
of DNSRecord objects.
Unsupported RR types are skipped (this is different from the packet
parser which will store and encode the RDATA as a binary blob)
>>> dig = os.path.join(os.path.dirname(__file__),"test","dig","google.com-A.dig")
>>> with open(dig) as f:
... l = DigParser(f)
... for record in l:
... print('---')
... print(repr(record))
---
<DNS Header: id=0x5c9a type=QUERY opcode=QUERY flags=RD rcode='NOERROR' q=1 a=0 ns=0 ar=0>
<DNS Question: 'google.com.' qtype=A qclass=IN>
---
<DNS Header: id=0x5c9a type=RESPONSE opcode=QUERY flags=RD,RA rcode='NOERROR' q=1 a=16 ns=0 ar=0>
<DNS Question: 'google.com.' qtype=A qclass=IN>
<DNS RR: 'google.com.' rtype=A rclass=IN ttl=299 rdata='62.252.169.183'>
<DNS RR: 'google.com.' rtype=A rclass=IN ttl=299 rdata='62.252.169.152'>
<DNS RR: 'google.com.' rtype=A rclass=IN ttl=299 rdata='62.252.169.172'>
<DNS RR: 'google.com.' rtype=A rclass=IN ttl=299 rdata='62.252.169.177'>
<DNS RR: 'google.com.' rtype=A rclass=IN ttl=299 rdata='62.252.169.157'>
<DNS RR: 'google.com.' rtype=A rclass=IN ttl=299 rdata='62.252.169.153'>
<DNS RR: 'google.com.' rtype=A rclass=IN ttl=299 rdata='62.252.169.182'>
<DNS RR: 'google.com.' rtype=A rclass=IN ttl=299 rdata='62.252.169.168'>
<DNS RR: 'google.com.' rtype=A rclass=IN ttl=299 rdata='62.252.169.178'>
<DNS RR: 'google.com.' rtype=A rclass=IN ttl=299 rdata='62.252.169.162'>
<DNS RR: 'google.com.' rtype=A rclass=IN ttl=299 rdata='62.252.169.187'>
<DNS RR: 'google.com.' rtype=A rclass=IN ttl=299 rdata='62.252.169.167'>
<DNS RR: 'google.com.' rtype=A rclass=IN ttl=299 rdata='62.252.169.148'>
<DNS RR: 'google.com.' rtype=A rclass=IN ttl=299 rdata='62.252.169.173'>
<DNS RR: 'google.com.' rtype=A rclass=IN ttl=299 rdata='62.252.169.158'>
<DNS RR: 'google.com.' rtype=A rclass=IN ttl=299 rdata='62.252.169.163'>
>>> dig = os.path.join(os.path.dirname(__file__),"test","dig","google.com-ANY.dig")
>>> with open(dig) as f:
... l = DigParser(f)
... for record in l:
... print('---')
... print(repr(record))
---
<DNS Header: id=0xb294 type=QUERY opcode=QUERY flags=RD rcode='NOERROR' q=1 a=0 ns=0 ar=0>
<DNS Question: 'google.com.' qtype=ANY qclass=IN>
---
<DNS Header: id=0xb294 type=RESPONSE opcode=QUERY flags=RD,RA rcode='NOERROR' q=1 a=14 ns=0 ar=0>
<DNS Question: 'google.com.' qtype=ANY qclass=IN>
<DNS RR: 'google.com.' rtype=A rclass=IN ttl=299 rdata='216.58.212.110'>
<DNS RR: 'google.com.' rtype=AAAA rclass=IN ttl=299 rdata='2a00:1450:4009:807::200e'>
<DNS RR: 'google.com.' rtype=CAA rclass=IN ttl=86399 rdata='0 issue "symantec.com"'>
<DNS RR: 'google.com.' rtype=MX rclass=IN ttl=599 rdata='40 alt3.aspmx.l.google.com.'>
<DNS RR: 'google.com.' rtype=MX rclass=IN ttl=599 rdata='10 aspmx.l.google.com.'>
<DNS RR: 'google.com.' rtype=NS rclass=IN ttl=86399 rdata='ns2.google.com.'>
<DNS RR: 'google.com.' rtype=MX rclass=IN ttl=599 rdata='20 alt1.aspmx.l.google.com.'>
<DNS RR: 'google.com.' rtype=SOA rclass=IN ttl=59 rdata='ns2.google.com. dns-admin.google.com. 144578247 900 900 1800 60'>
<DNS RR: 'google.com.' rtype=NS rclass=IN ttl=86399 rdata='ns1.google.com.'>
<DNS RR: 'google.com.' rtype=NS rclass=IN ttl=86399 rdata='ns4.google.com.'>
<DNS RR: 'google.com.' rtype=NS rclass=IN ttl=86399 rdata='ns3.google.com.'>
<DNS RR: 'google.com.' rtype=MX rclass=IN ttl=599 rdata='50 alt4.aspmx.l.google.com.'>
<DNS RR: 'google.com.' rtype=TXT rclass=IN ttl=3599 rdata='"v=spf1 include:_spf.google.com ~all"'>
<DNS RR: 'google.com.' rtype=MX rclass=IN ttl=599 rdata='30 alt2.aspmx.l.google.com.'>
"""
from __future__ import print_function
import glob,os.path,string
from dnslib.lex import WordLexer
from dnslib.dns import (DNSRecord,DNSHeader,DNSQuestion,DNSError,
RR,RD,RDMAP,QR,RCODE,CLASS,QTYPE)
class DigParser:
"""
Parse Dig output
"""
def __init__(self,dig,debug=False):
self.debug = debug
self.l = WordLexer(dig)
self.l.commentchars = ';'
self.l.nltok = ('NL',None)
self.i = iter(self.l)
def parseHeader(self,l1,l2):
_,_,_,opcode,_,status,_,_id = l1.split()
_,flags,_ = l2.split(';')
header = DNSHeader(id=int(_id),bitmap=0)
header.opcode = getattr(QR,opcode.rstrip(','))
header.rcode = getattr(RCODE,status.rstrip(','))
for f in ('qr','aa','tc','rd','ra'):
if f in flags:
setattr(header,f,1)
return header
def expect(self,expect):
t,val = next(self.i)
if t != expect:
raise ValueError("Invalid Token: %s (expecting: %s)" % (t,expect))
return val
def parseQuestions(self,q,dns):
for qname,qclass,qtype in q:
dns.add_question(DNSQuestion(qname,
getattr(QTYPE,qtype),
getattr(CLASS,qclass)))
def parseAnswers(self,a,auth,ar,dns):
sect_map = {'a':'add_answer','auth':'add_auth','ar':'add_ar'}
for sect in 'a','auth','ar':
f = getattr(dns,sect_map[sect])
for rr in locals()[sect]:
rname,ttl,rclass,rtype = rr[:4]
rdata = rr[4:]
rd = RDMAP.get(rtype,RD)
try:
if rd == RD and \
any([ x not in string.hexdigits for x in rdata[-1]]):
# Only support hex encoded data for fallback RD
pass
else:
f(RR(rname=rname,
ttl=int(ttl),
rtype=getattr(QTYPE,rtype),
rclass=getattr(CLASS,rclass),
rdata=rd.fromZone(rdata)))
except DNSError as e:
if self.debug:
print("DNSError:",e,rr)
else:
# Skip records we dont understand
pass
def __iter__(self):
return self.parse()
def parse(self):
dns = None
section = None
paren = False
rr = []
try:
while True:
tok,val = next(self.i)
if tok == 'COMMENT':
if 'Sending:' in val or 'Got answer:' in val:
if dns:
self.parseQuestions(q,dns)
self.parseAnswers(a,auth,ar,dns)
yield(dns)
dns = DNSRecord()
q,a,auth,ar = [],[],[],[]
elif val.startswith('; ->>HEADER<<-'):
self.expect('NL')
val2 = self.expect('COMMENT')
dns.header = self.parseHeader(val,val2)
elif val.startswith('; QUESTION'):
section = q
elif val.startswith('; ANSWER'):
section = a
elif val.startswith('; AUTHORITY'):
section = auth
elif val.startswith('; ADDITIONAL'):
section = ar
elif val.startswith(';') or tok[1].startswith('<<>>'):
pass
elif dns and section == q:
q.append(val.split())
elif tok == 'ATOM':
if val == '(':
paren = True
elif val == ')':
paren = False
else:
rr.append(val)
elif tok == 'NL' and not paren and rr:
if self.debug:
print(">>",rr)
section.append(rr)
rr = []
except StopIteration:
if rr:
self.section.append(rr)
if dns:
self.parseQuestions(q,dns)
self.parseAnswers(a,auth,ar,dns)
yield(dns)
if __name__ == '__main__':
import argparse,doctest,sys
p = argparse.ArgumentParser(description="DigParser Test")
p.add_argument("--dig",action='store_true',default=False,
help="Parse DiG output (stdin)")
p.add_argument("--debug",action='store_true',default=False,
help="Debug output")
args = p.parse_args()
if args.dig:
l = DigParser(sys.stdin,args.debug)
for record in l:
print(repr(record))
else:
doctest.testmod()
|