【问题标题】:Reading DNS packet in python在python中读取DNS数据包
【发布时间】:2020-11-29 13:43:42
【问题描述】:

This question had been asked before 但这个问题从未完全解决,而且是从 2013 年开始的。我正在使用 python 套接字来观察 DNS 数据包,它们看起来像这样:

b'\x01\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03www\x10googletagmanager\x03com\x00\x00\x01\x00\x01'

在研究 DNS 数据包的基础知识后,我发现它们的结构如下:

二维码 |操作码 |机管局 | TC |研发 | RA | Z |广告 |光盘 |编码

然后我将数据包解码为 ASCII:

>> str = b'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03www\x10googletagmanager\x03com\x00\x00\x01\x00\x01'
>> print(str.decode("ascii"))
wwwgoogletagmanagercom

这只会返回一个带有地址名称的字符串,而不是上面指定的其他信息。剩下的数据在哪里,比如 QR 和 OpCode?我是不是解码不正确?

明确地说,我不想使用外部库,我的目标是了解 DNS 数据包的结构以及如何解码它们;我知道dnslibscapy 等库。

【问题讨论】:

标签: python dns


【解决方案1】:

我不是套接字专家。从参考 - DNS 标头由位而不是字节组成......因此您需要将其解析为位。使用字节和掩码位。请参阅下面的示例。它不确定标头 hdr[12:] 的内容是什么?

以下是基于上述规范的一些示例代码:

def DNStoDict(hdr):
    '''
    Parse QNAME by using length (byte) +data sequence -- final length=0 signifies end of QNAME
    Refer to https://stackoverflow.com/questions/34841206/why-is-the-content-of-qname-field-not-the-original-domain-in-a-dns-message

    1) DNS knows nothing of URLs. DNS is older than the concept of a URL.

    2) Because that's how DNS's wire format works. What you see is the 
       domain name www.mydomain.com, encoded in the DNS binary format. 
       Length+data is a very common way of storing strings in general.
    '''

        # Build DNS dictionary of values... include QNAME
    l = len(hdr)
    argSize = hdr[10]*256+hdr[11]
    dnsDict = dict(ID     = hdr[0]*256+hdr[1],
                   QR     = bool(hdr[2] & int('10000000', 2)),
                   Opcode =     (hdr[2] & int('01111000', 2))>>3,
                   AA     = bool(hdr[2] & int('00000100', 2)),
                   TC     = bool(hdr[2] & int('00000010', 2)),
                   RD     = bool(hdr[2] & int('00000001', 2)),
                   RA     = bool(hdr[3] & int('10000000', 2)),
                   Z      = bool(hdr[3] & int('01000000', 2)),
                   AD     = bool(hdr[3] & int('00100000', 2)),
                   CD     = bool(hdr[3] & int('00010000', 2)),
                   RCode  = bool(hdr[3] & int('00001111', 2)),
                   QDCOUNT = hdr[4]*256+hdr[5],
                   ANCOUNT = hdr[6]*256+hdr[7],
                   NSCOUNT = hdr[8]*256+hdr[9],
                   ARCOUNT = argSize,
                   QTYPE   = hdr[l-4]*256+hdr[l-3],
                   QCLASS   = hdr[l-2]*256+hdr[l-2])

    # Parse QNAME
    n = 12
    mx = len(hdr)
    qname = ''

    while n < mx:
        try:
            qname += hdr[n:n+argSize].decode() + '.'

            n += argSize
            argSize = int(hdr[n])
            n += 1
            if argSize == 0 : 
                break
        except Exception as err:
            print("Parse Error", err, n, qname)
            break
    dnsDict['QNAME'] = qname[:-1]
    return dnsDict

# Sample DNS Packet Data 
hdr = b'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03www\x10googletagmanager\x03com\x00\x00\x01\x00\x01'


# Parse out the QNAME
dnsDict = DNStoDict(hdr)


print("\n DNS PACKET dictionary")
print(dnsDict)

输出:

DNS PACKET 字典 {'ID':257,'QR':假,'操作码':0,'AA':假,'TC':假,'RD':假,'RA':假,'Z':假,' AD':假,'CD':假,'RCode':假,'QDCOUNT':0,'ANCOUNT':0,'NSCOUNT':0,'ARCOUNT':3,'QTYPE':1,'QCLASS' : 0, 'QNAME': 'www.googletagmanager.com'}

Pyhon 位操作

参考

  1. https://wiki.python.org/moin/BitManipulation
  2. http://www.java2s.com/Tutorials/Python/Data_Types/How_to_create_integer_in_Python_octal_binary_hexadecimal_and_long_integer.htm

一个字节 (b'xxxx') 代表 4 个字节。每个字节由8位组成

0000 0000 - 0 0000 0001 - 1 0000 0010 - 2 0000 0100 - 4 0000 1000 - 8 0001 0000 - 16 0010 0000 - 32 0100 0000 - 64 1000 0000 - 128 1111 1111 - 255 (128+64+32+16+8+4+2+1)

在 python 中,格式 int('00000111', 2) 是使用模 2(位)转换字符串数组 ['0'/'1']。这将返回值 7 模 10。

参考 DNS 标头: https://www2.cs.duke.edu/courses/fall16/compsci356/DNS/DNS-primer.pdf http://www.networksorcery.com/enp/protocol/dns.htm

【讨论】:

  • 太棒了!这就是我所追求的。您能否具体解释一下&amp; int('10000000', 2) 的用法以及为什么每个信息片段的二进制值都不同?
  • 添加了一些额外的信息...享受吧!
  • 如何在没有周围 x00\x00\x01\x00\x01' 的情况下提取 qname?
猜你喜欢
  • 2013-06-03
  • 1970-01-01
  • 2015-09-27
  • 1970-01-01
  • 2014-09-08
  • 1970-01-01
  • 2011-11-25
  • 2011-05-07
  • 1970-01-01
相关资源
最近更新 更多