LoginSignup
0
2

More than 1 year has passed since last update.

【ubuntu 20.04】Postfix, amavis, clamavでウイルスメール対策

Last updated at Posted at 2022-03-23

はじめに

 これ系の記事は色々あるけど、自分環境用のメモです。結構ややこしい。内部からの送信メールをポート10026で、外部からの受信メールをポート10024amavisへ送り、amavisからはどちらもポート10025で受け取る設定。

自分環境?

 極小零細企業オンプレサーバー。クラウドを外注?そんなのに予算が出(略

Clamavのインストールとか設定とか

 まぁ一発で終わり。

apt install clamav clamav-daemon

 添付ファイルに対応するために一気にインストール。

apt install arj bzip2 cabextract cpio rpm2cpio file gzip lhasa nomarch pax rar unrar p7zip-full unzip zip lrzip lzip liblz4-tool lzop unrar-free

 実行ユーザーをrootに。デフォルトだったかなぁ覚えてない。

vi /etc/clamav/clamd.conf
/etc/clamav/clamd.conf
10行目あたり
User root

 clamavは誤検出がしばしばある。過去ファイルサーバーのエクセルvbaファイルが全部消えた経験あり(泣
 バックアップあったから良かったものの、無用なトラブルは避けたい。以下を最終行に追記。

/etc/clamav/clamd.conf
ExcludePath ^/home/samba

Clamavをパワーアップ:clamav-unofficial-sigs

 賛否両論あると思うが、サードパーティシグネチャを利用する。標準に比べ、未検出率、誤検出率を改善したというレポートがオフィシャルサイトにあるので、それぞれで検討。

apt install clamav-unofficial-sigs

clamav-unofficial-sigsの設定を編集

vi /usr/share/clamav-unofficial-sigs/conf.d/00-clamav-unofficial-sigs.conf

 シグネチャアップデートでエラーになるデータベースを無効化する。

/usr/share/clamav-unofficial-sigs/conf.d/00-clamav-unofficial-sigs.conf
# 154行目からすべてコメントアウト

# ========================
# SecuriteInfo Database(s)
# ========================
# Add or remove database file names between quote marks as needed.  To
# disable any SecuriteInfo database downloads, remove the appropriate
# lines below.  To disable all SecuriteInfo database file downloads,
# comment all of the following lines.
#si_dbs="
#   honeynet.hdb
#   securiteinfo.hdb
#   securiteinfobat.hdb
#   securiteinfodos.hdb
#   securiteinfoelf.hdb
#   securiteinfohtml.hdb
#   securiteinfooffice.hdb
#   securiteinfopdf.hdb
#   securiteinfosh.hdb
#"

~

# 177行目から

# =========================
# MalwarePatrol Database(s)
# =========================
# Add or remove database file names between quote marks as needed.  To
# disable any of the MalwarePatrol database file downloads, remove the
# appropriate database file name lines below.  To disable MalwarePatrol
# database downloads, comment all of the following lines.
#mbl_dbs="
#   mbl.ndb
#"

~

# 227行目から。SecuriteInfoとMalwarePatrolのworkdirをコメントアウト

# Set working directory paths (edit to meet your own needs). If these
# directories do not exist, the script will attempt to create them.
# Top level working directory path:
work_dir="/usr/unofficial-dbs"   #Top level working directory
# Sub-directory names:
ss_dir="$work_dir/ss-dbs"        # Sanesecurity sub-directory
#si_dir="$work_dir/si-dbs"        # SecuriteInfo sub-directory
#mbl_dir="$work_dir/mbl-dbs"      # MalwarePatrol sub-directory
config_dir="$work_dir/configs"   # Script configs sub-directory
gpg_dir="$work_dir/gpg-key"      # Sanesecurity GPG Key sub-directory
add_dir="$work_dir/add-dbs"      # User defined databases sub-directory

amavisとclamavの連携設定

 clamavに権限を与えてファイルスキャンできるようにする。

adduser clamav amavis
adduser amavis clamav

clamavは直接postfixと連携して動作せず、amavisd-newという連携ツール(以下、amavis)を介して連携している。従って、設定はamavispostfixが連携するように設定する。amavisclamavはインストールした時点で連携するようになっているが、動作はデフォルトで無効化されているので有効化する。

vi /etc/amavis/conf.d/15-content_filter_mode
/etc/amavis/conf.d/15-content_filter_mode
以下4行のコメントを外す。

@bypass_virus_checks_maps = (
   \%bypass_virus_checks, \@bypass_virus_checks_acl, \$bypass_virus_checks_re);

@bypass_spam_checks_maps = (
   \%bypass_spam_checks, \@bypass_spam_checks_acl, \$bypass_spam_checks_re);

 受信メールの添付ファイルが未チェックだと、タイトルに***UNCHECKED***と挿入される。何となく嫌なので無効化する。

vi /etc/amavis/conf.d/50-user
/etc/amavis/conf.d/50-user
use strict; と、1; の間に以下を追記

# Avoid rewrite module adding ***UNCHECKED*** on subject.
$undecipherable_subject_tag = '';

Postfixとamavisの連携設定

vi /etc/postfix/main.cf

 以下を最終行に追記。「postfixからamavisへポート10024でデータを渡しますよ」という意味。

/etc/postfix/main.cf
## Amavis-postfix and smtp-amavis integration setting.
content_filter = smtp-amavis:[127.0.0.1]:10024
smtpd_proxy_options = speed_adjust

 続いてポートごとの設定。

vi /etc/postfix/master.cf

 submissionsmtpsの最終行に以下を追記。「submissionとsmtpsはポート10026でデータを渡しますよ」という意味。

/etc/postfix/master.cf
    -o content_filter=smtp-amavis:[127.0.0.1]:10026

 一番下の行です↓
clamavamavis001.PNG

 smtpsの項目の一番下↓
clamavamavis002.PNG

 最終行に以下を丸ごと追記。「amavisからpostfixへポート10025でデータを返しますよ」という意味。

/etc/postfix/master.cf
# Amavis - Postfix integration.
smtp-amavis     unix    -       -       n       -       2       smtp
        -o smtp_data_done_timeout=1200
        -o smtp_send_xforward_command=yes
        -o disable_dns_lookups=yes
        -o max_use=30

127.0.0.1:10025 inet    n       -       -       -       -       smtpd
        -o content_filter=
        -o mynetworks_style=host
        -o mynetworks=127.0.0.0/8
        -o local_recipient_maps=
        -o relay_recipient_maps=
        -o smtpd_restriction_classes=
        -o smtpd_delay_reject=no
        -o smtpd_client_restrictions=permit_mynetworks,reject
        -o smtpd_helo_restrictions=
        -o smtpd_sender_restrictions=
        -o smtpd_recipient_restrictions=permit_mynetworks,reject
        -o smtpd_data_restrictions=reject_unauth_pipelining
        -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,no_unknown_recipient_checks,no_address_mappings

 amavisの設定にORIGINATINGポリシーを追加。use strict;1;の間に入れる。

vi /etc/amavis/conf.d/50-user

use strict;1;の間に入れる。

/etc/amavis/conf.d/50-user
$inet_socket_port = [10024,10026];
$interface_policy{'10026'} = 'ORIGINATING';

$policy_bank{'ORIGINATING'} = {  # mail supposedly originating from our users
  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 => ["virusalert\@$mydomain"],
  #spam_admin_maps  => ["virusalert\@$mydomain"],
  warnbadhsender   => 1,

  # force MTA conversion to 7-bit (e.g. before DKIM signing)
  smtpd_discard_ehlo_keywords => ['8BITMIME'],
  bypass_banned_checks_maps => [1],  # allow sending any file names and types
  terminate_dsn_on_notify_success => 0,  # don't remove NOTIFY=SUCCESS option
};

 ここまできたら一度再起動して設定を反映。

systemctl restart amavis postfix clamav-daemon

 メールソフトの送信サーバーのポート設定を587、暗号化通信利用なら465にする。

 これで送信メールは10026、受信メールは10024で連携するようになる。

参考にしたサイト

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2