7

I just want to create a DNS Server who listen to requests and always return the same specific ip. I'm using Python....

Larry Valdes
  • 103
  • 1
  • 1
  • 5

1 Answers1

12

Take a look at the dnslib module, specifically, dnslib.server.

class TestResolver:
     def resolve(self,request,handler):
         reply = request.reply()
         reply.add_answer(*RR.fromZone("abc.def. 60 A 1.2.3.4"))
         return reply
resolver = TestResolver()
server = DNSServer(resolver,port=8053,address="localhost",logger=logger,tcp=True)
server.start_thread()
a = q.send("localhost",8053,tcp=True)
    Request: [...] (tcp) / 'abc.def.' (A)
    Reply: [...] (tcp) / 'abc.def.' (A) / RRs: A
print(DNSRecord.parse(a))
    ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: ...
    ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
    ;; QUESTION SECTION:
    ;abc.def.                       IN      A
    ;; ANSWER SECTION:
    abc.def.                60      IN      A       1.2.3.4
server.stop()
Anonymous
  • 738
  • 4
  • 14
  • 36
Boa
  • 2,609
  • 1
  • 23
  • 38
  • this example is missing code (ex: declaration of `q`), and appears to have been copied from code in https://github.com/paulc/dnslib/blob/master/dnslib/server.py – BLuFeNiX Apr 15 '22 at 20:33