[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [ooni-probe/master] Half working mutator thing



commit 614e7b51762fb99018ab0022b275969c00684b31
Author: Arturo Filastò <hellais@xxxxxxxxxxxxxx>
Date:   Tue Jul 10 19:37:04 2012 +0200

    Half working mutator thing
---
 oonib/b0wser.py      |  117 ++++++++++++++++++++++++++++++++++++++++++++++++++
 oonib/oonibackend.py |    4 ++
 2 files changed, 121 insertions(+), 0 deletions(-)

diff --git a/oonib/b0wser.py b/oonib/b0wser.py
new file mode 100644
index 0000000..072b4b9
--- /dev/null
+++ b/oonib/b0wser.py
@@ -0,0 +1,117 @@
+from twisted.internet import protocol
+
+class Mutator:
+    idx = 0
+    step = 0
+    state = 0
+    waiting = False
+    waiting_step = 0
+
+    def __init__(self, steps):
+        self.steps = steps
+
+    def next_mutation(self):
+        """
+        Increases by one the mutation state.
+
+        ex. (* is the mutation state, i.e. the byte to be mutated)
+        before [___*] [____]
+               step1   step2
+        after  [____] [*___]
+        """
+        current_idx = self.idx + 1
+        current_step = self.step
+        current_data = self.steps[c_step]['data']
+        data_to_receive = self.steps[c_step]]['recv']
+
+        if self.waiting and self.waiting_step == data_to_receive:
+            self.waiting = False
+            self.waiting_step = 0
+
+        elif self.waiting:
+            self.waiting_step += 1
+
+        elif current_idx > len(current_data):
+            self.step += 1
+            self.idx = 0
+            self.waiting = True
+
+    @classmethod
+    def mutate(data, idx):
+        """
+        Mutate the idx bytes by increasing it's value by one
+
+        @param data: the data to be mutated.
+
+        @param idx: what byte should be mutated.
+        """
+        ret = data[:idx-1]
+        ret += chr(ord(data[idx]) + 1)
+        ret += data[idx:]
+        return ret
+
+    def get_mutation(self):
+        """
+        returns the current packet to be sent to the wire.
+        If no mutation is necessary it will return the plain data.
+        """
+        self.next_mutation()
+        if self.state != self.step or self.waiting:
+            return self.steps[self.state]
+
+        data = self.steps[self.state]
+        return self.mutate(data, self.idx)
+
+    def get_data(self, i):
+        """
+        XXX remove this shit.
+        """
+        j = 0
+        pkt_size = len(self.steps[j]['data'])
+        while i > pkt_size:
+            j += 1
+            pkt_size += len(self.steps[j])
+        # I am not in a state to send mutations
+        if j != self.state:
+            return self.steps[j]['data']
+
+        rel_idx = i % (pkt_size - len(self.steps[j-1]))
+        data = self.steps[j]
+        data[rel_idx] = chr(ord(data[rel_idx]) + 1)
+        return data
+
+class B0wserProtocol(protocol.Protocol):
+    steps = [{'data': "STEP1", 'recv': 20},
+             {'data': "STEP2", 'recv': 20},
+             {'data': "STEP3", 'recv': 20}]
+
+    mutator = None
+    state = 0
+    received_data = 0
+
+    def next_state(self):
+        data = self.mutator.get_mutation()
+        self.transport.write(data)
+        self.mutator.state += 1
+        self.received_data = 0
+
+    def dataReceived(self, data):
+        if len(self.steps) <= self.state:
+            self.transport.loseConnection()
+            return
+        self.received_data += len(data)
+        if self.received_data >= self.steps[self.state]['recv']:
+            print self.received_data
+            self.next_state()
+
+class B0wserServer(protocol.ServerFactory):
+    protocol = B0wserProtocol
+    mutations = {}
+    def buildProtocol(self, addr):
+        p = self.protocol()
+        p.factory = self
+
+        if addr.host not in self.mutations:
+            self.mutations[addr.host] = Mutation(p.steps)
+        p.mutator = self.mutations[addr.host]
+        return p
diff --git a/oonib/oonibackend.py b/oonib/oonibackend.py
index fc5bbdb..4ae275e 100755
--- a/oonib/oonibackend.py
+++ b/oonib/oonibackend.py
@@ -18,6 +18,7 @@ from twisted.names import dns
 
 from oonib.httpbackend import HTTPBackend
 from oonib.dnsbackend import ProxyDNSServer
+from oonib.b0wser import B0wserServer
 
 # This tells twisted to set the
 server.version = "Apache"
@@ -32,3 +33,6 @@ internet.TCPServer(8002, TCPDNSServer).setServiceParent(serviceCollection)
 UDPFactory = dns.DNSDatagramProtocol(TCPDNSServer)
 internet.UDPServer(5354, UDPFactory).setServiceParent(serviceCollection)
 
+# Start the ooni backend thing
+b0wser = B0wserServer()
+internet.TCPServer(9666, b0wser).setServiceParent(serviceCollection)



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits