Чтение DNS-пакета в Python

Nov 29 2020

Этот вопрос задавался раньше, но он никогда не был полностью рассмотрен, и относится к 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 | РА | 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 и OpCode? Я неправильно расшифровываю?

Чтобы было ясно, я не хочу использовать внешнюю библиотеку, и моя цель - понять, как структурированы пакеты DNS и как их декодировать; Мне известны такие библиотеки, как dnslibи scapy.

Ответы

2 frankr6591 Nov 29 2020 at 21:50

Я не эксперт по розеткам. Из справки - заголовок 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': False, 'Opcode': 0, 'AA': False, 'TC': False, 'RD': False, 'RA': False, '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) - это преобразование массива строк ['0' / '1'] с использованием модуля 2 (биты). Это возвращает значение 7 по модулю 10.

Заголовок справочного DNS: https://www2.cs.duke.edu/courses/fall16/compsci356/DNS/DNS-primer.pdf http://www.networksorcery.com/enp/protocol/dns.htm