之前曾經介紹過如何透過 apache 的.htaccess 強制使用 https 連線,但實際上此作法瀏覽器還是可能與機器建立 http 連線之後,再改而建立 https 連線。 如果可以的話,我們會希望一開始就嘗試建立 https 連線。


 

HSTS( HTTP Strict Transport Security )

https://developer.mozilla.org/en-US/docs/Web/Security/HTTP_strict_transport_security

簡單說明一下,原則上就是我們在網頁伺服器端( 如: apache、nginx 等)設置 response 的 header,瀏覽器若支援HSTS則會根據 header 作日後相對應的行為。

如:Strict-Transport-Security: max-age=31536000; includeSubDomains

支援 HSTS 的瀏覽器,會在日後一年對此網域的網站嘗試直接建立 https 連線。

支援度:

support

各主流瀏覽器大多皆有支援

好處:

有很多攻擊 https 網站的手法( sslstrip 等),都是透過剝離 https 連線,轉而取代使用 http 連線,進而達到資料竊取或攻擊的行為,使用 HSTS 可以有效避免此類攻擊。

apache設定:

  1. 開啟 apache headers 模組
    sudo a2enmod headers
  2. 重新啟動 apache
    sudo service apache2 restart
  3. 設定 /etc/apache2/conf-available/security.conf ,加上 header 設定
    Header always set Strict-Transport-Security "max-age=31536000;includeSubdomains; preload"
    
  4. 重新載入設定
    sudo service apache2 reload 
  5. 成功設定,可以試著用瀏覽器看一下 response header
    HSTS

參考資料:

http://www.wikiwand.com/zh-tw/HTTP%E4%B8%A5%E6%A0%BC%E4%BC%A0%E8%BE%93%E5%AE%89%E5%85%A8

1 Comment

Leave a Reply