按照這些操作說明建立 Google Cloud Armor 安全性政策,以篩選傳入應用程式的流量。如要瞭解安全性政策的概念資訊,請參閱「Cloud Armor 安全性政策總覽」。
事前準備
設定安全性政策前,請先完成下列工作:
請務必熟悉外部應用程式負載平衡器概念。
檢查現有的後端服務,找出尚未附加安全性政策的服務。這些後端服務及其相關聯的後端不受 Cloud Armor 保護。如要新增 Cloud Armor 提供的防護措施,請按照本文的操作說明,將新建立或現有的安全性政策附加至後端服務。
設定 Cloud Armor 安全性政策的 IAM 權限
下列作業需要 Identity and Access Management (IAM) 的 Compute 安全管理員角色 (roles/compute.securityAdmin):
- 設定、修改、更新及刪除 Cloud Armor 安全性政策
- 使用下列 API 方法:
SecurityPolicies insertSecurityPolicies deleteSecurityPolicies patchSecurityPolicies addRuleSecurityPolicies patchRuleSecurityPolicies removeRule
具備 Compute 網路管理員角色 (roles/compute.networkAdmin) 的使用者可以執行下列作業:
- 為後端服務設定 Cloud Armor 安全性政策
- 使用下列 API 方法:
BackendServices setSecurityPolicyBackendServices list(僅限gcloud)
具有安全管理員角色 (roles/iam.securityAdmin) 和 Compute 網路管理員角色 (roles/compute.networkAdmin) 的使用者,可以使用 SecurityPolicies API 方法 get、list 和 getRule 查看 Cloud Armor 安全性政策。
設定自訂角色的 IAM 權限
下表列出 IAM 角色的基本權限、相關聯的 API 方法,以及授予該權限的角色。
| IAM 權限 | API 方法 | 角色 |
|---|---|---|
compute.securityPolicies.create |
SecurityPolicies insert |
Compute 安全管理員 (roles/compute.securityAdmin) |
compute.securityPolicies.delete |
SecurityPolicies delete |
Compute 安全管理員 (roles/compute.securityAdmin) |
compute.securityPolicies.get |
SecurityPolicies getSecurityPolicies getRule |
安全管理員 (roles/iam.securityAdmin) |
compute.securityPolicies.list |
SecurityPolicies list |
安全管理員 (roles/iam.securityAdmin) |
符合下列兩項條件:
|
BackendServices |
Compute 網路管理員 (roles/compute.networkAdmin) |
compute.securityPolicies.update |
SecurityPolicies patchSecurityPolicies addRuleSecurityPolicies patchRuleSecurityPolicies removeRule |
Compute 安全管理員 (roles/compute.securityAdmin) |
建立安全性政策
您可以使用 Google Cloud 控制台、Google Cloud CLI 或 REST API,設定 Cloud Armor 安全性政策、規則和運算式。使用 gcloud CLI 建立安全性政策時,請使用 --type 旗標指定安全性政策是後端安全性政策還是邊緣安全性政策。
如果您不熟悉安全性政策設定,建議查看安全性政策範例。
運算式範例
以下是運算式範例。如要進一步瞭解運算式,請參閱 Cloud Armor 自訂規則語言參考資料。
如果您要設定的規則或運算式使用 ISO 3166-1 alpha 2 國家/地區代碼,Cloud Armor 會個別處理每個代碼。Cloud Armor 規則和運算式會明確使用這些區域代碼,允許或拒絕要求。
下列運算式會比對來自 IP 位址
1.2.3.4的要求,並在使用者代理程式標頭中包含字串example:inIpRange(origin.ip, '1.2.3.4/32') && has(request.headers['user-agent']) && request.headers['user-agent'].contains('example')或者,您也可以使用
origin.user_ip屬性,比對自訂用戶端 IP 位址標頭的 IP 位址範圍:inIpRange(origin.user_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('/path/[0-9]+/target_file.html')如果
user-id標頭的 Base64 解碼值包含特定值,下列運算式就會比對要求:has(request.headers['user-id']) && request.headers['user-id'].base64Decode().contains('myValue')下列運算式會使用預先設定的運算式集,比對 SQLi 攻擊:
evaluatePreconfiguredWaf('sqli-stable')
路徑遍歷和正規化
在以路徑為準的規則中,將內容正規化後,您就能直接管理路徑遍歷和編碼規則,並建立一般化規則,避免大小寫或內容專屬編碼。
撰寫使用 startsWith、endsWith 或 contains 比對 request.path 的規則時,建議您先使用 lower 和 urlDecode 正規化路徑,再進行比對。這有助於防止使用網址編碼或大小寫變化的規避行為。
舉例來說,如要封鎖 /admin 目錄的存取權,可以使用下列運算式:
request.path.lower().urlDecode().startsWith('/admin')
這項規則會封鎖 /admin、/Admin、/a%64min 和其他變體的請求。
撰寫使用 matches 比對 request.path 的規則時,建議您在規則運算式中也將反斜線字元 () 納入考量。
舉例來說,如要封鎖 /admin 目錄的存取權,可以使用下列運算式,封鎖使用反斜線字元 () 的繞過行為:
request.path.matches(r'^/\\*admin')
您也可以將此與 urlDecode 結合,同時處理網址編碼和反斜線字元 () 略過:
request.path.urlDecode().matches(r'^/\\*admin')
管理安全性政策
請參閱下列各節,瞭解如何列出專案中的安全性政策、更新安全性政策、刪除安全性政策,或測試安全性政策。
列出安全性政策
按照這些操作說明,列出目前專案或指定專案中的所有 Cloud Armor 安全性政策。
控制台
- 前往 Google Cloud 控制台的「Cloud Armor policies」(Cloud Armor 政策) 頁面。
- 如要查看特定政策,請前往「安全政策」頁面,然後在政策清單中點選該政策的名稱。
gcloud
gcloud compute security-policies list
例如:
gcloud compute security-policies list
輸出內容:
NAME: my-policy REGION: us-central1
更新安全性政策
按照這些操作說明更新 Cloud Armor 安全性政策。舉例來說,您可以修改政策說明、修改預設規則的行為、變更目標後端服務,或新增規則。
控制台
- 前往 Google Cloud 控制台的「Cloud Armor policies」(Cloud Armor 政策) 頁面。
如要更新特定政策,請前往「安全政策」頁面,在政策清單中點按要更新政策的「選單」。
- 如要更新預設規則動作的政策說明,請選取「編輯」,進行變更,然後按一下「更新」。
- 如要新增規則,請選取「新增規則」,然後按照「為安全性政策新增規則」中的指示操作。
- 如要變更政策的目標後端服務,請選取「Apply policy to target」(將政策套用至目標),按一下「Add Target」(新增目標),選取目標,然後按一下「Add」(新增)。
gcloud
如要更新安全性政策,請按照下列 Google Cloud CLI 指示操作:
- 如要更新安全性政策中的規則,請參閱「更新安全性政策中的單一規則」。
- 如要為安全性政策新增規則,請參閱「為安全性政策新增規則」。
- 如要從安全性政策中刪除規則,請參閱「從安全性政策中刪除規則」。
- 如要透過單一更新 (不可分割的更新) 更新多項規則,請參閱「在安全性政策中不可分割地更新多項規則」。
- 如要更新安全性政策中的非規則欄位 (例如說明欄位),請參閱「匯出安全性政策」和「匯入安全性政策」。
刪除安全性政策
按照這些操作說明刪除 Cloud Armor 安全性政策。您必須先從政策中移除所有後端服務,才能刪除政策。
控制台
- 前往 Google Cloud 控制台的「Cloud Armor policies」(Cloud Armor 政策) 頁面。
- 在「安全性政策」頁面中,選取要刪除的安全性政策名稱旁邊的核取方塊。
- 按一下頁面右上角的「刪除」圖示 。
gcloud
使用 gcloud compute security-policies delete NAME。將 NAME 替換為安全政策名稱:
gcloud compute security-policies delete NAME
測試安全性政策
建議您先以預覽模式部署所有新規則,然後檢查要求記錄,確認政策和規則的運作方式符合預期。
管理安全性政策規則
請參閱下列各節,瞭解如何列出、新增、更新或刪除安全性政策規則。
列出安全性政策中的規則
按照這些操作說明,列出 Cloud Armor 安全性政策中的規則。
控制台
- 前往 Google Cloud 控制台的「Cloud Armor policies」(Cloud Armor 政策) 頁面。
- 在「安全性政策」頁面上,按一下安全性政策的名稱。系統會顯示「Policy details」(政策詳細資料) 頁面,政策規則則會列在頁面中間的「Rules」(規則) 分頁標籤中。
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 安全性政策中新增規則。
控制台
- 前往 Google Cloud 控制台的「Cloud Armor policies」(Cloud Armor 政策) 頁面。
- 在「安全性政策」頁面上,按一下安全性政策的名稱。系統隨即會顯示「政策詳細資料」頁面。
- 按一下頁面中間的「規則」分頁標籤。
- 按一下「Add rule」(新增規則)。
- 選用:輸入規則說明。
選取模式:
- 基本模式:根據 IP 位址或 IP 範圍允許或拒絕流量。
- 進階模式:根據規則運算式允許或拒絕流量。
在「比對」欄位中,指定規則的適用條件:
基本模式:輸入一 (1) 到十 (10) 個要比對的 IP 位址範圍。最多可新增 10 個 IP 位址範圍。如需相關限制,請參閱「Cloud Armor 配額與限制」。
進階模式:
- 比對條件建構工具 (預覽版): 使用視覺化建構工具建立運算式,不必編寫原始 CEL 程式碼。您可以新增條件、將條件分組,以及套用字串轉換。詳情請參閱「使用相符條件建構工具」。
- 比對條件編輯器:輸入運算式或子運算式,以便評估傳入的要求。如要瞭解如何編寫運算式及解讀下列範例,請參閱自訂規則語言參考資料。
下列運算式會比對來自 IP 位址
1.2.3.4的要求,並在使用者代理程式標頭中包含字串example:
inIpRange(origin.ip, '1.2.3.4/32') && has(request.headers['user-agent']) && request.headers['user-agent'].contains('example')
* The following expression matches against requests that have a cookie
with a specific value:
has(request.headers['cookie']) && request.headers['cookie'].contains('cookie_name=cookie_value')
* The following expression matches against requests from the region
`AU`:
origin.region_code == 'AU'
* The following expression matches against requests from the region
`AU` that are not in the specified IP range:
origin.region_code == "AU" && !inIpRange(origin.ip, '1.2.3.0/24')
* The following expression matches against requests if the URI matches
a regular expression:
request.path.matches('/example_path/')
* The following expression matches against requests if the Base64
decoded value of the `user-id` header contains a specific value:
has(request.headers['user-id']) && request.headers['user-id'].base64Decode().contains('myValue')
* The following expression uses a preconfigured expression set to
match against SQLi attacks:
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
舉例來說,下列指令會新增規則,封鎖來自 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"
使用 --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('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 安全性政策中的單一規則。如要以原子方式更新多項規則,請參閱「以原子方式更新安全性政策中的多項規則」。
控制台
- 前往 Google Cloud 控制台的「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。
使用比對條件建構工具
比對條件建構工具是 Google Cloud 控制台中的視覺化介面,您可以使用這個工具建立複雜的一般運算語言 (CEL) 運算式,不必編寫原始程式碼。建構工具會透過結構化 UI 呈現邏輯,並支援分組、平面清單和深層巢狀邏輯。設定規則來保護多雲端後端或混合式部署時,這項工具特別實用,因為您可能需要先檢查特定要求屬性,再將流量轉送至外部端點。
比對條件建構工具的功能
- 邏輯和巢狀結構:您可以使用
AND和OR運算子合併多個條件。製作工具支援單一分組和巢狀層級,最多可有五個運算式。 - 字串轉換:針對以字串為基礎的屬性 (例如
request.path或request.headers),您可以套用轉換作業,在評估前將資料標準化。支援的轉換包括:- 小寫和大寫
- Base64 解碼
- 網址解碼和網址解碼 (Unicode)
- UTF-8 轉 Unicode
- 雙向剖析:您可以在視覺化比對條件建構工具和原始文字比對條件編輯器之間切換。使用者介面會嘗試將原始 CEL 程式碼自動剖析為視覺化區塊。
- 函式支援:建構工具提供特定 UI 控制項,適用於以函式為基礎的運算式,例如預先設定的 WAF 規則、位址群組和威脅情報。
限制
比對條件建構工具是建立進階規則的預設介面。不過,如果運算式含有不支援的邏輯 (例如不明巨集),建構工具就會停用。在這種情況下,您必須使用相符條件編輯器編輯規則。
此外,建構工具中某些運算式的可用性,取決於專案的 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 安全性政策中刪除規則。
控制台
- 前往 Google Cloud 控制台的「Google Cloud Armor policies」(Google 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 安全性政策附加至後端服務。安全性政策可附加至多個後端服務,但每個後端服務只能附加一項各類型的安全性政策。
控制台
- 前往 Google Cloud 控制台的「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 後端安全性政策或邊緣安全性政策。
控制台
- 前往 Google Cloud 控制台的「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 ""
將安全性政策附加至後端 bucket
按照這些操作說明,將 Cloud Armor 邊緣安全性政策附加至後端 bucket。邊緣安全政策可附加至多個後端 bucket。
控制台
- 前往 Google Cloud 控制台的「Cloud Armor policies」(Cloud Armor 政策) 頁面。
- 在「安全性政策」頁面上,按一下安全性政策的名稱。系統隨即會顯示「政策詳細資料」頁面。
- 按一下頁面中間的「目標」分頁標籤。
- 按一下「對新目標套用政策」。
- 點選「新增目標」。
- 在「目標」清單中選取目標,然後按一下「新增」。
gcloud
將邊緣安全政策附加至後端 bucket 時,請使用 cloud
compute backend-buckets 指令和 --edge-security-policy 旗標:
gcloud compute backend-services update my-bucket \
--edge-security-policy my-edge-policy
從後端 bucket 移除安全性政策
按照這些操作說明,從後端 bucket 移除 Cloud Armor 邊緣安全性政策。
控制台
- 前往 Google Cloud 控制台的「Cloud Armor policies」(Cloud Armor 政策) 頁面。
- 在「安全性政策」頁面上,按一下安全性政策的名稱。系統隨即會顯示「政策詳細資料」頁面。
- 按一下頁面中間的「目標」分頁標籤。
- 選取要移除政策的目標後端服務。
- 按一下 [移除]。
- 在「移除目標」訊息中,按一下「移除」。
gcloud
從後端 bucket 移除邊緣安全政策時,請使用 cloud compute backend-buckets 指令和 --edge-security-policy 旗標:
gcloud compute backend-services update my-bucket \
--edge-security-policy ""
匯入及匯出安全性政策
請參閱下列章節,以 YAML 或 JSON 檔案格式匯入及匯出安全性政策。
匯出安全性政策
您可以使用 Google Cloud CLI,將 Cloud Armor 安全性政策匯出為 YAML 或 JSON 檔案。這項功能可帶來下列好處:
儲存副本以進行版本控管或備份:匯出政策後,您會取得政策的本機副本。您可以將這個副本儲存在原始碼控制系統中做為備份,或追蹤一段時間內的變更。日後如有需要,可以重新匯入,還原先前的政策版本。
在本機修改及更新政策:您可以使用偏好的文字編輯器,離線修改匯出的政策。變更完成後,請重新匯入更新後的政策,將修改內容套用至 Cloud Armor。
gcloud
- 在下列指令中,
NAME是安全政策的名稱。有效檔案格式為 YAML 和 JSON。如果未提供檔案格式,Cloud Armor 會使用預設的 YAML。
gcloud compute security-policies export NAME \
--file-name FILE_NAME \
--file-format FILE_FORMAT
以下範例會以 YAML 格式將 my-policy 安全性政策匯出至 my-file 檔案:
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: allow
description: default rule
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
- 您可以使用任何文字編輯器修改匯出的檔案,然後使用
import指令將檔案匯入 Google Cloud 。
匯入安全性政策
您可以使用 Google Cloud CLI,從 YAML 或 JSON 檔案匯入 Cloud Armor 安全性政策。您無法使用 import 指令更新現有政策的規則。您必須改用「在安全性政策中更新單一規則」程序逐一更新規則,或使用「在安全性政策中以原子方式更新多個規則」程序一次更新所有規則。
gcloud
如要匯入安全性政策,請使用 gcloud compute security-policies
import NAME 指令。將 NAME 替換為要匯入的安全政策名稱。如未提供檔案格式,系統會根據檔案結構假設正確格式。如果結構無效,您會看到錯誤訊息。
gcloud compute security-policies import NAME \
--file-name FILE_NAME \
[--file-format FILE_FORMAT]
舉例來說,下列指令會匯入 my-file 檔案,藉此更新 my-policy 政策。
gcloud compute security-policies import my-policy \
--file-name my-file \
--file-format json
如果匯入政策時,政策的指紋已過期,Cloud Armor 會顯示錯誤。這表示政策在您上次匯出後已修改。如要修正這個問題,請對政策使用 describe 指令,取得最新指紋。合併所述政策與您的政策之間的差異,然後以最新指紋取代過時的指紋。
列出可用的預先設定規則
列出預先設定的規則,查看預先定義的應用程式保護規則和簽章,例如 Cloud Armor 提供的 OWASP 核心規則集。這些預先設定的規則包含多個內建簽章,Cloud Armor 會用來評估傳入的要求。您可以使用自訂規則語言參考資料,將這些預先設定的規則新增至新規則或現有規則。
詳情請參閱預先設定的規則。
gcloud
- 執行
gcloud compute security-policies list-preconfigured-expression-sets指令:
gcloud compute security-policies list-preconfigured-expression-sets
以下範例顯示指令的輸出格式:
EXPRESSION_SET
expression-set-1
RULE_ID SENSITIVITY
expression-set-1-id-1 sensitivity-value-1
expression-set-1-id-2 sensitivity-value-2
expression-set-2
alias-1
RULE_ID SENSITIVITY
expression-set-2-id-1 sensitivity-value-1
expression-set-2-id-2 sensitivity-value-2
以下範例包含指令的實際輸出內容範例。 請注意,實際輸出內容會包含「調整 Cloud Armor 網路應用程式防火牆規則」中列出的所有規則。
gcloud compute security-policies list-preconfigured-expression-sets
EXPRESSION_SET
sqli-canary
RULE_ID SENSITIVITY
owasp-crs-v042200-id942120-sqli 2
…
xss-canary
RULE_ID SENSITIVITY
owasp-crs-v042200-id941110-xss 1
owasp-crs-v042200-id941120-xss 2
…
sourceiplist-fastly
sourceiplist-cloudflare
sourceiplist-imperva