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数据包的基本原理后,我发现它们的结构如下:
QR | OpCode | AA | TC | RD | RA | Z | AD | CD | RCODE
然后我将数据包解码为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和操作码)在哪里?我解码错误了吗
说清楚一点,我不想使用外部库,我的目标是了解DNS数据包的结构以及如何解码它们;我知道像dnslib
和scapy
这样的库
我不是插座专家。从引用-DNS标头由位而不是字节组成。。。所以需要将其解析为位。使用字节和掩码位。见下面的示例。它不确定标头hdr[12]的内容是什么
以下是基于上述规范的一些示例代码:
输出:
DNS包字典 {'ID':257,'QR':False,'Opcode':0,'AA':False,'TC':False,'RD':False,'RA':False,'Z':False,'AD':False,'CD':False,'RCode':False,'QDCOUNT':0,'NSCOUNT':0,'ARCOUNT':3,'QTYPE':1,'QCLASS':0,'QNAME':'www.googletagmanager.com'}
Pyhon位操作
指
一个字节(
b'xxxx'
)表示4个字节。每个字节由8位组成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 1111111-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.pdfhttp://www.networksorcery.com/enp/protocol/dns.htm
相关问题 更多 >
编程相关推荐