Search code examples
pythonnetwork-programmingdnsscapy

Reading Raw Bytes using Scapy?


I have a project in which I am building a DNS Forwarder.

I am using a UDP Server socket to listen to the DNS requests on port 53 (client is using the dig command) and I have to forward the received raw DNS request to scapy for dissecting it. I know that scapy is used to forge/send/manipulate packets.

The raw request looks something like this -

b'\xd6t\x01\x00\x01\x00\x00\x00\x00\x00\x01\x03www\x08facebook\x03com\x00\x00\x01\x00\x01\x00\x00)\x10\x00\x00\x00\x00\x00\x00\x00'

Is there a method/function in scapy that is used to import the raw DNS requests to get the flags, what record is being asked?


Solution

  • Just initialise a DNS payload with your byte string:

    from scapy.all import DNS
    p = DNS(b'\xd6t\x01\x00\x01\x00\x00\x00\x00\x00\x01\x03www\x08facebook\x03com\x00\x00\x01\x00\x01\x00\x00)\x10\x00\x00\x00\x00\x00\x00\x00')
    

    You can then access its field:

    print(p.id)
    print(p.opcode)
    ...
    

    [Edit] And to print all its content:

    p.show()