clamav杀毒

aamAmiVirus(ClamAV)是一款免费而且开放源代码的防毒软件,软件与病毒库的更新皆由社区免费发布,官网地址: http://www.clamav.net/lang/en/。 目前ClamAV主要为Linux、Unix 。pyClamad ( http://xael.org/norman/python/pyclamd/)是一个 Python第三方模块,可让 Python直接使用 ClamAV病毒扫描守护进程clamd,来实现一个高效的病毐检测功能,另外, pyClamad模块也非常容易整合到我们已有的平台当中。
   
groupadd clamav
useradd - g clamav - s / bin / false - c
"Clam AntiVirus"
clamav
/bin /false是最严格的禁止login选项,一切服务都不能用,而 /sbin /nologin只是不允许系统login,可以使用其他ftp等服务
如果想要用false在禁止login的同时允许ftp,则必须在 /etc /shells里增加一行 /bin /false。


yum install -y bzip2  bzip2-devel  libbz2  libbz2-devel  gcc*

./configure - -prefix =/opt/clamav --disable-clamav  -with-zlib=/usr/local/zlib  ----enable-bzip2

make && make install

配置Clamav

创建目录

mkdir /opt/clamav/logs

mkdir /opt/clamav/updata
创建文件

touch /opt/clamav/logs/freshclam.log
touch /opt/clamav/logs/clamd.log

chown clamav: clamav clamd.log
chown clamav:clamav freshclam.log

修改配置文件
# vi /opt/clamav

/etc/clam.conf

# Example 注释掉这一行. 第8 行  

LogFile /opt/clamav/logs/clamd.log
删掉前面的注释目录改为 /opt/clamav/logs/clamd.log

    PidFile /opt/clamav/updata/clamd.pid
删掉前面的注释路径改为 /opt/clamav/updata/clamd.pid

    DatabaseDirectory/opt/clamav/updata
同上

# vi /opt/clamav

/etc/clamfreshclam.conf,将Example
这一行注释掉。


升级病毒库

/opt/clamav/bin/freshclam

扫描帮助

/opt/clamav/bin/clamscan - h


另一个安装方式
(客户端安装
"安装"
yum -y install clamav clamd clamav-update
"安装clamavp相关程序包"

chkconfig --levels 235 clamd on
“添加扫描搜狐进程clamd系统服务”

/usr/bin/freshclam
#更新病毒库,建议配置crontab定期更新

sed -i - e '^TCPAddr/( s/127.0.0.1/0.0.0.0/;)' /etc/clamd.conf
#更新守护进程监听IP配置文件(根据环境不同自行调整)
/etc/init.d/clamd start #启动扫描守护进程

'''

'''
服务器端
wget http://xael.org/norman/python/pyclamd/pyClamd-0.3.4.tar.gz (根据版本来修改wget)
tar -zxvf pyClamd-0.3.4.tar.gz
cd pyClamd-0.3.4
python setup.py install
''')

#pyClamad

'''
   1、ClamdNetworkSocket() 类,使用网络套接字操作clamd
   2、ClamdUnixSocket()类,使用网络套接字操作clamd

__init__(self,host='127.0.0.1',port=3310,timeout=None)
ClamdNetworkSocket类的初始化方法,
  参数host连接主机IP
  参数port为连接端口(/etc/clamd.conf配置文件中的TCPSocket参数保持一致)
  timeout为连接的超时时间
 
contscan_file(self,file)  
实现对指定的文件或目录在扫描发生错误或发现兵符将不终止,
  参数file(string类型)为指定的文件或目录的绝对路径
 
multiscan_file(self,file)
实现多线程扫描指定的文件或目录,多核环境速度更快,在扫描发生错误或发现病毒将不终止
  参数file(string类型)为指定的文件或目录的绝对路径
 
scan_file(self,file)
实现对指定的文件或目录在扫描发生错误或发现兵符将终止
  参数file(string类型)为指定的文件或目录的绝对路径
 
shutdown(self)方法 实现强制关闭clamd进程并退出

stats(salt) 获取clamscan当前状态

reload(self) 强制重载clamd病毒特征库,扫描前建议做reload操作

EICAR(self) 返回EICAR测试字符串, 生成具有病毒特征的字符串便于测试


脚本如下:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import time, 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

   def run(self):
       try:
           cd = pyclamd.ClamdNetworkSocket(self.IP, 3310)  # 创建网络套接字连接对象
           if cd.ping():  # 探测连通性
               self.connstr = self.IP + "connection [OK]"
               cd.reload()  # 强制重载clamd病毒特征库,扫描前建议做reload操作
               if self.scan_type == "contscan_file":
                   self.scanresult = "{0}\n".format(cd.contsacn_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.1', '192.168.1.2']  # 扫描主机列表
scantype = "multiscan_file"  # 扫描模式 支持multiscan_file、contscan_file、scan_file
scanfile = "/"  # 指定扫描的目录

i = 1

threadnum = 2  # 指定启动的线程数
scanlist = []  # 存储扫描scan类线程对象列表

for ip in IPs:
   currp = Scan(ip, scantype, scanfile)  # 创建扫描Scan类对象,参数(IP、扫描模式、扫描路径)
   scanlist.append(currp)  # 追加对象到列表

   if i % threadnum == 0 or i == len(IPs):  # 达到指定的线程数或IP列表后启动、退出线程
       for task in scanlist:
           task.start()  # 启动线程

       for task in scanlist:
           task.join()  # 等待所有子线程退出,并输出扫描结果
           print(task.connstr)  # 打印服务器连接信息
           print(task.scanresult)  # 打印扫描结果
       scanlist = []
   i += 1
   
   
   


猜你喜欢

转载自blog.51cto.com/951295286/2131008