【DDoS 防護】Google Cloud Armor 介紹與實作:設定白名單、Load Balancing 資源

資安攻擊手法層出不窮,Google Cloud 也持續推出更完善的資安服務,其中 Cloud Armor 擁有分散式阻斷服務(DDoS)防護機制與網路應用程式防火牆,可有效搭配 Load BalancingCloud CDN 使用來強化網路安全防護。

在資安防護中,白名單是最常被使用的機制之一,能夠防止不認識的 IP 存取到應用服務,在 Cloud Armor 中是一個很基礎但相當重要的概念。因此本篇文章的範圍將會著重在設定白名單 (只有指定的 IP、CIDR 範圍才能存取後端服務),也會涵蓋 Security Policy、Instance Group、Load Balancer 等資源的建立。

Cloud Armor 概念與用途

在 Google Cloud Platform(簡稱 GCP)中,Cloud Armor 可以協助您避免部署的資源受到多種攻擊的威脅,包括了常見的分散式阻斷服務攻擊 (DDoS) 、跨網站指令碼 (XSS) 和 SQL 注入 (SQL injection) 等惡意行為。一般來說,在設定 Cloud Armor 時必須搭配 Load Balancer 一起使用,且 Cloud Armor 也與其他 GCP 產品進行了高程度的整合,像是 GKE、Cloud CDN、Cloud Storage 等,甚至如果您的部署是採用混合雲的架構,也可以通過使用 Cloud Armor 來對 Hybrid 環境起到保護的作用。

要讓 Cloud Armor 保護您的資源,您需要先將 Security Policy 設定好。Security Policy 是由一條或以上的 Rules 來組成,這些規則根據傳入請求的 IP 地址 / CIDR 範圍、區域代碼或請求標頭等條件來為您過濾網路流量。此外,通過與其他 GCP 服務的搭配,您也可以同時做到網路的防護與監測。

Cloud Armor 基礎操作

以下將提供一個簡易的範例,只有特定 VM 存取 Load Balancer 時才能成功收到來自後端服務的回應,否則存取將被拒絕,您可以通過多種平台(像是其他 3C 用品、Cloud Shell)來進行效果的驗證。以下說明相關範例:

一、GCP 上操作所需的 IAM 權限

使用者需要在先專案層級 (Project Level) 中的 IAM 內擁有 3 個由 GCP 所事先定義的 Predefined Role:

  • Compute Admin」 [1] 這個 Role 能授予使用者具備建立、修改和刪除相關 Load Balancer 及後端服務的權限。
  • Compute Security Admin」 [2] 這個 Role 除了讓使用者擁有對 Firewalls 進行操作的所有權限之外,也包括建立、修改和刪除 Cloud Armor 所需要的 Security Policy。
  • Compute Network Admin」 [3] 這個 Role 能授予使用者具備附加Cloud Armor Security Policy 到後端服務的權限。

二、實作步驟

2-1 建立用來測試的 VM

在建立 Cloud Armor 之前,需要透過 Compute Engine [4] (後簡稱為 GCE) 這個 GCP 服務建立一台 VM 作為白名單來測試 Cloud Armor 的效果。

  1. 請先於 Project 內建立一台用於模擬來源 IP 的 VM [5]。請進入 GCE 的 Console 內,並請點選上方「CREATE INSTANCE」的按鈕。(此步驟需要您的 IAM 中有 Compute Admin 此 Predefined Role)。
  2. 請於 Name 的欄位為您的 VM 指定一個名字,本範例將以「demo-source-instance」做為例子。
  3. 將 Region 的欄位調整成「asia-east1」,接著到設定最下方展開 Advanced options 的下拉式選單,此範例中將以 Custom VPC 為主。在 Networking > Network interfaces 的部分,將 Network 更改成 Custom VPC「demo-custom-vpc-network」,此時 Subnetwork 會自動替換為「demo-custom-vpc-network-subnet1」,點擊「DONE」的按鈕來完成 Network 部分的設定。
圖 2-1:GCE 的 Network 設定
icon/enlarge

d. 點擊「CREATE」的按鈕來完成 VM 的建立。請先複製此台 VM 的 External IP 做後續使用。

圖 2-2:GCE 的外部 IP
icon/enlarge

2-2 於 Cloud Armor 中建立 Security Policy

Cloud Armor Security Policy 共分為 2 種類型 [6]:

  • Backend Security Policy:可用於過濾請求,但僅限於對後端服務 (Backend Service,像是 Instance Group、Network Endpoint Group) 的保護。
  • Edge Security Policy:可用於替在緩存中的內容設定過濾請求和訪問策略,適用於後端服務與後端值區 (Backend Storage)。

在這個範例中,我們將新增的 Security Policy 是屬於 Backend 這個類型的,請參考下面的步驟進行設定:

  1. 點擊 Navigation menu > Network Security > Cloud Armor 的路徑進到 Cloud Armor 的 Console 中,請點擊上方「CREATE POLICY」的按鈕 [7]。
  2. 在 Configure policy 的部分,將此 Policy 命名為「demo-security-policy」。於 Policy type 的地方選擇「Backend security policy」的選項。最後在下方 Default rule action 勾選 Deny 後點擊「NEXT STEP」的按鈕 (因為此範例要演示的是設定白名單,因此需要先拒絕所有的存取請求。預設的請求錯誤代碼將會是 403,但這部分是允許使用者自行去調整的)。
圖 2-3:Cloud Armor Policy 類型與動作
icon/enlarge

c. 點擊「ADD RULE」的按鈕,在 Match 的欄位貼上剛剛複製的 External IP,在本範例中即是「34.80.220.214 /32」 (如果您想要指定特定的網段,CIDR 的部分是必須要添加的)。下方 Action 欄位,請調整成「Allow」這個選項。最後請在 Priority 的欄位添加此 Policy 的優先程度,像是 1 或是其他數字 (數字越小越優先)。

圖 2-4:Cloud Armor Policy 允許來源
icon/enlarge

d. 請先點擊「DONE」的按鈕,接著再點擊「CREATE POLICY」來完成此次 Policy 的建立。

2-3 建立後端服務(Backend Service)

  1. 建立 Load Balancer 之前,需要先建立一個後端服務 (Backend Service),此範例中將使用 Managed Instance Group,故需要先建立一個 Instance Template。請先進到 GCE 的 Console,點選左側 Instance templates 的選單,並點擊上方「CREATE INSTANCE TEMPLATE」的按鈕 [8]。
  2. 請於 Name 的欄位為您的 template 指定一個名字,本範例將以「demo-template」做為例子。
  3. 到設定最下方展開 Advanced options 的下拉式選單,在 Networking 的部分先加上一個名為「allow-health-check」的 tag。
圖 2-5:Network tags 設定
icon/enlarge

d. 將 Network interfaces 的地方更改成 Custom VPC「demo-custom-vpc-network」與所需的 Subnet。

圖 2-6:Network Interface 設定
icon/enlarge

e. 接著請在 Management > Automation 的部分,將以下 Startup Script 給複製貼上。

1#! /bin/bash 2sudo apt-get update 3sudo apt-get install apache2 -y 4sudo a2ensite default-ssl 5sudo a2enmod ssl 6vm_hostname="$(curl -H "Metadata-Flavor:Google" \ 7http://metadata.google.internal/computeMetadata/v1/instance/name)" 8sudo echo "Page served from: $vm_hostname" | \ 9tee /var/www/html/index.html 10sudo systemctl restart apache2
圖 2-7:Startup script 設定
icon/enlarge

f. 點擊「CREATE」的按鈕來完成此次 Instance Template 的建立。

g. 從左側選單中選擇 Instance groups 的選項,並點擊上方「CREATE INSTANCE GROUP」的按鈕 [9]。

h. 請於 Name 的欄位為您的 group 指定一個名字,本範例將以「demo-instance-group」做為例子。

i. 為了達到更好的 High Availability (HA) 效果,請先選擇要使用 Multiple zones,接著將 Region 設定為「asia-east1」,之後再從 Instance template 的下拉式選單中選擇剛剛建立完成的「demo-template」。

圖 2-8:選擇 template 與區域
icon/enlarge

j. 在Autoscaling 的部分,請確保能自動增加、減少 Instances 的 auto-scaling 功能是開啟的,將「Minimum number of instances」的欄位更改為 2,以確保無論何時此 Instance Group 內部至少都會有 2 台 VM 在運作。

圖 2-9:Autoscaling 設定
icon/enlarge

k. 在 Port mapping 的部分,請先點擊「ADD PORT」的按鈕,接著於 Port name 1 的欄位中填入「http」,於 Port numbers 1 的欄位則填入「80」。

圖 2-10:Port mapping 設定
icon/enlarge

i. 點擊 "CREATE" 的按鈕來完成此次 Instance Group 的建立。

2-4 建立 VPC Firewall

  1. 接著需要建立一個用於 Health Check 的防火牆,請進入 VPC 的 Console,並選擇左側導覽列中 Firewall 的選項,點擊上方「CREATE FIREWALL RULE」的按鈕 [10]。
  2. 請於 Name 的欄位為您的 Firewall 指定一個名字,本範例將以「demo-custom-vpc-network-allow-health-check」做為例子,並請將 Network 的欄位改成 Custom VPC「demo-custom-vpc-network」。
圖 2-11:變更使用的 VPC Network
icon/enlarge

c. 在 Targets 的部分,請選擇「Specified target tags」的選項,並於下方欄位填入先前 Instance Template 中所設定的 tag「allow-health-check」。Source 的部分則需要填入 2 個 Google 所使用的 CIDR 網段:「130.211.0.0/22」及「35.191.0.0/16」。

圖 2-12:Network tags 與 IP 地址的設定
icon/enlarge

d. 最後請將「TCP」做勾選的動作,並在下方填入 80。到這裡為止 Firewall 的設定就已經完成了,還請記得按「CREATE」來完成 Firewall 的建立。

圖 2-13:設定開放的 Port
icon/enlarge

2-5 建立 Health Check

  1. 接下來要為 Backend Service 建立一個新的 Health Check。請進入 GCE 的 Console,並選擇左側導覽列中 Health checks 的選項,並點擊上方「CREATE HEALTH CHECK」的按鈕 [11]。
  2. 請於 Name 的欄位為您的 Health Check 指定一個名字,本範例將以「demo- health-check」做為例子。
  3. 請將 Protocol 更改為「HTTP」的選項,並確保 Port 使用「80」,之後即可點擊「CREATE」的按鈕來完成 Health Check 的建立。
圖 2-14:Protocol 與 Port 的設定
icon/enlarge

2-6 建立負載平衡器 (Load Balancer)

目前 Cloud Armor 所支援的 Load Balancer 共有下面列出的幾種 [12],當您把 Security Policy 設定完成後,就可以將其附加到要保護的後端服務與後端值區上。

  • Global external HTTP(S) load balancer / Global external HTTP(S) load balancer (classic)
  • External TCP proxy load balancer / External SSL proxy load balancer

在這個範例中,我們將新增一個 HTTP (S) 類型的 Load Balancer 來作為 Instance Group 的前端,請參考下面的步驟進行設定:

  1. 接著建立 Load Balancer 來置於 Instance Group 之前,請您先進入 Load Balancer 的 Console,接著點擊上方「CREATE LOAD BALANCER」的按鈕,並請選擇類型為 HTTP(S) 的 Load Balancer [13]。
  2. 點擊「CONTINUE」的按鈕,接著於 Name 的欄位為您的 Load Balancer 指定一個名字,本範例將以「demo-lb」做為例子。
  3. 在 Frontend configuration 的頁面,為其取名「demo-http-port」。在 Protocol 的下拉式選單中選擇使用「HTTP」,並在 Port 的欄位則設定為「80」 (如果是要使用 HTTPS 的話則請設定為 443 Port),點擊「DONE」的按鈕來完成 frontend 的設定。
圖 2-15:Frontend 設定
icon/enlarge

d. 在 Backend configuration 的頁面,請在下拉式選單中點擊「CREATE A BACKEND SERVICE」的按鈕。

e. 於 Name 的欄位為您的 Backend Service 指定一個名字,本範例將以「demo-backend-service」做為例子。

f. 於 Backend type 的欄位請選擇使用「Instance group」的選項,Protocol 則的欄位則是使用與 frontend 相同的「HTTP」。

圖 2-16:Backend 類型與 Protocol
icon/enlarge

g. 在 Backend 的區塊,請選擇先前建立好的 Instance Group「demo-instance-group」,此時您應會發現 Port numbers 的欄位也自動帶入了「80」的數值,點擊「DONE」的按鈕來儲存。

圖 2-17:選擇 Instance group
icon/enlarge

h. 在 Health check 的下拉式選單中選擇「demo-health-check」的選項。

圖 2-18:Health check 設定
icon/enlarge

i. 在下方 Cloud Armor backend security policy 的下拉式選單中選擇「demo-security-policy」的選項,並點擊「DONE」的按鈕來完成 Backend Service 的建立。

圖 2-19:Cloud Armor 設定
icon/enlarge

j. 點擊「CREATE」的按鈕來完成此次 Load Balancer 的建立,並先複製其 IP Address 以在後續測試中做使用。

圖 2-20:Load Balancer 的 IP
icon/enlarge

2-7 Cloud Armor 效果測試

  1. 為了驗證 Cloud Armor 的設定是否有生效,請先透過 SSH 進入一開始建立的 demo-source-instance 內 (請注意,您需有對應開放 22 Port 的 Firewall 才可連線)。
  2. 使用 curl 指令搭配剛剛複製的 Load Balancer IP,您應該可以得到類似於「Page served from: demo-instance-group-hhmc」的內容,因為此台 VM 的 IP Address 有符合 Cloud Armor 內所定義的規則。
圖 2-21:從來源 IP (白名單) 進行存取 Load Balancer
icon/enlarge

c. 但如果您使用 Console 上的 Cloud Shell 或是本地端的任一瀏覽器,輸入 Load Balancer 的 IP Address 則應該會得到「403 Forbidden」的內容,因為其 IP Address 並不符合 Cloud Armor 內所定義的規則,因此瀏覽過程中就被擋下了。

圖 2-22:嘗試以網頁來存取 Load Balancer
icon/enlarge

結論

其實設定白名單、黑名單在 Cloud Armor 是一個基礎的應用,Cloud Armor 還具備其他功能,像是配置威脅情報 (Threat Intelligence)、限制請求的速率、機器人偵測 (與另一產品 reCAPTCHA Enterprise 整合)、避免 DDoS 等的防護措施。另外,當您選擇使用 Cloud Armor 後,我們會建議您同時搭配 Cloud Logging 與 Cloud Monitoring 來進行進一步分析,可以更好的了解請求流量是如何發生的。以上建立 Instance Group 及 Load Balancer 的步驟,您可以進一步參考文件 [14]。如果您想更多的了解 Cloud Armor 的功能或是官方定義的 Best Practices,還請您再分別參考文件 [15] [16]。

撰文者:Oaklynn Lin, Cloud Support Engineer

協助客戶調查、解決有關雲端環境上的疑難雜症,並負責 MSP 客戶專案環境的代管。

參考資料

[1] Compute Admin

https://cloud.google.com/iam/docs/understanding-roles#compute.admin

[2] Compute Security Admin

https://cloud.google.com/iam/docs/understanding-roles#compute.securityAdmin

[3] Compute Network Admin

https://cloud.google.com/iam/docs/understanding-roles#compute.networkAdmin

[4] Compute Engine Overview

https://cloud.google.com/compute

[5] Create a VM instance from an image

https://cloud.google.com/compute/docs/instances/create-start-instance#startinginstancewithimage

[6] Types of security policies

https://cloud.google.com/armor/docs/security-policy-overview#policy-types

[7] Configure security policies for HTTP(S) Load Balancing

https://cloud.google.com/armor/docs/configure-security-policies#create-example-policies

[8] Create a new instance template

https://cloud.google.com/compute/docs/instance-templates/create-instance-templates#create_a_new_instance_template

[9] Create a MIG with VMs in multiple zones in a region

https://cloud.google.com/compute/docs/instance-groups/distributing-instances-with-regional-instance-groups#creating_a_regional_managed_instance_group

[10] Create VPC firewall rules

https://cloud.google.com/vpc/docs/using-firewalls#creating_firewall_rules

[11] Creating health checks

https://cloud.google.com/load-balancing/docs/health-checks#create-hc

[12] How Google Cloud Armor works

https://cloud.google.com/armor/docs/cloud-armor-overview#how_works

[13] Set up the load balancer

https://cloud.google.com/load-balancing/docs/https/ext-https-lb-simple#load-balancer

[14] Set up a global external HTTP(S) load balancer with VM instance group backends

https://cloud.google.com/load-balancing/docs/https/setup-global-ext-https-compute

[15] Google Cloud Armor best practices

https://cloud.google.com/armor/docs/cloud-armor-overview

[16] Google Cloud Armor best practices

https://cloud.google.com/armor/docs/best-practices

訂閱 CloudMile 電子報

所有 CloudMile 最新消息、產品動態、活動資訊和特別優惠,立即掌握。