-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.py
154 lines (136 loc) · 5.1 KB
/
dns.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import os
import argparse
from twisted.internet import reactor, protocol
from twisted.names import dns, server
from twisted.python import log
script_dir = os.path.dirname(os.path.abspath(__file__))
class SimpleDNSProtocol(protocol.DatagramProtocol):
def datagramReceived(self, datagram, address):
message = dns.Message()
try:
message.fromStr(datagram)
except (EOFError, dns.UnknownOpcode):
return
for query in message.queries:
self.logQuery(query, address)
response = self.createResponse(message, query)
self.transport.write(response.toStr(), address)
def logQuery(self, query, address):
log.msg(
f"DNS Query Received - Query Name: {query.name.name}, Type: {dns.QUERY_TYPES.get(query.type, 'UNKNOWN')}, Class: {dns.QUERY_CLASSES.get(query.cls, 'UNKNOWN')}, From: {address}"
)
def createResponse(self, message, query):
response = dns.Message(id=message.id, answer=1)
response.queries = [query]
query_name_str = (
query.name.name.decode()
if isinstance(query.name.name, bytes)
else query.name.name
)
if query.type == dns.A:
answer = dns.RRHeader(
name=query_name_str,
type=dns.A,
cls=query.cls,
ttl=60,
payload=dns.Record_A(address="127.0.0.1"),
)
response.answers.append(answer)
elif query.type == dns.AAAA:
answer = dns.RRHeader(
name=query_name_str,
type=dns.AAAA,
cls=query.cls,
ttl=60,
payload=dns.Record_AAAA(address="::1"),
)
response.answers.append(answer)
elif query.type == dns.TXT:
answer = dns.RRHeader(
name=query_name_str,
type=dns.TXT,
cls=query.cls,
ttl=60,
payload=dns.Record_TXT(data=["dummy response"]),
)
response.answers.append(answer)
elif query.type == dns.MX:
answer = dns.RRHeader(
name=query_name_str,
type=dns.MX,
cls=query.cls,
ttl=60,
payload=dns.Record_MX(preference=10, exchange="mail.example.com"),
)
response.answers.append(answer)
elif query.type == dns.CNAME:
answer = dns.RRHeader(
name=query_name_str,
type=dns.CNAME,
cls=query.cls,
ttl=60,
payload=dns.Record_CNAME(name="cname.example.com"),
)
response.answers.append(answer)
elif query.type == dns.NS:
answer = dns.RRHeader(
name=query_name_str,
type=dns.NS,
cls=query.cls,
ttl=60,
payload=dns.Record_NS(name="ns.example.com"),
)
response.answers.append(answer)
elif query.type == dns.SOA:
answer = dns.RRHeader(
name=query_name_str,
type=dns.SOA,
cls=query.cls,
ttl=60,
payload=dns.Record_SOA(
mname="ns.example.com",
rname="hostmaster.example.com",
serial=12345,
refresh=3600,
retry=600,
expire=86400,
minimum=3600,
),
)
response.answers.append(answer)
elif query.type == dns.PTR:
answer = dns.RRHeader(
name=query_name_str,
type=dns.PTR,
cls=query.cls,
ttl=60,
payload=dns.Record_PTR(name="ptr.example.com"),
)
response.answers.append(answer)
else:
log.msg(f"Received unsupported DNS query type: {query.type}")
return response
class SimpleDNSServerFactory(server.DNSServerFactory):
def __init__(self):
clients = [SimpleDNSProtocol()]
super(SimpleDNSServerFactory, self).__init__(clients=clients)
def main():
LOG_FILE_PATH = os.path.join(script_dir, "dns_honeypot.log")
parser = argparse.ArgumentParser(description="Run a DNS honeypot server.")
parser.add_argument(
"--host", type=str, default="0.0.0.0", help="Host to bind the server to."
)
parser.add_argument(
"--port", type=int, default=5353, help="Port to bind the server to."
)
args = parser.parse_args()
print(f"DNS HONEYPOT ACTIVE ON HOST: {args.host}, PORT: {args.port}")
print(f"ALL DNS queries will be logged in: {LOG_FILE_PATH}")
log_observer = log.FileLogObserver(open(LOG_FILE_PATH, "a"))
log.startLoggingWithObserver(log_observer.emit, setStdout=False)
dns_factory = SimpleDNSServerFactory()
reactor.listenUDP(args.port, SimpleDNSProtocol(), interface=args.host)
reactor.listenTCP(args.port, dns_factory, interface=args.host)
reactor.run()
if __name__ == "__main__":
main()