src/OsecFS.py
author ft
Wed, 11 Jun 2014 14:47:59 +0200
changeset 18 e1e5f93eb930
parent 17 cd94e91c37c4
child 19 d2ab00839ba5
permissions -rwxr-xr-x
added file flush in release function
     1 #!/usr/bin/python
     2 
     3 from fuse import Fuse
     4 import fuse
     5 
     6 import ConfigParser
     7 
     8 import sys
     9 
    10 import logging
    11 import os
    12 import errno
    13 import time
    14 
    15 from importlib import import_module
    16 
    17 
    18 import subprocess
    19 
    20 import urllib3
    21 import netifaces
    22 import netaddr
    23 import hashlib
    24 
    25 
    26 sys.stderr = open('/var/log/osecfs_error.log', 'a+')
    27 
    28 
    29 MINOPTS = { "Main" : ["Logfile", "LogLevel", "Mountpoint", "Rootpath", "ScannerPath", "ScannerModuleName", "ScannerClassName", "ScannerConfig", "ReadOnly"]}
    30 
    31 CONFIG_NOT_READABLE = "Configfile is not readable"
    32 CONFIG_WRONG = "Something is wrong with the config"
    33 CONFIG_MISSING = "Section: \"%s\" Option: \"%s\" in configfile is missing"
    34 SCAN_WRONG_RETURN_VALUE = "The return Value of the malware scanner is wrong. Has to be an dictionary"
    35 SCAN_RETURN_VALUE_KEY_MISSING = "The dictionary has to include key \"infected\" (True, False) and \"virusname\" (String)"
    36 VIRUS_FOUND = "Virus found. Access denied"
    37 NOTIFICATION_CRITICAL = "critical"
    38 NOTIFICATION_INFO = "info"
    39 LOG = None
    40 MalwareScanner = None
    41 STATUS_CODE_OK = 200
    42 
    43 SYSTEM_FILE_COMMAND = "file"
    44 httpPool = urllib3.PoolManager(num_pools = 1, timeout = 3)
    45 
    46 def checkMinimumOptions (config):
    47     for section, options in MINOPTS.iteritems ():
    48         for option in options:
    49             if (config.has_option(section, option) == False):
    50                 print (CONFIG_MISSING % (section, option))
    51                 exit (129)
    52 
    53 def printUsage ():
    54     print ("Usage:")
    55     print ("%s configfile mountpath ro/rw" % (sys.argv[0]))
    56     exit (128)
    57 
    58 def loadConfig ():
    59     print ("load config")
    60 
    61     if (len (sys.argv) < 4):
    62         printUsage ()
    63 
    64     configfile = sys.argv[1]
    65     config = ConfigParser.SafeConfigParser ()
    66 
    67     if ((os.path.exists (configfile) == False) or (os.path.isfile (configfile) == False) or (os.access (configfile, os.R_OK) == False)):
    68         print (CONFIG_NOT_READABLE)
    69         printUsage ()
    70 
    71     try:
    72         config.read (sys.argv[1])
    73     except Exception, e:
    74         print (CONFIG_WRONG)
    75         print ("Error: %s" % (e))
    76 
    77 
    78     config.set("Main", "Mountpoint", sys.argv[2])
    79     if (sys.argv[3] == "rw"):
    80         config.set("Main", "ReadOnly", "false")
    81     else:
    82         config.set("Main", "ReadOnly", "true")
    83 
    84     checkMinimumOptions (config)
    85 
    86     return config
    87 
    88 def initLog (config):
    89     print ("init log")
    90 
    91     global LOG
    92     logfile = config.get("Main", "Logfile")
    93     
    94     numeric_level = getattr(logging, config.get("Main", "LogLevel").upper(), None)
    95     if not isinstance(numeric_level, int):
    96         raise ValueError('Invalid log level: %s' % loglevel)
    97 
    98     # ToDo move log level and maybe other things to config file
    99     logging.basicConfig(
   100                         level = numeric_level,
   101                         format = "%(asctime)s %(name)-12s %(funcName)-15s %(levelname)-8s %(message)s",
   102                         datefmt = "%Y-%m-%d %H:%M:%S",
   103                         filename = logfile,
   104                         filemode = "a+",
   105     )
   106     LOG = logging.getLogger("fuse_main")
   107 
   108 
   109 def fixPath (path):
   110     return ".%s" % (path)
   111 
   112 def rootPath (rootpath, path):
   113     return "%s%s" % (rootpath, path)
   114 
   115 def flag2mode (flags):
   116     md = {os.O_RDONLY: 'r', os.O_WRONLY: 'w', os.O_RDWR: 'w+'}
   117     m = md[flags & (os.O_RDONLY | os.O_WRONLY | os.O_RDWR)]
   118 
   119     # windows sets append even if it would overwrite the whole file (seek 0)
   120     # so ignore append option
   121     #if flags | os.O_APPEND:
   122     #    m = m.replace('w', 'a', 1)
   123 
   124     return m
   125 
   126 def scanFile (path, fileobject):
   127     LOG.debug ("Scan File \"%s\" with malware Scanner" %(path,) )
   128     return MalwareScanner.scanFile (path, fileobject)
   129 
   130 
   131 def scanFileClamAV (path):
   132     infected = False
   133 
   134     LOG.debug ("Scan File: %s" % (path))
   135 
   136     result = pyclamav.scanfile (path)
   137     LOG.debug ("Result of file \"%s\": %s" % (path, result))
   138     if (result[0] != 0):
   139         infected = True
   140 
   141     if (infected == True):
   142         LOG.error ("Virus found, deny Access %s" % (result,))
   143 
   144     return infected
   145 
   146 def whitelistFile (path):
   147     whitelisted = False;
   148 
   149     LOG.debug ("Execute \"%s\" command on \"%s\"" %(SYSTEM_FILE_COMMAND, path))
   150     
   151     result = None
   152     try:
   153         result = subprocess.check_output ([SYSTEM_FILE_COMMAND, path]);
   154         # ToDo replace with real whitelist
   155         whitelisted = True
   156     except Exception as e:
   157         LOG.error ("Call returns with an error!")
   158         LOG.error (e)
   159 
   160     LOG.debug ("Type: %s" %(result))
   161 
   162     return whitelisted
   163 
   164 def sendDataToRest (urlpath, data):
   165     netifaces.ifaddresses("eth0")[2][0]["addr"]
   166     
   167     # Get first address in network (0 = network ip -> 192.168.0.0)
   168     remote_ip = netaddr.IPNetwork("%s/%s" %(netifaces.ifaddresses("eth0")[2][0]["addr"], netifaces.ifaddresses("eth0")[2][0]["netmask"]))[1]
   169     
   170     url = ("http://%s:8090//%s" %(remote_ip, urlpath))
   171 
   172     LOG.debug ("Send data to \"%s\"" %(url, ))
   173     LOG.debug ("Data: %s" %(data, ))
   174     
   175     try:
   176         response = httpPool.request_encode_body("POST", url, fields=data, retries=0)
   177     except Exception, e:
   178         LOG.error("Remote host not reachable")
   179         LOG.error ("Exception: %s" %(e,))
   180         return
   181     
   182     if response.status == STATUS_CODE_OK:
   183         LOG.info("Data sent successfully to rest server")
   184         return True
   185     else:
   186         LOG.error("Server returned errorcode: %s" %(response.status,))
   187         return False
   188     
   189 
   190 def sendNotification (type, message):
   191     data = {"msgtype" : type, "message" : message}
   192     sendDataToRest ("notification", data)
   193 
   194 def sendReadOnlyNotification():
   195     sendNotification("critical", "Filesystem is in read only mode. If you want to export files please initialize an encrypted filesystem.")
   196     
   197 def sendLogNotPossibleNotification():
   198     sendNotification ("critical", "Send log entry to opensecurity rest server failed.")
   199     
   200 def sendFileLog(filename, filesize, filehash, hashtype):
   201     data = {"filename" : filename, "filesize" : "%s" %(filesize,), "filehash" : filehash, "hashtype" : hashtype}
   202     retval = sendDataToRest ("log", data)
   203     if (retval == False):
   204         sendLogNotPossibleNotification()
   205 
   206 def calcMD5 (path, block_size=256*128, hr=True):
   207     md5 = hashlib.md5()
   208     with open(path,'rb') as f: 
   209         for chunk in iter(lambda: f.read(block_size), b''): 
   210              md5.update(chunk)
   211     if hr:
   212         return md5.hexdigest()
   213     return md5.digest()
   214 
   215 class OsecFS (Fuse):
   216 
   217     __rootpath = None
   218 
   219     # default fuse init
   220     def __init__(self, rootpath, *args, **kw):
   221         self.__rootpath = rootpath
   222         Fuse.__init__ (self, *args, **kw)
   223         LOG.debug ("Init complete.")
   224         sendNotification("information", "Filesystem successfully mounted.")
   225 
   226     # defines that our working directory will be the __rootpath
   227     def fsinit(self):
   228         os.chdir (self.__rootpath)
   229 
   230     def getattr(self, path):
   231         LOG.debug ("*** getattr (%s)" % (fixPath (path)))
   232         return os.lstat (fixPath (path));
   233 
   234     def getdir(self, path):
   235         LOG.debug ("*** getdir (%s)" % (path));
   236         return os.listdir (fixPath (path))
   237 
   238     def readdir(self, path, offset):
   239         LOG.debug ("*** readdir (%s %s)" % (path, offset));
   240         for e in os.listdir (fixPath (path)):
   241             yield fuse.Direntry(e)
   242 
   243     def chmod (self, path, mode):
   244         LOG.debug ("*** chmod %s %s" % (path, oct(mode)))
   245         if (config.get("Main", "ReadOnly") == "true"):
   246             sendReadOnlyNotification()
   247             return -errno.EACCES
   248         os.chmod (fixPath (path), mode)
   249 
   250     def chown (self, path, uid, gid):
   251         LOG.debug ("*** chown %s %s %s" % (path, uid, gid))
   252         if (config.get("Main", "ReadOnly") == "true"):
   253             sendReadOnlyNotification()
   254             return -errno.EACCES
   255         os.chown (fixPath (path), uid, gid)
   256 
   257     def link (self, targetPath, linkPath):
   258         LOG.debug ("*** link %s %s" % (targetPath, linkPath))
   259         if (config.get("Main", "ReadOnly") == "true"):
   260             sendReadOnlyNotification()
   261             return -errno.EACCES
   262         os.link (fixPath (targetPath), fixPath (linkPath))
   263 
   264     def mkdir (self, path, mode):
   265         LOG.debug ("*** mkdir %s %s" % (path, oct(mode)))
   266         if (config.get("Main", "ReadOnly") == "true"):
   267             sendReadOnlyNotification()
   268             return -errno.EACCES
   269         os.mkdir (fixPath (path), mode)
   270 
   271     def mknod (self, path, mode, dev):
   272         LOG.debug ("*** mknod %s %s %s" % (path, oct (mode), dev))
   273         if (config.get("Main", "ReadOnly") == "true"):
   274             sendReadOnlyNotification()
   275             return -errno.EACCES
   276         os.mknod (fixPath (path), mode, dev)
   277 
   278     # to implement virus scan
   279     def open (self, path, flags):
   280         LOG.debug ("*** open %s %s" % (path, oct (flags)))
   281         self.file = os.fdopen (os.open (fixPath (path), flags), flag2mode (flags))
   282         self.written = False
   283         self.fd = self.file.fileno ()
   284         
   285         LOG.debug(self.__rootpath)
   286         LOG.debug(path)
   287         
   288         retval = scanFile (rootPath(self.__rootpath, path), self.file)
   289         
   290         #if type(retval) is not dict:
   291         if (isinstance(retval, dict) == False):
   292             LOG.error(SCAN_WRONG_RETURN_VALUE)
   293             self.file.close ()
   294             return -errno.EACCES
   295         
   296         if ((retval.has_key("infected") == False) or (retval.has_key("virusname") == False)):
   297             LOG.error(SCAN_RETURN_VALUE_KEY_MISSING)
   298             self.file.close ()
   299             return -errno.EACCES
   300             
   301         
   302         if (retval.get("infected") == True):
   303             self.file.close ()
   304             sendNotification(NOTIFICATION_CRITICAL, "%s\nFile: %s\nVirus: %s" %(VIRUS_FOUND, path, retval.get("virusname")))
   305             LOG.error("%s" %(VIRUS_FOUND,))
   306             LOG.error("Virus: %s" %(retval.get("virusname"),))
   307             return -errno.EACCES
   308         
   309         whitelisted = whitelistFile (rootPath(self.__rootpath, path))
   310         if (whitelisted == False):
   311             self.file.close ()
   312             sendNotification(NOTIFICATION_CRITICAL, "File not in whitelist. Access denied.")
   313             return -errno.EACCES
   314 
   315     def read (self, path, length, offset):
   316         LOG.debug ("*** read %s %s %s" % (path, length, offset))
   317         self.file.seek (offset)
   318         return self.file.read (length)
   319 
   320     def readlink (self, path):
   321         LOG.debug ("*** readlink %s" % (path))
   322         return os.readlink (fixPath (path))
   323 
   324     def release (self, path, flags):
   325         LOG.debug ("*** release %s %s" % (path, oct (flags)))
   326         self.file.flush()
   327         os.fsync(self.file.fileno())
   328         self.file.close ()
   329         
   330         if (self.written == True):
   331             hashsum = calcMD5(fixPath(path))
   332             filesize = os.path.getsize(fixPath(path))
   333             sendFileLog(path, filesize, hashsum, "md5")
   334         
   335 
   336     def rename (self, oldPath, newPath):
   337         LOG.debug ("*** rename %s %s %s" % (oldPath, newPath, config.get("Main", "ReadOnly")))
   338         if (config.get("Main", "ReadOnly") == "true"):
   339             sendReadOnlyNotification()
   340             return -errno.EACCES
   341         os.rename (fixPath (oldPath), fixPath (newPath))
   342 
   343     def rmdir (self, path):
   344         LOG.debug ("*** rmdir %s %s" % (path, config.get("Main", "ReadOnly")))
   345         if (config.get("Main", "ReadOnly") == "true"):
   346             sendReadOnlyNotification()
   347             return -errno.EACCES
   348         os.rmdir (fixPath (path))
   349 
   350     def statfs (self):
   351         LOG.debug ("*** statfs")
   352         return os.statvfs(".")
   353 
   354     def symlink (self, targetPath, linkPath):
   355         LOG.debug ("*** symlink %s %s %s" % (targetPath, linkPath, config.get("Main", "ReadOnly")))
   356         if (config.get("Main", "ReadOnly") == "true"):
   357             sendReadOnlyNotification()
   358             return -errno.EACCES
   359         os.symlink (fixPath (targetPath), fixPath (linkPath))
   360 
   361     def truncate (self, path, length):
   362         LOG.debug ("*** truncate %s %s %s" % (path, length, config.get("Main", "ReadOnly")))
   363         if (config.get("Main", "ReadOnly") == "true"):
   364             sendReadOnlyNotification()
   365             return -errno.EACCES
   366         f = open (fixPath (path), "w+")
   367         f.truncate (length)
   368         f.close ()
   369 
   370     def unlink (self, path):
   371         LOG.debug ("*** unlink %s %s" % (path, config.get("Main", "ReadOnly")))
   372         if (config.get("Main", "ReadOnly") == "true"):
   373             sendReadOnlyNotification()
   374             return -errno.EACCES
   375         os.unlink (fixPath (path))
   376 
   377     def utime (self, path, times):
   378         LOG.debug ("*** utime %s %s" % (path, times))
   379         os.utime (fixPath (path), times)
   380 
   381     def write (self, path, buf, offset):
   382         #LOG.debug ("*** write %s %s %s %s" % (path, buf, offset, config.get("Main", "ReadOnly")))
   383         LOG.debug ("*** write %s %s %s %s" % (path, "filecontent", offset, config.get("Main", "ReadOnly")))
   384         if (config.get("Main", "ReadOnly") == "true"):
   385             self.file.close()
   386             sendReadOnlyNotification()
   387             return -errno.EACCES
   388         self.file.seek (offset)
   389         self.file.write (buf)
   390         self.written = True
   391         return len (buf)
   392 
   393     def access (self, path, mode):
   394         LOG.debug ("*** access %s %s" % (path, oct (mode)))
   395         if not os.access (fixPath (path), mode):
   396             return -errno.EACCES
   397 
   398     def create (self, path, flags, mode):
   399         LOG.debug ("*** create %s %s %s %s %s" % (fixPath (path), oct (flags), oct (mode), flag2mode (flags), config.get("Main", "ReadOnly")))
   400         if (config.get("Main", "ReadOnly") == "true"):
   401             sendReadOnlyNotification()
   402             return -errno.EACCES
   403 
   404         self.file = os.fdopen (os.open (fixPath (path), flags), flag2mode(flags))
   405         self.written = True
   406         self.fd = self.file.fileno ()
   407 
   408 
   409 if __name__ == "__main__":
   410     # Set api version
   411     fuse.fuse_python_api = (0, 2)
   412     fuse.feature_assert ('stateful_files', 'has_init')
   413 
   414     config = loadConfig ()
   415     initLog (config)
   416     
   417     #sendNotification("Info", "OsecFS started")
   418     
   419     # Import the Malware Scanner
   420     sys.path.append(config.get("Main", "ScannerPath"))
   421     
   422     MalwareModule = import_module(config.get("Main", "ScannerModuleName"))
   423     MalwareClass = getattr(MalwareModule, config.get("Main", "ScannerClassName"))
   424     
   425     MalwareScanner = MalwareClass (config.get("Main", "ScannerConfig"));
   426     
   427     osecfs = OsecFS (config.get ("Main", "Rootpath"))
   428     osecfs.flags = 0
   429     osecfs.multithreaded = 0
   430 
   431     fuse_args = [sys.argv[0], config.get ("Main", "Mountpoint")];
   432     osecfs.main (fuse_args)