-
Notifications
You must be signed in to change notification settings - Fork 4
/
pns.py
executable file
·74 lines (61 loc) · 2.09 KB
/
pns.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
#!/usr/bin/env python
#
# pns.py is a simple DNS server which only allows pornagraphic material through.
#
# Bits and pieces robbed from here:
# https://gist.github.com/johnboxall/1147973
# http://twistedmatrix.com/trac/wiki/TwistedWeb
import socket
import dns.resolver as DNS
from twisted.internet.protocol import Factory, Protocol
from twisted.internet import reactor
from twisted.names import dns
from twisted.names import client, server
from twisted.web import server as webserver
from twisted.web import resource
import sys
index = open('index.html').read()
class Simple(resource.Resource):
isLeaf = True
def render_GET(self, request):
return index
class DNSServerFactory(server.DNSServerFactory):
def gotResolverResponse(self, (ans, auth, add), protocol, message, address):
qname = message.queries[0].name.name
print qname
blocked_resolver = DNS.Resolver()
blocked_resolver.nameservers = ['208.67.222.123','208.67.220.123']
porn = False
results = blocked_resolver.query(qname)
for r in results:
print r
if str(r).startswith('67.215.65'):
print "PRON"
porn = True
if porn == False:
print "NOT PRON"
for answer in ans:
if answer.type != dns.A:
continue
answer.payload.address = socket.inet_aton(ip_address)
answer.payload.ttl = 60
#address = ('127.0.0.1', 43160)
args = (self, (ans, auth, add), protocol, message, address)
result=server.DNSServerFactory.gotResolverResponse(*args)
print result
return result
verbosity = 0
ip_address = ""
if len(sys.argv) > 1:
ip_address = sys.argv[1]
else:
ip_address = '127.0.0.1'
resolver = client.Resolver(servers=[('8.8.8.8', 53)])
factory = DNSServerFactory(clients=[resolver], verbose=verbosity)
protocol = dns.DNSDatagramProtocol(factory)
factory.noisy = protocol.noisy = verbosity
reactor.listenUDP(53, protocol)
reactor.listenTCP(53, factory)
site = webserver.Site(Simple())
reactor.listenTCP(8080, site)
reactor.run()