跳转到内容

ClamAV

来自 Arch Linux 中文维基

Clam AntiVirus 是一款 UNIX 下开源的 (GPL) 反病毒工具包。它提供了包括一个灵活且可扩展的多线程守护进程,一个命令行扫描程序及用于自动更新数据库的高级工具在内的多个实用程序。ClamAV 主要用被在文件和邮件服务器上,它通过内置的特征库来检测恶意软件,且不能被当作传统的终端安全套件来使用。

由于多重原因,Linux 反恶意软件产品的现状并不理想:

  1. 使用有限:和 Windows 相比,Linux 的用户和终端数较少,导致公司不愿为 Linux 平台开发产品。
  2. 安于现状:很多人认为 Linux 本身就是安全的,导致缺乏恶意软件防护的意识。这也导致主动防御机制上的空缺。
  3. 缺乏功能:现有工具往往缺乏 Windows 反恶意软件产品中常见的一些高级功能,因此在 Linux 上的效果较差。

由于基于 Linux 的服务器和物联网设备数量不断增加,Linux 上的恶意软件数量也在不断增加,可能的攻击面也在不断扩大,因此这种情况尤为糟糕。

ClamAV 是当前 Linux 上为数不多的,且积极开发中的反恶意软件解决方案之一。

安装[编辑 | 编辑源代码]

安装 clamav 软件包。

该操作将安装以下工具:

clamd:ClamAV 守护进程
clamonacc:访问实时保护
clamdscan:一个简易的扫描客户端
clamdtop:针对 clamd 的资源监控界面
freshclam:特征库更新守护进程
clamconf:用于创建和检查配置文件的工具

所有 ClamAV 相关的工具、服务和守护进程都通过套接字与 clamd 进行通信。

默认情况下,这是通过一个名为“LocalSocket”的本地套接字完成的。

ClamAV 还提供了通过使用网络套接字进行远程通信的功能,该套接字名为“TCPSocket”。

警告:在 LocalSocket 改为使用 TCPSocket 时,请注意与 clamd 端口安全相关的警告。通常来说会使用本地 unix 套接字,但请注意该端口未经验证或保护。

更多信息请参考:

https://blog.clamav.net/2016/06/regarding-use-of-clamav-daemons-tcp.html

https://docs.clamav.net/manual/Usage/Scanning.html#clamd-v0101 (Daemon and then ClamD section)

另一个需要注意的问题是,在使用 LocalSocket 时,clamd 需要在一个有权限扫描受监控文件的用户下运行。

配置[编辑 | 编辑源代码]

正常情况下应已有默认配置文件,否则可以通过 clamconf 手动进行生成:

clamconf -g freshclam.conf > freshclam.conf
clamconf -g clamd.conf > clamd.conf
clamconf -g clamav-milter.conf > clamav-milter.conf

下列文件中包含了对应的配置项:

  • freshclam:/etc/clamav/freshclam.conf
  • clamd:/etc/clamav/clamd.conf
  • clamd 邮件过滤:/etc/clamav/clamav-milter.conf

另外,你可以通过执行 clamconf 来检查配置文件是否可用。

默认安装方式将创建正常的默认配置,如 clamav 用户/用户组和所需的 clamd 配置。

还可以设置其它建议配置:

/etc/clamav/clamd.conf
# Log time with each message.
# Default: no
LogTime yes

# Log additional information about the infected file, such as its
# size and hash, together with the virus name.
ExtendedDetectionInfo yes

# Run as another user (clamd must be started by root for this option to work)
# Default: don't drop privileges
User clamav

# Maximum depth directories are scanned at.
# Default: 15
MaxDirectoryRecursion 20

DetectPUA yes
HeuristicAlerts yes
ScanPE yes
ScanELF yes
ScanOLE2 yes
ScanPDF yes
ScanSWF yes
ScanXMLDOCS yes
ScanHWP3 yes
ScanOneNote yes
ScanMail yes
ScanHTML yes
ScanArchive yes
Bytecode yes
AlertBrokenExecutables yes
AlertBrokenMedia yes
AlertEncrypted yes
AlertEncryptedArchive yes
AlertEncryptedDoc yes
AlertOLE2Macros yes
AlertPartitionIntersection yes

