Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中读取DNS数据包_Python_Sockets_Character Encoding_Dns_Udp - Fatal编程技术网

在Python中读取DNS数据包

在Python中读取DNS数据包,python,sockets,character-encoding,dns,udp,Python,Sockets,Character Encoding,Dns,Udp,我正在使用Python套接字,并决定看看是否可以实现一个非常基本的名称服务器(即,从域名到IP地址的查找表)。到目前为止,我已经将服务器设置为只转储接收到的数据 #!/usr/bin/python import socket s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) host = '' port = 53 size = 512 s.bind((host, port)) while True: data, addr = s.

我正在使用Python套接字,并决定看看是否可以实现一个非常基本的名称服务器(即,从域名到IP地址的查找表)。到目前为止,我已经将服务器设置为只转储接收到的数据

#!/usr/bin/python
import socket

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
host = ''
port = 53
size = 512
s.bind((host, port))
while True:
    data, addr = s.recvfrom(size)
    print repr(data)
当我运行上述代码并将DNS指向127.0.0.1时,我得到类似于以下内容的内容:

'Y\x04\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x01\x00\x01'
'J\xaa\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x1c\x00\x01'
'Y\x04\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x01\x00\x01'
'E\x00$\x00\xe4\x96\x00\x00@\x01\x00\x00\x7f\x00\x00\x01\x7f\x00\x00\x01\x03\x03X\xb6\x00\x00\x00\x00E\x00V\x00m\x82\x00\x00\xff\x11\x00\x00\x7f\x00\x00\x01\x7f\x00\x00\x01\xf3\xe1\x005\x00B\x00\x00'
我假设这与DNS问题包结构有关,但我不确定

A) 以上是转义字符吗?特定的文本编码?或者仅仅是字节

B) 我如何解释数据并使用它

编辑:更改套接字以获取原始数据而不是数据报会导致以下结果:

'Y\x04\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x01\x00\x01'
'J\xaa\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x1c\x00\x01'
'Y\x04\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x06google\x03com\x00\x00\x01\x00\x01'
'E\x00$\x00\xe4\x96\x00\x00@\x01\x00\x00\x7f\x00\x00\x01\x7f\x00\x00\x01\x03\x03X\xb6\x00\x00\x00\x00E\x00V\x00m\x82\x00\x00\xff\x11\x00\x00\x7f\x00\x00\x01\x7f\x00\x00\x01\xf3\xe1\x005\x00B\x00\x00'

您可以从以下内容开始:

#!/usr/bin/env python

import pprint
import socket
import struct


def decode_labels(message, offset):
    labels = []

    while True:
        length, = struct.unpack_from("!B", message, offset)

        if (length & 0xC0) == 0xC0:
            pointer, = struct.unpack_from("!H", message, offset)
            offset += 2

            return labels + decode_labels(message, pointer & 0x3FFF), offset

        if (length & 0xC0) != 0x00:
            raise StandardError("unknown label encoding")

        offset += 1

        if length == 0:
            return labels, offset

        labels.append(*struct.unpack_from("!%ds" % length, message, offset))
        offset += length


DNS_QUERY_SECTION_FORMAT = struct.Struct("!2H")

def decode_question_section(message, offset, qdcount):
    questions = []

    for _ in range(qdcount):
        qname, offset = decode_labels(message, offset)

        qtype, qclass = DNS_QUERY_SECTION_FORMAT.unpack_from(message, offset)
        offset += DNS_QUERY_SECTION_FORMAT.size

        question = {"domain_name": qname,
                    "query_type": qtype,
                    "query_class": qclass}

        questions.append(question)

    return questions, offset


DNS_QUERY_MESSAGE_HEADER = struct.Struct("!6H")

def decode_dns_message(message):

    id, misc, qdcount, ancount, nscount, arcount = DNS_QUERY_MESSAGE_HEADER.unpack_from(message)

    qr = (misc & 0x8000) != 0
    opcode = (misc & 0x7800) >> 11
    aa = (misc & 0x0400) != 0
    tc = (misc & 0x200) != 0
    rd = (misc & 0x100) != 0
    ra = (misc & 0x80) != 0
    z = (misc & 0x70) >> 4
    rcode = misc & 0xF

    offset = DNS_QUERY_MESSAGE_HEADER.size
    questions, offset = decode_question_section(message, offset, qdcount)

    result = {"id": id,
              "is_response": qr,
              "opcode": opcode,
              "is_authoritative": aa,
              "is_truncated": tc,
              "recursion_desired": rd,
              "recursion_available": ra,
              "reserved": z,
              "response_code": rcode,
              "question_count": qdcount,
              "answer_count": ancount,
              "authority_count": nscount,
              "additional_count": arcount,
              "questions": questions}

    return result


s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
host = ''
port = 53
size = 512
s.bind((host, port))
while True:
    data, addr = s.recvfrom(size)
    pprint.pprint(decode_dns_message(data))

然后填写剩余记录的解码函数。

下面是使用套接字和


如果您只需要ip上的域名。。你可以做
socket.getfqdn(“173.194.37.144”)
也可以做
socket.getfqdn(“www.google.com”)
谢谢,不过我很想实现我自己的简单解决方案,不过一定要记住这一点!试试scapy,它会为你解码。那么有没有办法将数据作为字节数组抓取呢?
map(ord,list(data))
在python2.x中,只需
list(data)
在python3.x中,但我强烈建议使用
struct
模块来解码。完整的DNS记录可以大于512。此外,receive可能不会在go中接收整个缓冲区,即使它足够大。您是正确的。我刚从OP的代码开始,给他们一些开始的东西。但是,如果您要将自己的实现投入生产,您需要知道如何正确处理数据包(并通过扩展整个协议)。经过进一步的调查,我目前认为我错了,尽管python文档对此并不清楚。我现在认为上面的
512
recv
返回之前要读取的最小字节数。它没有定义最大值。(Python recv会自动生成所需的缓冲区,不管缓冲区有多大)。此外,我的印象是,对于UDP/DNS,单个DNS事务始终适合单个UDP帧,并且UDP始终以原子方式传输(或失败)。所以你的代码是完美的,好吧。我自己的DNS测试显示recv(1024)得到的缓冲区大小为4096。