Schlagwort-Archive: Antivirus ClamAV

ClamAV is an open source antivirus engine for detecting trojans, viruses, malware and other malicious threats.

ClamAV und ClamSMTP Installation unter Debian 11

ClamAV eMail Virus Scanner on Debian 11 bullseye

ClamAV E-Mail-Virenscanner auf Debian 11 bullseye

In diesem Tutorial wird ClamAV und ClamSMTP unter Debian 11 installiert.  ClamAV  ist eine Open Source Antivirus Engine zum Erkennen von Trojanern, Viren, Malware, Adware, Rootkits und anderen bösartigen Bedrohungen.

ClamAV enthält einen Multi-threaded-Scanner-Daemon, mit Befehlszeilen Dienstprogramme für das On-Demand-Dateiscannen und automatische Signatur-Updates. Der Hauptdienst ist der ClamSMTP-Daemon dieser auf Mailservern als serverseitiger Proxy für E-Mail-Virenscanner eingesetzt wird.

INSTALLATION

Die Voraussetzung zu diesem Tutorial ist ein konfiguriertes SMTP-Relay (MTA) auf Postfix. Für die Installation sind wir bereits root, zuvor noch mit Update die letzte ClamAV Version bereitgestellt werden soll.

$ apt update
$ apt-cache policy clamav clamav-daemon
$ apt install clamsmtp clamav clamav-daemon

Nach der Installation wird Postfix konfiguriert, dazu wir die Hauptkonfiguration main.cf editieren. Postfix mit seinem modularen Aufbau, übergibt die eingehenden E-Mails an den ClamSMTP-Daemon, bevor diese in die Queue gespoolt und zur User Mailbox zugestellt werden.

$ vi /etc/postfix/main.cf
...
content_filter = scan:127.0.0.1:10026

  Wer sich mit vi schwer tut, kann natürlich nano oder den favorisierten Editor verwenden.

Die Services in Postfix master.cf am Ende hinzufügen, hierdurch finden die gescannten E-Mails wieder zu Postfix zurück.

$ vi /etc/postfix/master.cf
...
scan unix -      -      n      -      16      smtp
   -o smtp_data_done_timeout=1200
   -o smtp_send_xforward_command=yes
   -o disable_dns_lookups=yes
127.0.0.1:10025 inet n      -      n      -      16       smtpd
   -o content_filter=
   -o local_recipient_maps=
   -o relay_recipient_maps=
   -o smtpd_restriction_classes=
   -o smtpd_client_restrictions=
   -o smtpd_helo_restrictions=
   -o smtpd_sender_restrictions=
   -o smtpd_recipient_restrictions=permit_mynetworks,reject
   -o mynetworks_style=host
   -o smtpd_authorized_xforward_hosts=127.0.0.0/8

Mit dem neuen ClamAV virus database updater (clamav-freshclam.service) soll kein Notify mehr an Clamd erfolgen.

$ sed -i -e "s/^NotifyClamd/#NotifyClamd/g" /etc/clamav/freshclam.conf

Die ClamSMTP-conf Datei in den Editor der Wahl laden.

$ vi /etc/clamsmtpd.conf
OutAddress: 10025
Listen: 127.0.0.1:10026
ClamAddress: /var/run/clamav/clamd.ctl
Header: X-AV-Checked: ClamAV using ClamSMTP
TempDirectory: /var/spool/clamsmtp
PidFile: /var/run/clamsmtp/clamsmtpd.pid
User: clamsmtp

Die meisten Werte können übernommen werden, der Listen Port 10026 ggf. ändern, es muss dieselbe Port Adresse sein, wie in main.cf bei content_filter. Die Kommentar Zeile bei Header: mit Hash (#) entfernen aktivieren.

Grundsätzllich ist die Installation hier schon abgeschlossen. Die nachfolgenden Einstellungen zur überprüfung, falls erforderlich diese entsprechend ändern.

$ chown -R clamsmtp. /run/clamsmtp
$ chown -R clamsmtp. /var/spool/clamsmtp
$ chmod -R 775 /var/spool/clamsmtp

Die zu ClamAV gehörenden Konfigurationsdateien sind clamd.conf
und freshclam.conf, diese folgenden Inhalt haben.

$ vi /etc/clamav/clamd.conf
#Automatically Generated by clamav-daemon postinst
#To reconfigure clamd run #dpkg-reconfigure clamav-daemon
#Please read /usr/share/doc/clamav-daemon/README.Debian.gz for details
LocalSocket /var/run/clamav/clamd.ctl
FixStaleSocket true
LocalSocketGroup clamav
LocalSocketMode 666
# TemporaryDirectory is not set to its default /tmp here to make overriding
# the default with environment variables TMPDIR/TMP/TEMP possible
User clamav
ScanMail true
ScanArchive true
ArchiveBlockEncrypted false
MaxDirectoryRecursion 15
FollowDirectorySymlinks false
FollowFileSymlinks false
ReadTimeout 180
MaxThreads 12
MaxConnectionQueueLength 15
LogSyslog false
LogRotate true
LogFacility LOG_LOCAL6
LogClean false
LogVerbose false
PreludeEnable no
PreludeAnalyzerName ClamAV
DatabaseDirectory /var/lib/clamav
OfficialDatabaseOnly false
SelfCheck 3600
Foreground false
Debug false
ScanPE true
MaxEmbeddedPE 10M
ScanOLE2 true
ScanPDF true
ScanHTML true
MaxHTMLNormalize 10M
MaxHTMLNoTags 2M
MaxScriptNormalize 5M
MaxZipTypeRcg 1M
ScanSWF true
ExitOnOOM false
LeaveTemporaryFiles false
AlgorithmicDetection true
ScanELF true
IdleTimeout 30
CrossFilesystems true
PhishingSignatures true
PhishingScanURLs true
PhishingAlwaysBlockSSLMismatch false
PhishingAlwaysBlockCloak false
PartitionIntersection false
DetectPUA false
ScanPartialMessages false
HeuristicScanPrecedence false
StructuredDataDetection false
CommandReadTimeout 30
SendBufTimeout 200
MaxQueue 100
ExtendedDetectionInfo true
OLE2BlockMacros false
AllowAllMatchScan true
ForceToDisk false
DisableCertCheck false
DisableCache false
MaxScanTime 120000
MaxScanSize 100M
MaxFileSize 25M
MaxRecursion 16
MaxFiles 10000
MaxPartitions 50
MaxIconsPE 100
PCREMatchLimit 10000
PCRERecMatchLimit 5000
PCREMaxFileSize 25M
ScanXMLDOCS true
ScanHWP3 true
MaxRecHWP3 16
StreamMaxLength 25M
LogFile /var/log/clamav/clamav.log
LogTime true
LogFileUnlock false
LogFileMaxSize 0
Bytecode true
BytecodeSecurity TrustSigned
BytecodeTimeout 60000
OnAccessMaxFileSize 5M

Die Datei clamd.conf lässt sich mit dpkg-reconfigure clamav-daemon konfigurieren, dabei die defaut Werte vorgeschalgen werden.

Um die Methode für Virendatenbank-Updates zu wählen und clamav-freshclam zu konfigurieren, ist es der Befehl dpkg-reconfigure clamav-freshclam.

$ vi /etc/clamav/freshclam.conf
# Automatically created by the clamav-freshclam postinst
# Comments will get lost when you reconfigure the clamav-freshclam package

DatabaseOwner clamav
UpdateLogFile /var/log/clamav/freshclam.log
LogVerbose false
LogSyslog false
LogFacility LOG_LOCAL6
LogFileMaxSize 0
LogRotate true
LogTime true
Foreground false
Debug false
MaxAttempts 5
DatabaseDirectory /var/lib/clamav
DNSDatabaseInfo current.cvd.clamav.net
ConnectTimeout 30
ReceiveTimeout 0
TestDatabases yes
ScriptedUpdates yes
CompressLocalDatabase no
Bytecode true
#NotifyClamd /etc/clamav/clamd.conf
# Check for new database 24 times a day
Checks 24
DatabaseMirror db.local.clamav.net
DatabaseMirror database.clamav.net

Testing ClamAV ClamSMTP Postfix

Die systemd control commands für die ClamAV-Daemons.

$ systemctl restart clamav-daemon clamsmtp postfix
$ systemctl status clamav-daemon clamsmtp postfix
$ systemctl restart clamav-freshclam.service
$ systemctl status clamav-freshclam.service

Sind die Daemon ClamSMTP und Postfix konfiguriert, können die aktiven Ports im Loop überprüft werden, mit netstat oder ss.

$ netstat -tpln | grep 127.0.0.1
tcp        0      0 127.0.0.1:10025         0.0.0.0:*               LISTEN      1513/master
tcp        0      0 127.0.0.1:10026         0.0.0.0:*               LISTEN      690/clamsmtpd

Die EICAR-Test-Signatur um den E-Mail-Virenscanner zu überprüfen in eine Datei speichern, beispielsweise in eicar-signatur.txt.

From: hostmaster@example.org
To: email@example.org
Subject: test - virus scanner test pattern

X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*

Die EICAR-Test-Signatur mit sendmail an sich selber senden.

$ echo "Subject: EICAR-Test-Signatur" | sendmail email@example.org < eicar-signatur.txt

In /var/log/mail.log ist der Eintrag 250 Virus Detected; Discarded Email zu finden. Die E-Mail mit der EICAR-Test-Signatur sollte selbsvertändlich nicht zugestellt werden.

Nov 21 14:01:41 debian clamsmtpd: 100001: from=root@example.org, to=email@example.org, status=VIRUS:Eicar-Signature
Nov 21 14:01:41 debian postfix/smtp[2077]: D93FA10001A: to=<email@example.org>, relay=127.0.0.1[127.0.0.1]:10026, delay=0.07, delays=0.02/0.02/0.01/0.02, dsn=2.0.0, status=sent (250 Virus Detected; Discarded Email)

Conclusion

Der Beitrag zeigt die Installation und Anwendung von ClamAV auf einem Mail Transfer Agent, mit abschliesssender überprüfung mittels Test-Signatur. ClamAV ist eine Open-Source-(GPL-)Antiviren-Engine, die in einer Vielzahl von Situationen verwendet wird, einschließlich E-Mail- und Web-Scans sowie Endpunktsicherheit. Es bietet viele Dienstprogramme für Benutzer, darunter einen flexiblen und skalierbaren Multithread-Daemon, einen Befehlszeilen-Scanner und ein fortschrittliches Tool für automatische Datenbank-Updates.

ClamAV Postfix Integration auf CentOS

ClamAV ist ein Open-Source (GPL) Anti-Virus Toolkit für UNIX/Linux

Speziell für E-Mail-Scanning auf Mail-Gateways. Es bietet eine Reihe von Dienstprogrammen, einschließlich eines flexiblen und skalierbaren Multi-Threaded Daemon, sowie ein Kommandozeilen-Scanner als Fortgeschrittenes Werkzeug für die automatische Datenbankaktualisierungen. Der Kern des Pakets stellt eine Anti-Virus-Engine in Form einer gemeinsam genutzten Bibliothek zur Verfügung.

Dieser Artikel beschreibt die Integration von ClamAV auf einem CentOS 6 Mail Gateway (MTA) mit Postfix.

Als erstes wird der ClamAV Daemon aus dem EPL-Repo auf dem MTA installiert.

yum --enablerepo=epel -y install clamd clamsmtp

Nach der Installation wird der ClamAV-SMTP Daemon konfiguriert, bei den aufgeführten Zeilen das Kommentarzeichen-# entfernen (uncomment).

vi /etc/clamsmtpd.conf

Listen: 0.0.0.0:10025
Header: X-Virus-Scanned: ClamAV using ClamSMTP
Action: drop

Nun können die Dienste gestartet werden.

service clamsmtpd start

Mit freshclam die Antivirus-DB herunterladen.

freshclam

Den ClamAV Daemon starten

service clamsmtp-clamd start

Automatischer Systemstart aktivieren.

chkconfig clamsmtpd on
chkconfig clamsmtp-clamd on

Die Postfix content-filter Integration erfolgt in main.cf,  über Port 10025 zu ClamAV, aus master.cf über Port 10026 der Rücktransport zu Postfix.

vi /etc/postfix/main.cf

content_filter = scan:127.0.0.1:10025

vi /etc/postfix/master.ch

scan unix  -       -       n       -       16       smtp
   -o smtp_data_done_timeout=1200
   -o smtp_send_xforward_command=yes
   -o disable_dns_lookups=yes
127.0.0.1:10026 inet n       -       n       -       16       smtpd
   -o content_filter=
   -o local_recipient_maps=
   -o relay_recipient_maps=
   -o smtpd_restriction_classes=
   -o smtpd_client_restrictions=
   -o smtpd_helo_restrictions=
   -o smtpd_sender_restrictions=
   -o smtpd_recipient_restrictions=permit_mynetworks,reject
   -o mynetworks_style=host
   -o smtpd_authorized_xforward_hosts=127.0.0.0/8

Postfix muss nun neu gestartet werden.

service postfix restart

Mit netstat kann die  Daemon Bereitschaft überprüft werden.

[root@mail ~]# netstat -talpn | grep clam
tcp      0      0 0.0.0.0:10025       0.0.0.0:*       LISTEN     21645/clamsmtpd

ClamAV kann mit telnet überprüft werden, wenn alles funktioniert sollte folgendes Resultat ausgegeben werden.

[root@mail ~]# telnet localhost 10025
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 smtp.passthru
ehlo localhost
250-smtp.passthru
250-SIZE 22000000
250-VRFY
250-ETRN
250-XFORWARD NAME ADDR PROTO HELO SOURCE PORT
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
quit
221 2.0.0 Bye
Connection closed by foreign host.

[root@mail ~]# telnet localhost 10026
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 mail.odyssee.net ESMTP Postfix
ehlo localhost
250-mail.odyssee.net
250-PIPELINING
250-SIZE 22000000
250-VRFY
250-ETRN
250-XFORWARD NAME ADDR PROTO HELO SOURCE PORT
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
quit
221 2.0.0 Bye
Connection closed by foreign host.

Auch sollte das Mail Protokoll konsultiert werden.

tail -f /var/log/maillog

Bei den E-Mail Internetkopfzeilen (SMTP-Envelope) erscheint die X-Header Signatur.

SMTP-Internetkopfzeilen
SMTP-Internetkopfzeilen

Troubleshooting

Werden keine Mails mehr empfangen, ist wahrscheinlich folgender Fehler in maillog zu finden:

clamsmtpd: 100006: CLAMAV: couldn’t connect to: /var/run/clamd.clamsmtp/clamd.sock: No such file or directory
clamsmtpd: 100004: SERVER: couldn’t connect to: 127.0.0.1:10026: Transport endpoint is not connected

Der clamd deamon ist nicht mehr aktive. Um die clamav Services neu zu starten, können diese in der Console beendet, und anschlissend wieder gestartet werden:

service clamd stop
service clamsmtp-clamd stop
service clamsmtpd stop
service clamd start
service clamsmtp-clamd start
service clamsmtpd start

Nach dem clamd und clamsmtpd gestartet ist, können die aktiven Ports wie folgt überprüft werden:

ls -al /var/run/clamd.clamsmtp/clamd.pid
-rw-rw-r-- 1 clamsmtp mail 6 Oct 28 16:24 /var/run/clamd.clamsmtp/clamd.pid

netstat -tulpn
tcp     0     0 127.0.0.1:10025        0.0.0.0:*        LISTEN      21134/clamsmtpd
tcp     0     0 127.0.0.1:10026        0.0.0.0:*        LISTEN      21272/master

Weiter könnten noch Mails in der Queue liegen, diese mit dem Befehl mailq abgerfagt werden und postqueue abgearbeitet werden.

mailq
postqueue -f

Quelle: https://www.clamav.net/