自动化ClamAV在Linux上执行每日系统扫描和发送电子邮件通知

时间:2020-03-21 11:42:40  来源:igfitidea点击:

ClamAV是一种开源(GPL)防我将毒引擎,旨在检测特洛伊木马,我将毒,恶意软件和其他恶意威胁。

今天的计划是安装和配置ClamAV软件,以执行每日自动系统扫描,并在检测到恶意软件时发送电子邮件。

安装ClamAV

在Debian/Ubuntu上,执行以下操作:

# apt-get update && apt-get install clamav clamav-freshclam

在CentOS上,执行以下操作:

# yum install epel-release
# yum install clamav clamav-update

如果未自动启动,请在Debian上启动ClamAV我将毒数据库更新程序:

# service clamav-freshclam start

或者选择执行以下操作:

# /etc/init.d/clamav-freshclam start

上面的命令将以守护程序模式启动freshclam:

# ps -ef | grep fresh | grep clam
clamav  1951   1  1 17:19 ?  00:00:03 /usr/bin/freshclam -d --quiet

默认情况下,freshclam每小时都会查找新更新:

# grep -i check /etc/clamav/freshclam.conf
# Check for new database 24 times a day
Checks 24

请注意,我们总是可以通过键入以下命令来手动更新ClamAV:

# freshclam -v

安装SSMTP

为了能够发送电子邮件,我们需要一些简单的东西,例如SSMTP。

在Debian/Ubuntu上,执行以下操作:

# apt-get install ssmtp heirloom-mailx

在CentOS上,执行以下操作:

# yum install ssmtp mailx

打开配置文件:

# vim /etc/ssmtp/ssmtp.conf

适当更改以下设置(确保详细信息正确):

[email protected]
mailhub=mail.example.com:465
AuthUser=[USERNAME]
AuthPass=[]
UseTLS=YES
AuthMethod=LOGIN
RewriteDomain=example.com
Hostname=debian
FromLineOverride=yes #enables to use mail -r option

SSMTP配置文件包含我们的电子邮件登录详细信息,因此,最好的做法是限制普通用户访问:

# chmod 0600 /etc/ssmtp/ssmtp.conf

测试我们是否能够发送电子邮件:

# echo test | mail -v -s "testing ssmtp setup" [email protected]
[] EHLO debian
[] AUTH LOGIN
[] d2VibWFzdGVyQG5ldmFyLmx0
[] From: "root" 
[->] Date: Fri, 17 Jan 2014 17:28:17 +0000
[->] To: [email protected]
[->] Subject: testing ssmtp setup
[->] User-Agent: Heirloom mailx 12.5 6/20/10
[->] MIME-Version: 1.0
[->] Content-Type: text/plain; charset=us-ascii
[->] Content-Transfer-Encoding: 7bit
[->]
[->] test
[->] .
[] QUIT
["$LOGFILE";
else
DIRSIZE=$(du -sh "$DIRTOSCAN" 2>/dev/null | cut -f1);
echo "Starting a daily scan of "$DIRTOSCAN" directory.
Amount of data to be scanned is "$DIRSIZE".";
clamscan -ri "$DIRTOSCAN" &>"$LOGFILE";
fi
# get the value of "Infected lines" 
MALWARE=$(tail "$LOGFILE"|grep Infected|cut -d" " -f3); 
# if the value is not equal to zero, send an email with the log file attached 
if [ "$MALWARE" -ne "0" ];then 
  #using heirloom-mailx below 
echo "$EMAIL_MSG"|mail -a "$LOGFILE" -s "Malware Found" -r "$EMAIL_FROM" "$EMAIL_TO"; 
fi 
exit 0

到目前为止看起来一切都很好。

# chmod 0755 /root/.myscripts/clamscan_daily.sh

我们将创建一个新目录来存储脚本文件:

$git clone https://github.com/crylium/clamav-daily

创建每日扫描脚本

现在为脚本打开一个新文件:

并添加以下代码:

保存文件。
确保其可执行文件:

# ln /root/.myscripts/clamscan_daily.sh /etc/cron.daily/clamscan_daily

您可以从GitHub获取该脚本的最新版本(您需要安装git):

# ls -li /etc/cron.daily/clamscan_daily
44626 -rwxr-xr-x 2 root root 493 Jan 17 16:28 /etc/cron.daily/clamscan_daily

现在,有了脚本后,我们希望它每天自动执行。

通过创建每日计划任务可以轻松实现这一点。
假定系统是24/7(在这种情况下为服务器)或者至少在大多数时间都是在线的。
否则,花ac可能是更好的选择。

让我们如下创建一个硬链接:

  • 创建一个符号链接。

  • 将脚本文件移到cron.daily文件夹。

  • 使用<em> crontab </em>执行脚本。

检查以确保已创建硬链接:

将脚本添加到Cron.daily

在我的特殊情况下,创建硬链接而不是符号链接有一个主要优点。

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

为了方便起见,我倾向于将所有自定义脚本放在一个位置,有时会重命名它们。
我通常没有时间去修复所有损坏的符号链接。

# freshclam -v