python - 使用 Scapy 解析多个 DNS 响应
问题描述
尝试使用 Scapy 解析 DNS 响应(参见下面的函数)。我的问题是 rdata[] 字段中的所有答案都没有显示。当我使用 Wireshark 进行数据包捕获时,我会在该rdata[]
字段中看到多个答案,对于那些不熟悉 DNS 的人来说,单个响应数据包中通常会有两个或三个答案。
我只返回一个答案(第一个)。我尝试使用sr()
而不是,sr1()
也尝试multi=True
在发送数据包时添加作为参数,但这些都不起作用。
有任何想法吗?
def send_query_recursion(resolver, target):
dns_req = IP(dst=f'{resolver}')/UDP(dport=53)/DNS(qr=0, rd=1, qd=DNSQR(qname=f'{target}'))
answer = sr1(dns_req, verbose=1)
for received in answer:
if received.haslayer(DNS):
for x in received:
print(str(x[DNS].id))
print("rrname: " + str(x[DNSRR].rrname))
print("Type: " + str(x[DNSRR].type))
if str(x[DNSRR].rclass) == "1":
print("Class: " + str(x[DNSRR].rclass) + " IN")
print("TTL: " + str(x[DNSRR].ttl))
print("Resource Data Length: " + str(x[DNSRR].rdlen))
print("Resource Data: " + str(x[DNSRR].rdata[:-1]))
解决方案
我和你有同样的问题。我无法使用 scapy 解决它,所以我使用 dpkt 包来解析DNS 响应的答案,如下所示(python 3):
with open('your_pcap.pcap', 'rb') as f:
pcap = dpkt.pcap.Reader(f)
for timestamp, buf in pcap:
try:
eth = dpkt.ethernet.Ethernet(buf)
except:
continue
if eth.type != 2048:
continue
try:
ip = eth.data
except:
continue
if ip.p != 17:
continue
#filter on UDP assigned ports for DNS
try:
udp = ip.data
except:
continue
if udp.sport != 53 and udp.dport != 53:
continue
#make the dns object out of the udp data and
#check for it being a RR (answer) and for opcode QUERY
try:
dns = dpkt.dns.DNS(udp.data)
except:
continue
if dns.qr != dpkt.dns.DNS_R:
continue
if dns.opcode != dpkt.dns.DNS_QUERY:
continue
if dns.rcode != dpkt.dns.DNS_RCODE_NOERR:
continue
if len(dns.an) < 1:
continue
for qname in dns.qd:
print("The DNS response packet has ", len(dns.an), " answers")
print("The answers (in a list) are: ", dns.an)
您可以在以下页面上找到有关使用 dpkt 解析 DNS 的更多信息:
https://dpkt.readthedocs.io/en/latest/_modules/dpkt/dns.html
https://mmishou.wordpress.com/2010/04/13/passive-dns-mining-from-pcap-with-dpkt-python/
推荐阅读
- ios - iOS - 发布模式下 xcode 上的 Flutter audioplayers 插件错误(未找到)
- alexa - 如何从外部呼叫 Alexa?
- sql - Oracle SQL 中具有相同列名的多个表连接
- c# - 使用 ITextSharp TextRenderInfo.GetTextRenderMode 忽略 pdf 中的隐藏文本
- c# - 设置 TargetFramework=net472 仍然给出错误“无法加载文件或程序集'netstandard,版本 = 2.1.0.0”
- aws-api-gateway - 如何防止 CDK 为新域名创建默认基本路径映射
- c++ - 我无法让我的向量显示其内容 C++
- java - Apache Beam 的 BigQueryIO (Java):无法将 TIMESTAMP 字段写入 BigQuery——fastxml.jackson 异常“类型不支持”
- python - 过滤 pyspark DataFrame 时出错
- twilio - 多
在出站呼叫时,响应显示“Digits=timeout”,呼叫提前断开