Clam AntiVirus(ClamAV)是免费而且开放源代码的防毒软件,软件病毒与病毒库的更新全是社区免费发布。官网地址:http://www.clamav.net/lang/en。ClamAV目前未系统提供病毒扫描】查杀等服务。pyClamad(http:xael.org)是一个Python第三方模块,可以让Python直接使用ClamAV的守护进程clamd,来实现高校的病毒检测功能。
工具准备
pyClamad模块安装方法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| # 一、客户端(病毒扫描源)安装
# 1 安装epel扩展源 yum install -y epel-release
# 2 安装依赖 yum install -y clamav clamd clamav-update # 安装相关包 chkconfig --levels 235 clamd on # 添加扫描守护进程clamd系统服务 /usr/bin/freshclam # 更新病毒库,建议配置到crontab定期更新
# 更新守护进程监听IP配置文件,根据不同环境自行修改监听的IP,"0.0.0.0"为监听所有主机IP。 # sed -i -e '/^TCPAddr/{ s/127.0.0.1/0.0.0.0/;}' /etc/clamd.conf
/etc/init.d/clamd start # 启动扫描进程
# 3 检查软件版本 which freshclam freshclam --version
which clamscan clamscan --version
|
1 2
| # 扫描当前目录 clamscan --remove ./
|
1 2
| # 2.主控端部署pyClamad环境 pip install pyclamd
|
代码实践


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
import time import pyclamd from threading import Thread
class Scan(Thread): def __init__(self,IP,scan_type,file): """构造方法,参数初始化""" Thread.__init__(self) self.IP = IP self.scan_type = scan_type self.file = file self.connstr = "" self.scanresult = ""
def run(self): """多进程run方法""" try: cd = pyclamd.ClamdNetworkSocket(self.IP,3310) if cd.ping(): self.connstr = self.IP + " connect [ok]" cd.reload() if self.scan_type == "contscan_file": self.scanresult = "{0}\n".format(cd.contscan_file(self.file)) elif self.scan_type == "multiscan_file": self.scanresult = "{0}\n".format(cd.multiscan_file(self.file)) elif self.scan_type == "scan_file": self.scanresult = "{0}\n".format(cd.scan_file(self.file)) time.sleep(1)
else: self.connstr = self.IP + "ping error,exit" return
except Exception as e: self.connstr = self.IP + " " + str(e)
IPs = ['192.168.1.0','192.168.2.1'] scantype = "multiscan_file" scanfile = "/data/www" i = 1
threadnum = 2 scanlist = []
for ip in IPs: currp = Scan(ip,scantype,scanfile) scanlist.append(currp)
if i % threadnum == 0 or i ==len(IPs): for task in scanlist: task.start()
for task in scanlist: task.join() print(task.connstr) print(task.scanresult)
scanlist = []
i += 1
|
clamd服务未运行:确保clamd服务在您的服务器上已经启动。您可以使用如下命令来检查服务状态:
1
| sudo service clamd status
|
如果服务没有运行,您需要启动它:
1
| sudo service clamd start
|
如果不行,看列表有没有,如果没有,说明没有装好
1
| systemctl list-unit-files --type=service
|
1
| sudo systemctl start 对应服务
|
clamd配置:检查clamd服务的配置文件(通常位于/etc/clamd.conf
或/etc/clamd.d/scan.conf
),确保TCPAddr
和TCPSocket
选项正确设置。TCPAddr
应该设置为服务器的IP地址或0.0.0.0
(表示监听所有地址),TCPSocket
应该设置为3310
。
我出现了上面两个问题,一是相关服务没有开启,而是配置文件仅仅取消注释了IP地址,忽略了TCPSocket
应该为3310
也要取消注释
- 启动clamd@scan扫描服务,服务启动不了的话,可以查看/var/log/clamd.scan
1 2 3
| systemctl enable clamd@scan systemctl start clamd@scan systemctl status clamd@scan
|
生成测试病毒样
pyClamd
提供了一个EICAR()
方法,该方法可以快速生成病毒样本,用于测试。复制文件到扫描目录中:
1 2 3 4 5
| >>> import pyclamd >>> cd = pyclamd.ClamdAgnostic() >>> cd.EICAR() b'X5O!P%@AP[4\\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*' >>>
|
tip:可以看到输出的是一个python字节串。虽然是病毒样本,但是我们并没有去执行它,所以是无害的,大家不要担心机器中毒!!
1
| void = open('/data/www/EICAR','w').write(cd.EICAR().decode())
|
参考文章
Linux之ClamAV杀毒软件YUM安装和使用_yum clamav-CSDN博客
Python自动化运维-第四章-刘天斯
ClamAV(Clam AntiVirus)安装使用 - 紫藤萝的沉默 - 博客园 (cnblogs.com)
python 使用ClamAV实现病毒扫-腾讯云开发者社区-腾讯云 (tencent.com)
Linux病毒扫描工具ClamAV(Clam AntiVirus)安装使用 - 知乎 (zhihu.com)
Centos | Clamav | 启动clamd@scan服务_clamd@scan卸载-CSDN博客
基于「ClamAv」通过python进行病毒检测(2)– pyClamd控制clamd详解 - 简书 (jianshu.com)