本頁說明如何管理 Google Cloud Armor 安全性政策,以及篩選傳入應用程式的流量。Cloud Armor 安全性政策可保護您的網頁應用程式和服務,不受網際網路的分散式阻斷服務 (DDoS) 攻擊及其他威脅侵擾。如要強制執行嚴格的資安態勢,您必須建立、測試及整合安全政策,藉此管理這些政策。如要建立或查看安全性政策,請參閱「建立及查看 Cloud Armor 安全性政策」。
管理安全性政策
下列各節說明如何列出、更新、刪除或測試安全性政策。
列出安全性政策
請按照本節的步驟,列出專案中的 Cloud Armor 安全性政策。
控制台
前往 Cloud de Confiance 控制台的「Cloud Armor policies」(Cloud Armor 政策) 頁面。
如要查看政策,請在政策清單中按一下該政策的名稱。
gcloud
gcloud compute security-policies list
例如:
gcloud compute security-policies list
輸出內容:
NAME: my-policy REGION: us-central1
更新安全性政策
本節的步驟說明如何更新 Cloud Armor 安全性政策。您可以修改政策說明、修改預設規則、變更目標後端服務或新增規則。
控制台
前往 Cloud de Confiance 控制台的「Cloud Armor policies」(Cloud Armor 政策) 頁面。
在政策清單中找出要更新的政策名稱,然後點按該政策的「選單」圖示 。
- 如要更新說明或預設規則動作,請選取「編輯」,進行變更,然後按一下「更新」。
- 如要新增規則,請選取「新增規則」,然後按照「將規則新增至安全性政策」一文中的步驟操作。
- 如要取代目標後端服務,請選取「對目標套用政策」,點選「新增目標」,選取目標,然後點選「新增」。
gcloud
如要更新安全性政策,請按照下列 Google Cloud CLI 步驟操作:
- 如要更新規則,請參閱「更新安全性政策中的單一規則」。
- 如要新增規則,請參閱「為安全性政策新增規則」。
- 如要刪除規則,請參閱「從安全性政策中刪除規則」。
- 如要透過單一不可分割的更新作業更新多項規則,請參閱「透過不可分割的更新作業更新安全性政策中的多項規則」。
- 如要更新非規則欄位 (例如說明),請參閱「匯出安全性政策」和「匯入安全性政策」。
刪除安全性政策
本節說明如何刪除 Cloud Armor 安全性政策。 請先移除政策中的所有後端服務,再刪除政策。
控制台
前往 Cloud de Confiance 控制台的「Cloud Armor policies」(Cloud Armor 政策) 頁面。
在要刪除的政策旁,選取核取方塊。
按一下頁面右上角的「刪除」圖示 。
gcloud
使用 gcloud compute security-policies delete NAME。將 NAME 替換為安全政策名稱:
gcloud compute security-policies delete NAME
測試安全性政策
建議您先在預覽模式中部署所有新規則,然後檢查要求記錄,確認政策和規則的運作方式符合預期。
管理安全性政策規則
下列各節說明如何列出、新增、更新或刪除安全性政策規則。
列出安全性政策中的規則
請按照本節中的步驟,列出 Cloud Armor 安全性政策中的規則。
控制台
前往 Cloud de Confiance 控制台的「Cloud Armor policies」(Cloud Armor 政策) 頁面。
按一下特定安全性政策的名稱。「政策詳細資料」頁面的「規則」分頁標籤會顯示政策規則。
gcloud
使用下列 gcloud 指令,列出安全政策中的所有規則:
gcloud compute security-policies describe NAME \
使用下列 gcloud 指令,列出指定區域中單一安全性政策的所有規則,以及政策說明:
gcloud compute security-policies describe NAME \ --region REGION
使用下列 gcloud 指令說明規則:
gcloud compute security-policies rules describe PRIORITY \
--security-policy POLICY_NAME
舉例來說,下列指令會說明安全性政策 my-policy 中優先順序為 1000 的規則:
gcloud compute security-policies rules describe 1000 \
--security-policy my-policy
輸出內容:
action: deny(403) description: block traffic from 192.0.2.0/24 and 198.51.100.0/24 kind: compute#securityPolicyRule match: srcIpRanges: - '192.0.2.0/24' - '198.51.100.0/24' preview: false priority: 1000
在安全性政策中新增規則
按照本節中的步驟,將規則新增至 Cloud Armor 安全性政策。
控制台
前往 Cloud de Confiance 控制台的「Cloud Armor policies」(Cloud Armor 政策) 頁面。
按一下安全性政策名稱,開啟「政策詳細資料」頁面。
按一下頁面中間的「規則」分頁標籤。
按一下「Add rule」(新增規則)。
選用:輸入規則說明。
選取模式:
- 基本模式:根據 IP 位址或 IP 範圍允許或拒絕流量。
- 進階模式:根據規則運算式允許或拒絕流量。
在「比對」欄位中,指定規則適用的條件:
基本模式:輸入一 (1) 到 10 個要比對規則的 IP 位址範圍。最多可新增 10 個 IP 位址範圍。如需限制,請參閱「Cloud Armor 配額與限制」。
進階模式:
- 比對條件建構工具 (預覽版): 使用視覺化建構工具建立運算式,不必編寫原始 CEL 程式碼。合併及分組條件,並套用字串轉換。請參閱「使用比對條件建構工具」。
- 比對條件編輯器:輸入運算式,根據傳入的要求進行評估。詳情請參閱自訂規則語言參考資料。
下列運算式會比對來自 IP 位址
1.2.3.4的要求,這些要求在User-Agent標頭中包含字串example:inIpRange(origin.ip, '1.2.3.4/32') && has(request.headers['user-agent']) && request.headers['user-agent'].contains('example')下列運算式會比對含有特定值的 Cookie 的要求:
has(request.headers['cookie']) && request.headers['cookie'].contains('cookie_name=cookie_value')下列運算式會比對來自
AU區域的要求:origin.region_code == 'AU'下列運算式會比對來自
AU區域的要求,但不在指定的 IP 範圍內:origin.region_code == "AU" && !inIpRange(origin.ip, '1.2.3.0/24')如果 URI 符合規則運算式,下列運算式就會比對要求:
request.path.matches('/example_path/')如果
user-id標頭包含 Base64 編碼值,且解碼後包含特定字串,下列運算式就會比對要求:has(request.headers['user-id']) && request.headers['user-id'].base64Decode().contains('myValue')下列運算式使用預先設定的運算式集,比對 SQLi 攻擊:
evaluatePreconfiguredWaf('sqli-stable')
在「動作」清單中,選取「允許」或「拒絕」。
如果您要設定拒絕規則,請選取「拒絕狀態」訊息。
如要啟用規則的預覽模式,請選取「啟用」。
在「Priority」欄位中輸入正整數。
按一下「新增」。
gcloud
使用 gcloud compute security-policies rules create
PRIORITY 指令。將 PRIORITY 替換為政策中規則的優先順序:
gcloud compute security-policies rules create PRIORITY \
--security-policy POLICY_NAME \
--description DESCRIPTION \
--src-ip-ranges IP_RANGES | --expression EXPRESSION \
--action=[ allow | deny-403 | deny-404 | deny-502 ] \
--preview
舉例來說,下列指令會新增優先順序為 1000 的規則,封鎖來自 IP 位址範圍 192.0.2.0/24 和 198.51.100.0/24 的流量:
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"
使用 --expression 標記指定條件。下列指令會新增規則,允許來自 IP 位址 1.2.3.4 的流量,該位址在 User-Agent 標頭中包含字串 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('80=BLAH')" \
--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-502 \
--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
更新安全性政策中的單一規則
本節說明如何更新 Cloud Armor 安全性政策中的單一規則。如要以原子方式更新多項規則,請參閱「以原子方式更新安全性政策中的多項規則」。
控制台
前往 Cloud de Confiance 控制台的「Cloud Armor policies」(Cloud Armor 政策) 頁面。
按一下安全性政策名稱,開啟「政策詳細資料」頁面。
按一下頁面中間的「規則」分頁標籤。
找到要更新的規則,然後按一下「編輯」。系統會顯示「編輯規則」頁面。
進行變更,然後按一下「更新」。
gcloud
使用下列指令更新規則。一次只能更新一項安全性政策:
gcloud compute security-policies rules update PRIORITY [ \
--security-policy POLICY_NAME \
--description DESCRIPTION \
--src-ip-ranges IP_RANGES | --expression EXPRESSION \
--action=[ allow | deny-403 | deny-404 | deny-502 ] \
--preview
]
舉例來說,下列指令會更新優先順序為 1111 的規則,允許來自 IP 位址範圍 192.0.2.0/24 的流量:
gcloud compute security-policies rules update 1111 \
--security-policy my-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
update。
如要更新規則的優先順序,請使用 REST API。請參閱「securityPolicies.patchRule」。
使用比對條件建構工具
比對條件建構工具是 Cloud de Confiance 控制台中的視覺化介面,可協助您建立複雜的 Cloud Armor 規則運算式,完全不需要編寫一般運算語言 (CEL) 程式碼。您不必編寫程式碼,只要使用規則建立工具的結構化 UI,就能合併條件和群組邏輯,避免語法錯誤,並更清楚瞭解規則結構。建構工具支援平面清單和巢狀邏輯,最多可達五層運算式。設定規則時,請使用這項工具,協助保護多雲後端或混合式部署,您可能需要先檢查特定要求屬性,再轉送流量。
比對條件建構工具的功能
比對條件建構工具包含下列功能:
- 邏輯和巢狀結構:使用
AND和OR運算子合併多個條件。建構工具支援最多五個運算式的分組和巢狀結構。 - 字串轉換:針對以字串為基礎的屬性 (例如
request.path或request.headers),請先套用轉換來正規化資料,再進行評估。支援的轉換包括:- 小寫和大寫
- Base64 解碼
- 網址解碼和網址解碼 (Unicode)
- 將 UTF-8 轉換為 Unicode
- 雙向剖析:在視覺化比對條件建構工具和原始文字比對條件編輯器之間切換。使用者介面會自動將原始 CEL 程式碼剖析為視覺化方塊。
- 函式支援:建構工具提供函式型運算式的 UI 控制項,例如預先設定的 WAF 規則、位址群組和威脅情報。
限制
比對條件建構工具是建立進階規則的預設介面。如果運算式含有不支援的邏輯 (例如不明巨集),系統會停用建構工具。在這些情況下,請使用比對條件編輯器中的原始文字編輯規則。
此外,建構工具中某些運算式的可用性取決於專案的 Google Cloud Armor Enterprise 級別和安全性政策類型。
在安全性政策中以原子方式更新多項規則
您可以使用原子更新功能,在單一更新中對多項規則套用變更。更新單一規則可能會導致非預期行為 (例如錯誤),因為新舊規則可能會在短時間內重疊。
如要以原子方式更新多項規則,請將目前的安全性政策匯出為 JSON 或 YAML 檔案,然後進行修改。使用修改後的檔案建立安全性政策,然後切換後端服務的安全性政策。
gcloud
匯出要更新的政策,如下列範例所示:
gcloud compute security-policies export my-policy \ --file-name my-file \ --file-format yaml匯出的政策會類似以下範例:
description: my description fingerprint: PWfLGDWQDLY= id: '123' name: my-policy rules: - action: deny(404) description: my-rule-1 match: expr: expression: evaluatePreconfiguredWaf('xss-stable') versionedExpr: SRC_IPS_V1 preview: false priority: 1 - action: allow description: my-rule-2 match: config: srcIpRanges: - '1.2.3.4' versionedExpr: SRC_IPS_V1 preview: false priority: 2 - action: deny description: default rule kind: compute#securityPolicyRule match: config: srcIpRanges: - '*' versionedExpr: SRC_IPS_V1 preview: false priority: 2147483647 selfLink: https://www.googleapis.com/compute/v1/projects/my-project/global/securityPolicies/my-policy使用文字編輯器修改政策。舉例來說,您可以修改現有規則的優先順序,並新增規則:
description: my description fingerprint: PWfLGDWQDLY= id: '123' name: my-policy rules: - action: deny(404) description: my-rule-1 match: expr: expression: evaluatePreconfiguredWaf('xss-stable') versionedExpr: SRC_IPS_V1 preview: false priority: 1 - action: allow description: my-new-rule match: config: srcIpRanges: - '1.2.3.1' versionedExpr: SRC_IPS_V1 preview: false priority: 10 - action: allow description: my-rule-2 match: config: srcIpRanges: - '1.2.3.4' versionedExpr: SRC_IPS_V1 preview: false priority: 11 - action: deny description: default rule kind: compute#securityPolicyRule match: config: srcIpRanges: - '*' versionedExpr: SRC_IPS_V1 preview: false priority: 2147483647 selfLink: https://www.googleapis.com/compute/v1/projects/my-project/global/securityPolicies/my-policy建立新的 Cloud Armor 安全性政策,並指定修改後的檔案名稱和格式,如下列範例所示:
gcloud compute security-policies create new-policy \ --file-name modified-policy \ --file-format yaml從相關後端服務中移除舊安全性政策,如下列範例所示:
gcloud compute backend-services update my-backend \ --security-policy ""將新的安全性政策新增至後端服務,如下列範例所示:
gcloud compute backend-services update my-backend \ --security-policy new-policy如果未使用舊政策,請刪除:
gcloud compute security-policies delete my-policy
刪除安全性政策中的規則
請按照本節中的步驟,從 Cloud Armor 安全性政策刪除規則。
控制台
前往 Cloud de Confiance 控制台的「Cloud Armor policies」(Cloud Armor 政策) 頁面。
按一下安全性政策名稱,開啟「政策詳細資料」頁面。
在頁面中間的「規則」分頁中,找出要刪除的規則,勾選旁邊的核取方塊。
按一下 「Delete」(刪除)。
gcloud
使用下列指令移除規則。一次只能修改一項安全性政策,但可以一次刪除多項規則:
gcloud compute security-policies rules delete PRIORITY [...] [
--security-policy POLICY_NAME \
]
例如:
gcloud compute security-policies rules delete 1000 \
--security-policy my-policy
附加及移除安全性政策
下列各節說明如何附加及移除安全性政策。
將安全性政策附加至後端服務
請按照本節的步驟,將 Cloud Armor 安全性政策附加至後端服務。您可以將安全性政策附加至多項後端服務,但每項後端服務只能附加一項各類型的安全性政策。
控制台
前往 Cloud de Confiance 控制台的「Cloud Armor policies」(Cloud Armor 政策) 頁面。
按一下安全性政策名稱,開啟「政策詳細資料」頁面。
按一下頁面中間的「目標」分頁標籤。
按一下「對新目標套用政策」。
點選「新增目標」。
在「目標」清單中選取目標,然後按一下「新增」。
gcloud
將後端安全政策附加至後端服務時,請使用 gcloud compute backend-services 指令和 --security-policy 旗標:
gcloud compute backend-services update my-backend \
--security-policy my-policy
將邊緣安全性政策附加至後端服務時,請使用 gcloud compute backend-services 指令和 --edge-security-policy 旗標:
gcloud compute backend-services update my-backend \
--edge-security-policy my-policy
從後端服務移除安全性政策
請按照本節中的步驟,從後端服務移除 Cloud Armor 安全性政策。
控制台
前往 Cloud de Confiance 控制台的「Cloud Armor policies」(Cloud Armor 政策) 頁面。
按一下安全性政策名稱,開啟「政策詳細資料」頁面。
按一下頁面中間的「目標」分頁標籤。
選取要移除政策的目標後端服務。
按一下 [移除]。
在「移除目標」訊息中,按一下「移除」。
gcloud
如要移除後端安全政策,請使用 gcloud compute
backend-services 指令和 --security-policy 旗標:
gcloud compute backend-services update my-backend \
--security-policy ""
如要移除邊緣安全政策,請使用 gcloud compute
backend-services 指令和 --edge-security-policy 旗標:
gcloud compute backend-services update my-backend \
--edge-security-policy ""