# HG changeset patch # User ft # Date 1392734280 -3600 # Node ID ca2023eb2463d7524101ec1b8f23b378e1fe1953 Initial commit working ikarus scanner engine diff -r 000000000000 -r ca2023eb2463 config/IkarusScanner.cfg --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/config/IkarusScanner.cfg Tue Feb 18 15:38:00 2014 +0100 @@ -0,0 +1,12 @@ +[Main] +# the maximum file size in MB that is scanned +MaxFileSize: 50 + +# the URL of the local scan server +LocalScanserverURL: http://localhost/virusscan + +# the URL of the remote scan server +RemoteScanserverURL: http://192.168.63.129/virusscan + +# wait time in seconds until a new connection attempt to remote server is made +RetryTimeout: 600 \ No newline at end of file diff -r 000000000000 -r ca2023eb2463 src/IkarusScanner.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/IkarusScanner.py Tue Feb 18 15:38:00 2014 +0100 @@ -0,0 +1,143 @@ +#!/usr/bin/python + +import ConfigParser + +import sys + +import logging +import os +import errno +import time + +import urllib3 + +class IkarusScanner: + + # User the existing logger instance + __LOG = logging.getLogger("IkarusScanner") + + __MINOPTS = { "Main" : ["LocalScanserverURL", "RemoteScanserverURL", "MaxFileSize", "RetryTimeout"]} + __CONFIG_NOT_READABLE = "Configfile is not readable" + __CONFIG_WRONG = "Something is wrong with the config" + __CONFIG_MISSING = "Section: \"%s\" Option: \"%s\" in configfile is missing" + __LOCAL_SCANSERVER_URL = "" + __REMOTE_SCANSERVER_URL = "" + __STATUS_CODE_OK = 200 + __STATUS_CODE_INFECTED = 210 + __STATUS_CODE_NOT_FOUND = 404 + __MAX_SCAN_FILE_SIZE = 50 * 0x100000 + __SCANSERVER_RETRY_TIMEOUT = 60 + + # Global http pool manager used to connect to the scan server + __remoteScanserverReachable = True + __scanserverTimestamp = 0 + __httpPool = urllib3.PoolManager(num_pools = 1, timeout = 3) + + def __init__ (self, scanner_config_path): + config = self.loadConfig (scanner_config_path) + + self.__scanserverTimestamp = time.time() + + __LOCAL_SCANSERVER_URL = config.get("Main", "LocalScanserverURL") + __REMOTE_SCANSERVER_URL = config.get("Main", "RemoteScanserverURL") + __SCANSERVER_RETRY_TIMEOUT = int(config.get("Main", "RetryTimeout")) + + # Convert file size from MB to byte + __MAX_SCAN_FILE_SIZE = int(config.get("Main", "MaxFileSize")) * 0x100000 + + + def checkMinimumOptions (self, config): + for section, options in self.__MINOPTS.iteritems (): + for option in options: + if (config.has_option(section, option) == False): + self.__LOG.error (self.__CONFIG_MISSING % (section, option)) + exit (129) + + def loadConfig (self, scanner_config_path): + + configfile = scanner_config_path + config = ConfigParser.SafeConfigParser () + + if ((os.path.exists (scanner_config_path) == False) or (os.path.isfile (scanner_config_path) == False) or (os.access (scanner_config_path, os.R_OK) == False)): + self.__LOG.error(self.__CONFIG_NOT_READABLE); + raise SystemError(self.__CONFIG_NOT_READABLE) + + try: + config.read (scanner_config_path) + except Exception, e: + self.__LOG.error("Error: %s" % (e)); + raise SystemError("Error: %s" % (e)) + + self.checkMinimumOptions (config) + + return config + + def contactScanserver(self, url, fields): + return httpPool.request_encode_body('POST', url, fields = fields, retries = 0) + + def scanFile (self, path, fileobject): + return self.scanFileIkarus (path, fileobject) + + def scanFileIkarus (self, path, fileobject): + retval = { "infected" : False, "virusname" : "Unknown" } + self.__LOG.debug ("Scan File: %s" % (path)) + + + + if (os.fstat(fileobject.fileno()).st_size > self.__MAX_SCAN_FILE_SIZE): + self.__LOG.info("File max size exceeded. The file is not scanned.") + retval["infected"] = True + retval["virusname"] = "File is to big to be scanned." + return retval + + fields = { 'up_file' : fileobject.read() } + + if (self.__remoteScanserverReachable == False) and ((self.__scanserverTimestamp + self.__SCANSERVER_RETRY_TIMEOUT) < time.time()): + self.__remoteScanserverReachable = True + + if self.__remoteScanserverReachable: + try: + response = contactScanserver(self.__REMOTE_SCANSERVER_URL, fields) + # We should catch socket.error here, but this does not work. Needs checking. + except: + self.__LOG.info("Remote scan server unreachable, using local scan server.") + self.__LOG.info("Next check for remote server in %s seconds." % (self.__SCANSERVER_RETRY_TIMEOUT)) + + self.__remoteScanserverReachable = False + self.__scanserverTimestamp = time.time() + + try: + response = contactScanserver(self.__LOCAL_SCANSERVER_URL, fields) + except: + self.__LOG.error ("Connection to local scan server could not be established.") + self.__LOG.error ("Exception: %s" %(sys.exc_info()[0])) + return retval + else: + try: + response = contactScanserver(self.__LOCAL_SCANSERVER_URL, fields) + except: + self.__LOG.error ("Connection to local scan server could not be established.") + self.__LOG.error ("Exception: %s" %(sys.exc_info()[0])) + return retval + + + if response.status == self.__STATUS_CODE_OK: + retval["infected"] = False + elif response.status == self.__STATUS_CODE_INFECTED: + # Parse xml for info if desired + #contentXML = r.content + #root = ET.fromstring(contentXML) + #status = root[1][2].text + retval["infected"] = True + else: + self.__LOG.error ("Connection error to scan server.") + + if (retval["infected"] == True): + self.__LOG.error ("Virus found, denying access.") + else: + self.__LOG.debug ("No virus found.") + + return retval + + +