def scan(self, message): message = base64.b64decode(message) self.logger.debug('received message\n %s' % message) response = "not processed" # test for clam daemon if pyclamd.ping(): # message contains directory if os.path.isdir(message): self.logger.debug('received directory\n %s' % message) path = message # scan files inside directory dirList = os.listdir(path) for fname in dirList: self.logger.debug('contains file %s \n' % path + os.sep + fname) if os.path.isfile(path + os.sep + fname): self.logger.debug('scanning file %s \n' % fname) # call pyclamd with absolute path of file response = path + os.sep + fname + "," + str( pyclamd.scan_file(path + os.sep + fname)) # message contains single file if os.path.isfile(message): self.logger.debug('received file\n %s' % message) fname = message # call pyclamd with absolute path of file response = fname + "," + str(pyclamd.scan_file(fname)) return response
def scan(self, message): message=base64.b64decode(message) self.logger.debug('received message\n %s'% message) response = "not processed" # test for clam daemon if pyclamd.ping(): # message contains directory if os.path.isdir(message): self.logger.debug('received directory\n %s'% message) path=message # scan files inside directory dirList=os.listdir(path) for fname in dirList: self.logger.debug('contains file %s \n'% path+os.sep+fname) if os.path.isfile(path+os.sep+fname): self.logger.debug('scanning file %s \n'% fname) # call pyclamd with absolute path of file response = path+os.sep+fname+","+str(pyclamd.scan_file(path+os.sep+fname)) # message contains single file if os.path.isfile(message): self.logger.debug('received file\n %s'% message) fname=message # call pyclamd with absolute path of file response = fname+","+str(pyclamd.scan_file(fname)) return response
def scan_file(self, fname): result = False if self.result: try: pyclamd.scan_file(fname) result = True except pyclamd.ScanError: raise return result
def clean(self, data, initial=None): f = super(yatsFileField, self).clean(initial or data) if f is None: return None elif not data and initial: return initial if settings.FILE_UPLOAD_VIRUS_SCAN: # virus scan try: pyclamd.init_network_socket('localhost', 3310) # We need to get a file object for clamav. We might have a path or we might # have to read the data into memory. if hasattr(data, 'temporary_file_path'): chmod(data.temporary_file_path(), 0664) result = pyclamd.scan_file(data.temporary_file_path()) else: if hasattr(data, 'read'): result = pyclamd.scan_stream(data.read()) else: result = pyclamd.scan_stream(data['content']) except: from socket import gethostname raise ValidationError( self.error_messages['virus_engine_error'] % gethostname()) if result: raise ValidationError(self.error_messages['virus_found'] % result[result.keys()[0]]) return f
def clean(self, data, initial=None): f = super(yatsFileField, self).clean(initial or data) if f is None: return None elif not data and initial: return initial if settings.FILE_UPLOAD_VIRUS_SCAN: # virus scan try: pyclamd.init_network_socket('localhost', 3310) # We need to get a file object for clamav. We might have a path or we might # have to read the data into memory. if hasattr(data, 'temporary_file_path'): chmod(data.temporary_file_path(), 0664) result = pyclamd.scan_file(data.temporary_file_path()) else: if hasattr(data, 'read'): result = pyclamd.scan_stream(data.read()) else: result = pyclamd.scan_stream(data['content']) except: from socket import gethostname raise ValidationError(self.error_messages['virus_engine_error'] % gethostname()) if result: raise ValidationError(self.error_messages['virus_found'] % result[result.keys()[0]]) return f
def _do_virus(self): # 进行病毒邮件检测 try: pyclamd.init_unix_socket(clamav_sock) res = pyclamd.scan_file(self.mail_path) except Exception, err: outerror(u'virus check error :{}'.format(self.task_info)) outerror(traceback.format_exc()) return False
def scan_file(self, fqfn_in, action="alert"): """ Description: scan file Return Value: results (dict) """ # initialize scan results scan_results = {} scan_time = 0 try: # logging msg = "Scanning [%s]" % (fqfn_in) self._mylog.log_this(msg, "info") _start_scan = time.time() # current time in seconds pyclamd.init_network_socket( self._myconfig.config["scan_this"]["clamd_host"], self._myconfig.config["scan_this"]["clamd_port"] ) scan_results = pyclamd.scan_file(fqfn_in) if action in self._actions.keys(): if scan_results is not None: self._actions[action](fqfn_in) else: # logging msg = "Action [%s] is not supported." % (action) self._mylog.log_this(msg, "critical") _stop_scan = time.time() # current time in seconds # elapsed time _scan_time = int(_stop_scan - _start_scan) return True, scan_results, convert_seconds(_scan_time) except Exception, err: self._mylog.log_traceback(traceback.extract_tb(sys.exc_info()[2]), str(err), "critical") # logging msg = "Failed to run scan using clamd on [%s] port [%s]." % ( self._myconfig.config["scan_this"]["clamd_host"], self._myconfig.config["scan_this"]["clamd_port"], ) self._mylog.log_this(msg, "critical") sys.exit() return False, scan_results, convert_seconds(_scan_time)
def check_for_virus(data): # Writing data to a file for scanning path = '/tmp/file_to_scan.txt' file_to_scan = open(path, 'w+') file_to_scan.write(str(data)) file_to_scan.close() # Initialize pyclamd socket pyclamd.init_unix_socket('/tmp/clamd.socket') # Scanning file virus_found = pyclamd.scan_file(path) # Remove the file that was to scan os.remove(path) return virus_found
def scan_input(): try: (tmp_file, tmp_file_name) = tempfile.mkstemp() os.fchmod(tmp_file, 0644) copy_file(0, tmp_file) os.close(0) except OSError as e: syslog.syslog('Temporary file creation failed: \'%s\'\n' % str(e)) return (None, None, None) try: pyclamd.init_unix_socket() found_virus = pyclamd.scan_file(tmp_file_name) except pyclamd.ScanError as e: syslog.syslog('Virus scan failed: \'%s\'\n' % str(e)) return (None, None, None) return (tmp_file, tmp_file_name, found_virus)
def av_scan_s3(tmpdir, bucket_name): av=[] t = Terminal() path=os.path.join(tmpdir, bucket_name) for subdir, dirs, files in os.walk(path): for file in files: subdir_path= os.path.join(path, subdir) file_path= os.path.join(subdir_path, file) av.append(pyclamd.scan_file(file_path)) av=[x for x in av if x is not None] for n in range(len(av)): index_number= n if str(file_path) in av[n]: file= file_path virus= list(av[n][str(file_path)]) virus.remove("FOUND") virus=str(virus)[2:-2] print t.red('Critical !')+ " I found "+virus+ " in the infected file "+file
def scan_one(self, path): try: tmp = pyclamd.scan_file(path) if tmp: self.results.update(tmp) except: pass
def scan_file(self, filepath): found = pyclamd.scan_file(filepath) virus = found and '\n'.join(found.values()) or '' return found, virus