启用实时保护(访问时扫描,OnAccessScan)[编辑 | 编辑源代码]

访问时扫描是一个实时保护守护进程,会在读取/写入或执行文件时对其进行扫描。

它可被配置为在检测到问题时发出提示或进行屏蔽。

访问时扫描需要内核包含有 fanotify 模块(内核版本 >= 3.8),Arch Linux 的官方内核软件包都满足该需求。在启用访问时扫描前,请使用以下命令确认 fanotify 是否已被启用:

$ zgrep FANOTIFY /proc/config.gz

可以通过编辑 /etc/clamav/clamd.conf 来配置访问时扫描;使用该访问时扫描需要进行以下更改:

/etc/clamav/clamd.conf
# Exclude the UID of the scanner itself from checking, to prevent loops
OnAccessExcludeUname clamav

# WARNING: for security reasons, clamd should NEVER run as root.
# Previous instructions in this wiki included this line, remove it:
# User root    # REMOVE THIS
# Add this instead:
User clamav

建议进行以下更改来配置访问时扫描器为仅提醒模式:

/etc/clamav/clamd.conf
# Set the mount point where to recursively perform the scan,
# this could be every path or multiple path (one line for path)
OnAccessMountPath /

# Alternatively, add some directories instead of mount points
# OnAccessIncludePath /home

# Prevention doesn't work with OnAccessMountPath.
# It works with OnAccessIncludePath, as long as /usr and /etc are not included.
# Including /var while activating prevention is also not recommended, because
# this would slow down package installation by a factor of 1000.
OnAccessPrevention no

# Perform scans on newly created, moved, or renamed files
OnAccessExtraScanning yes

# Optionallyexclude root-owned processes
# OnAccessExcludeRootUID true

接下来,允许 clamav 用户通过 sudo 以任何带有自定义环境变量的用户身份运行 notify-send

/etc/sudoers.d/clamav
clamav ALL = (ALL) NOPASSWD: SETENV: /usr/bin/notify-send

为警报弹出通知提醒[编辑 | 编辑源代码]

目前为止,ClamAV 会静默记录所有检测结果,但不会通知用户。可以通过下面的操作在检测到问题时提醒用户。

首先,在 clamd 配置中添加一行:

/etc/clamav/clamd.conf
VirusEvent /etc/clamav/virus-event.bash

接着,创建 /etc/clamav/virus-event.bash 文件,添加以下内容并将其设为可执行

/etc/clamav/virus-event.bash
#!/bin/bash
PATH=/usr/bin
ALERT="Signature detected by clamav: $CLAM_VIRUSEVENT_VIRUSNAME in $CLAM_VIRUSEVENT_FILENAME"

