如何使dns-python中的dns查询为dig(附加记录部分)?

tbi*_*icr 6 python dns dnspython dig

我尝试使用dns python并希望获取所有ANY类型查询的记录:

import dns.name
import dns.message
import dns.query

domain = 'google.com'
name_server = '8.8.8.8'

domain = dns.name.from_text(domain)
if not domain.is_absolute():
    domain = domain.concatenate(dns.name.root)

request = dns.message.make_query(domain, dns.rdatatype.ANY)

response = dns.query.udp(request, name_server)
print response.answer
print response.additional
print response.authority
Run Code Online (Sandbox Code Playgroud)

但它回报了我

[]
[]
[]
Run Code Online (Sandbox Code Playgroud)

当我尝试使用以下命令发出此请求dig:

$ dig @8.8.8.8  google.com -t ANY

; <<>> DiG 9.9.2-P1 <<>> @8.8.8.8 google.com -t ANY
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 2848
;; flags: qr rd ra; QUERY: 1, ANSWER: 25, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;google.com.            IN  ANY

;; ANSWER SECTION:
google.com.     299 IN  A   173.194.40.14
google.com.     299 IN  A   173.194.40.1
google.com.     299 IN  A   173.194.40.7
google.com.     299 IN  A   173.194.40.4
google.com.     299 IN  A   173.194.40.3
google.com.     299 IN  A   173.194.40.0
google.com.     299 IN  A   173.194.40.8
google.com.     299 IN  A   173.194.40.6
google.com.     299 IN  A   173.194.40.5
google.com.     299 IN  A   173.194.40.2
google.com.     299 IN  A   173.194.40.9
google.com.     299 IN  AAAA    2a00:1450:4002:804::1000
google.com.     21599   IN  TYPE257 \# 23 0009697373756577696C6473796D616E7465632E636F6D
google.com.     21599   IN  TYPE257 \# 19 0005697373756573796D616E7465632E636F6D
google.com.     21599   IN  NS  ns2.google.com.
google.com.     21599   IN  NS  ns3.google.com.
google.com.     599 IN  MX  50 alt4.aspmx.l.google.com.
google.com.     599 IN  MX  10 aspmx.l.google.com.
google.com.     3599    IN  TXT "v=spf1 include:_spf.google.com ip4:216.73.93.70/31 ip4:216.73.93.72/31 ~all"
google.com.     599 IN  MX  20 alt1.aspmx.l.google.com.
google.com.     21599   IN  SOA ns1.google.com. dns-admin.google.com. 2013070800 7200 1800 1209600 300
google.com.     599 IN  MX  30 alt2.aspmx.l.google.com.
google.com.     21599   IN  NS  ns1.google.com.
google.com.     599 IN  MX  40 alt3.aspmx.l.google.com.
google.com.     21599   IN  NS  ns4.google.com.

;; Query time: 52 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Tue Jul 16 18:23:46 2013
;; MSG SIZE  rcvd: 623
Run Code Online (Sandbox Code Playgroud)

当我检查请求时,wireshark发现dig并且dns python有不同的请求:

dig:

0000  c8 64 c7 3a e3 40 50 46  5d a5 70 99 08 00 45 00   .d.:.@PF ].p...E.
0010  00 43 9f 60 00 00 40 11  09 8f c0 a8 01 03 08 08   .C.`..@. ........
0020  08 08 8e 9e 00 35 00 2f  71 cf ef 49 01 20 00 01   .....5./ q..I. ..
0030  00 00 00 00 00 01 06 67  6f 6f 67 6c 65 03 63 6f   .......g oogle.co
0040  6d 00 00 ff 00 01 00 00  29 10 00 00 00 00 00 00   m....... ).......
0050  00                
Run Code Online (Sandbox Code Playgroud)

dns python:

0000  c8 64 c7 3a e3 40 50 46  5d a5 70 99 08 00 45 00   .d.:.@PF ].p...E.
0010  00 38 00 00 40 00 40 11  68 fa c0 a8 01 03 08 08   .8..@.@. h.......
0020  08 08 b8 62 00 35 00 24  23 6b 3d 31 01 00 00 01   ...b.5.$ #k=1....
0030  00 00 00 00 00 00 06 67  6f 6f 67 6c 65 03 63 6f   .......g oogle.co
0040  6d 00 00 ff 00 01                                  m.....           
Run Code Online (Sandbox Code Playgroud)

对于DNS查询部分:

digAD bit: Set旗帜:

002C-002D:01 20for dig01 00fordns python

Additional records部分除了dns-python:

0046-0050:00 00 29 10 00 00 00 00 00 00 00.

这个实际也不仅适用google.comlogitech.commayby其他.

那么,如何可以提出请求与dns pythondig这个额外的部分?

tbi*_*icr 16

我找到了解决方案,我提出了以下要求dig:

import dns.name
import dns.message
import dns.query
import dns.flags

domain = 'google.com'
name_server = '8.8.8.8'
ADDITIONAL_RDCLASS = 65535

domain = dns.name.from_text(domain)
if not domain.is_absolute():
    domain = domain.concatenate(dns.name.root)

request = dns.message.make_query(domain, dns.rdatatype.ANY)
request.flags |= dns.flags.AD
request.find_rrset(request.additional, dns.name.root, ADDITIONAL_RDCLASS,
                   dns.rdatatype.OPT, create=True, force_unique=True)
response = dns.query.udp(request, name_server)


print response.answer
print response.additional
print response.authority
Run Code Online (Sandbox Code Playgroud)

随着ADDITIONAL_RDCLASS = 4096dig所有的工作太多,但我将它完全是在安全方面.

它的效果非常好.