[tor-commits] [ooni-probe/master] Implement TCP Connect port scanner

art at torproject.org art at torproject.org
Sat Mar 10 21:03:06 UTC 2012


commit 81b5de65d178d08ac1a01c7e84f8bde397e60c03
Author: Arturo Filastò <hellais at gmail.com>
Date:   Sat Mar 10 13:03:41 2012 -0800

    Implement TCP Connect port scanner
---
 assets/example.txt |    2 +
 ooniprobe.py       |   24 ++++++++------
 plugoo/assets.py   |    1 -
 plugoo/tests.py    |    6 ++--
 tests/tcpscan.py   |   84 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 103 insertions(+), 14 deletions(-)

diff --git a/assets/example.txt b/assets/example.txt
index e69de29..01ffcdf 100644
--- a/assets/example.txt
+++ b/assets/example.txt
@@ -0,0 +1,2 @@
+127.0.0.1:9050
+127.0.0.1:9051
diff --git a/assets/tcpscan.txt b/assets/tcpscan.txt
new file mode 100644
index 0000000..e69de29
diff --git a/ooniprobe.py b/ooniprobe.py
index a542153..814dab5 100755
--- a/ooniprobe.py
+++ b/ooniprobe.py
@@ -135,36 +135,40 @@ class ooni(object):
         """
         self.load_tests()
         for name in self.runtests:
-            print "running %s" % name
+             self.logger.info("running %s" % name)
             try:
                 self.tests[name].module.run(self)
             except Exception, e:
-                print "ERR: %s" % e
+                self.logger.error("ERR: %s" % e)
 
-    def run_test(self, test):
+    def run_test(self, test, asset):
         """
         Run a single test
         """
         self.load_tests()
-        self.tests[test].module.run(self)
+        self.tests[test].module.run(self, asset)
 
 if __name__ == "__main__":
 
     o = ooni()
 
     parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
-                                    description=getlogo()+'\n\n Open Observatory of Network Interference.')
+                                     description=getlogo() +
+                                     '\n\n Open Observatory of Network Interference.')
+
     parser.add_argument('-t', '--list-tests', help='List all the available tests',
-            action='store_true', dest='list_tests')
+                        action='store_true', dest='list_tests')
 
     parser.add_argument('-l', '--list-assets', help='List all the assets',
-            action='store_true', dest='list_assets')
+                        action='store_true', dest='list_assets')
 
     parser.add_argument('-r', '--run', help='Run a certain test', action='store')
 
-    parser.add_argument('-a', '--asset', help='Use this asset for the test', action='store')
+    parser.add_argument('-a', '--asset', help='Use this asset for the test',
+                        action='store')
 
-    parser.add_argument('--runall', help='Run all the tests in the config', action='store_true')
+    parser.add_argument('--runall', help='Run all the tests in the config',
+                        action='store_true')
 
     args = parser.parse_args()
 
@@ -175,7 +179,7 @@ if __name__ == "__main__":
         o.list_assets()
 
     if args.run:
-        o.run_test(args.run)
+        o.run_test(args.run, args.asset)
 
     elif args.runall:
         o.run_tests()
diff --git a/plugoo/assets.py b/plugoo/assets.py
index bb2cb78..6f566fe 100644
--- a/plugoo/assets.py
+++ b/plugoo/assets.py
@@ -45,4 +45,3 @@ class Asset:
         except:
             raise StopIteration
 
-
diff --git a/plugoo/tests.py b/plugoo/tests.py
index 0e87874..d57c0a8 100644
--- a/plugoo/tests.py
+++ b/plugoo/tests.py
@@ -12,10 +12,10 @@ class Test:
     This is a ooni probe Test.
     Also known as a Plugoo!
     """
-    def __init__(self, ooni):
+    def __init__(self, ooni, name="test"):
         self.config = ooni.config
         self.logger = ooni.logger
-        self.name = "test"
+        self.name = name
         self.report = Report(ooni,
                              scp=ooni.config.report.ssh,
                              file=ooni.config.report.file,
@@ -75,7 +75,7 @@ class Test:
                 # Append to the job queue
                 jobs.append(gevent.spawn(self.experiment, **args))
                 # If the buffer is full run the jobs
-                if i % buffer == (buffer-1):
+                if i % buffer == (buffer - 1):
                     # Run the jobs with the selected timeout
                     gevent.joinall(jobs, timeout=timeout)
                     for job in jobs:
diff --git a/tests/tcpscan.py b/tests/tcpscan.py
new file mode 100644
index 0000000..38322a1
--- /dev/null
+++ b/tests/tcpscan.py
@@ -0,0 +1,84 @@
+"""
+    TCP Port Scanner
+    ****************
+
+    Does a TCP connect scan on the IP:port pairs.
+
+"""
+import os
+import socket
+
+import socks
+
+from plugoo.assets import Asset
+from plugoo.tests import Test
+
+__plugoo__ = "TCP Port Scanner"
+__desc__ = "This a test template to be used to build your own tests"
+
+class TCPScanAsset(Asset):
+    """
+    This is the asset that should be used by the Test. It will
+    contain all the code responsible for parsing the asset file
+    and should be passed on instantiation to the test.
+    """
+    def __init__(self, file=None):
+        self = Asset.__init__(self, file)
+
+
+class TCPScan(Test):
+    """
+    The main Test class
+    """
+
+    def experiment(self, *a, **kw):
+        """
+        Fill this up with the tasks that should be performed
+        on the "dirty" network and should be compared with the
+        control.
+        """
+        addr = kw['data']
+        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        res = False
+        try:
+            self.logger.debug('Doing a connection to %s' % addr)
+            s.connect((addr.split(':')[0], int(addr.split(':')[1])))
+            res = True
+        except socket.error, msg:
+            self.logger.debug('Connection failed to %s: %s' % (addr, msg))
+
+        finally:
+            s.close()
+
+        return (addr, res)
+
+    def control(self):
+        """
+        Fill this up with the control related code.
+        """
+        return True
+
+def run(ooni, asset=None):
+    """
+    This is the function that will be called by OONI
+    and it is responsible for instantiating and passing
+    the arguments to the Test class.
+    """
+    config = ooni.config
+
+    # This the assets array to be passed to the run function of
+    # the test
+    if asset:
+        assets = [TCPScanAsset(asset)]
+    else:
+        assets = [TCPScanAsset(os.path.join(config.main.assetdir, \
+                                            "tcpscan.txt"))]
+
+    # Instantiate the Test
+    thetest = TCPScan(ooni)
+    ooni.logger.info("starting TCP Scan...")
+    # Run the test with argument assets
+    thetest.run(assets)
+    ooni.logger.info("finished.")
+
+



More information about the tor-commits mailing list