2015-05-12 75 views
1

我正在使用dpkt讀取pcap文件。使用dpkt從DNS響應中讀取IP地址和TTL

 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 answer in dns.an: 
      if answer.type == 5: 
       print "CNAME request", answer.name, "\tresponse", answer.cname 

我想閱讀每個答案的IP地址和TTL。我做了dir(answer),但我找不到任何似乎返回響應的IP地址/ TTL的東西,而且我在文檔中找不到任何東西。

有沒有一種方法呢?

回答

0

From this question

我假設你的東西讀取PCAP文件是這樣的:

import dpkt 
import sys 

f=file(sys.argv[1],"rb") 
pcap=dpkt.pcap.Reader(f) 

分析每個單獨層與以太網開始。

for ts, buf in pcap: 
    eth=dpkt.ethernet.Ethernet(buf) 
    ip=eth.data 
    udp=ip.data 

然後從IP頭中拉出相關的TTL和IP值。

TTL和IP值位於IP標頭中,而不是DNS答案。

+0

我不認爲你會看到在IP報頭中的DNS信息。 IP層低於DNS – Bob

+0

這是正確的。但是,您可以在處理DNS數據之前解析IP標頭。 – chemdt

0

的RR對象有一個ttl屬性,它是你在找什麼:(這是一個可悲的遲到的回答)

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 answer in dns.an: 
     if answer.type == 5: 
      print "CNAME request", answer.name, "\tresponse", answer.cname, "\tttl", answer.ttl 
0

鮑勃你要求發送的回覆服務器的IP地址,或CNAME記錄中名稱的IP地址? DNS答覆的資源記錄(RR)部分中的規範名稱(CNAME)數據僅具有域名和TTL。我測試過的名稱服務器在不同的附加信息RR記錄中返回CNAME應答的IP地址。

如果你想讀原章的詩句:https://www.ietf.org/rfc/rfc1035.txt

這裏是有點用dpkt,可以讓你生成和探索DNS答覆的內容Python代碼。

import dpkt 
import random 
import socket 

# build query 
query_id = int(random.random() * 10000) 
query = dpkt.dns.DNS(id=query_id) 
my_q = dpkt.dns.DNS.Q(name="www.yahoo.com") 
query.qd.append(my_q) 

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
sock.connect(("8.8.8.8", 53)) 
sock.send(str(query)) 
buf = sock.recv(0xffff) 

# parse response 
response = dpkt.dns.DNS(buf) 
if query_id != response.id: 
    print "Expected %d but received id %d" % (query_id, response.id) 
elif response.qr != dpkt.dns.DNS_R: 
    print "Not a response!" 
elif response.opcode != dpkt.dns.DNS_QUERY: 
    print "Not a query op!" 
elif response.rcode != dpkt.dns.DNS_RCODE_NOERR: 
    print "Not a successful response!" 
elif len(response.an) == 0: 
    print"Response has no answers!" 
else: 
    print "%d bytes received, id is %d" % (len(buf), response.id) 
    for rr in response.an: 
     print "AN: class is %d, type is %d, name is %s" % (rr.cls, rr.type, rr.name) 
     if hasattr(rr, 'ip'): 
      print "\tIP is %s" % socket.inet_ntoa(rr.ip) 
    for rr in response.ns: 
     print "NS: class is %d, type is %d, name is %s" % (rr.cls, rr.type, rr.name) 
    for rr in response.ar: 
     print "AR: class is %d, type is %d, name is %s" % (rr.cls, rr.type, rr.name) 

,我看到(坐東海岸)結果:

240 bytes received, id is 5848 
AN: class is 1, type is 5, name is www.yahoo.com 
AN: class is 1, type is 1, name is fd-fp3.wg1.b.yahoo.com 
    IP is 98.139.180.149 
AN: class is 1, type is 1, name is fd-fp3.wg1.b.yahoo.com 
    IP is 98.139.183.24 
NS: class is 1, type is 2, name is wg1.b.yahoo.com 
NS: class is 1, type is 2, name is wg1.b.yahoo.com 
NS: class is 1, type is 2, name is wg1.b.yahoo.com 
NS: class is 1, type is 2, name is wg1.b.yahoo.com 
AR: class is 1, type is 1, name is yf2.yahoo.com 
AR: class is 1, type is 1, name is yf1.yahoo.com 
AR: class is 1, type is 1, name is yf3.a1.b.yahoo.net 
AR: class is 1, type is 1, name is yf4.a1.b.yahoo.net