DNS/実装/python/

1. dnslib/lame_check/Get_ns.py

について、ここに記述してください。

NSレコードのAを検索;

   1 from dnslib.dns import DNSRecord,DNSHeader,DNSQuestion,DNSError,QTYPE
   2 def get_ns(domain, address):
   3     # print ('get_ns', domain, address)
   4     ns = set ()  
   5     try:
   6                q0 = DNSQuestion(domain, QTYPE.NS)
   7                q = DNSRecord(q=q0)
   8                a_pkt = q.send(address, 53, tcp=False)
   9                a = DNSRecord.parse(a_pkt)
  10                # print ('answer', a)
  11                if a.header.tc :        # Truncated - retry in TCP mode
  12                    a_pkt = q.send(address, 53,tcp=True)
  13                    a = DNSRecord.parse(a_pkt)
  14                 # NXDOMAIN  rcode
  15                if a.header.rcode == 0 : # NOERROR
  16                   for r in a.rr:   # ANSWER section
  17                      if r.rtype == QTYPE.NS:
  18                           ns.add(str(r.rdata))
  19     except DNSError as e:
  20               p.error(e)
  21                              
  22     return ns          
  23 
  24 def checkns(d, nsset):
  25       print ('NS', d, nsset)
  26       # from Get_ns import get_ns
  27       for name in nsset:
  28              zonens = get_ns(d,name)
  29              if zonens:
  30                 if nsset == zonens :
  31                    print ('match', d, name, '==')
  32                 elif nsset < zonens : 
  33                    print ('plus', d, name, '++', list(zonens - nsset))
  34                 else:
  35                    print ('zone', d, zonens)
  36              else:
  37                 print ('RF', d, name)   # no zone
  38  
  39 def delegation(address,domain):
  40     from dnslib.dns import DNSRecord,DNSHeader,DNSQuestion,DNSError,QTYPE,EDNS0
  41     ns = { }   # nslist   (+ ipaddress list) 
  42 
  43     try:
  44         q0 = DNSQuestion(domain, QTYPE.NS)
  45         q = DNSRecord(q=q0)
  46         # print('question ok', q)
  47         a_pkt = q.send(address, 53, tcp=False)
  48         a = DNSRecord.parse(a_pkt)
  49         if a.header.tc :        # Truncated - retry in TCP mode
  50            a_pkt = q.send(address, 53,tcp=True)
  51            a = DNSRecord.parse(a_pkt)
  52         # NXDOMAIN  rcode
  53         if a.header.rcode != 0 : # ['NOERROR', 'NXDOMAIN', 'SERVFAIL', 'REFUSED']:
  54            print ('ZONEX', domain, a.header.rcode)
  55         else:
  56           for r in a.auth:
  57               if r.rtype == QTYPE.NS:
  58                  ns[str(r.rdata)] = []  # address list
  59           for t in a.ar:
  60               if t.rtype == QTYPE.A :
  61                  tt = str(t.rname) 
  62                  if tt in ns :
  63                      ns[tt].append(str(t.rdata))
  64         return ns
  65     except DNSError as e:
  66                 p.error(e)

address == dest (IP address かと思っていたが、sock.connect で使われるので、名前でも大丈夫らしい。)

-- ToshinoriMaeno 2021-03-05 02:35:40

query + response までを分離する。(重複)

MoinQ: DNS/実装/python/dnslib/lame_check/Get_ns.py (last edited 2021-03-14 02:36:10 by ToshinoriMaeno)