iT邦幫忙

2025 iThome 鐵人賽

DAY 29
0
Build on AWS

從一個網站的誕生,看懂 AWS 架構與自動化的全流程!系列 第 29

Day 29 雲端安全防護:WAF x CloudFront 打造多層防禦機制

  • 分享至 

  • xImage
  •  

https://ithelp.ithome.com.tw/upload/images/20251012/20172743VYv8bgG0p9.png

一、前言

隨著系統對外開放 API 或網站入口,惡意流量與自動化攻擊(如 SQL Injection、XSS、Bot Flooding)變得常見。若沒有流量層級的防護,惡意請求可能在抵達後端前造成壓力、或直接觸發漏洞。本次 Lab 的目標是利用 AWS WAF 搭配 CloudFront 實現全球分佈的 Web Application Firewall,主動攔截異常流量與特定區域的 IP 存取,確保網站與 API 的前端安全性。

本 Lab 屬於整體 Serverless 架構的「安全防護層(Security Layer)」,位於所有使用者請求進入後端(S3、API Gateway、Lambda)之前。前幾日的實作已經涵蓋資料分析與檔案處理,本日聚焦於「進入點安全性」。

WAF + CloudFront 可視為安全閘道,透過全域邊緣節點在「離攻擊源最近的地方」過濾流量,減少對應用層的壓力,並確保只有合規的 HTTP 請求能進入系統。

二、需要使用到的服務

(1) AWS WAF:建立 Web ACL,利用 AWS Managed Rules 自動偵測並阻擋 SQLi、XSS、Bot 等常見攻擊。
(2) Amazon CloudFront:作為全域 CDN 與 WAF 的前端整合點,讓防護規則能在全球邊緣節點執行。
(3) Amazon S3:作為靜態網站或 API 儲存後端,WAF 放行的請求會進入此層。
(4) IAM:提供 WAF 與 CloudFront 操作所需的最低權限角色,確保設定過程安全。

三、架構/概念圖

https://ithelp.ithome.com.tw/upload/images/20251012/201727430nbFMFZwMY.png

四、技術重點

  1. 經常更新 WAF Managed Rules,以獲得最新威脅防護。
  2. 建議搭配 CloudWatch Metrics 監控 WAF 攻擊統計,並設定 SNS 通知。
  3. 若網站支援 API 請求,應針對 Header/Body 大小限制與 JSON 結構進行額外規則設計。
  4. 若為多地部署系統,可搭配 AWS Shield Advanced 進一步防禦 DDoS。
  5. 使用 GeoMatch 條件時,應僅封鎖高風險或業務不相關區域,避免誤封。

五、Lab流程

1️⃣ 前置作業

1. 創建 CloudFront ,並用S3發佈靜態網站。(Day6)

2️⃣ 主要配置

