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); "復制代碼")