Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
tutoriaux:install-email-server:install-email-server-part-8 [2023/01/06 16:41] – supprimée - modification externe (Unknown date) 127.0.0.1 | tutoriaux:install-email-server:install-email-server-part-8 [2024/07/06 01:17] (Version actuelle) – frater | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
+ | ====== Part 8 - Setting Up Amavis and ClamAV ====== | ||
+ | In this article, I will show you how to use **Amavis** and **ClamAV** to scan viruses in email messages. | ||
+ | |||
+ | **Amavis** (A Mail Virus Scanner) is a high-performance interface between a message transfer agent (MTA) such as **Postfix** and content filters. A content filter is a program that scans the headers and body of an email message, and usually takes some action based on what it finds. The most common examples are **ClamAV virus scanner** and **SpamAssassin**. | ||
+ | |||
+ | Amavis speaks standard SMTP protocol and can also use the Sendmail milter interface. It’s commonly used for | ||
+ | |||
+ | * virus-scanning by integrating with ClamAV (Clam AntiVirus) | ||
+ | * spam-checking by integrating with SpamAssassin | ||
+ | * DKIM signing and verification. (Actually, I prefer to use OpenDKIM for DKIM signing and verification.) | ||
+ | |||
+ | ===== Prerequisites ===== | ||
+ | |||
+ | You should have completed at least [[tutoriaux: | ||
+ | |||
+ | Note that if you used iRedMail or Modoboa to set up your mail server, then Amavis and ClamAV are already installed and configured, so you don’t need to follow this tutorial. | ||
+ | |||
+ | <WRAP round alert> | ||
+ | Warning: Amavis and ClamAV require a fair amount of RAM. Make sure you have at least 1.5 GB free RAM on your server before installing Amavis and ClamAV. The whole mail server stack (Postfix, Dovecot, Amavis, ClamAV, SpamAssassin, | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Install Amavis on Debian ===== | ||
+ | |||
+ | Amvis is available from the default Debian repository, so run the following command to install it. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Once installed, it automatically starts. You can check its status with: | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Output: | ||
+ | |||
+ | <code bash> | ||
+ | ● amavis.service - Interface between MTA and virus scanner/ | ||
+ | | ||
+ | | ||
+ | Docs: http:// | ||
+ | Main PID: 3027845 (/ | ||
+ | Tasks: 6 (limit: 19172) | ||
+ | | ||
+ | CPU: 9.167s | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | |||
+ | If it’s not running, you can start it with: | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Enable auto-start at boot time. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | By default, it listen on 127.0.0.1: | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Sample output: | ||
+ | |||
+ | <code bash> | ||
+ | tcp 0 0 127.0.0.1: | ||
+ | </ | ||
+ | |||
+ | And it runs as the amavis user. To check the version number, run | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Sample output: | ||
+ | |||
+ | < | ||
+ | |||
+ | To check the logs of Amavis, you can run | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Viruses are commonly spread as attachments to email messages. Install the following packages for Amavis to extract and scan archive files in email messages such as '' | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Note that if your server doesn’t use a fully-qualified domain name (FQDN) as the hostname, Amavis might fail to start. And the OS hostname might change, so it’s recommended to set a valid hostname directly in the Amavis configuration file. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Find the following line. | ||
+ | |||
+ | < | ||
+ | # | ||
+ | </ | ||
+ | |||
+ | Remove the comment character (#) and change mail.example.com to your real hostname. | ||
+ | |||
+ | < | ||
+ | |||
+ | Save and close the file. Restart Amavis for the changes to take effect. | ||
+ | |||
+ | <code bash> | ||
+ | ===== Integrate Postfix SMTP Server With Amavis ===== | ||
+ | |||
+ | Amavisd-new works as an SMTP proxy. Email is fed to it through SMTP, processed, and fed back to the MTA through a new SMTP connection. | ||
+ | |||
+ | Edit the Postfix main configuration file. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Add the following line at the end of the file. This tells Postfix to turn on content filtering by sending every incoming email message to Amavis, which listens on // | ||
+ | |||
+ | <code ini> | ||
+ | |||
+ | Also, add the following line. | ||
+ | |||
+ | <code ini> | ||
+ | |||
+ | This will delay Postfix connection to content filter until the entire email message has been received, which can prevent content filters from wasting time and resources for slow SMTP clients. | ||
+ | |||
+ | Save and close the file. Then edit the '' | ||
+ | |||
+ | < | ||
+ | |||
+ | Add the following lines at the end of the file. This instructs Postfix to use a special SMTP client component called '' | ||
+ | |||
+ | <code ini> | ||
+ | smtp-amavis | ||
+ | -o syslog_name=postfix/ | ||
+ | -o smtp_data_done_timeout=1200 | ||
+ | -o smtp_send_xforward_command=yes | ||
+ | -o disable_dns_lookups=yes | ||
+ | -o max_use=20 | ||
+ | -o smtp_tls_security_level=none | ||
+ | </ | ||
+ | |||
+ | Then add the following lines at the end of the file. This tells Postfix to run an additional smtpd daemon listening on '' | ||
+ | |||
+ | <code ini> | ||
+ | 127.0.0.1: | ||
+ | -o syslog_name=postfix/ | ||
+ | -o content_filter= | ||
+ | -o mynetworks_style=host | ||
+ | -o mynetworks=127.0.0.0/ | ||
+ | -o local_recipient_maps= | ||
+ | -o relay_recipient_maps= | ||
+ | -o strict_rfc821_envelopes=yes | ||
+ | -o smtp_tls_security_level=none | ||
+ | -o smtpd_tls_security_level=none | ||
+ | -o smtpd_restriction_classes= | ||
+ | -o smtpd_delay_reject=no | ||
+ | -o smtpd_client_restrictions=permit_mynetworks, | ||
+ | -o smtpd_helo_restrictions= | ||
+ | -o smtpd_sender_restrictions= | ||
+ | -o smtpd_recipient_restrictions=permit_mynetworks, | ||
+ | -o smtpd_end_of_data_restrictions= | ||
+ | -o smtpd_error_sleep_time=0 | ||
+ | -o smtpd_soft_error_limit=1001 | ||
+ | -o smtpd_hard_error_limit=1000 | ||
+ | -o smtpd_client_connection_count_limit=0 | ||
+ | -o smtpd_client_connection_rate_limit=0 | ||
+ | -o receive_override_options=no_header_body_checks, | ||
+ | </ | ||
+ | |||
+ | Save and close the file. Restart Postfix for the changes to take effect. | ||
+ | |||
+ | <code bash> | ||
+ | ===== Integrate Amavis with ClamAV ===== | ||
+ | |||
+ | Now that Postfix can pass incoming emails to Amavis, we need to install the ClamAV virus scanner and integrate it with Amavis, so incoming emails can be scanned by ClamAV. | ||
+ | |||
+ | Install ClamAV on Debian. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | There will be two systemd services installed by ClamAV: | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | First, check the status of '' | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | {{ tutoriaux: | ||
+ | |||
+ | As you can see, it’s active (running) and uses 4.5M RAM on my mail server. Then check the journal/ | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | {{ tutoriaux: | ||
+ | |||
+ | <WRAP round info> | ||
+ | Hint: If the above command doesn’t quit immediately, | ||
+ | </ | ||
+ | |||
+ | We can see that '' | ||
+ | |||
+ | * daily.cvd | ||
+ | * main.cvd | ||
+ | * bytecode.cvd | ||
+ | |||
+ | |||
+ | If your instance complain that < | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | As you can see, it failed to start because a condition wasn’t met. In the / | ||
+ | |||
+ | <code ini> | ||
+ | ConditionPathExistsGlob=/ | ||
+ | ConditionPathExistsGlob=/ | ||
+ | </ | ||
+ | |||
+ | The '' | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Now it should be running. By the way, it uses 1.2GB of RAM on my mail server. If your mail server doesn’t have enough RAM left, the service will fail. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | {{ tutoriaux: | ||
+ | |||
+ | The '' | ||
+ | |||
+ | Now we need to turn on virus-checking in Amavis. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Uncomment the following lines to enable virus-checking. | ||
+ | |||
+ | <code python> | ||
+ | # | ||
+ | # \%bypass_virus_checks, | ||
+ | </ | ||
+ | |||
+ | Save and close the file. There are lots of antivirus scanners in the ''/ | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Then restart Amavis and ClamAV daemon for the changes to take effect. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Check the logs. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | You can see that Amavis is now using ClamAV to scan viruses. | ||
+ | |||
+ | {{ tutoriaux: | ||
+ | |||
+ | Now if you send an email from other mail servers like Gmail to your own mail server and check the email headers, you can find a line like below, which indicates this email has been scanned by Amavis. | ||
+ | |||
+ | < | ||
+ | |||
+ | You should also check the mail log (''/ | ||
+ | |||
+ | ===== Use A Dedicated Port for Email Submissions ===== | ||
+ | |||
+ | ClamAV can scan both incoming and outgoing emails now. Amavis listens on port 10024 for both incoming and outgoing email messages. However, it’s a good practice to use a different port such as 10026 for email submissions from authenticated users. Edit the Amavis configuration file. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Custom settings should be added between the use '' | ||
+ | |||
+ | <code python> | ||
+ | |||
+ | Then add the following line, which sets the “ORIGINATING” policy for port 10026. | ||
+ | |||
+ | <code python> | ||
+ | |||
+ | Next, add the following lines, which define the “ORIGINATING” policy. | ||
+ | |||
+ | <code python> | ||
+ | $policy_bank{' | ||
+ | originating => 1, # declare that mail was submitted by our smtp client | ||
+ | allow_disclaimers => 1, # enables disclaimer insertion if available | ||
+ | |||
+ | # notify administrator of locally originating malware | ||
+ | virus_admin_maps => [" | ||
+ | spam_admin_maps | ||
+ | warnbadhsender | ||
+ | |||
+ | # force MTA conversion to 7-bit (e.g. before DKIM signing) | ||
+ | smtpd_discard_ehlo_keywords => [' | ||
+ | bypass_banned_checks_maps => [1], # allow sending any file names and types | ||
+ | terminate_dsn_on_notify_success => 0, # don't remove NOTIFY=SUCCESS option | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | Save and close the file. Restart Amavis. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Check its status to see if the restart is successful. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Next, edit the Postfix master configuration file. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Add the following line to the '' | ||
+ | |||
+ | < | ||
+ | |||
+ | Like this: | ||
+ | |||
+ | {{ tutoriaux: | ||
+ | |||
+ | If you have enabled the smtps service for Microsoft Outlook users, then you also need to add this line to the smtps service. | ||
+ | |||
+ | Save and close the file. Restart Postfix for the changes to take effect. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Check its status to see if the restart is successful. | ||
+ | |||
+ | <code bash> | ||
+ | ===== Spam Filtering in Amavis ===== | ||
+ | |||
+ | <WRAP round important> | ||
+ | If you have followed my SpamAssassin tutorial, you don’t need to enable spam-checking in Amavis. If you enable it, each email will be checked twice by SpamAssassin. | ||
+ | </ | ||
+ | |||
+ | To enable spam-checking in Amavis, install SpamAssassin and related packages. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Edit an Amavis configuration file. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Uncomment the following lines to enable spam-checking. | ||
+ | |||
+ | <code python> | ||
+ | # | ||
+ | # | ||
+ | </ | ||
+ | |||
+ | Save and close the file. Then restart Amavis. | ||
+ | |||
+ | <code bash> | ||
+ | ===== DKIM in Amavis ===== | ||
+ | |||
+ | Two common pieces of software that can do DKIM signing and verification on Linux are [[tutoriaux: | ||
+ | |||
+ | By default, Amavis can verify the DKIM signature of incoming email messages. If you have OpenDKIM running on your mail server, then you can disable DKIM verification in Amavis. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Find the following line and change 1 to 0, so Amavis won’t verify DKIM signatures. | ||
+ | |||
+ | <code python> | ||
+ | |||
+ | Save and close the file. Then restart Amavis. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | When receiving incoming emails, Postfix will call OpenDKIM via the sendmail milter interface to verify DKIM signatures, then pass the email to Amavis for virus-checking. When sending outgoing emails, Postfix will call OpenDKIM to sign the emails, then pass them to Amavis for virus-checking. | ||
+ | ===== Improving Amavis Performance ===== | ||
+ | |||
+ | By default, Amavis runs 2 processes. If you see the following lines in the mail log (''/ | ||
+ | |||
+ | < | ||
+ | postfix/ | ||
+ | postfix/ | ||
+ | postfix/ | ||
+ | postfix/ | ||
+ | postfix/ | ||
+ | postfix/ | ||
+ | postfix/ | ||
+ | postfix/ | ||
+ | </ | ||
+ | |||
+ | To improve performance, | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Add the following line in the file between the use '' | ||
+ | |||
+ | <code python> | ||
+ | $max_servers = 4; | ||
+ | </ | ||
+ | |||
+ | Save and close the file. Then edit the Postifx master configuration file. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Find the '' | ||
+ | |||
+ | <code ini> | ||
+ | smtp-amavis | ||
+ | -o syslog_name=postfix/ | ||
+ | -o smtp_data_done_timeout=1200 | ||
+ | -o smtp_send_xforward_command=yes | ||
+ | -o disable_dns_lookups=yes | ||
+ | -o max_use=20 | ||
+ | -o smtp_tls_security_level=none | ||
+ | </ | ||
+ | |||
+ | Save and close the file. Then restart Amavis and Postfix for the changes to take effect. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Now run the following command. You should see that there are 4 Amavis processes now. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | {{ tutoriaux: | ||
+ | |||
+ | Press Ctrl+C to stop amavisd-nanny. | ||
+ | |||
+ | ===== PostGrey : Postfix Greylisting Policy Server | ||
+ | |||
+ | When a request for delivery of a mail is received by Postfix via SMTP, the triplet CLIENT_IP / SENDER / RECIPIENT is built. If it is the first time that this triplet is seen, or if the triplet was first seen, less than 5 minutes ago, then the mail gets rejected with a temporary error. Hopefully spammers or viruses will not try again later, as it is however required per RFC. | ||
+ | |||
+ | ==== Installation ==== | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | the package is self installing, you have noting to change to the postfix' | ||
+ | ==== Configuration ==== | ||
+ | |||
+ | '' | ||
+ | |||
+ | You may want to " | ||
+ | |||
+ | Edit the two files in ''/ | ||
+ | |||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | ==== examples ==== | ||
+ | |||
+ | <code sshconfig whitelist_recipients> | ||
+ | # postgrey whitelist for mail recipients | ||
+ | # -------------------------------------- | ||
+ | # put this file in / | ||
+ | |||
+ | # do not delay mail to : | ||
+ | postmaster@ | ||
+ | abuse@ | ||
+ | </ | ||
+ | |||
+ | <code sshconfig whitelist_clients> | ||
+ | # postgrey whitelist for mail client hostnames | ||
+ | # -------------------------------------------- | ||
+ | # put this file in / | ||
+ | # | ||
+ | # postgrey version: 1.36, build date: 2015-09-01 | ||
+ | |||
+ | # Debian-specific additions | ||
+ | # I *know* they run real mail queues, so greylisting only creates bigger load for them. | ||
+ | debconf.org | ||
+ | debian.org | ||
+ | spi-inc.org | ||
+ | |||
+ | # you may also add IPs | ||
+ | 1.1.1.1 | ||
+ | |||
+ | # or regex expressions: | ||
+ | / | ||
+ | </ | ||
+ | ===== Skip Virus-Checking for Your Newsletters ===== | ||
+ | |||
+ | If you use your mail server to send newsletters, | ||
+ | |||
+ | Edit the Postfix master configuration file. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Add the following lines at the beginning of this file. This will enable '' | ||
+ | |||
+ | <code ini> | ||
+ | 127.0.0.1: | ||
+ | -o syslog_name=postfix/ | ||
+ | -o postscreen_greet_action=ignore | ||
+ | -o content_filter= | ||
+ | </ | ||
+ | |||
+ | Then add the following lines at the end of this file. Replace 12.34.56.78 with the mail server’s public IP address. This will create another Postfix submission daemon listening on port 10587. This is for client connections from another server. | ||
+ | |||
+ | <code ini> | ||
+ | 12.34.56.78: | ||
+ | -o syslog_name=postfix/ | ||
+ | -o smtpd_tls_security_level=encrypt | ||
+ | -o smtpd_tls_wrappermode=no | ||
+ | -o smtpd_sasl_auth_enable=yes | ||
+ | -o smtpd_relay_restrictions=permit_sasl_authenticated, | ||
+ | -o smtpd_recipient_restrictions=permit_mynetworks, | ||
+ | -o smtpd_sasl_type=dovecot | ||
+ | -o smtpd_sasl_path=private/ | ||
+ | -o content_filter= | ||
+ | </ | ||
+ | |||
+ | Save and close the file. Then Restart postfix. | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | If your newsletter application runs on the mail server, then specify '' | ||
+ | ===== Troubleshooting ===== | ||
+ | |||
+ | If your Postfix SMTP server can’t send outgoing emails, and you find the following error message in the mail log (''/ | ||
+ | |||
+ | < | ||
+ | relay=none, delay=239, delays=239/ | ||
+ | </ | ||
+ | |||
+ | it could be that amavis is not running, so you need to check its status: | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | You can restart it with: | ||
+ | |||
+ | <code bash> | ||
+ | |||
+ | Another cause for this error is that you are enforcing TLS connection for Postfix when sending outgoing emails with the following setting in the ''/ | ||
+ | |||
+ | < | ||
+ | |||
+ | Since Postfix can’t establish TLS connection to Amavis, so the email is deferred. You should use the following setting. | ||
+ | |||
+ | < | ||
+ | |||
+ | Then restart Postfix. | ||
+ | |||
+ | {{page> |