SickSad

Don't Worry Government, I Got This Porn Filter Sorted

So i hear the UK government wants to make a porn filter. About bloody time i reckon. I’m fed up of happily browsing the Internet for boobs, only to have non-porn related subject matter thrust down my face hole.

So taking inspiration from other great Internet filtering nations such as North Korea, China, Syria, Iran, Cuba, Bahrain, Belarus, Burma, Uzbekistan, Saudia Arabia and Vietnam I decided to help out the UK government and build an Internet filter that only allows pornographic material through.

You’re Welcome.

Setting Up

All code is available here https://github.com/SickSad/UKPR0nFilter

Just follow this simple step-by-step video walk-through and you’ll have a porn filter running in no time!

Nerd Stuff

The filter is a dns server which checks all queries against the OpenDNS FamilySheild DNS server. Any request that is denied by OpenDNS is then allowed by our DNS server, and any request allowed by OpenDNS is blocked by us.

The server itself it built using the python Twisted framework which handles both the DNS requests and acts as a simple web-server to host the denial page.

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 pornographic 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()