Skip to main content

5.ClamAV简单使用(未完成)

(文章状态:未完成)

文章主要内容转载自:在 EC2 Linux 操作系统上部署 ClamAV 并开启实时防护、集中日志采集和统一告警

背景

计算机信息系统等级保护(俗称等保)的要求之一是应用服务器具有病毒防护手段,并定期升级病毒特征库。这个防护场景通常被称为Endpoint Detection and Response(EDR),也可简称终端防护。EDR一般是采用侵入性的防护方式。所谓侵入性意味着需要在虚拟机内植入专门的Agent才可以进行安全防护。

商用的EDR方案有完整的检测、识别、发现、预防、干预、强化、集中管控等一整套管理功能,不过相对带来的问题是成本较高。在一些非严格等保的场合,如果只是要求实现病毒扫描和防护能力,那么可使用本文介绍的ClamAV方案进行替代。

ClamAV是一个开源的引擎,用于检测病毒、木马、恶意软件和其他威胁。ClamAV支持Windows、Linux和MacOS等系统。Github官网在这里。ClamAV由Talos所有,后者又被Cisco收购,用于为Cisco供应威胁情报。ClamAV社区提供了免费的持续的病毒特征库升级。因此可实现一次部署,长期使用。

本文介绍如何在Rocky Linux 操作系统上部署,其他系统如Ubuntu安装过程相仿请参考对应文档。

安装ClamAV并执行手工扫描

安装软件包

注意:

ClamAV加载特征库和全盘扫描比较消耗CPU和内存资源,因此建议在不小于3GB内存的机型上安装部署。最低配置要求是:内存为1GB、2GB的机型,最低配置下运行速度可能较为缓慢,甚至出现部分时间假死的情况。

预编译软件包下载地址:https://www.clamav.net/downloads

下载软件包

cd /opt
sudo wget https://www.clamav.net/downloads/production/clamav-1.3.0.linux.x86_64.rpm

本文采用操作系统自带的软件源。执行如下命令安装。

dnf update -y
dnf install epel-release -y
dnf install clamav clamd -y

安装好的ClamAV有几个主要组成部分:

  • clamscan:手工扫描程序
  • clamdscan:依赖后台服务的扫描程序
  • clamonacc:按需扫描程序(实时监控),对应配置文件 /etc/clamd.d/scan.conf
  • clamd:按需扫描的后台进程,对应配置文件 /etc/clamd.d/scan.conf
  • fleshclam:病毒库升级工具,对应配置文件 /etc/fleshclam.conf
  • clamconf:配置文件生成工具

手工更新病毒库

首次安装后,必须先下载病毒库。执行如下命令更新病毒库:

freshclam

返回结果如下表示升级成功:

ClamAV update process started at Sat Dec  3 14:15:51 2022
daily database available for download (remote version: 26739)
Time: 0.7s, ETA: 0.0s [========================>] 57.54MiB/57.54MiB
Testing database: '/usr/local/share/clamav/tmp.645c56b964/clamav-293585c0119a1f028368c597169d1634.tmp-daily.cvd' ...
Database test passed.
daily.cvd updated (version: 26739, sigs: 2013388, f-level: 90, builder: raynman)
main database available for download (remote version: 62)
Time: 0.6s, ETA: 0.0s [========================>] 162.58MiB/162.58MiB
Testing database: '/usr/local/share/clamav/tmp.645c56b964/clamav-e66a7c48ee3b6ac03f488235d68bf206.tmp-main.cvd' ...
Database test passed.
main.cvd updated (version: 62, sigs: 6647427, f-level: 90, builder: sigmgr)
bytecode database available for download (remote version: 333)
Time: 0.0s, ETA: 0.0s [========================>] 286.79KiB/286.79KiB
Testing database: '/usr/local/share/clamav/tmp.645c56b964/clamav-12719bf9f9650a8a8000cad457a59542.tmp-bytecode.cvd' ...
Database test passed.
bytecode.cvd updated (version: 333, sigs: 92, f-level: 63, builder: awillia2)

