Skip to content

Instantly share code, notes, and snippets.

@thepacketgeek
Last active July 7, 2023 11:43
Show Gist options
  • Save thepacketgeek/6928674 to your computer and use it in GitHub Desktop.
Save thepacketgeek/6928674 to your computer and use it in GitHub Desktop.
Simple DNS Query with Scapy
from scapy.all import *
answer = sr1(IP(dst="8.8.8.8")/UDP(dport=53)/DNS(rd=1,qd=DNSQR(qname="www.thepacketgeek.com")),verbose=0)
print answer[DNS].summary()
@Dodain
Copy link

Dodain commented Jun 16, 2016

Hi,
I need your help . can you please help me with the following

I need to use scapy or similar library to generate network packets to transfer files e.g. ASCII, JPG, PDF from one machine to another while pretending to be the following legitimate protocol

DNS
Skype
Windows SMB

While reviewing the header the packet should resemble like the legit protocol.

@MarryamZulfiqar
Copy link

scapy sr1(IP(dst="8.8.8.8")/UDP(dport=53)/DNS(rd=1,qd=DNSQR(qname="www.thepacketgeek.com")),verbose=0)

when we try to run this command on the terminal, it generates an error unexpected "("

@fwaechter
Copy link

@MarryamZulfiqar Scapy has a REPL. Just type scapy to your console (needs a superuser/admin account) and start forging packets and requests. Here's the documentation: https://scapy.readthedocs.io/en/latest/usage.html#simple-one-liners

@txhai
Copy link

txhai commented Nov 13, 2019

Please help, when I try
sr1(IP(dst="8.8.8.8")/UDP(dport=53)/DNS(rd=1,qd=DNSQR(qname="google.com")),verbose=0)
It hangs, but when I use
sr1(IP(dst="192.168.1.1")/UDP(dport=53)/DNS(rd=1,qd=DNSQR(qname="google.com")),verbose=0), everything works.
192.168.1.1 is my wifi gateway, I don't know how to connect to 8.8.8.8

Edited:
Problem is solved by adding sport.
sr1(IP(dst="8.8.8.8")/UDP(sport=RandShort(), dport=53)/DNS(rd=1,qd=DNSQR(qname="google.com")),verbose=0)

@setrus
Copy link

setrus commented Feb 17, 2021

I suggest getting some DNS bins online to track your requests. In this case I used requestbin.net
Here is an example that works

>>> ans = sr1(IP(dst="8.8.8.8")/UDP(sport=RandShort(), dport=53)/DNS(rd=1,qd=DNSQR(qname="7a645c14a2eaac.d.requestbin.net",qtype="A")))
Begin emission:
Finished to send 1 packets.
*
Received 1 packets, got 1 answers, remaining 0 packets
>>> **ans.an.rdata**
'127.0.0.1'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment