Rspamd Einrichten: Unterschied zwischen den Versionen

Aus Xinux Wiki
Zur Navigation springen Zur Suche springen
Zeile 253: Zeile 253:
 
</pre>
 
</pre>
  
 +
=== /etc/clamav/clamd.conf ===
 +
<pre>
 +
# Automatisch generiert und manuell angepasst fuer Rspamd nINSTREAM
 +
TCPSocket 3310
 +
TCPAddr 127.0.0.1
 +
User clamav
 +
ScanMail true
 +
ScanArchive true
 +
ArchiveBlockEncrypted false
 +
MaxDirectoryRecursion 15
 +
FollowDirectorySymlinks false
 +
FollowFileSymlinks true
 +
ReadTimeout 180
 +
MaxThreads 12
 +
MaxConnectionQueueLength 15
 +
LogSyslog false
 +
LogRotate true
 +
LogFacility LOG_LOCAL6
 +
LogClean false
 +
LogVerbose false
 +
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
 +
ExtendedDetectionInfo true
 +
AllowAllMatchScan true
 +
ForceToDisk false
 +
DisableCertCheck false
 +
DisableCache false
 +
MaxScanTime 120000
 +
MaxScanSize 100M
 +
MaxFileSize 25M
 +
MaxRecursion 16
 +
MaxFiles 10000
 +
# Wichtig fuer Rspamd Integration
 +
StreamMaxLength 100M
 +
LogFile /var/log/clamav/clamav.log
 +
LogTime true
 +
Bytecode true
 +
BytecodeSecurity TrustSigned
 +
BytecodeTimeout 60000
 +
</pre>
  
 +
=== Hinweise zur ClamAV Konfiguration ===
  
 +
Die Parameter TCPSocket und TCPAddr erlauben die Kommunikation ueber das Netzwerkinterface (Loopback), waehrend StreamMaxLength sicherstellt, dass groessere Mails ohne "Size limit exceeded" Fehler verarbeitet werden.
  
 +
<pre>
 +
systemctl restart clamav-daemon
 +
</pre>
  
 
== Anleitung: ClamAV-Integration in Rspamd (TCP-Modus) ==
 
== Anleitung: ClamAV-Integration in Rspamd (TCP-Modus) ==

Version vom 21. März 2026, 16:56 Uhr

Rspamd und ClamAV installieren

Debian/Ubuntu

apt install rspamd clamav-daemon clamav-freshclam  redis

RHEL/CentOS

dnf install rspamd clamav clamav-update redis

Enablen und Startend er Dienste

systemctl enable  rspamd --now
systemctl enable  clamav-daemon --now

Redis-Anbindung (Das Gedächtnis von Rspamd)

Damit Rspamd Scanergebnisse zwischenspeichern und statistische Analysen (Bayes) durchführen kann, ist eine Datenbank-Anbindung zwingend erforderlich. Ohne Redis arbeiten viele Module (wie Antivirus oder Greylisting) nur eingeschränkt oder gar nicht.

Installation und Aktivierung

Zuerst muss der Redis-Server auf dem System installiert und gestartet sein.

 apt install redis-server
 systemctl enable --now redis-server

Konfiguration in Rspamd

Rspamd benötigt einen zentralen Ort, an dem die Server-Adresse von Redis definiert ist. Dies geschieht üblicherweise in einer lokalen Konfigurationsdatei.

Datei: /etc/rspamd/local.d/redis.conf

# Definition des lokalen Redis-Servers
servers = "127.0.0.1:6379";


Prüfung der Anbindung

Ob Rspamd erfolgreich mit Redis kommuniziert, lässt sich über das Admin-Tool prüfen:

 rspamadm control stat

In der Ausgabe sollte unter "Nodes" der Status des Redis-Servers als "online" oder "active" erscheinen.

Warum das für ClamAV wichtig ist

Virenscanner-Ergebnisse sind CPU-intensiv. Rspamd speichert das Ergebnis eines Scans (z.B. "Diese Datei mit Hash XY ist ein Virus") für eine gewisse Zeit in Redis. Wenn dieselbe Mail erneut reinkommt, muss ClamAV nicht noch einmal rechnen – Rspamd holt die Antwort direkt aus dem Redis-Cache.

systemctl start redis-server
systemctl enable  redis-server.service

Läuft alles

systemctl status  rspamd
systemctl status  clamav-daemon
systemctl status redis

Dienste und Ports von Rspamd

Rspamd Proxy

  • Der Rspamd Proxy dient als Eingangsfilter für E-Mails und leitet sie an die entsprechenden Worker weiter, um die Last zu verteilen.
  
Port: 11332/tcp (Standardport für Proxy-Dienste)  

Rspamd Worker

  • Der Rspamd Worker führt die eigentliche Spam-Analyse durch und verarbeitet eingehende Nachrichten anhand von Regeln, Statistiken und externen Diensten.
  
Port: 11333/tcp (Standardport für Worker-Dienste)  

Rspamd Controller

  • Der Rspamd Controller stellt eine API für Konfiguration, Statistiken und manuelles Training des Filters zur Verfügung.
  
Port: 11334/tcp (Standardport für den Controller)  

Passwort für den Rspamd Controller und Port nach aussen öffen

Passwort erzeugen

rspamadm pw

Enter passphrase: 
$2$mqbbp8yb4fz8febgpxk7rb4db9p5njwg$xior3gxjbuc76bhsq4rapq7x98cssrm9qkr49kwapgdsahmpzjny
In der Worker Datei eintragen
Port an allen Interfaces lauschen lassen
cat  /etc/rspamd/override.d/worker-controller.inc
 
password =  "$2$mqbbp8yb4fz8febgpxk7rb4db9p5njwg$xior3gxjbuc76bhsq4rapq7x98cssrm9qkr49kwapgdsahmpzjny";
bind_socket ="*:11334";

Restart

systemctl restart rspamd

Zugriff auf das Webinterface

Rspamd-1.png

Milters

  • Milters sind Mail-Filter, die über das Milter-Protokoll mit MTA-Software wie Postfix oder Sendmail kommunizieren.
  • Sie ermöglichen die Verarbeitung und Filterung von E-Mails in Echtzeit, bevor sie zugestellt werden.
  • Milter werden häufig für Spam-Filterung, Virenscans und Inhaltsüberprüfungen eingesetzt.

Einbinden in Postfix

postconf -e "smtpd_milters=inet:127.0.0.1:11332"
postconf -e "non_smtpd_milters=inet:127.0.0.1:11332"
postfix reload

Gtube Testmail

  • echo 'XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X' | mail -s "Boese Mail" martha

Resultat

Rspamd-2.png

Die Logs

journalctl  -u postfix | grep cleanup
Mar 18 19:21:22 mail.it113.int postfix/cleanup[7407]: 88BDDE0792: milter-reject: END-OF-MESSAGE from localhost[127.0.0.1]: 5.7.1 Gtube pattern; from=<root@mail.it113.int> to=<martha@mail.it113.int>
Mar 18 19:21:22 mail.it113.int postfix/cleanup[7407]: 88BDDE0792: to=<martha@mail.it113.int>, orig_to=<martha>, relay=none, delay=0.19, delays=0.19/0/0/0, dsn=5.7.1, status=bounced (Gtube pattern)
Mar 18 19:21:22 mail.it113.int postfix/cleanup[7410]: B3D24E07C2: message-id=<20250318182122.B3D24E07C2@mail.it113.int>

Restart

  • systemctl restart rspamd

RBL-Integration in Rspamd und Postfix (Proof of Concept)

Rspamd mit lokaler RBL

Configuration

vi /etc/rspamd/local.d/multimap.conf
blacklist_ip {
    type = "ip";
    map = "/etc/rspamd/local.d/local_rbl.txt";
    symbol = "LOCAL_RBL_HIT";
    action = "reject";
    description = "Lokale RBL-Hit";
}

Blackliste

echo 10.88.113.21 > /etc/rspamd/local.d/local_rbl.txt 

Rspamd neustarten

systemctl restart rspamd

Tests

postiv
echo hallo welt | rspamc -i 10.88.113.22
Results for file: stdin (0 seconds)
[Metric: default]
Action: add header
Spam: true
Score: 7.90 / 15.00
Symbol: ARC_NA (0.00)
Symbol: MIME_GOOD (-0.10)[text/plain]
Symbol: MIME_TRACE (0.00)[0:+]
Symbol: MISSING_DATE (1.00)
Symbol: MISSING_FROM (2.00)
Symbol: MISSING_MID (2.50)
Symbol: MISSING_SUBJECT (0.50)
Symbol: MISSING_TO (2.00)
Symbol: RCVD_COUNT_ZERO (0.00)[0]
Message-ID: undef
negativ
  • echo hallo welt | rspamc -i 10.88.113.21
Results for file: stdin (0 seconds)
[Metric: default]
Action: reject
Spam: true
Score: -0.10 / 15.00
Symbol: ARC_NA (0.00)
Symbol: LOCAL_RBL_HIT (0.00)[10.88.113.21]
Symbol: MIME_GOOD (-0.10)[text/plain]
Symbol: MIME_TRACE (0.00)[0:+]
Message-ID: undef
Message - smtp_message: Matched map: LOCAL_RBL_HIT

Rspamd Schwellenwerte (Actions)

Die Bewertung erfolgt über Scores. Diese werden in der Datei /etc/rspamd/local.d/actions.conf definiert.

Aktion Empfohlener Score Bedeutung
reject 15.0 Die Mail wird hart abgelehnt (SMTP 5xx).
add_header 6.0 Die Mail wird zugestellt, erhält aber einen Header (z.B. X-Spam: Yes).
greylist 4.0 Der Absender wird temporär abgewiesen (Soft-Fail). Ein echter Mailserver versucht es erneut.
no action < 4.0 Die Mail gilt als sauber (Ham).

Kurzer Check der Funktionsweise

Um zu sehen, welche Symbole Rspamd einer Mail vergibt, ohne eine echte Mail zu senden:

rspamc < /pfad/zu/einer/test.eml

Das Tool rspamc gibt den kompletten Scan-Report direkt auf der Konsole aus.

ClamAV in Rspamd einbinden

Konfiguration Rspamd und ClamAV Integration

Die folgenden Dateien definieren die Anbindung an den Virenscanner sowie grundlegende Diensteinstellungen unter /etc/rspamd/.

/etc/rspamd/override.d/antivirus.conf

enabled = true;
clamav {
    type = "clamav";
    symbol = "CLAM_VIRUS";
    servers = "127.0.0.1:3310";
    # Erlaubt das Scannen von reinem Text (Body)
    scan_text_mime = true;
    scan_mime_parts = true;
    # Scannt auch winzige Dateien (EICAR ist < 100 Bytes)
    min_size = 0;
    # Ignoriert, dass die Mail von localhost/trusted IP kommt
    scan_unauthenticated = true;
    # Technische Uebertragung via nINSTREAM
    stream = true;
    # Das Symbol soll sofort zum Reject fuehren
    score = 20.0;
}

/etc/rspamd/override.d/worker-controller.inc

password = "$2$5dj6zn4mt56sbmh8nw944c41wt537tdb$z4op8fwmunpwhe7yzbm94wgeqsdq8ajgjc4zs387e5qxnzppw46y";
bind_socket ="*:11334";

/etc/rspamd/local.d/logging.inc

debug_modules = ["antivirus", "clamav"];

/etc/rspamd/local.d/redis.conf

servers = "127.0.0.1";

/etc/rspamd/local.d/rbl.conf

enabled = false;

Aktivierung der Konfiguration

Nach den Aenderungen muss die Konfiguration geprueft und der Dienst neu gestartet werden.

rspamadm configtest
systemctl restart rspamd
redis-cli flushall

/etc/clamav/clamd.conf

# Automatisch generiert und manuell angepasst fuer Rspamd nINSTREAM
TCPSocket 3310
TCPAddr 127.0.0.1
User clamav
ScanMail true
ScanArchive true
ArchiveBlockEncrypted false
MaxDirectoryRecursion 15
FollowDirectorySymlinks false
FollowFileSymlinks true
ReadTimeout 180
MaxThreads 12
MaxConnectionQueueLength 15
LogSyslog false
LogRotate true
LogFacility LOG_LOCAL6
LogClean false
LogVerbose false
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
ExtendedDetectionInfo true
AllowAllMatchScan true
ForceToDisk false
DisableCertCheck false
DisableCache false
MaxScanTime 120000
MaxScanSize 100M
MaxFileSize 25M
MaxRecursion 16
MaxFiles 10000
# Wichtig fuer Rspamd Integration
StreamMaxLength 100M
LogFile /var/log/clamav/clamav.log
LogTime true
Bytecode true
BytecodeSecurity TrustSigned
BytecodeTimeout 60000

Hinweise zur ClamAV Konfiguration

Die Parameter TCPSocket und TCPAddr erlauben die Kommunikation ueber das Netzwerkinterface (Loopback), waehrend StreamMaxLength sicherstellt, dass groessere Mails ohne "Size limit exceeded" Fehler verarbeitet werden.

systemctl restart clamav-daemon

Anleitung: ClamAV-Integration in Rspamd (TCP-Modus)

Diese Dokumentation beschreibt die Konfiguration von ClamAV über Systemd-Sockets und die Anbindung an Rspamd.

Systemd-Socket-Konfiguration

Die Socket-Unit muss angepasst werden, um den TCP-Port 3310 freizugeben. Um das Original-File unter /lib/ zu erhalten, wird eine Kopie in /etc/systemd/system/ erstellt und mittels sed angepasst:

 sed "s^ListenStream=/run/clamav/clamd.ctl^ListenStream=127.0.0.1:3310^" /lib/systemd/system/clamav-daemon.socket > /etc/systemd/system/clamav-daemon.socket

Aktivierung der Änderungen:

 systemctl daemon-reload
 systemctl restart clamav-daemon.socket
 systemctl restart clamav-daemon.service

Überprüfung des Sockets:

 ss -ltpn | grep 3310

Rspamd-Modul Konfiguration

Die Konfiguration erfolgt als Override, um lokale Scans und kleine Dateien (EICAR) zu erzwingen.

Datei: /etc/rspamd/override.d/antivirus.conf

enabled = true;
clamav {
    type = "clamav";
    symbol = "CLAM_VIRUS";
    servers = "127.0.0.1:3310";
    scan_text_mime = true;
    scan_mime_parts = true;
    min_size = 0;
    scan_unauthenticated = true;
    stream = true;
    score = 20.0;
}

Neustart des Dienstes:

 systemctl restart rspamd

Erstellung der Test-E-Mail

Für die Validierung muss eine Datei mit korrekter MIME-Struktur und dem EICAR-Teststring erstellt werden.

Datei: /tmp/virus.eml

From: Test <test@it213.int>
To: Root <root@it213.int>
Subject: EICAR Test
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="Grenzlinie"

--Grenzlinie
Content-Type: text/plain; charset="utf-8"

Antivirus Testmail.

--Grenzlinie
Content-Type: application/com-file; name="eicar.com"
Content-Disposition: attachment; filename="eicar.com"
Content-Transfer-Encoding: 7bit

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

--Grenzlinie--

Test der Konfiguration

Der Scan wird mit rspamc simuliert:

 rspamc --ip 1.2.3.4 /tmp/virus.eml

Ein erfolgreicher Scan wird durch das Symbol CLAM_VIRUS im Output bestätigt.