Python pyclamad病毒扫描与目录病毒扫描脚本(转载)

简介: Clam AntiVirus(Clam AV)是一个免费而且开放源码的防毒软件,软件与病毒库的更新由开源社区免费发布,目前ClamdAV主要为Linux、Uinux系统提供病毒扫描查杀pyClamad是一个python的第三方模块,可让python直接使用ClamAV病毒扫描守护进程clamd来实现一个高效的病毒检测功能。

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

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](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>

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](javascript:void(0); "复制代码")

目录
相关文章
|
21天前
|
Linux Shell Python
Linux执行Python脚本
Linux执行Python脚本
26 1
|
1月前
|
Python
在 Python 中,如何处理文件和目录?
在 Python 中,如何处理文件和目录?
66 0
|
1月前
|
安全 Unix Linux
在Python中,如何处理文件和目录的访问权限?
【2月更文挑战第15天】【2月更文挑战第43篇】在Python中,如何处理文件和目录的访问权限?
|
10天前
|
JSON 测试技术 持续交付
自动化测试与脚本编写:Python实践指南
【4月更文挑战第9天】本文探讨了Python在自动化测试中的应用,强调其作为热门选择的原因。Python拥有丰富的测试框架(如unittest、pytest、nose)以支持自动化测试,简化测试用例的编写与维护。示例展示了使用unittest进行单元测试的基本步骤。此外,Python还适用于集成测试、系统测试等,提供模拟外部系统行为的工具。在脚本编写实践中,Python的灵活语法和强大库(如os、shutil、sqlite3、json)助力执行复杂测试任务。同时,Python支持并发、分布式执行及与Jenkins、Travis CI等持续集成工具的集成,提升测试效率和质量。
|
17天前
|
存储 监控 异构计算
【Python】GPU内存监控脚本
【Python】GPU内存监控脚本
|
17天前
|
Ubuntu Unix Linux
【Linux/Ubuntu】Linux/Ubuntu运行python脚本
【Linux/Ubuntu】Linux/Ubuntu运行python脚本
|
25天前
|
XML Shell Linux
性能工具之 JMeter 使用 Python 脚本快速执行
性能工具之 JMeter 使用 Python 脚本快速执行
40 1
性能工具之 JMeter 使用 Python 脚本快速执行
|
29天前
|
开发者 Python
Python语言的文件及目录访问
Python语言的文件及目录访问
|
1月前
|
Python
Python文件目录操作就是这么6
Python文件目录操作就是这么6
15 0
|
1月前
|
数据采集 测试技术 Python
Python自动化脚本的魅力与实践
Python自动化脚本的魅力与实践
48 0

热门文章

最新文章