Having trouble building a Dns Packet in Python

自闭症网瘾萝莉.ら 提交于 2019-12-12 08:08:50

问题


I'm trying to build a dns packet to send over a socket. I don't want to use any libraries because I want direct access to the socket variable that sends it. Whenever I send the DNS packet, wireshark says that it's malformed. What exactly am I doing wrong?

Some things that are wrong with the Dns packet itself: It says it has 256 questions, no class and no type

  class DnsPacketBuilder:

def __init__(self):
    pass

def build_packet(self, url):
    packet = struct.pack("H", 12049)  # Query Ids (Just 1 for now)
    packet += struct.pack("H", 256)  # Flags
    packet += struct.pack("H", 1)  # Questions
    packet += struct.pack("H", 0)  # Answers
    packet += struct.pack("H", 0)  # Authorities
    packet += struct.pack("H", 0)  # Additional
    split_url = url.split(".")
    for part in split_url:
        packet += struct.pack("B", len(part))
        for byte in bytes(part):
            packet += struct.pack("c", byte)
    packet += struct.pack("B", 0)  # End of String
    packet += struct.pack("H", 1)  # Query Type
    packet += struct.pack("H", 1)  # Query Class
    return packet

# Sending the packet
builder = DnsPacketBuilder()
packet = builder.build_packet("www.northeastern.edu")
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('', 8888))
sock.settimeout(2)
sock.sendto(bytes(packet), ("208.67.222.222", 53))
print("Packet Sent")
data, addr = sock.recvfrom(1024)
print("Response: " + data)
sock.close()

回答1:


Your system is using "little endian" byte order natively.

You need to reverse the byte order of the 16-bit fields into "big endian" (aka "network order") using the ">H" format string in struct.pack().



来源:https://stackoverflow.com/questions/24814044/having-trouble-building-a-dns-packet-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!