[tor-commits] [ooni-probe/master] Add scapy protocol and example for it

art at torproject.org art at torproject.org
Wed Jun 13 23:22:27 UTC 2012


commit 654be17e244713320f6d218f447de1a62546c6b3
Author: Arturo Filastò <art at baculo.org>
Date:   Thu Jun 14 01:24:36 2012 +0200

    Add scapy protocol and example for it
    * Add example of writing a tcp connect scanner
---
 ooni/plugins/examplescapy.py |   49 +++++++++++++++++++++++++++++++++
 ooni/plugins/tcpconnect.py   |   62 ++++++++++++++++++++++++++++++++++++++++++
 ooni/protocols/scapy.py      |   49 +++++++++++++++++++++++++++++++++
 3 files changed, 160 insertions(+), 0 deletions(-)

diff --git a/ooni/plugins/examplescapy.py b/ooni/plugins/examplescapy.py
new file mode 100644
index 0000000..aa6d81b
--- /dev/null
+++ b/ooni/plugins/examplescapy.py
@@ -0,0 +1,49 @@
+import random
+from zope.interface import implements
+from twisted.python import usage
+from twisted.plugin import IPlugin
+from twisted.internet import protocol, defer
+from ooni.plugoo.tests import ITest, OONITest
+from ooni.plugoo.assets import Asset
+from ooni import log
+from ooni.protocols.scapy import ScapyTest
+
+from ooni.lib.txscapy import txsr, txsend
+
+class scapyArgs(usage.Options):
+    optParameters = []
+
+class ExampleScapyTest(ScapyTest):
+    """
+    An example of writing a scapy Test
+    """
+    implements(IPlugin, ITest)
+
+    shortName = "example_scapy"
+    description = "An example of a scapy test"
+    requirements = None
+    options = scapyArgs
+    blocking = False
+
+    receive = True
+    pcapfile = 'example_scapy.pcap'
+    def initialize(self, reactor=None):
+        if not self.reactor:
+            from twisted.internet import reactor
+            self.reactor = reactor
+
+        self.request = {}
+        self.response = {}
+
+    def build_packets(self):
+        """
+        Override this method to build scapy packets.
+        """
+        from scapy.all import IP, TCP
+        return IP()/TCP()
+
+    def load_assets(self):
+        return {}
+
+examplescapy = ExampleScapyTest(None, None, None)
+
diff --git a/ooni/plugins/tcpconnect.py b/ooni/plugins/tcpconnect.py
new file mode 100644
index 0000000..75f32c1
--- /dev/null
+++ b/ooni/plugins/tcpconnect.py
@@ -0,0 +1,62 @@
+"""
+This is a self genrated test created by scaffolding.py.
+you will need to fill it up with all your necessities.
+Safe hacking :).
+"""
+from zope.interface import implements
+from twisted.python import usage
+from twisted.plugin import IPlugin
+from twisted.internet import reactor
+from twisted.internet.protocol import Factory, Protocol
+from twisted.internet.endpoints import TCP4ClientEndpoint
+
+from ooni.plugoo.tests import ITest, OONITest
+from ooni.plugoo.assets import Asset
+from ooni import log
+
+class tcpconnectArgs(usage.Options):
+    optParameters = [['blabla', 'a', None, 'Asset file'],
+                     ['resume', 'r', 0, 'Resume at this index']]
+
+class tcpconnectTest(OONITest):
+    implements(IPlugin, ITest)
+
+    shortName = "tcpconnect"
+    description = "tcpconnect"
+    requirements = None
+    options = tcpconnectArgs
+    blocking = False
+
+    def experiment(self, args):
+        host, port = args['blabla'].split(':')
+        class DummyFactory(Factory):
+            def buildProtocol(self, addr):
+                return Protocol()
+
+        def gotProtocol(p):
+            p.transport.loseConnection()
+            log.msg("Got a connection!")
+            log.msg(str(p))
+            return {'result': True, 'target': [host, port]}
+
+        def gotError(err):
+            log.msg("Had error :(")
+            log.msg(err)
+            return {'result': False, 'target': [host, port]}
+
+        # What you return here gets handed as input to control
+        point = TCP4ClientEndpoint(reactor, host, int(port))
+        d = point.connect(DummyFactory())
+        d.addCallback(gotProtocol)
+        d.addErrback(gotError)
+        return d
+
+    def load_assets(self):
+        if self.local_options:
+            return {'blabla': Asset(self.local_options['blabla'])}
+        else:
+            return {}
+
+# We need to instantiate it otherwise getPlugins does not detect it
+# XXX Find a way to load plugins without instantiating them.
+tcpconnect = tcpconnectTest(None, None, None)
diff --git a/ooni/protocols/__init__.py b/ooni/protocols/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/ooni/protocols/scapy.py b/ooni/protocols/scapy.py
new file mode 100644
index 0000000..ba2ae66
--- /dev/null
+++ b/ooni/protocols/scapy.py
@@ -0,0 +1,49 @@
+import random
+from zope.interface import implements
+from twisted.python import usage
+from twisted.plugin import IPlugin
+from twisted.internet import protocol, defer
+from ooni.plugoo.tests import ITest, OONITest
+from ooni.plugoo.assets import Asset
+from ooni import log
+
+from ooni.lib.txscapy import txsr, txsend
+
+class ScapyTest(OONITest):
+    """
+    A utility class for writing scapy driven OONI tests.
+    """
+
+    receive = True
+    pcapfile = 'scapytest.pcap'
+    def initialize(self, reactor=None):
+
+        if not self.reactor:
+            from twisted.internet import reactor
+            self.reactor = reactor
+
+        self.request = {}
+        self.response = {}
+
+    def experiment(self, args):
+        log.msg("Running experiment")
+        if self.receive:
+            d = txsr(self.build_packets(), pcapfile=self.pcapfile)
+        else:
+            d = txsend(self.build_packets())
+        def finished(data):
+            return data
+
+        d.addCallback(finished)
+        return d
+
+    def build_packets(self):
+        """
+        Override this method to build scapy packets.
+        """
+        from scapy.all import IP, TCP
+        return IP()/TCP()
+
+    def load_assets(self):
+        return {}
+



More information about the tor-commits mailing list