SMTP通信を暗号化するためにPostfixにTLS/SSLを設定
SMTP通信を自己署名証明書(オレオレ証明書)を使って暗号化処理をメールサーバ(Postfix/Dovecot)に設定した時のメモ
認証局が発行した証明書を使うことが望ましいのですが、自己署名証明書(オレオレ証明書)でも暗号化は可能なので、今回は自己署名証明書を使用して設定します。
秘密鍵を生成
# mkdir /etc/tls/ # cd /etc/tls/ # openssl genrsa -out postfix.key 2048
CSRを生成
# openssl req -new -key postfix.key -out postfix.csr Country Name (2 letter code) [XX]:JP[国識別子][Enter] State or Province Name (full name) []:Tokyo[都道府県名][Enter] Locality Name (eg, city) [Default City]:Shibuya-ku[市区町村名][Enter] Organization Name (eg, company) [Default Company Ltd]:Example Inc.[組織名][Enter] Organizational Unit Name (eg, section) []:[Enter] Common Name (eg, your name or your server's hostname) []:mail.example.com[ドメイン名][Enter] Email Address []:[Enter] Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:[Enter] An optional company name []:[Enter]
証明書を生成
※期間を10年で作成 # openssl x509 -in postfix.csr -out postfix.crt -req -signkey postfix.key -days 3650
パーミッションを変更
# chmod 400 postfix.*
Postfixの設定
/etc/postfix/main.cf
変更前にバックアップ
# cp -p /etc/postfix/main.cf /etc/postfix/main.cf_`date "+%Y%m%d%H%M%S"`
※最終行に追記 # TLS smtp_tls_security_level = may smtpd_tls_cert_file = /etc/tls/postfix.crt smtpd_tls_key_file = /etc/tls/postfix.key smtpd_tls_loglevel = 1
/etc/postfix/master.cf
変更前にバックアップ
# cp -p /etc/postfix/master.cf /etc/postfix/master.cf_`date "+%Y%m%d%H%M%S"`
#submission inet n - n - - smtpd # -o syslog_name=postfix/submission # -o smtpd_tls_security_level=encrypt # -o smtpd_sasl_auth_enable=yes # -o smtpd_tls_auth_only=yes # -o smtpd_reject_unlisted_recipient=no # -o smtpd_client_restrictions=$mua_client_restrictions # -o smtpd_helo_restrictions=$mua_helo_restrictions # -o smtpd_sender_restrictions=$mua_sender_restrictions # -o smtpd_recipient_restrictions= # -o smtpd_relay_restrictions=permit_sasl_authenticated,reject # -o milter_macro_daemon_name=ORIGINATING ↓ submission inet n - n - - smtpd -o smtpd_tls_security_level=encrypt -o smtpd_sasl_auth_enable=yes -o smtpd_client_restrictions=permit_sasl_authenticated,reject
#smtps inet n - n - - smtpd # -o syslog_name=postfix/smtps # -o smtpd_tls_wrappermode=yes # -o smtpd_sasl_auth_enable=yes # -o smtpd_reject_unlisted_recipient=no # -o smtpd_client_restrictions=$mua_client_restrictions # -o smtpd_helo_restrictions=$mua_helo_restrictions # -o smtpd_sender_restrictions=$mua_sender_restrictions # -o smtpd_recipient_restrictions= # -o smtpd_relay_restrictions=permit_sasl_authenticated,reject # -o milter_macro_daemon_name=ORIGINATING ↓ smtps inet n - n - - smtpd -o smtpd_tls_wrappermode=yes -o smtpd_sasl_auth_enable=yes -o smtpd_client_restrictions=permit_sasl_authenticated,reject
Dovecotの設定
/etc/dovecot/conf.d/10-ssl.conf
変更前にバックアップ
# cp -p /etc/dovecot/conf.d/10-ssl.conf /etc/dovecot/conf.d/10-ssl.conf_`date "+%Y%m%d%H%M%S"`
ssl = required ↓ #ssl = required ssl_cert = </etc/pki/dovecot/certs/dovecot.pem ssl_key = </etc/pki/dovecot/private/dovecot.pem ↓ #ssl_cert = </etc/pki/dovecot/certs/dovecot.pem #ssl_key = </etc/pki/dovecot/private/dovecot.pem
※「10-ssl.conf」ではコメントアウトして、「local.conf」で設定をまとめてます。
local.confの設定
/etc/dovecot/local.conf
変更前にバックアップ
# cp -p /etc/dovecot/local.conf /etc/dovecot/local.conf_`date "+%Y%m%d%H%M%S"`
service imap-login { inet_listener imap { port = 143 } inet_listener imaps { #port = 993 #ssl = yes } } ↓ service imap-login { inet_listener imap { port = 143 } inet_listener imaps { port = 993 ssl = yes } } service pop3-login { inet_listener pop3 { port = 110 } inet_listener pop3s { #port = 995 #ssl = yes } } ↓ service pop3-login { inet_listener pop3 { port = 110 } inet_listener pop3s { port = 995 ssl = yes } } ssl = required ↓ ssl = yes ssl_cert = </etc/pki/dovecot/certs/dovecot.pem ssl_key = </etc/pki/dovecot/private/dovecot.pem ↓ ssl_cert = </etc/tls/postfix.crt ssl_key = </etc/tls/postfix.key 追記 ssl_cipher_list = HIGH:-SSLv2:-SSLv3:!aNULL:!eNULL:!DH:!3DES
修正後のlocal.conf
### 10-mail.conf ### mail_location = maildir:/var/spool/vmail/%d/%n first_valid_uid = 10000 first_valid_gid = 10000 mail_plugins = quota ### 10-master.conf ### service imap-login { inet_listener imap { port = 143 } inet_listener imaps { port = 993 ssl = yes } } service pop3-login { inet_listener pop3 { port = 110 } inet_listener pop3s { port = 995 ssl = yes } } service auth { # Postfix smtp-auth unix_listener /var/spool/postfix/private/auth { mode = 0600 user = postfix group = postfix } } ### 10-ssl.conf ### ssl = yes ssl_cert = </etc/tls/postfix.crt ssl_key = </etc/tls/postfix.key ssl_cipher_list = HIGH:-SSLv2:-SSLv3:!aNULL:!eNULL:!DH:!3DES #outlook Express #ssl_cipher_list = HIGH:-SSLv2:-SSLv3:!aNULL:!eNULL:!DH:-3DES:DES-CBC3-SHA@STRENGTH ### 20-imap.conf ### protocol imap { imap_client_workarounds = delay-newmail tb-extra-mailbox-sep mail_plugins = $mail_plugins imap_quota } ### 20-pop3.conf ### protocol pop3 { pop3_client_workarounds = outlook-no-nuls oe-ns-eoh mail_plugins = $mail_plugins } ### 90-quota.conf ### plugin { quota = maildir:User quota } ### auth-sql.conf.ext ### passdb { driver = sql args = /etc/dovecot/dovecot-sql.conf } userdb { driver = sql args = /etc/dovecot/dovecot-sql.conf.ext }
PostfixとDovecotを再起動
# systemctl restart postfix.service # systemctl restart dovecot.service
iptablesの設定
(※本サイトの「CentOS7を安全に運用する為の初期設定(iptablesの設定)」を参照ください)
smtps(465)、pop3s(995)、imaps(993)ポートを許可
/etc/sysconfig/iptables
変更前にバックアップ
# cp -p /etc/sysconfig/iptables /etc/sysconfig/iptables_`date "+%Y%m%d%H%M%S"`
# smtps -A INPUT -p tcp -m state --state NEW -m tcp --dport 465 -j ACCEPT # pop3s -A INPUT -p tcp -m state --state NEW -m tcp --dport 995 -j ACCEPT # imaps -A INPUT -p tcp -m state --state NEW -m tcp --dport 993 -j ACCEPT
修正後のiptables
*filter :INPUT DROP [0:0] :FORWARD DROP [0:0] :OUTPUT ACCEPT [0:0] # 確立済みの通信は許可 -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # 自ホストからの通信は全て許可 -A INPUT -i lo -j ACCEPT # pingは許可(1秒間に1回の制限) -A INPUT -p icmp -m limit --limit 1/s --limit-burst 1 -j ACCEPT # プライベートアドレスを破棄 -A INPUT -s 10.0.0.0/8 -j DROP -A INPUT -s 172.16.0.0/12 -j DROP -A INPUT -s 192.168.0.0/16 -j DROP # ローカルループバックアドレスを破棄 -A INPUT -s 127.0.0.0/8 -j DROP # リンクローカルアドレスを破棄 -A INPUT -s 169.254.0.0/16 -j DROP # テストネットワークアドレスを破棄 -A INPUT -s 192.0.2.0/24 -j DROP -A INPUT -s 198.51.100.0/24 -j DROP -A INPUT -s 203.0.113.0/24 -j DROP # クラスDを破棄 -A INPUT -s 224.0.0.0/4 -j DROP # クラスEを破棄 -A INPUT -s 240.0.0.0/4 -j DROP # Current networkを破棄 -A INPUT -d 0.0.0.0/8 -j DROP # ブロードキャストアドレスを破棄 -A INPUT -d 255.255.255.255 -j DROP # データを持たないパケットを破棄 -A INPUT -p tcp --tcp-flags ALL NONE -j DROP # SYNフラッド攻撃と思われる接続を破棄 -A INPUT -p tcp -m state --state NEW ! --syn -j DROP # ステルススキャン攻撃と思われる接続を破棄 -A INPUT -p tcp --tcp-flags ALL ALL -j DROP # フラグメントパケット攻撃と思われる接続を破棄 -A INPUT -f -j DROP # ssh :SSH - [0:0] :SSH_ATTACK - [0:0] # -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT # 同じホストから60秒以内に5回のSSH接続があったら、そのホストからのSSH接続を10分間受け付けない -A INPUT -p tcp -m state --state NEW -m tcp --dport 10022 -j SSH -A SSH -m recent --name sshbadcon --rcheck --seconds 600 -j REJECT -A SSH -m recent --name sshcon --rcheck --seconds 60 --hitcount 5 -j SSH_ATTACK -A SSH -m recent --name sshcon --set -A SSH -j ACCEPT -A SSH_ATTACK -m recent --name sshbadcon --set -A SSH_ATTACK -j LOG --log-prefix "iptables:SSH_ATTACK: " -A SSH_ATTACK -j REJECT # http -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT # smtp -A INPUT -p tcp -m state --state NEW -m tcp --dport 25 -j ACCEPT # submission -A INPUT -p tcp -m state --state NEW -m tcp --dport 587 -j ACCEPT # smtps -A INPUT -p tcp -m state --state NEW -m tcp --dport 465 -j ACCEPT # pop3 -A INPUT -p tcp -m state --state NEW -m tcp --dport 110 -j ACCEPT # pop3s -A INPUT -p tcp -m state --state NEW -m tcp --dport 995 -j ACCEPT # imap -A INPUT -p tcp -m state --state NEW -m tcp --dport 143 -j ACCEPT # imaps -A INPUT -p tcp -m state --state NEW -m tcp --dport 993 -j ACCEPT -A INPUT -j LOG --log-prefix "iptables:" --log-level=warning -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT
iptablesの再起動
# systemctl restart iptables.service
関連
メールサーバ(Postfix/Dovecot)を構築
(※本サイトの「CentOS7にメールサーバ(Postfix/Dovecot)を構築」を参照ください)
迷惑メールに判定されないようDKIMとSPFを設定
(※本サイトの「Postfixで迷惑メールに判定されないようDKIMとSPFを設定」を参照ください)
CentOS7インストール直後の初期設定
(※本サイトの「CentOS7インストール直後の初期設定」を参照ください)