Python pyclamad病毒扫描与目录病毒扫描脚本(转载)
Clam AntiVirus(Clam AV)是一个免费而且开放源码的防毒软件,软件与病毒库的更新由开源社区免费发布,目前ClamdAV主要为Linux、Uinux系统提供病毒扫描查杀pyClamad是一个python的第三方模块,可让python直接使用ClamAV病毒扫描守护进程clamd来实现一个高效的病毒检测功能。 一、实现集中式的病毒扫描 1、客户端(病毒扫描源)安装clamavp clamd 服务的相关程序包 yum install clamav clamd clamav-update -y chkconfig clamd on 更新病毒库 /usr/bin/freshclam 更改配置文件修改监听地址到所有网络,启动服务 sed -i -e '/^TCPAddr/{ s/127.0.0.1/0.0.0.0/;}' /etc/clamd.conf /etc/init.d/clamd start 2、主控端安装pyClamd模块 参考:http://xael.org/pages/pyclamd-en.html pip install pyclamd 验证安装结果: <pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(238, 238, 221); text-decoration-style: initial; text-decoration-color: initial;">>>> import pyclamd cd = pyclamd.ClamdAgnostic() cd.ping() True</pre> 工作原理:管理服务器通过python发出多线程指令连接业务服务器的3310端口,执行病毒扫描,然后返回结果给管理服务器。 业务服务器必须安装clamd相关程序包,并启动服务监听在3310端口才能正常收到指令;可以针对不同业务环境定制相应的扫描策略,比如扫描对象、描述模式、扫描路径、调试频率等。 实现代码:simplel.py [ 复制代码 ](javascript:void(0); "复制代码") <pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> 1 #!/usr/bin/env python 2 # -- coding: utf-8 -- 3 import time 4 import pyclamd 5 from threading import Thread 6 class Scan(Thread): #继承多线程Thread类 7 def init (self,IP,scan_type,file): 8 """构造方法""" 9 Thread.init(self) 10 self.IP = IP 11 self.scan_type=scan_type 12 self.file = file 13 self.connstr="" 14 self.scanresult="" 15 def run(self): 16 """多进程run方法""" 17 try: 18 cd = pyclamd.ClamdNetworkSocket(self.IP,3310) 19 """探测连通性""" 20 if cd.ping(): 21 self.connstr=self.IP+" connection [OK]" 22 """重载clamd病毒特征库""" 23 cd.reload() 24 """判断扫描模式""" 25 if self.scan_type=="contscan_file": 26 self.scanresult="{0}\n".format(cd.contscan_file(self.file)) 27 elif self.scan_type=="multiscan_file": 28 self.scanresult="{0}\n".format(cd.multiscan_file(self.file)) 29 elif self.scan_type=="scan_file": 30 self.scanresult="{0}\n".format(cd.scan_file(self.file)) 31 time.sleep(1) 32 else: 33 self.connstr=self.IP+" ping error,exit" 34 return 35 except Exception,e: 36 self.connstr=self.IP+" "+str(e) 37 IPs=['172.16.65.201','172.16.65.202'] #扫描主机的列表 38 scantype="multiscan_file" #指定扫描模式,支持 multiscan_file、contscan_file、scan_file 39 scanfile="/usr/local/bin" #指定扫描路径 40 i=1 41 threadnum=2 #指定启动的线程数 42 scanlist = [] #存储Scan类线程对象列表 43 for ip in IPs: 44 """将数据值带入类中,实例化对象""" 45 currp = Scan(ip,scantype,scanfile) 46 scanlist.append(currp) #追加对象到列表 47 """当达到指定的线程数或IP列表数后启动线程""" 48 if i%threadnum==0 or i==len(IPs): 49 for task in scanlist: 50 task.start() #启动线程 51 for task in scanlist: 52 task.join() #等待所有子线程退出,并输出扫描结果 53 print task.connstr #打印服务器连接信息 54 print task.scanresult #打印结果信息 55 scanlist = [] 56 i+=1</pre> [ 复制代码 ](javascript:void(0); "复制代码")