prelude-correlator/master: Make it possible to specify your own DShield database file

[email protected] Thu, 9 Jul 2009 17:57:27 +0200 (CEST)
Newsgroups gmane.comp.security.ids.prelude.cvs
Message-ID <[email protected]>
commit a08f613cf167d71692e9ff61582618d901a90e84
Author: Yoann Vandoorselaere <[email protected]>
Date:   Mon Jul 6 11:55:29 2009 +0200

    Make it possible to specify your own DShield database file
    
    This is useful on system with no direct internet access, so that
    a static DShield database can be used by using the 'filename'
    option in combination with a 'reload' option set to 0.


========================================

 PreludeCorrelator/plugins/dshield.py |   38 +++++++++++++++++----------------
 1 files changed, 20 insertions(+), 18 deletions(-)

========================================

diff --git a/PreludeCorrelator/plugins/dshield.py b/PreludeCorrelator/plugins/dshield.py
index 22fe9a5..630cd47 100644
--- a/PreludeCorrelator/plugins/dshield.py
+++ b/PreludeCorrelator/plugins/dshield.py
@@ -30,49 +30,50 @@ class DshieldPlugin(Plugin):
     DSHIELD_SERVER = "www.dshield.org"
     DSHIELD_URI = "/ipsascii.html?limit=10000"
     DSHIELD_TIMEOUT = 10
+    DSHIELD_FILENAME = siteconfig.lib_dir + "/dshield.dat"
 
     def __ipNormalize(self, ip):
         return ".".join([ i.lstrip("0") for i in ip.split(".") ])
 
-    def __loadData(self, fname, age=0):
-        cnt = 0
+    def __loadData(self, age=0):
         self.__iphash.clear()
 
-        for line in open(fname, "r"):
+        for line in open(self.__filename, "r"):
             if line[0] == '#':
                 continue
 
             ip, reports, attacks, first_seen, last_seen = line.split('\t')
             self.__iphash[self.__ipNormalize(ip)] = (int(reports), int(attacks), first_seen, last_seen)
 
-            cnt = cnt + 1
-
-        Timer(self.__reload - age, self.__retrieveData).start()
-
-    def __retrieveData(self, timer=None):
-        fname = siteconfig.lib_dir + "/dshield.dat"
-
-        try:
-            st = os.stat(fname)
-            if time.time() - st.st_mtime < self.__reload:
-                return self.__loadData(fname, time.time() - st.st_mtime)
-        except:
-            pass
+        if self.__reload > 0:
+            Timer(self.__reload - age, self.__retrieveData).start()
 
+    def __downloadData(self):
         self.info("Downloading host list, this might take some time...")
 
         con = httplib.HTTPConnection(self.__server, timeout=self.__timeout)
+
         con.request("GET", self.__uri)
         r = con.getresponse()
         if r.status != 200:
             raise Exception, "Could not download DShield host list, error %d" % r.status
 
-        fd = open(fname, "w")
+        fd = open(self.__filename, "w")
         fd.write(r.read())
         fd.close()
 
         self.info("Downloading done, processing data.")
-        self.__loadData(fname)
+
+    def __retrieveData(self, timer=None):
+        try:
+            st = os.stat(self.__filename)
+            if self.__reload <= 0 or time.time() - st.st_mtime < self.__reload:
+                return self.__loadData(time.time() - st.st_mtime)
+        except OSError:
+            pass
+
+        self.__downloadData()
+        self.__loadData()
 
 
     def __init__(self, env):
@@ -80,6 +81,7 @@ class DshieldPlugin(Plugin):
 
         self.__iphash = { }
         self.__reload = self.getConfigValue("reload", self.DSHIELD_RELOAD)
+        self.__filename = self.getConfigValue("filename", self.DSHIELD_FILENAME)
         self.__server = self.getConfigValue("server", self.DSHIELD_SERVER)
         self.__uri = self.getConfigValue("uri", self.DSHIELD_URI)
         self.__timeout = float(self.getConfigValue("timeout", self.DSHIELD_TIMEOUT))
_______________________________________________
Prelude-cvslog site list
[email protected]
http://lists.prelude-ids.org/mailman/listinfo/prelude-cvslog