本頁說明不同類型的負載平衡器和安全性政策的安全性政策設定範例。
設定外部應用程式負載平衡器的安全性政策
以下是設定 Google Cloud Armor 安全性政策的概略步驟,可啟用允許或拒絕流量傳入全域外部應用程式負載平衡器或傳統版應用程式負載平衡器的規則:
- 建立 Google Cloud Armor 安全性政策。
- 根據 IP 位址清單、自訂運算式或預先設定的運算式組合,在安全性政策中新增規則。
- 將安全性政策附加至全域外部應用程式負載平衡器或傳統版應用程式負載平衡器的後端服務,以便控管存取權。
- 視需要更新安全性政策。
在以下範例中,您會建立兩個 Google Cloud Armor 安全性政策,並套用至不同的後端服務。
在本範例中,Google Cloud Armor 安全性政策如下:
mobile-clients-policy
適用於games
服務的外部使用者。internal-users-policy
適用於貴機構的test-network
團隊。
您將 mobile-clients-policy
套用至 games
服務,該服務的後端服務稱為 games
,並將 internal-users-policy
套用至測試團隊的內部 test
服務,該服務的對應後端服務稱為 test-network
。
如果後端服務的後端執行個體位於多個區域,則與該服務相關聯的 Google Cloud Armor 安全性政策就適用於所有區域的執行個體。在上述範例中,安全性政策 mobile-clients-policy
適用於 us-central
中的 1、2、3 和 4 個執行個體,以及 us-east
中的 5 和 6 個執行個體。
建立範例
請按照這些操作說明建立上一節所述的範例設定。
主控台
設定外部使用者的安全性政策:
在 Google Cloud 控制台中,前往「Google Cloud Armor 政策」頁面。
按一下「Create policies」(建立政策)。
在「Name」(名稱) 欄位中輸入
mobile-clients-policy
。在「Description」欄位中輸入
Policy for external users
。在「預設規則動作」中,選取「拒絕」。
在「拒絕狀態」中,選取「404 (找不到)」。
點選「下一步」。
新增更多規則:
- 按一下 [新增規則]。
- 在「Description」欄位中輸入
allow traffic from 192.0.2.0/24
。 - 在「模式」中,選取「基本模式 (僅限 IP 位址/範圍)」。
- 在「Match」欄位中輸入
192.0.2.0/24
。 - 在「動作」部分,選取「允許」。
- 在「Priority」欄位中輸入
1000
。 - 按一下 [完成]。
- 點選「下一步」。
對目標套用政策:
- 點選「新增目標」。
- 在「Target」清單中選取目標。
- 按一下 [完成]。
- 按一下「建立政策」。
您可以視需要啟用 Google Cloud Armor Adaptive Protection:
- 如要啟用 Adaptive Protection,請勾選「Enable」核取方塊。
為內部使用者設定安全性政策:
- 按一下「Create policies」(建立政策)。
- 在「Name」(名稱) 欄位中輸入
internal-users-policy
。 - 在「Description」欄位中輸入
Policy for internal test users
。 - 在「預設規則動作」中,選取「拒絕」。
- 在「拒絕狀態」中,選取「502 (Bad Gateway)」。
- 點選「下一步」。
新增更多規則:
- 按一下 [新增規則]。
- 在「Description」欄位中輸入
allow traffic from 198.51.100.0/24
。 - 在「模式」中,選取「基本模式 (僅限 IP 位址/範圍)」。
- 在「Match」欄位中輸入
198.51.100.0/24
。 - 在「Action」中,選取「Allow」。
- 如要使用「僅預覽」,請勾選「啟用」核取方塊。
- 在「Priority」欄位中輸入
1000
。 - 按一下 [完成]。
- 點選「下一步」。
對目標套用政策:
- 點選「新增目標」。
- 在「Target」清單中選取目標。
- 按一下 [完成]。
- 按一下「建立政策」。
gcloud
建立 Google Cloud Armor 安全性政策:
gcloud compute security-policies create mobile-clients-policy \ --description "policy for external users"
gcloud compute security-policies create internal-users-policy \ --description "policy for internal test users"
更新安全性政策的預設規則,拒絕流量:
gcloud compute security-policies rules update 2147483647 \ --security-policy mobile-clients-policy \ --action "deny-404"
gcloud compute security-policies rules update 2147483647 \ --security-policy internal-users-policy \ --action "deny-502"
在安全性政策中新增規則:
gcloud compute security-policies rules create 1000 \ --security-policy mobile-clients-policy \ --description "allow traffic from 192.0.2.0/24" \ --src-ip-ranges "192.0.2.0/24" \ --action "allow"
gcloud compute security-policies rules create 1000 \ --security-policy internal-users-policy \ --description "allow traffic from 198.51.100.0/24" \ --src-ip-ranges "198.51.100.0/24" \ --action "allow"
將安全性政策附加至後端服務:
gcloud compute backend-services update games \ --security-policy mobile-clients-policy
gcloud compute backend-services update test-network \ --security-policy internal-users-policy
視需要啟用 Adaptive Protection:
gcloud compute security-policies update mobile-clients-policy \ --enable-layer7-ddos-defense
gcloud compute security-policies update internal-users-policy \ --enable-layer7-ddos-defense
建立安全性政策
您可以使用 Google Cloud 主控台或 gcloud CLI 建立安全性政策。本節的操作說明假設您要設定安全性政策,以便套用至現有的全域外部應用程式負載平衡器或傳統應用程式負載平衡器和後端服務。如要瞭解如何填寫這些欄位,請參閱「建立範例」一節。
主控台
建立 Google Cloud Armor 安全性政策和規則,並將安全性政策附加至後端服務:
在 Google Cloud 控制台中,前往「Google Cloud Armor 政策」頁面。
按一下「Create policies」(建立政策)。
在「Name」欄位中輸入政策名稱。
選用:輸入政策說明。
針對「政策類型」,請選擇「後端安全性政策」或「邊緣安全性政策」。
針對「預設規則動作」,如果是允許存取的預設規則,請選取「允許」;如果是禁止存取 IP 位址或 IP 位址範圍的預設規則,請選取「拒絕」。
預設規則的優先順序最低,只有在沒有其他適用規則時才會生效。
如果您要設定「拒絕」規則,請選取「拒絕狀態」訊息。當沒有存取權的使用者嘗試取得存取權時,Google Cloud Armor 會顯示這則錯誤訊息。
無論您要設定的規則類型為何,請點選「下一步」。
新增更多規則:
- 按一下 [新增規則]。
- 選用步驟:輸入規則說明。
選取模式:
- 基本模式:依據 IP 位址或 IP 範圍允許或拒絕流量。
- 進階模式:根據規則運算式允許或拒絕流量。
在「比對」欄位中,指定規則適用的條件:
- 基本模式:在規則中輸入要比對的 IP 位址或 IP 範圍。
- 進階模式:輸入運算式或子運算式,以便針對傳入的請求進行評估。如要瞭解如何編寫運算式,請參閱「設定自訂規則語言屬性」。
在「Action」中,選取「Allow」或「Deny」,即可在規則相符時允許或拒絕流量。
如要啟用預覽模式,請勾選「啟用」核取方塊。在預覽模式中,您可以查看規則的運作方式,但規則不會啟用。
輸入規則的「優先順序」。這個值可以是 0 到 2,147,483,646 之間的任何正整數 (含首尾)。如要進一步瞭解評估順序,請參閱「規則評估順序」。
按一下 [完成]。
如要新增更多規則,請按一下「新增規則」,然後重複上述步驟。否則,請點選「下一步」。
對目標套用政策:
- 點選「新增目標」。
- 在「Target」清單中選取目標。
- 如要新增更多目標,請按一下「新增目標」。
- 按一下 [完成]。
- 按一下「建立政策」。
gcloud
如要建立新的 Google Cloud Armor 安全性政策,請使用
gcloud compute security-policies create
指令。在
type
欄位中,使用CLOUD_ARMOR
建立後端安全性政策,或使用CLOUD_ARMOR_EDGE
建立邊緣安全性政策。type
旗標為選用項目;如果未指定類型,系統會預設建立後端安全性政策:gcloud compute security-policies create NAME \ [--type=CLOUD_ARMOR|CLOUD_ARMOR_EDGE] \ [--file-format=FILE_FORMAT | --description=DESCRIPTION] \ [--file-name=FILE_NAME]
更改下列內容:
NAME
:安全性政策的名稱DESCRIPTION
:安全性政策的說明
以下指令會更新先前建立的政策、啟用 JSON 剖析,並將記錄層級變更為
VERBOSE
:gcloud compute security-policies update my-policy \ --json-parsing=STANDARD \ --log-level=VERBOSE
如要為安全性政策新增規則,請使用
gcloud compute security-policies rules create PRIORITY
指令。gcloud compute security-policies rules create PRIORITY \ [--security-policy POLICY_NAME] \ [--description DESCRIPTION] \ --src-ip-ranges IP_RANGE,... | --expression EXPRESSION \ --action=[ allow | deny-403 | deny-404 | deny-502 ] \ [--preview]
將
PRIORITY
換成在政策中指派給規則的優先順序。如要瞭解規則優先順序的運作方式,請參閱「規則評估順序」。舉例來說,下列指令會新增規則,封鎖來自 IP 位址範圍
192.0.2.0/24
和198.51.100.0/24
的流量。此規則的優先順序為 1000,是名為my-policy
的政策中的規則。gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --description "block traffic from 192.0.2.0/24 and 198.51.100.0/24" \ --src-ip-ranges "192.0.2.0/24","198.51.100.0/24" \ --action "deny-403"
加入
--preview
標記後,系統會將規則新增至政策,但不會強制執行,且只會記錄觸發規則的任何流量。gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --description "block traffic from 192.0.2.0/24 and 198.51.100.0/24" \ --src-ip-ranges "192.0.2.0/24","198.51.100.0/24" \ --action "deny-403" \ --preview
使用
--expression
標記指定自訂條件。詳情請參閱「設定自訂規則語言屬性」。下列指令會新增規則,允許來自 IP 位址1.2.3.4
的流量,並在使用者代理程式標頭中包含字串example
:gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --expression "inIpRange(origin.ip, '1.2.3.4/32') && has(request.headers['user-agent']) && request.headers['user-agent'].contains('example')" \ --action allow \ --description "Block User-Agent 'example'"
以下指令會新增規則,在要求的 Cookie 包含特定值時封鎖要求:
gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --expression "has(request.headers['cookie']) && request.headers['cookie'].contains('cookie_name=cookie_value')" \ --action "deny-403" \ --description "Cookie Block"
下列指令會新增規則,封鎖來自
AU
的請求:gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --expression "origin.region_code == 'AU'" \ --action "deny-403" \ --description "AU block"
下列指令會新增規則,封鎖來自區域
AU
且不在指定 IP 範圍內的請求:gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --expression "origin.region_code == 'AU' && !inIpRange(origin.ip, '1.2.3.0/24')" \ --action "deny-403" \ --description "country and IP block"
下列指令會新增規則,封鎖 URI 與規則運算式相符的要求:
gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --expression "request.path.matches('/example_path/')" \ --action "deny-403" \ --description "regex block"
下列指令會新增規則,如果
user-id
標頭的 Base64 解碼值包含特定值,就會封鎖要求:gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --expression "has(request.headers['user-id']) && request.headers['user-id'].base64Decode().contains('myValue')" \ --action "deny-403" \ --description "country and IP block"
下列指令會新增使用預先設定的運算式集的規則,以減輕 SQLi 攻擊:
gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --expression "evaluatePreconfiguredWaf('sqli-stable')" \ --action "deny-403"
以下指令會新增一項規則,使用預先設定的運算式,允許來自已命名 IP 位址清單的所有 IP 位址存取:
gcloud compute security-policies rules create 1000 \ --security-policy my-policy \ --expression "evaluatePreconfiguredWaf('sourceiplist-fastly')" \ --action "allow"
為區域性外部應用程式負載平衡器設定安全性政策
本節說明如何為區域性外部應用程式負載平衡器設定區域性 Google Cloud Armor 安全性政策。
保護區域性負載平衡的工作負載
請按照下列步驟設定安全性政策,以保護區域性後端服務:
建立區域性安全性政策。
gcloud compute security-policies create POLICY_NAME \ --type=CLOUD_ARMOR \ --region=REGION
將區域範圍的安全性政策附加至區域範圍的後端服務。將
BACKEND_NAME
替換為現有區域範圍後端服務的名稱。gcloud compute backend-services update BACKEND_NAME \ --security-policy=POLICY_NAME \ --region=REGION
套用區域範圍的 Google Cloud Armor 安全性政策
舉例來說,假設您是安全性管理員,想要符合居住地要求,也就是將所有後端工作負載和 WAF 規則部署在特定區域。假設您已事先完成下列操作:
- 您在區域中建立了區域範圍的負載平衡後端服務。
- 您在部署中停用了任何現有的全域範圍安全性政策。
- 您在同一區域中建立並附加區域範圍的安全性政策 (如上一節所述)。
您可以使用下列範例指令,在滿足需求的同時,將 WAF 規則和其他進階規則新增至政策:
在政策中新增 WAF 規則:
gcloud compute security-policies rules create 1000 --action=deny-404 \ --expression="evaluatePreconfiguredWaf('xss-v33-stable', ['owasp-crs-v030301-id941100-xss', 'owasp-crs-v030301-id941160-xss'])" \ --security-policy=POLICY_NAME \ --region=REGION
在政策中新增進階規則:
gcloud compute security-policies rules create 1000 --action=allow \ --expression="has(request.headers['cookie']) && request.headers['cookie'].contains('80=EXAMPLE')" \ --security-policy=POLICY_NAME \ --region=REGION
為政策新增頻率限制規則:
gcloud compute security-policies rules create 1000 --action=throttle \ --src-ip-ranges="1.1.1.1/32" \ --rate-limit-threshold-count=1000 \ --rate-limit-threshold-interval-sec=120 \ --conform-action="allow" \ --exceed-action="deny-429" \ --enforce-on-key=IP \ --ban-duration-sec=999 \ --ban-threshold-count=5000 \ --ban-threshold-interval-sec=60 \ --security-policy=POLICY_NAME \ --region=REGION