设置后台自动升级病毒库:

systemctl start clamav-freshclam
systemctl enable clamav-freshclam
systemctl status clamav-freshclam

默认的升级频率是每2小时升级1次,折合每天12次。如果需要调整,可以编辑如下配置文件 /etc/freshclam.conf,找到其中的如下一段,把数值改为1:

vim /etc/freshclam.conf
#更改以下参数
# Number of database checks per day.
# Default: 12 (every two hours)
Checks 1

重启服务,即可将升级频率设置为每天升级1次。

systemctl restart clamav-freshclam

手工扫描特定文件夹

手工对某一个目录执行扫描。执行如下命令:

clamscan -r -i /

其中 -r参数表示扫描子目录,-i参数表示只输出疑似病毒扫描结果。在启动后,首先ClamAV会验证特征库的签名,然后执行扫描。对根目录扫描的时间会很长,可能长达数分钟。

返回如下结果表示扫描完成。

----------- SCAN SUMMARY -----------
Known viruses: 8685259
Engine version: 1.0.4
Scanned directories: 22362
Scanned files: 97292
Infected files: 0
Total errors: 571
Data scanned: 7362.39 MB
Data read: 3454.23 MB (ratio 2.13:1)
Time: 595.189 sec (9 m 55 s)
Start Date: 2024:02:15 15:39:36
End Date: 2024:02:15 15:49:31

扫描过程中可能会报错,如下:

LibClamAV Warning: fmap_readpage: pread fail: asked for 4094 bytes @ offset 2, got 0
WARNING: Can't open file /sys/module/uhci_hcd/uevent: Permission denied
LibClamAV Warning: fmap_readpage: pread fail: asked for 4094 bytes @ offset 2, got 0
LibClamAV Warning: fmap_readpage: pread fail: asked for 4094 bytes @ offset 2, got 0
LibClamAV Warning: fmap_readpage: pread fail: asked for 4094 bytes @ offset 2, got 0
WARNING: Can't open file /sys/module/crc32_pclmul/uevent: Permission denied
LibClamAV Warning: fmap_readpage: pread fail: asked for 4095 bytes @ offset 1, got 0
LibClamAV Warning: fmap_readpage: pread fail: asked for 4072 bytes @ offset 24, got 0
LibClamAV Warning: fmap_readpage: pread fail: asked for 4094 bytes @ offset 2, got 0
LibClamAV Warning: fmap_readpage: pread fail: asked for 4092 bytes @ offset 4, got 0
LibClamAV Warning: fmap_readpage: pread fail: asked for 4090 bytes @ offset 6, got 0
LibClamAV Warning: fmap_readpage: pread fail: asked for 4091 bytes @ offset 5, got 0

出现以上报错可以先检查selinux

getenforce
#enforcing代表selinux开启状态
Enforcing

开启实时检测和定期检测

ClamAV在Linux系统上支持实时检测,也就是对当前系统读取和写入的文件进行检测,被称为 on-access scanning

修改配置文件

实时检测需要启动clamd服务。这个服务的配置文件根据不同安装方式,可能配置文件路径和文件名不一样。本文使用的Rocky Linux 的epel源安装后,配置文件是 /etc/clamd.d/scan.conf。这其中要修改监听管道、启动用户等多处配置。

编辑 /etc/clamd.d/scan.conf,修改如下的几个参数(排名和顺序可能所有不同,不影响效果)。

# Path to a local socket file the daemon will listen on.
# Default: disabled (must be specified by a user)
LocalSocket /tmp/clamd.socket

# Sets the group ownership on the unix socket.
# Default: disabled (the primary group of the user running clamd)
LocalSocketGroup clamscan

# Sets the permissions on the unix socket to the specified mode.
# Default: disabled (socket is world accessible)
LocalSocketMode 660

