Postfix + メールサーバウイルス対策 (amavisd + ClamAV)

  • メールサーバにウイルス対策機能を付加したかった。
    • 早い、安い、巧い、ClamAV。
    • スキャナとしてamavisd。
  • SPAMは今回は対策しない。基本的にメール内容自体には手を付けず、あくまでも実害のあるもののみ対応する。
  • written by member/すながわひろゆき

前提条件

  • CentOS 6.2 x86_64 minimal
    • Tips/Linux/Postfix/Postfix

ClamAV

  • EPELレポジトリダウンロード
    rpm -ivh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
  • /etc/yum.repos.d/epel.repo
    [epel]
    enabled=0
    • 通常はyumでEPELレポジトリから引っ張らないようにする。
  • ClamAVインストール
    yum --enablerepo=epel install clamd
  • 本体設定
    • /etc/clamd.conf(変更点のみ)
      # ポートを開かない
      #TCPSocket 3310
      # clamdの起動ユーザをrootに
      #User clam
  • 起動&サービス登録
    /etc/init.d/clamd start
    chkconfig clamd on
  • アップデートの設定
    • /etc/freshclam.conf
      #Example
  • アップデートテスト
    freshclam
  • アップデート予約
    • /etc/cron.daily/freshclam があることを確認
  • ウイルス検索テスト
    clamscan --infected --recursive

amavisd

  • インストール
    yum --enablerepo=epel install amavisd-new
  • 設定
    • /etc/amavisd/amavisd.conf
      # スパムチェックを行わない(コメントアウトを外す)
      @bypass_spam_checks_maps  = (1);  # controls running of anti-spam code
      
      # メールサーバのデフォルトドメイン
      $mydomain = 'example.com';   # a convenient default for other settings
      
      # 自ホスト名
      $myhostname = 'mail.example.com';
      
      # 不正ヘッダ処理となったメールでもそのまま通過させる。
      # $final_bad_header_destiny = D_BOUNCE;
      $final_bad_header_destiny = D_PASS;
      
      # SOME OTHER VARIABLES WORTH CONSIDERING (see amavisd.conf-default for all)
      # $defang_bad_header, $defang_undecipherable, $defang_spam
      # パスワード付zip受信時にメール件名に「***UNCHECKED***」と付加しない(追加)
      $undecipherable_subject_tag = '';
      
      # exe-ms|dllをコメントアウトしてexeファイル受信を認める
      ### BLOCKED ANYWHERE
      # qr'^UNDECIPHERABLE$',  # is or contains any undecipherable components
      #  qr'^\.(exe-ms|dll)$',                   # banned file(1) types, rudimentary
      # qr'^\.(exe|lha|cab|dll)$',              # banned file(1) types
      
      # ソケットの変更(ClamAVで指定したソケット)
      ['ClamAV-clamd',
        \&ask_daemon, ["CONTSCAN {}\n", "/var/run/clamav/clamd.sock"],
        qr/\bOK$/m, qr/\bFOUND$/m,
        qr/^.*?: (?!Infected Archive)(.*) FOUND$/m ],
    • $final_bad_header_destinyで通過させている理由は、live.comのサインアップメールが不正ヘッダとして処理されてしまうための対策。
  • 起動&サービス登録
    /etc/init.d/amavisd start
    chkconfig amavisd on
  • Postfixに登録
    • /etc/postfix/master.cf(追記部分のみ)
      smtp-amavis unix -    -    n    -    2  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    -    -  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=127.0.0.0/8
          -o strict_rfc821_envelopes=yes
          -o smtpd_error_sleep_time=0
          -o smtpd_soft_error_limit=1001
          -o smtpd_hard_error_limit=1000
          -o receive_override_options=no_address_mappings,no_header_body_checks,no_unknown_recipient_checks
      • 最後のreceive_override_optionsでno_address_mappingsしないと、なぜかメールが2通転送先に届く現象が発生する。
      • あと、複数のメールフィルタをかます場合(spamassassinとか)はさらにオプションを追加しないとメールがループするらしい。
      • http://www.aconus.com/~oyaji/suse9.3/amavisd-new_antivir_suse9.3.htm
  • /etc/postfix/main.cf(追記部分のみ)
    content_filter=smtp-amavis:[127.0.0.1]:10024
  • Postfix再起動 /etc/init.d/postfix restart 
  • テスト
    • テストウイルス(Eicar)あたりをメールに添付してサーバに打ち込んでみる。

参考