#author("2018-08-23T09:55:07+09:00","","") #author("2018-08-23T09:56:51+09:00","","") [[CentOS7]] *Webサーバー間通信内容暗号化(Apache+mod_SSL+Certbot) [#bed78bac] ユーザー名やパスワード等の機密情報をWebブラウザから入力する場合、盗み見される恐れがあるため、Webサーバー間の通信内容を暗号化する。 ここでは、Webサーバーに''mod_ssl''を導入して、URLをhttp://~ではなく、https://~でアクセスすることによって、Webサーバー間の通信内容を暗号化するようにする。 また、Webサーバーとの通信内容を暗号化するには、サーバー証明書を発行する必要があるが、自作したサーバー証明書ではクライアントが通信を行うたび(Webブラウザ起動毎)にセキュリティの警告が表示されてしまう。対処としては、有料のサーバー証明書を発行するか、自作サーバー証明書を発行して各クライアントにインポートすればよいが、有料のサーバー証明書は高額かつ年単位に継続して費用がかかる、自作サーバー証明書のインポートは不特定多数のすべての利用者に対してサーバー証明書をインポートさせるのは現実的ではないことから、ここでは、''[[Certbot>https://certbot.eff.org/]]''の無料サーバー証明書を導入し、無料でセキュリティの警告が表示されずに暗号化通信が行えるようにする。 ''【Certbotの特徴】'' -サーバー証明書を無料で発行できる -サーバー証明書の取得がコマンドのみで行える(Web経由、メール経由等での煩わしい手続き等がない) -サーバー証明書の更新がコマンドのみで行える(維持=サーバー証明書の更新/差し替えを完全自動化) *Certbotクライアントインストール [#t4fa86a1] |BGCOLOR(black):COLOR(white):|c |[root@localhost ~]# cd /usr/local/ &color(lime){← ディレクトリ移動};| |[root@localhost local]# git clone https://github.com/certbot/certbot &color(lime){← Certbotクライアントダウンロード};| |[root@localhost local]# cd &color(lime){← ディレクトリ移動};| |[root@localhost ~]# /usr/local/certbot/certbot-auto -n &color(lime){← Certbotクライアントインストール};| *サーバー証明書取得 [#b18e94d0] |BGCOLOR(black):COLOR(white):|c |[root@localhost ~]# /usr/local/certbot/certbot-auto certonly --webroot -w &color(lime){ドキュメントルート}; -m &color(lime){メールアドレス}; -d &color(lime){Webサーバー名}; --agree-tos &color(lime){← サーバー証明書取得};| |&color(lime){--パラメータ指定例-- &br; ドキュメントルート⇒/var/www/html/centos/ &br; メールアドレス⇒任意のメールアドレス &br; Webサーバー名⇒hoge.com};| |IMPORTANT NOTES: &br; - Congratulations! Your certificate and chain have been saved at &br; /etc/letsencrypt/live/hoge.com/fullchain.pem. Your cert &br; will expire on 2016-07-24. To obtain a new version of the &br; certificate in the future, simply run Certbot again. &br; - If you lose your account credentials, you can recover through &br; e-mails sent to xxxxxxxx@xxxxxxxx.com. &br; - Your account credentials have been saved in your Certbot &br; configuration directory at /etc/certbot. You should make a &br; secure backup of this folder now. This configuration directory will &br; also contain certificates and private keys obtained by Let's &br; Encrypt so making regular backups of this folder is ideal. &br; - If you like Certbot, please consider supporting our work by: &br; &br; Donating to ISRG / Certbot: https://certbot.org/donate &br; Donating to EFF: https://eff.org/donate-le| *mod_sslインストール [#oe4d3925] |BGCOLOR(black):COLOR(white):|c |[root@localhost ~]# yum -y install mod_ssl &color(lime){← mod_sslインストール};| *Apache設定 [#vee3900c] **Apache設定(Certbot対応) [#b569956f] |BGCOLOR(black):COLOR(white):|c |[root@localhost ~]# vi /etc/httpd/conf.d/ssl.conf &color(lime){← SSL設定ファイル編集};| |# Server Certificate: &br; # Point SSLCertificateFile at a PEM encoded certificate. If &br; # the certificate is encrypted, then you will be prompted for a &br; # pass phrase. Note that a kill -HUP will prompt again. A new &br; # certificate can be generated using the genkey(1) command. &br; SSLCertificateFile /etc/letsencrypt/live/&color(lime){hoge.com};/cert.pem &color(lime){← 公開鍵指定}; &br; &br; # Server Private Key: &br; # If the key is not combined with the certificate, use this &br; # directive to point at the key file. Keep in mind that if &br; # you've both a RSA and a DSA private key you can configure &br; # both in parallel (to also allow the use of DSA ciphers, etc.) &br; SSLCertificateKeyFile /etc/letsencrypt/live/&color(lime){hoge.com};/privkey.pem &color(lime){← 秘密鍵指定}; &br; &br; # Server Certificate Chain: &br; # Point SSLCertificateChainFile at a file containing the &br; # concatenation of PEM encoded CA certificates which form the &br; # certificate chain for the server certificate. Alternatively &br; # the referenced file can be the same as SSLCertificateFile &br; # when the CA certificates are directly appended to the server &br; # certificate for convinience. &br; SSLCertificateChainFile /etc/letsencrypt/live/&color(lime){hoge.com};/chain.pem &color(lime){← 中間証明書指定};| **Apache設定(SSL Server Test対応) [#o2a63694] [[SSL Server Test>https://www.ssllabs.com/ssltest/]]で''A+''評価を得るための設定を行う |BGCOLOR(black):COLOR(white):|c |[root@localhost ~]# vi /etc/httpd/conf.d/ssl.conf &color(lime){← SSL設定ファイル編集}; &br; # SSL Protocol support: &br; # List the enable protocol levels with which clients will be able to &br; # connect. Disable SSLv2 access by default: &br; SSLProtocol all -SSLv2 -SSLv3 &color(lime){← SSLv2,SSLv3を無効化}; &br; &br; # Speed-optimized SSL Cipher configuration: &br; # If speed is your main concern (on busy HTTPS servers e.g.), &br; # you might want to force clients to specific, performance &br; # optimized ciphers. In this case, prepend those ciphers &br; # to the SSLCipherSuite list, and enable SSLHonorCipherOrder. &br; # Caveat: by giving precedence to RC4-SHA and AES128-SHA &br; # (as in the example below), most connections will no longer &br; # have perfect forward secrecy - if the server's key is &br; # compromised, captures of past or future traffic must be &br; # considered compromised, too. &br; #SSLCipherSuite RC4-SHA:AES128-SHA:HIGH:MEDIUM:!aNULL:!MD5 &br; #SSLHonorCipherOrder on &br; SSLCipherSuite ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS &color(lime){← 追加(使用する暗号化方法)}; &br; SSLHonorCipherOrder on &color(lime){← 追加(暗号化方法をサーバー側で決定)}; &br; &br; Header always set Strict-Transport-Security "max-age=15768000" &color(lime){← 追加(HSTS=常時HTTPSアクセスの有効化)}; &br; </VirtualHost>| **Apache設定(AWStats対応) [#i7aa8391] Apacheアクセスログ解析(AWStats)で解析できるように''https''アクセスログを''http''アクセスログと同じファイルに同じフォーマットで出力するようにする。 |BGCOLOR(black):COLOR(white):|c |[root@localhost ~]# vi /etc/httpd/conf.d/ssl.conf &color(lime){← SSL設定ファイル編集};| |# Use separate log files for the SSL virtual host; note that LogLevel &br; # is not inherited from httpd.conf. &br; ErrorLog logs/error_log &color(lime){← ログファイル名変更}; &br; CustomLog logs/access_log combined env=!no_log &color(lime){← ログ取得ディレクティブとログファイル名変更}; &br; LogLevel warn| *Apache設定反映 [#p0646572] **Apache設定反映 [#e265b4a3] |BGCOLOR(black):COLOR(white):|c |[root@localhost ~]# systemctl reload httpd &color(lime){← Apache設定反映};| **TCP443番ポート開放 [#q40cf771] ''【ルーター】'' ルーター側の設定で、TCP443番ポートへのアクセスをサーバーに転送するようにする。 ※ルーターの設定は各ルーターのマニュアルまたはメーカー別ルーターポート開放手順を参照 ''【ファイアウォール】'' サーバー側のファイアウォール設定で、TCP443番ポートへのアクセスを許可するようにする。 [[Portチェックテスト【外部からのPort開放確認】>http://www.cman.jp/network/support/port.html]]で「ホスト名(''FQDN'') または グローバルIPアドレス」にサーバー名(例:hoge.com)、「チェックポート番号」に''443''と入力、「ご注意・制約事項」を確認チェックして「Portチェック実行」ボタンを押下し、 -ホスト:hoge.com -ポート:''443'' -にアクセスできました と表示されることを確認。 *Apache確認 [#e40e7686] Webブラウザでhttps://Webサーバー名(例:https://hoge.com)へアクセスしてセキュリティの警告が表示されないこと [[SSL Server Test>https://www.ssllabs.com/ssltest/]]で''A+''評価となることを確認。 **サーバー証明書自動更新設定 [#dd9b3ba9] ''Certbot''のサーバー証明書の有効期間は3ヶ月のため、毎月自動でサーバー証明書を更新するようにする。 |BGCOLOR(black):COLOR(white):|c |[root@localhost ~]# vi /etc/cron.monthly/certbot &color(lime){← サーバー証明書自動更新スクリプト作成};| |#!/bin/sh &br; log=`mktemp` &br; code=0 &br; &br; # &br; # 証明書更新 &br; # &br; for conf in `ls /etc/letsencrypt/renewal/` &br; do &br; # ドメイン名取得 &br; domain=`echo ${conf}|sed -e 's/\([^ ]*\)\.conf/\1/p' -e d` &br; &br; # 認証方式取得 &br; authenticator=`grep authenticator /etc/letsencrypt/renewal/${conf} | awk '{print $3}'` &br; &br; if [ ${authenticator} = 'webroot' ]; then &br; # Web認証の場合 &br; &br; # ドキュメントルート取得 &br; webroot=`grep -A 1 webroot_map /etc/letsencrypt/renewal/${conf} | grep = | awk '{print $3}'` &br; &br; # 証明書更新 &br; /usr/local/certbot/certbot-auto certonly --webroot \ &br; -w ${webroot} -d ${domain} --renew-by-default >> ${log} 2>&1 &br; [ $? -ne 0 ] && cat ${log} &br; else &br; # スタンドアロン認証の場合 &br; &br; # 証明書更新 &br; lsof -i:80 > /dev/null 2>&1 &br; if [ $? -eq 0 ]; then &br; echo 'Webサーバー稼働中のためスタンドアロン認証不可' &br; else &br; /usr/local/certbot/certbot-auto certonly -a standalone \ &br; -d ${domain} --renew-by-default >> ${log} 2>&1 &br; [ $? -ne 0 ] && cat ${log} &br; fi &br; fi &br; done &br; &br; # &br; # 証明書更新反映 &br; # &br; &br; # Webサーバー設定再読込み &br; lsof -i:443 > /dev/null 2>&1 &br; if [ $? -eq 0 ]; then &br; rpm -q systemd > /dev/null 2>&1 &br; if [ $? -eq 0 ]; then &br; systemctl reload httpd &br; else &br; /etc/rc.d/init.d/httpd reload > /dev/null 2>&1 &br; fi &br; fi &br; &br; # SMTPサーバー設定再読込み &br; lsof -i:465 > /dev/null 2>&1 &br; if [ $? -eq 0 ]; then &br; rpm -q systemd > /dev/null 2>&1 &br; if [ $? -eq 0 ]; then &br; systemctl reload postfix &br; else &br; /etc/rc.d/init.d/postfix reload > /dev/null 2>&1 &br; fi &br; fi &br; &br; # IMAPサーバー設定再読込み &br; lsof -i:995 > /dev/null 2>&1 &br; if [ $? -eq 0 ]; then &br; rpm -q systemd > /dev/null 2>&1 &br; if [ $? -eq 0 ]; then &br; systemctl reload dovecot &br; else &br; /etc/rc.d/init.d/dovecot reload > /dev/null 2>&1 &br; fi &br; fi &br; &br; # &br; # ログをsyslogへ出力後削除 &br; # &br; cat ${log}|logger -t `basename ${0}` ; rm -f ${log}| |#!/bin/sh &br; log=`mktemp` &br; code=0 &br; &br; # &br; # 証明書更新 &br; # &br; for conf in `ls /etc/letsencrypt/renewal/` &br; do &br; # ドメイン名取得 &br; domain=`echo ${conf} | sed -e 's/\([^ ]*\)\.conf/\1/p' -e d` &br; &br; # 認証方式取得 &br; authenticator=`grep authenticator /etc/letsencrypt/renewal/${conf} | awk '{print $3}'` &br; &br; if [ ${authenticator} = 'webroot' ]; then &br; # Web認証の場合 &br; &br; # ドキュメントルート取得 &br; webroot=`grep -A 1 webroot_map /etc/letsencrypt/renewal/${conf} | grep = | awk '{print $3}'` &br; &br; # 証明書更新 &br; /usr/local/certbot/certbot-auto certonly --webroot \ &br; -w ${webroot} -d ${domain} --renew-by-default >> ${log} 2>&1 &br; [ $? -ne 0 ] && cat ${log} &br; else &br; # スタンドアロン認証の場合 &br; &br; # 証明書更新 &br; lsof -i:80 > /dev/null 2>&1 &br; if [ $? -eq 0 ]; then &br; echo 'Webサーバー稼働中のためスタンドアロン認証不可' &br; else &br; /usr/local/certbot/certbot-auto certonly -a standalone \ &br; -d ${domain} --renew-by-default >> ${log} 2>&1 &br; [ $? -ne 0 ] && cat ${log} &br; fi &br; fi &br; done &br; &br; # &br; # 証明書更新反映 &br; # &br; &br; # Webサーバー設定再読込み &br; lsof -i:443 > /dev/null 2>&1 &br; if [ $? -eq 0 ]; then &br; rpm -q systemd > /dev/null 2>&1 &br; if [ $? -eq 0 ]; then &br; systemctl reload httpd &br; else &br; /etc/rc.d/init.d/httpd reload > /dev/null 2>&1 &br; fi &br; fi &br; &br; # SMTPサーバー設定再読込み &br; lsof -i:465 > /dev/null 2>&1 &br; if [ $? -eq 0 ]; then &br; rpm -q systemd > /dev/null 2>&1 &br; if [ $? -eq 0 ]; then &br; systemctl reload postfix &br; else &br; /etc/rc.d/init.d/postfix reload > /dev/null 2>&1 &br; fi &br; fi &br; &br; # IMAPサーバー設定再読込み &br; lsof -i:995 > /dev/null 2>&1 &br; if [ $? -eq 0 ]; then &br; rpm -q systemd > /dev/null 2>&1 &br; if [ $? -eq 0 ]; then &br; systemctl reload dovecot &br; else &br; /etc/rc.d/init.d/dovecot reload > /dev/null 2>&1 &br; fi &br; fi &br; &br; # &br; # ログをsyslogへ出力後削除 &br; # &br; cat ${log} | logger -t `basename ${0}` ; rm -f ${log}| |[root@localhost ~]# chmod +x /etc/cron.monthly/certbot &color(lime){← サーバー証明書自動更新スクリプトへ実行権限付加};|