1

pcap ファイルの読み取りに dpkt を使用しています。

        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を返すと思われるものは何も見つかりませんでした。ドキュメントには何も見つかりません。

そのための方法はありますか?

4

3 に答える 3

2

この質問から

次のようなもので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

次に、関連する TTL および IP 値を IP ヘッダーから取得します。

TTL と IP の値は、DNS の回答ではなく、IP ヘッダーにあります。

于 2015-05-12T21:25:29.713 に答える
1

(これは非常に遅い回答です。) Bob は、応答を送信したサーバーの IP アドレスを要求していますか、それとも CNAME レコード内の名前の IP アドレスを要求していますか? DNS 応答のリソース レコード (RR) 部分の正規名 (CNAME) データには、ドメイン名と TTL のみが含まれます。私がテストしたネームサーバーは、別の追加情報 RR レコードで CNAME 回答の IP アドレスを返します。

元の章と節を読みたい場合: https://www.ietf.org/rfc/rfc1035.txt

これは、DNS 応答の内容を生成して調べることができる dpkt を使用した 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
于 2015-10-05T14:15:25.543 に答える