Contents

Linux - Web 伺服器(httpd) 使用 SSL (TLS)

HTTP 伺服器固然方便,不過明碼在網路世界上裸奔還是有一定的風險,尤其有個資或是機敏資料傳輸時顯得格外不安全,必須透過 HTTPS 傳輸更為安全。

練習環境
可以使用 HowHow 的創建 Lab 練習環境,來實作 Web 伺服器與存取操作,使用方式請參閱 HowHow 網站 - Linux 練習 Lab 第二版

1. TLS 簡介

TLS (傳輸層安全協定) 是 HTTPS 使用的協議,用來保護 Web 流量受到對其真實性、機敏性及完整性的攻擊,TLS 藉由一對公私鑰組合來加密 TLS Session,每一個伺服器除了公私鑰組合之外,其中也並須先由證書簽章機構(CA)進行簽章,才能確保其可靠性與真實性。

TLS 自簽憑證

在 HowHow 的 lab 環境可以直接在 bastion 中的 /data/home/student/lab_playbook 目錄下執行 git stash save && git pull 拉取自動產生 TLS Ansible 的 Playbook ,並自動簽發到指定的主機 /var/ssl 目錄內。

1
2
3
4
# bastion 主機
cd /data/home/student/lab_playbook
git stash save && git pull
ansible-playbook -l serverc.lab.example.com playbook.yml

2. 安裝及設定 httpd Web 伺服器

1
2
3
4
5
使用 Server C 為 httpd Server 範例:

    1. Server C 上執行 httpd ,並為 www-a.lab.example.com 網域提供服務來至 /srv/www-a/www/ 其目錄內容 。
    2. 其他網域來至 /srv/default/www/ 期目錄內。
    3. 並且所有 http 的流量都強制轉至 https 。

2.1. 安裝 httpd 及所需加密套件

1
2
3
4
5
[root@serverc ~]# yum install httpd mod_ssl -y | tail -n 4
  mod_ssl-1:2.4.37-51.module+el8.7.0+1155+5163394a.1.x86_64
  rocky-logos-httpd-86.3-1.el8.noarch

Complete!

2.2. 創建 web 目錄及檔案

  1. 創建 /srv/www-a/www//srv/default/www/ 目錄。
1
[root@serverc ~]#  mkdir -p /srv/{www-a,default}/www
  1. 創建 html 顯示內容 。
1
2
[root@serverc ~]# echo 'www-a' > /srv/www-a/www/index.html
[root@serverc ~]# echo 'coming soon'  > /srv/default/www/index.html
  1. 設定 SELinux Context,並載入使生效
1
2
3
4
5
[root@serverc ~]# restorecon -Rv /srv/
Relabeled /srv/www-a/www from unconfined_u:object_r:var_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
Relabeled /srv/www-a/www/index.html from unconfined_u:object_r:var_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
Relabeled /srv/default/www from unconfined_u:object_r:var_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0
Relabeled /srv/default/www/index.html from unconfined_u:object_r:var_t:s0 to unconfined_u:object_r:httpd_sys_content_t:s0

2.3. 設定 default 預設虛擬主機

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@serverc ~]# cat > /etc/httpd/conf.d/default.conf << EOF
<VirtualHost *:443>
  ServerName serverc.lab.example.com
  ServerAlias serverc
  SSLEngine On
  SSLProtocol all -SSLv2 -SSLv3 -TLSv1
  SSLCipherSuite HIGH:MEDIUM:!aNull:!MD5
  SSLHonorCipherOrder on
  SSLCertificateFile /var/ssl/lab.crt
  SSLCertificateKeyFile /var/ssl/lab.key
  DocumentRoot /srv/default/www
</VirtualHost>

<Directory /srv/default/www>
  Require all granted
</Directory>

<VirtualHost *:80>
  ServerName serverc.lab.example.com
  ServerAlias serverc
  Redirect "/" "https://serverc.lab.example.com"
</VirtualHost>
EOF
TLS 自簽憑證
使用 Lab 環境執行 Bastion 的 Playbook 會自動簽發 30 天的憑證,如果非 Lab 環境需自行簽章。

2.4. 設定 www-a.lab.example.com 虛擬主機

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@serverc ~]# cat > /etc/httpd/conf.d/www-a.conf <<EOF
<VirtualHost *:443>
  ServerName www-a.lab.example.com
  ServerAlias www-a
  SSLEngine On
  SSLProtocol all -SSLv2 -SSLv3 -TLSv1
  SSLCipherSuite HIGH:MEDIUM:!aNull:!MD5
  SSLHonorCipherOrder on
  SSLCertificateFile /var/ssl/lab.crt
  SSLCertificateKeyFile /var/ssl/lab.key
  DocumentRoot /srv/www-a/www
</VirtualHost>

<Directory /srv/www-a/www>
  Require all granted
</Directory>

<VirtualHost *:80>
  ServerName www-a.lab.example.com
  ServerAlias www-a
  Redirect "/" "https://www-a.lab.example.com"
</VirtualHost>
EOF

2.5. 啟動伺服器及開放防火牆規則

1
2
3
4
5
6
[root@serverc ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@serverc ~]# firewall-cmd --permanent --add-service=http --add-service=https
success
[root@serverc ~]# firewall-cmd --reload
success

3. Client 存取 Web 伺服器

3.1. 設定靜態解析

1
2
3
[root@serverd ~]# cat >> /etc/hosts << EOF
172.25.250.13 www-a.lab.example.com www-a
EOF

3.2. 測試使用 www-awww-a.lab.example.com 存取

1
2
3
4
[root@serverd ~]# curl -k  https://www-a.lab.example.com
www-a
[root@serverd ~]# curl -k  https://www-a
www-a

3.3. 測試其他任意方式存取

1
2
3
4
[root@serverd ~]# curl -k  https://serverc.lab.example.com
coming soon
[root@serverd ~]# curl -k  https://172.25.250.13
coming soon
Note
囿於僅開放 www-xwww-x.lab.example.com 可以存取到正式的頁面,其他的流量均只能至 default 頁面。

4. 資安弱掃 Issue

透過 -SSLv2 可以取消此協議,範例如下 :

1
2
3
4
5
6
7
<VirtualHost *:443>
  ServerName serverc.lab.example.com
  ServerAlias serverc
  SSLEngine On
  SSLProtocol all -SSLv2 -SSLv3 -TLSv1
...output omitted...
</VirtualHost>
SSLProtocol
建議只開放 TLS v2 以上的協議,其餘的以下的均不安全不建議開放。

透過 curl 檢測可以驗證 tls 1.1 協議已經被關閉。

1
2
3
4
[root@serverd ~]# curl -k --tlsv1.1  --tls-max 1.1  https://serverc.lab.example.com
curl: (35) error:141E70BF:SSL routines:tls_construct_client_hello:no protocols available
[root@serverd ~]# curl -k --tlsv1.1  --tls-max 1.2  https://serverc.lab.example.com
comming soon

5. 小結

HTTPS 在目前的網路世界是非常重要的議題,也是資安必定會出稽查報告的最基本事件,除非此網路有針對部分白名單開放,不建議走 http 明碼裸奔方式在網路上傳輸。



如果你還沒有註冊 Like Coin,你可以在文章最下方看到 Like 的按鈕,點下去後即可申請帳號,透過申請帳號後可以幫我的文章按下 Like,而 Like 最多可以點五次,而你不用付出任何一塊錢,就能給我寫這篇文章的最大的回饋!