-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse_dns.py
64 lines (50 loc) · 1.57 KB
/
reverse_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
#!/usr/bin/env python3
from dnslib import RCODE, RR, CNAME, QTYPE
from dnslib.server import DNSServer, BaseResolver
from time import sleep
import json
import argparse
def arg_parse():
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config", required=True, help="Input with json config")
args = parser.parse_args()
return args
class Resolver(BaseResolver):
def __init__(self, config):
self.config = config
def resolve(self,request,handler):
reply = request.reply()
segements = request.q.qname.label[0:4]
ip = ".".join(reversed([str(i, 'utf-8') for i in segements]))
if ip in self.config.keys():
url = self.config[ip]
reply.add_answer(RR(request.q.qname, QTYPE.CNAME, ttl=60, rdata=CNAME(url)))
reply.header.rcode = getattr(RCODE, "NOERROR")
else:
reply.header.rcode = getattr(RCODE,'NXDOMAIN')
return reply
def main():
args = arg_parse()
# Read config content
configPath = args.config
with open(configPath, 'r') as f:
config = json.load(f)
if not config:
return -1
resolver = Resolver(config)
servers = [
DNSServer(resolver, port=5053, address="0.0.0.0", tcp=True),
DNSServer(resolver, port=5053, address="0.0.0.0", tcp=False)
]
for server in servers:
server.start_thread()
try:
while True:
sleep(1)
except KeyboardInterrupt:
pass
finally:
for server in servers:
server.stop()
if __name__ == '__main__':
main()