# Send an alert to all graphical users.
for ADDRESS in /run/user/*; do
    USERID=${ADDRESS#/run/user/}
    /usr/bin/sudo -u "#$USERID" DBUS_SESSION_BUS_ADDRESS="unix:path=$ADDRESS/bus" PATH=${PATH} \
        /usr/bin/notify-send -w -t 999999999 -i dialog-warning "Virus found!" "$ALERT"
done

该文件使你可以自定义 clamd 访问时扫描服务检测到问题时弹出的消息内容。

默认情况下,clamonacc 会将刚访问的文件名称传递给 clamav 进行扫描。问题是,如果 clamav 用户无法访问对应的文件,就不能使用该方式进行扫描。作为替代,可以让 clamonacc(其始终以根用户权限运行)传递文件描述符。参考以下内容编辑 clamav-clamonacc.service

[Service]
ExecStart=
ExecStart=/usr/sbin/clamonacc -F --fdpass --log=/var/log/clamav/clamonacc.log

最后,启动/启用重启 clamav-clamonacc.serviceclamav-daemon.service

参考:ClamAV#启动 ClamAV + OnAccessScanning 守护进程

如果 AppArmor 拒绝了 clamd,可以将其设为 complain 模式:

# aa-complain clamd

更新病毒库[编辑 | 编辑源代码]

通过下列命令更新病毒库:

# freshclam

如果你处于代理后,需编辑 /etc/clamav/freshclam.conf 并更新 HTTPProxyServer,HTTPProxyPort,HTTPProxyUsername 和 HTTPProxyPassword 的信息。

病毒库保存在下列文件中:

/var/lib/clamav/daily.cld
/var/lib/clamav/main.cld
/var/lib/clamav/bytecode.cvd

For automatic updates first create and set the requires freshclam.log file:

touch /var/log/clamav/freshclam.log
chmod 600 /var/log/clamav/freshclam.log
chown clamav /var/log/clamav/freshclam.log

启动/启用 clamav-freshclam.serviceclamav-freshclam-once.timer 来保持病毒库为最新。

clamav-freshclam.service 将启动 freshclam 为守护进程模式,默认每天进行 12 次检查(每 2 小时一次)。检查频率可以在 /etc/clamav/freshclam.conf 中进行修改。

clamav-freshclam-once.timer 将启动 freshclam 为每天检查一次。检查频率可在 /usr/lib/systemd/system/clamav-freshclam-once.timer 中进行修改。

注意:
  • 以守护进程模式启动的 freshclam 在每次启动服务时会进行检查。
  • 每小时检查超过 1 次将被 CDN 屏蔽 24 小时。
  • .timer 单元将遵循计划配置,与设备重启或服务重启无关。
  • You can add additional virus signature databases by following #Adding_more_databases/signatures_repositories

启动 ClamAV + OnAccessScanning 守护进程[编辑 | 编辑源代码]

该操作将把所有病毒特征加载到内存中。以 2024 年 2 月为例,这些特征共将占用至少 1.6GB 内存空间。在定期更新特征时,将短时占用多一倍的内存空间。

注意:
  • 在首次启动服务前需要先运行 freshclam,否则将出现报错,并无法正常启动 ClamAV。
  • 如果你只需要使用独立扫描器进行扫描,就无需启动守护进程。更多信息可参考下方的扫描病毒部分。
  • The daemon by itself does not do anything yet unless you enable On-Access Scanning. See: #Enabling_real-time_protection_OnAccessScan

对应的服务是 clamav-daemon.service启动它并启用使它自动在启动时运行。

Additionally start and enable clamav-clamonacc.service for real-time on access protection.

测试[编辑 | 编辑源代码]

为了确保 ClamAV 和定义安装正确,请使用 clamscan 扫描 EICAR 测试文件(无病毒代码的无害签名)。

$ curl https://secure.eicar.org/eicar.com.txt | clamscan -

输出必须包括

stdin: Win.Test.EICAR_HDB-1 FOUND

实时防护[编辑 | 编辑源代码]

您可以下载 eicar 文件并保存在您配置 clamonacc 监控的目录中。例如:

$ cd /home/user/Downloads/
$ wget https://secure.eicar.org/eicar.com.txt
$ cat eicar.com.txt

添加更多病毒库/特征库[编辑 | 编辑源代码]

ClamAV 可以使用来自其他存储库或安全厂商的病毒/特征库。

可通过安装 clamav-unofficial-sigsAUR(参见 GitHub 描述)或是 python-fangfrischAUR(参见线上文档)来添加最重要的部分。两者都会添加来自如 MalwarePatrol,SecuriteInfo,Yara,Linux Malware Detect 等著名供应商的特征/病毒库。

注意:必须启动 clamav-freshclam.service 才能从 ClamAV 镜像更新官方特征信息。

选项 #1:配置 Fangfrisch[编辑 | 编辑源代码]

Fangfrisch 在设计上目标成为更安全,灵活和方便的 clamav-unofficial-sigs 替代品,且无需太多配置/etc/fangfrisch/fangfrisch.conf)。

最重要的是,与 clamav-unofficial-sigs 不同,Fangfrisch 用不需要以 root 权限运行

通过以下命令创建库结构:

# sudo -u clamav /usr/bin/fangfrisch --conf /etc/fangfrisch/fangfrisch.conf initdb

启用 fangfrisch.timer(系统层级)。

选项 #2:配置 clamav-unofficial-sigs[编辑 | 编辑源代码]

启用 clamav-unofficial-sigs.timer

这将根据 /etc/clamav-unofficial-sigs 目录下的配置文件信息定期更新非官方特征信息。

可通过如下方法手动更新特征:

# clamav-unofficial-sigs.sh

可参考 /etc/clamav-unofficial-sigs/user.conf 来修改默认设置。

MalwarePatrol 病毒库[编辑 | 编辑源代码]

如果你想使用 MalwarePatrol 病毒库,需要先在 https://malwareblocklist.org 注册账号(需付费)。

/etc/clamav-unofficial-sigs/user.conf 中,修改以下内容来启用该功能:

malwarepatrol_receipt_code="YOUR-RECEIPT-NUMBER" # 输入收据编号
malwarepatrol_product_code="8" # 免费账号设为 8,高级客户设为 15
malwarepatrol_list="clamav_basic" # clamav_basic 或是 clamav_ext
malwarepatrol_free="yes" # 免费账号设为 yes,高级客户设为 no 

来源:https://www.malwarepatrol.net/clamav-configuration-guide/

扫描病毒[编辑 | 编辑源代码]

有两种方法进行按需扫描:

使用独立扫描器[编辑 | 编辑源代码]

clamscan 可用以扫描文件, 用户目录亦或是整个系统:

$ clamscan myfile
$ clamscan --recursive --infected /home/archie
# clamscan --recursive --infected --exclude-dir='^/sys|^/dev' /
  • 如果希望 clamscan 删除感染的文件,请使用 --remove 参数。
  • 使用 -l path/to/file 参数可以将 clamscan 的日志写入 log 文件。

如果希望 clamscan 删除感染的文件,请使用 --remove 参数,也可以使用 --move=/dir 来隔离文件。

你可能会想要 clamscan 扫描大文件,在这一情况下,可将 {{ic|1=--max-filesize=4000M} 和 --max-scansize=4000M 添加到命令中。'4000M' 指的是最大值,可按需降低。

-l /path/to/file 选项将把 clamscan 的日志输出为文本文件,方便定位受感染文件。

使用守护进程[编辑 | 编辑源代码]

clamdscan 与上一选项类似,但使用了守护进程,因此必须要守护进程运行中才能使用。由于守护进程会读取 /etc/clamav/clamd.conf 中指定的配置,大多数选项都将被忽略。

使用 milter[编辑 | 编辑源代码]

Milter 会检查 sendmail 服务器中的邮件是否含有病毒。根据需要编辑 /etc/clamav/clamav-milter.conf,例如:

/etc/clamav/clamav-milter.conf
MilterSocket /tmp/clamav-milter.socket
MilterSocketMode 660
FixStaleSocket yes
User clamav
MilterSocketGroup clamav
PidFile /run/clamav/clamav-milter.pid
TemporaryDirectory /tmp
ClamdSocket unix:/run/clamav/clamd.ctl
LogSyslog yes
LogInfected Basic

创建 /etc/systemd/system/clamav-milter.service

/etc/systemd/system/clamav-milter.service
[Unit]
Description='ClamAV Milter'
After=clamav-daemon.service

[Service]
Type=forking
ExecStart=/usr/bin/clamav-milter --config-file /etc/clamav/clamav-milter.conf

[Install]
WantedBy=multi-user.target

启用启动 clamav-milter.service

对于 Postfix,将以下内容添加到 /etc/postfix/main.cf

/etc/postfix/main.cf
smtpd_milters = unix:/tmp/clamav-milter.socket
milter_default_action = tempfail

检查 journalctl 来确认 postfix 对 clamav-milter.socket 的访问权限是否正常。如有问题,请将 postfix 添加到 clamav 组中。

小贴士[编辑 | 编辑源代码]

使用多线程运行[编辑 | 编辑源代码]

使用 clamscan[编辑 | 编辑源代码]

通过命令行使用 clamscan 扫描文件或文件夹时只会使用单 CPU 线程。如果不考虑时间或者希望电脑不卡,就没什么大问题。但如果你要扫描大型目录,或者需要快速扫描 U 盘,就最好使用全部 CPU 核心来加速进程。

clamscan 设计上为单线程,因此可以使用 xargs 进行多线程扫描:

$ find /home/archie -type f -print0 | xargs -0 -P $(nproc) clamscan

在这一例子中,xargs-P 参数同时调用了尽可能多的 CPU 核心(数量由 nproc 提供)。--max-lines--max-args 参数可以更精细地调整线程间的负载分配。

This will consume loads of RAM as all processes are individual and will load the signature files. A single thread will consume around 1G (or more) of RAM, and may hang your computer unless OOM is clever enough. You may want to consider using clamdscan instead.

使用 clamdscan[编辑 | 编辑源代码]

If you already have clamd daemon running clamdscan can be used instead (see #Starting the daemon):

$ clamdscan --multiscan --fdpass /home/archie

Here the --multiscan parameter enables clamd to scan the contents of the directory in parallel using available threads. --fdpass parameter is required to pass the file descriptor permissions to clamd as the daemon is running under clamav user and group.

The number of available threads for clamdscan is determined in /etc/clamav/clamd.conf via MaxThreads parameter clamd.conf(5). Even though you may see that the number of MaxThreads specified is more than one (current default is 10), when you start the scan using clamdscan from command line and do not specify --multiscan option, only one effective CPU thread will be used for scanning.

启用 TCPSocket[编辑 | 编辑源代码]

If you enable TCPSocket in /etc/clamav/clamd.conf, you must edit clamav-daemon.socket too (see FS#57669). The systemd socket file needs to be configured with a matching port and IP address:

/etc/systemd/system/clamav-daemon.socket.d/override.conf
[Socket]
ListenStream=
ListenStream=/run/clamav/clamd.ctl
ListenStream=127.0.0.1:3310

And finally restart clamav-daemon.socket to see a Clamd binding at TCP port 3310:

# ss -tulpn | grep clamd
tcp   LISTEN 0      4096       127.0.0.1:3310      0.0.0.0:*    users:(("clamd",pid=599,fd=4),("systemd",pid=1,fd=44))

疑难解答[编辑 | 编辑源代码]

注意: 确保运行 clamscan 的用户拥有病毒库文件(/var/lib/clamav/*.c?d)的读取权限。

Error: Clamd was NOT notified[编辑 | 编辑源代码]

如果你在运行 freshclam 命令之后出现下列信息:

WARNING: Clamd was NOT notified: Cannot connect to clamd through 
/var/lib/clamav/clamd.sock connect(): No such file or directory

为 clamav 添加一个 sock 文件:

警告:参考 #OnAccessScan 中的警告了解 clamd 端口安全的相关信息。
# touch /run/clamav/clamd.ctl
# chown clamav:clamav /run/clamav/clamd.ctl

然后, 编辑 /etc/clamav/clamd.conf,去掉该行注释:

LocalSocket /run/clamav/clamd.ctl

保存文件并重启 clamav-daemon.service

Error: No supported database files found[编辑 | 编辑源代码]

当启动守护进程时出现下列错误信息:

LibClamAV Error: cli_loaddb(): No supported database files found
in /var/lib/clamav ERROR: Not supported data format

This happens because of mismatch between /etc/clamav/freshclam.conf setting DatabaseDirectory and /etc/clamav/clamd.conf setting DatabaseDirectory. /etc/clamav/freshclam.conf pointing to /var/lib/clamav, but /etc/clamav/clamd.conf (default directory) pointing to /usr/share/clamav, or other directory. Edit in /etc/clamav/clamd.conf and replace with the same DatabaseDirectory as in /etc/clamav/freshclam.conf. After that clamav will start up successfully.

Error: Can't create temporary directory[编辑 | 编辑源代码]

如果提示如下错误并给出包含 UID 和 GID 的提示,

# can't create temporary directory

请修改权限:

# chown UID:GID /var/lib/clamav && chmod 755 /var/lib/clamav

参考[编辑 | 编辑源代码]