1. 建立 Web ACL(WAF 規則集):

  1. 進入「WAF & Shield」頁面。
    https://ithelp.ithome.com.tw/upload/images/20251012/2017274366OTFhCbv0.png

  2. 創建一個新的web ACL。
    https://ithelp.ithome.com.tw/upload/images/20251012/201727438Mv7BbsumW.png

  3. 創建Global WAF,並設定名稱。

    💡WAF Web ACL 在建立時,區域的選擇決定了它可以關聯哪些 AWS 服務。

    1. CloudFront 部署 (全球性):
      • WAF 區域:如果您要將 WAF 附加到 CloudFront 分發 (Distribution),您必須在 Global (全域) 區域建立 Web ACL。這是因為 CloudFront 是一個全球性服務,而 WAF 規則必須部署到全球的邊緣節點上。
    2. API Gateway 部署 (區域性):
      • WAF 區域:如果您要將 WAF 附加到 API Gateway 或 ALB (Application Load Balancer),您必須在 該服務所在的特定 AWS 區域 (例如 us-east-1ap-northeast-2) 建立 Web ACL。
        https://ithelp.ithome.com.tw/upload/images/20251012/20172743DES0ggwrg1.png
  4. 選擇來源為「CloudFront」。
    https://ithelp.ithome.com.tw/upload/images/20251012/20172743HRLpuXDeoa.png

  5. 下一步。
    https://ithelp.ithome.com.tw/upload/images/20251012/20172743dLENwx7XwA.png

  6. 新增AWS內建規則。
    https://ithelp.ithome.com.tw/upload/images/20251012/20172743y9CkgTiCzC.png

  7. 啟用「SQL database」規則。(防止SQL注入)
    https://ithelp.ithome.com.tw/upload/images/20251012/20172743iFTNSw3FtH.png

  8. 新增自訂義規則。
    https://ithelp.ithome.com.tw/upload/images/20251012/201727430fom3dl0jy.png

  9. 防止特定國家訪問。(這邊設定「限制台灣用戶IP訪問」)
    https://ithelp.ithome.com.tw/upload/images/20251012/20172743U286rRhEtk.png
    https://ithelp.ithome.com.tw/upload/images/20251012/20172743iGYbyel2dD.png
    https://ithelp.ithome.com.tw/upload/images/20251012/20172743f8JJdbZT2j.png

  10. 下一步。
    https://ithelp.ithome.com.tw/upload/images/20251012/20172743mJp7Yc1Mqh.png

  11. 調整規則優先級。(本次不做調整)
    https://ithelp.ithome.com.tw/upload/images/20251012/20172743O9aoW0vRdg.png

  12. 設定CloudWatch Metric (CloudWatch 指標) 的名稱。
    https://ithelp.ithome.com.tw/upload/images/20251012/20172743gCzv29nzqn.png

  13. 確認創建。
    https://ithelp.ithome.com.tw/upload/images/20251012/20172743BJBl1J63AH.png

2. 至CloudFront確認WAF已綁定

  1. 至「CloudFront」頁面。
    https://ithelp.ithome.com.tw/upload/images/20251012/20172743SP4dP2jdtS.png

  2. 進入CloudFront資源內。
    https://ithelp.ithome.com.tw/upload/images/20251012/20172743jPAEbiMaik.png

  3. 點選「安全」頁面,確認WAF已生效。
    https://ithelp.ithome.com.tw/upload/images/20251012/20172743iHoyqKGdMH.png

  4. 【補充】沒開啟的話,請點這邊啟用。
    https://ithelp.ithome.com.tw/upload/images/20251012/20172743NsadL7IdXx.png
    https://ithelp.ithome.com.tw/upload/images/20251012/20172743DkdRTnobwW.png

3️⃣ 測試驗證

1. 區域封鎖測試:

說明:使用 VPN 切換至被封鎖區域(台灣 IP),重新訪問網站,應顯示封鎖頁面或 Access Denied
https://ithelp.ithome.com.tw/upload/images/20251012/20172743LLROC3VUB9.png

2. 正常請求測試:

說明:使用非封鎖區域 IP 正常訪問 CloudFront URL,網站應能正確顯示。
https://ithelp.ithome.com.tw/upload/images/20251012/20172743qPPU3gH68c.png

3. 惡意請求(SQL注入)測試:

說明:使用 curl 模擬 SQL Injection 請求,確認是否被阻擋。(記得使用非封鎖區域)

curl -X POST "https://<your-cloudfront-domain>" -d "id=1' OR '1'='1"

結果應回傳:403 Forbidden
https://ithelp.ithome.com.tw/upload/images/20251012/20172743781QLXNUNL.png

六、結語

本次 Lab 建立了一層全球級的安全防護架構,透過 CloudFront + WAF,在流量進入應用層前就能進行過濾與阻擋。此設計有效降低後端負載,避免惡意請求造成服務中斷或資料外洩。
最終成果是一個結合 高可用性、安全性與成本效益 的 Web 防護方案,為整體 Serverless 架構打下安全基礎。


上一篇
Day 28 商業智慧儀表板:QuickSight x Athena 報表與視覺化
系列文
從一個網站的誕生,看懂 AWS 架構與自動化的全流程!29
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言