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):passdef build_packet(self, url):packet = struct.pack("H", 12049) # Query Ids (Just 1 for now)packet += struct.pack("H", 256) # Flagspacket += struct.pack("H", 1) # Questionspacket += struct.pack("H", 0) # Answerspacket += struct.pack("H", 0) # Authoritiespacket += struct.pack("H", 0) # Additionalsplit_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 Stringpacket += struct.pack("H", 1) # Query Typepacket += struct.pack("H", 1) # Query Classreturn 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()