首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用dpkt库从DNS响应包中提取域名

使用dpkt库从DNS响应包中提取域名
EN

Stack Overflow用户
提问于 2013-07-26 12:09:13
回答 1查看 6.5K关注 0票数 5

我试图使用dpkt库从pcap生成所有域名及其相应IP地址的列表( 这里 )

我的代码主要是基于

代码语言:javascript
复制
filename = raw_input('Type filename of pcap file (without extention): ')
path = 'c:/temp/PcapParser/' + filename + '.pcap'
f = open(path, 'rb')
pcap = dpkt.pcap.Reader(f)
for ts, buf in pcap:
    #make sure we are dealing with IP traffic
    try:
        eth = dpkt.ethernet.Ethernet(buf)
    except:
        continue
    if eth.type != 2048:
        continue
    #make sure we are dealing with UDP protocol
    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
    #process and print responses based on record type
    for answer in dns.an:
        if answer.type == 1: #DNS_A
            print 'Domain Name: ', answer.name, '\tIP Address: ', socket.inet_ntoa(answer.rdata)

问题是answer.name对我来说不够好,因为我需要原始的域名请求,而不是它的‘CNAME表示。例如,最初的DNS请求之一是针对www.paypal.com的,但它的CNAME表示形式是paypal.112.2o7.net

我仔细查看了代码,发现我实际上是从DNS响应(而不是查询)中提取信息。然后我查看了wireshark中的响应包,发现原来的域在“查询”和“答案”下面,所以我的问题是如何提取它?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-28 06:33:33

为了通过由dns.qd提供的dpkt.dns对象从DNS响应的“问题”部分获取名称,我只需要做以下工作:

代码语言:javascript
复制
for qname in dns.qd: print qname.name
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17880931

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档