# Modifies fanotify blocking behaviour when handling permission events.
# If off, fanotify will only notify if the file scanned is a virus,
# and not perform any blocking.
# Default: no
OnAccessPrevention yes

# This option allows exclusions via user names when using the on-access
# scanning client. It can be used multiple times.
# It has the same potential race condition limitations of the
# OnAccessExcludeUID option.
# Default: disabled
OnAccessExcludeUname clamscan

# Set the include paths (all files inside them will be scanned). You can have
# multiple OnAccessIncludePath directives but each directory must be added
# in a separate line.
# Default: disabled
OnAccessIncludePath /home

# Uncomment this option to enable logging.
# LogFile must be writable for the user running daemon.
# A full path is required.
# Default: disabled
LogFile /var/log/clamd.scan

# Maximum size of the log file.
# Value of 0 disables the limit.
# You may use 'M' or 'm' for megabytes (1M = 1m = 1048576 bytes)
# and 'K' or 'k' for kilobytes (1K = 1k = 1024 bytes). To specify the size
# in bytes just don't use modifiers. If LogFileMaxSize is enabled, log
# rotation (the LogRotate option) will always be enabled.
# Default: 1M
LogFileMaxSize 20M

# Log time with each message.
# Default: no
LogTime yes

参数简介:

LocalSocket

  • 服务监听参数。启动有localsocket和网络监听两种模式,可独立使用,也可以同时使用。对于Rockylinux系统的本机扫描而言,适用Unix管道的localsocket模式即可。

LocalSocketGroup

  • 指定 Unix 套接字的组所有权。使用运行 clamd 的用户的主组。套接字是全局可访问的。

LocalSocketMode

  • 设置 Unix 套接字的权限模式。

OnAccess

  • OnAccess的参数是实时扫描相关,分别表示:打开实时防护并拦截有风险的访问;设置要执行实施监控的路径(不建议对 /根路径监控,建议只监控应用路径,本文以 /home为例);

OnAccessPrevention

  • 修改当处理权限事件时的 fanotify 阻塞行为。

OnAccessExcludeUname

  • 当使用 on-access 扫描客户端时,允许通过用户名来排除文件。
  • 由于clamd不能以root身份运行,而需要以普通用户身份运行,在本文使用操作系统安装源时候,会自动创建名为 clamscan的新用户,然后以这个身份运行。
  • 此在配置文件中,这里需要设置一个与clamd服务启动的用户名保持一致,否则会发生自己触发扫描自己的递归调用,造成不必要的资源消耗。

OnAccessIncludePath

  • 设置包含路径(其中的所有文件将被扫描)。可以有多个 OnAccessIncludePath 指令,但每个目录必须单独添加在一行中。

LogFile

  • 启用日志记录的选项。LogFile 必须对运行守护进程的用户可写。
  • 必须是完整路径。

LogSyslog

  • 默认配置 LogSyslog yes这个参数的生效,因此扫描到的病毒结果会输出到系统日志中。本文为了便于查询,额外又设置了如上配置文件中的日志地址 /var/log/clamd.scan并且配置了最大体积为20MB。

LogFileMaxSize

  • 日志文件的最大大小。0 表示不限制大小。
  • 可以使用 'M' 或 'm' 表示兆字节(1M = 1m = 1048576 字节),使用 'K' 或 'k' 表示千字节(1K = 1k = 1024 字节)。如果启用了 LogFileMaxSize,则日志轮换(LogRotate 选项)将始终启用。

LogTime

  • 每条消息是否记录时间。

都修改完毕后,保存退出。

执行如下命令创建日志文件:

touch /var/log/clamd.scan
chown clamscan:clamscan /var/log/clamd.scan

启动clamd和clamonacc后台服务

以root身份执行如下命令启动 clamd服务:

https://docs.clamav.net/Introduction.html