ft@22: #!/usr/bin/python ft@22: ft@22: # ------------------------------------------------------------ ft@22: # opensecurity package file ft@22: # ft@22: # Autor: Karlberger Christoph ft@22: # X-Net Services GmbH ft@22: # ft@22: # Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology ft@22: # ft@22: # IKARUS Security Software GmbH ft@22: # Blechturmgasse 11 ft@22: # 1050 Wien ft@22: # AUSTRIA ft@22: # http://www.ikarussecurity.com ft@22: # ft@22: # X-Net Technologies GmbH ft@22: # Elisabethstrasse 1 ft@22: # 4020 Linz ft@22: # AUSTRIA ft@22: # https://www.x-net.at ft@22: # ft@22: # AIT Austrian Institute of Technology ft@22: # Donau City Strasse 1 ft@22: # 1220 Wien ft@22: # AUSTRIA ft@22: # http://www.ait.ac.at ft@22: # ft@22: # ft@22: # Licensed under the Apache License, Version 2.0 (the "License"); ft@22: # you may not use this file except in compliance with the License. ft@22: # You may obtain a copy of the License at ft@22: # ft@22: # http://www.apache.org/licenses/LICENSE-2.0 ft@22: # ft@22: # Unless required by applicable law or agreed to in writing, software ft@22: # distributed under the License is distributed on an "AS IS" BASIS, ft@22: # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ft@22: # See the License for the specific language governing permissions and ft@22: # limitations under the License. ft@22: # ------------------------------------------------------------ ft@22: ft@22: import ConfigParser ft@22: ft@22: import sys ft@22: ft@22: import logging ft@22: import os ft@22: import errno ft@22: import time ft@22: ft@22: import urllib3 ft@22: import xml.etree.ElementTree as ET ft@22: ft@22: class IkarusScanner: ft@22: ft@22: # User the existing logger instance ft@22: __LOG = logging.getLogger("IkarusScanner") ft@22: ft@22: __MINOPTS = { "Main" : ["LocalScanserverURL", "RemoteScanserverURL", "MaxFileSize", "RetryTimeout"]} ft@22: __CONFIG_NOT_READABLE = "Configfile is not readable" ft@22: __CONFIG_WRONG = "Something is wrong with the config" ft@22: __CONFIG_MISSING = "Section: \"%s\" Option: \"%s\" in configfile is missing" ft@22: __LOCAL_SCANSERVER_URL = "" ft@22: __REMOTE_SCANSERVER_URL = "" ft@22: __STATUS_CODE_OK = 200 ft@22: __STATUS_CODE_INFECTED = 210 ft@22: __STATUS_CODE_NOT_FOUND = 404 ft@22: __MAX_SCAN_FILE_SIZE = 50 * 0x100000 ft@22: __SCANSERVER_RETRY_TIMEOUT = 60 ft@22: ft@22: # Global http pool manager used to connect to the scan server ft@22: __remoteScanserverReachable = True ft@22: __scanserverTimestamp = 0 ft@22: __httpPool = urllib3.PoolManager(num_pools = 1, timeout = 3) ft@22: ft@22: def __init__ (self, scanner_config_path): ft@22: config = self.loadConfig (scanner_config_path) ft@22: ft@22: self.__scanserverTimestamp = time.time() ft@22: ft@22: self.__LOCAL_SCANSERVER_URL = config.get("Main", "LocalScanserverURL") ft@22: self.__REMOTE_SCANSERVER_URL = config.get("Main", "RemoteScanserverURL") ft@22: self.__SCANSERVER_RETRY_TIMEOUT = int(config.get("Main", "RetryTimeout")) ft@22: ft@22: # Convert file size from MB to byte ft@22: self.__MAX_SCAN_FILE_SIZE = int(config.get("Main", "MaxFileSize")) * 0x100000 ft@22: ft@22: ft@22: def checkMinimumOptions (self, config): ft@22: for section, options in self.__MINOPTS.iteritems (): ft@22: for option in options: ft@22: if (config.has_option(section, option) == False): ft@22: self.__LOG.error (self.__CONFIG_MISSING % (section, option)) ft@22: exit (129) ft@22: ft@22: def loadConfig (self, scanner_config_path): ft@22: ft@22: configfile = scanner_config_path ft@22: config = ConfigParser.SafeConfigParser () ft@22: ft@22: 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)): ft@22: self.__LOG.error(self.__CONFIG_NOT_READABLE); ft@22: raise SystemError(self.__CONFIG_NOT_READABLE) ft@22: ft@22: try: ft@22: config.read (scanner_config_path) ft@22: except Exception, e: ft@22: self.__LOG.error("Error: %s" % (e)); ft@22: raise SystemError("Error: %s" % (e)) ft@22: ft@22: self.checkMinimumOptions (config) ft@22: ft@22: return config ft@22: ft@22: def contactScanserver(self, url, fields): ft@22: self.__LOG.debug("Contacting server %s" % url) ft@22: return self.__httpPool.request_encode_body('POST', url, fields = fields, retries = 0) ft@22: ft@22: def scanFile (self, path, fileobject): ft@22: return self.scanFileIkarus (path, fileobject) ft@22: ft@22: def scanFileIkarus (self, path, fileobject): ft@22: retval = { "infected" : False, "virusname" : "Unknown" } ft@22: self.__LOG.debug ("Scan File: %s" % (path)) ft@22: ft@22: if (os.fstat(fileobject.fileno()).st_size > self.__MAX_SCAN_FILE_SIZE): ft@22: self.__LOG.info("File max size exceeded. The file is not scanned.") ft@22: retval["infected"] = False ft@22: retval["virusname"] = "File is to big to be scanned." ft@22: return retval ft@22: ft@22: fields = { 'up_file' : fileobject.read() } ft@22: ft@22: if (self.__remoteScanserverReachable == False) and ((self.__scanserverTimestamp + self.__SCANSERVER_RETRY_TIMEOUT) < time.time()): ft@22: self.__remoteScanserverReachable = True ft@22: ft@22: if self.__remoteScanserverReachable: ft@22: try: ft@22: response = self.contactScanserver(self.__REMOTE_SCANSERVER_URL, fields) ft@22: # We should catch socket.error here, but this does not work. Needs checking. ft@22: except: ft@22: self.__LOG.info("Remote scan server unreachable, using local scan server.") ft@22: self.__LOG.debug("Exception: %s: %s" % (sys.exc_info()[0], sys.exc_info()[1])) ft@22: self.__LOG.info("Next check for remote server in %s seconds." % (self.__SCANSERVER_RETRY_TIMEOUT)) ft@22: ft@22: self.__remoteScanserverReachable = False ft@22: self.__scanserverTimestamp = time.time() ft@22: ft@22: try: ft@22: response = self.contactScanserver(self.__LOCAL_SCANSERVER_URL, fields) ft@22: except: ft@22: self.__LOG.error ("Connection to local scan server could not be established.") ft@22: self.__LOG.debug ("Exception: %s" % (sys.exc_info()[0])) ft@22: return retval ft@22: else: ft@22: try: ft@22: response = self.contactScanserver(self.__LOCAL_SCANSERVER_URL, fields) ft@22: except: ft@22: self.__LOG.error ("Connection to local scan server could not be established.") ft@22: self.__LOG.error ("Exception: %s" %(sys.exc_info()[0])) ft@22: return retval ft@22: ft@22: ft@22: if response.status == self.__STATUS_CODE_OK: ft@22: retval["infected"] = False ft@22: elif response.status == self.__STATUS_CODE_INFECTED: ft@22: # Parse xml for info ft@22: root = ET.fromstring(response.data) ft@22: ft@22: # this should be done in a more generic way ft@22: retval["virusname"] = root[1][3][0].text ft@22: retval["infected"] = True ft@22: else: ft@22: self.__LOG.error ("Connection error to scan server.") ft@22: ft@22: if (retval["infected"] == True): ft@22: self.__LOG.error ("Virus found, denying access.") ft@22: else: ft@22: self.__LOG.debug ("No virus found.") ft@22: ft@22: return retval ft@22: ft@22: ft@22: