本頁面提供有關設定 Google Cloud Armor 安全性政策規則以管理機器人的相關資訊。設定機器人管理功能前,請先熟悉機器人管理總覽中的資訊。
事前準備
以下各節說明設定 Google Cloud Armor 安全性政策時,所需的所有 Identity and Access Management (IAM) 角色和權限。針對本文的用途,您只需要 compute.securityPolicies.create
和 compute.securityPolicies.update
權限。
為 Google Cloud Armor 安全性政策設定 IAM 權限
下列作業需要具備身分與存取權管理 (IAM) Compute 安全性管理員角色 (roles/compute.securityAdmin
):
- 設定、修改、更新及刪除 Google Cloud Armor 安全性政策
- 使用下列 API 方法:
SecurityPolicies insert
SecurityPolicies delete
SecurityPolicies patch
SecurityPolicies addRule
SecurityPolicies patchRule
SecurityPolicies removeRule
具備 Compute 網路管理員角色 (roles/compute.networkAdmin
) 的使用者可以執行下列操作:
- 為後端服務設定 Google Cloud Armor 安全性政策
- 使用下列 API 方法:
BackendServices setSecurityPolicy
BackendServices list
(僅限gcloud
)
具有安全性管理員角色 (roles/iam.securityAdmin
)和 Compute Network 管理員角色的使用者,可以使用 SecurityPolicies
API 方法 get
、list
和 getRule
查看 Google Cloud Armor 安全性政策。
設定自訂角色的 IAM 權限
下表列出 IAM 角色的基本權限,以及相關的 API 方法。
IAM 權限 | API 方法 |
---|---|
compute.securityPolicies.create |
SecurityPolicies insert |
compute.securityPolicies.delete |
SecurityPolicies delete |
compute.securityPolicies.get |
SecurityPolicies get SecurityPolicies getRule |
compute.securityPolicies.list |
SecurityPolicies list |
compute.securityPolicies.use |
BackendServices setSecurityPolicy |
compute.securityPolicies.update |
SecurityPolicies patch SecurityPolicies addRule SecurityPolicies patchRule SecurityPolicies removeRule |
compute.backendServices.setSecurityPolicy |
BackendServices setSecurityPolicy |
使用 reCAPTCHA 手動驗證問題,區分人類或自動化客戶端
如要將 reCAPTCHA 與 Google Cloud Armor 搭配使用,您必須將 CHALLENGEPAGE
類型的 reCAPTCHA WAF 網站金鑰 (reCAPTCHA 金鑰) 與安全性政策建立關聯。如要進一步瞭解 reCAPTCHA 金鑰,請參閱 reCAPTCHA 金鑰總覽。
如要將 reCAPTCHA 金鑰與安全政策建立或解除關聯,請使用下列指令:
gcloud compute security-policies update SECURITY_POLICY \ --recaptcha-redirect-site-key SITE_KEY
更改下列內容:
SECURITY_POLICY
:安全性政策的名稱SITE_KEY
:您的 reCAPTCHACHALLENGEPAGE
金鑰
關聯 reCAPTCHA 金鑰
以下範例會將 reCAPTCHA 金鑰與安全性政策建立關聯。關聯的 reCAPTCHA 金鑰會套用至在特定安全政策下使用手動挑戰功能的所有規則。
gcloud compute security-policies update SECURITY_POLICY \ --recaptcha-redirect-site-key "SITE_KEY"
取消關聯 reCAPTCHA 金鑰
如要將 reCAPTCHA 金鑰與安全政策解除關聯,請使用以下指令:
gcloud compute security-policies update SECURITY_POLICY \ --recaptcha-redirect-site-key ""
重新導向 reCAPTCHA 評估流量
將 reCAPTCHA 金鑰與安全政策建立關聯後,您可以在該政策中建立規則,在內部重新導向 reCAPTCHA 評估流量。在 gcloud CLI 中使用下列格式重新導向流量:
gcloud compute security-policies rules create PRIORITY \ --security-policy SECURITY_POLICY \ {--expression EXPRESSION | --src-ip-ranges SRC_IP_RANGE} \ --action redirect \ --redirect-type google-recaptcha
更改下列內容:
PRIORITY
:您要建立規則的優先順序SECURITY_POLICY
:安全性政策的名稱EXPRESSION
:與您要強制執行 reCAPTCHA 評估的流量相符的自訂規則語言運算式SRC_IP_RANGE
:IP 位址範圍。使用這個值,可針對這個範圍內的所有要求強制執行 reCAPTCHA 評估。
以下範例會建立規則,將嘗試存取 /login.html
的流量重新導向至 reCAPTCHA 手動驗證:
gcloud compute security-policies rules create 1000 \ --security-policy SECURITY_POLICY \ --expression "request.path.matches(\"/login.html\")" \ --action redirect \ --redirect-type google-recaptcha
強制執行 reCAPTCHA 無摩擦評估
繼續操作前,請參閱機器人管理總覽,瞭解使用 reCAPTCHA 動作符記或工作階段符記的必要條件。
如要從 reCAPTCHA 動作符記擷取屬性,您可以使用 token.recaptcha_action.ATTRIBUTE
。請在 Google Cloud Armor 規則語言中,將 ATTRIBUTE
替換為有效的符記屬性。同樣地,您也可以使用 token.recaptcha_session.ATTRIBUTE
從 reCAPTCHA 工作階段符記中擷取屬性。如要進一步瞭解可用 reCAPTCHA 符記屬性的語法,請參閱規則語言參考資料。
動作權杖可以來自網頁應用程式、iOS 應用程式或 Android 應用程式,但工作階段權杖只能來自網頁應用程式。每個平台都需要專屬的 reCAPTCHA 金鑰。運算式 token.recaptcha_action.ATTRIBUTE
和 token.recaptcha_session.ATTRIBUTE
適用於任何平台的權杖。為區分不同平台的權杖,並防止權杖遭竊,建議您在設定使用這些運算式的規則時,連結 reCAPTCHA 金鑰。
範例
第一個範例會建立規則,允許以 reCAPTCHA 動作權杖 (分數不得低於 0.8
) 指定 /login.html
的流量。
gcloud compute security-policies rules create 1000 \ --security-policy SECURITY_POLICY \ --expression "request.path.matches(\"/login.html\") && token.recaptcha_action.score >= 0.8" \ --action allow
第二個範例與第一個範例相同,但也要求動作符記必須使用 example-site-key-1
或 example-site-key-2
的 reCAPTCHA 金鑰核發:
gcloud compute security-policies rules create 1000 \ --security-policy SECURITY_POLICY \ --expression "request.path.matches(\"/login.html\") && token.recaptcha_action.score >= 0.8" \ --recaptcha-action-site-keys "example-site-key-1,example-site-key-2" \ --action allow
第三個範例會建立規則,允許流量指定 /login.html
,並使用 reCAPTCHA 金鑰 example-site-key-3
核發的 reCAPTCHA 工作階段權杖,且分數不得低於 0.8
。
gcloud compute security-policies rules create 1000 \ --security-policy SECURITY_POLICY \ --expression "request.path.matches(\"/login.html\") && token.recaptcha_session.score >= 0.8" \ --recaptcha-session-site-keys "example-site-key-3" \ --action allow
重新導向 (302 回應)
如要建立規則將流量重新導向至使用者設定的網址,請在 Google Cloud CLI 中使用下列格式:
gcloud compute security-policies rules create PRIORITY \ --security-policy SECURITY_POLICY \ {--expression EXPRESSION | --src-ip-ranges SRC_IP_RANGE} \ --action redirect \ --redirect-type external-302 \ --redirect-target REDIRECT_URL
更改下列內容:
PRIORITY
:您要建立規則的優先順序SECURITY_POLICY
:安全性政策的名稱EXPRESSION
:與您要強制執行 reCAPTCHA 評估的流量相符的自訂規則語言運算式SRC_IP_RANGE
:IP 位址範圍。使用這個值,可針對這個範圍內的所有要求強制執行 reCAPTCHA 評估。REDIRECT_URL
:您要將流量重新導向至的網址
以下範例會建立規則,將來自 10.10.10.0/24 的流量重新導向至 https://www.example.com。
gcloud compute security-policies rules create 1000 \ --security-policy SECURITY_POLICY \ --src-ip-ranges "10.10.10.0/24" \ --action redirect \ --redirect-type external-302 \ --redirect-target "https://www.example.com"
裝飾要求
如要建立規則,允許流量,但在傳送至受保護的後端之前,會新增自訂標頭和使用者定義的靜態值,請在 gcloud CLI 中使用下列格式:
gcloud compute security-policies rules create PRIORITY \ --security-policy SECURITY_POLICY \ {--expression EXPRESSION | --src-ip-ranges SRC_IP_RANGE} \ --action allow \ --request-headers-to-add HEADER_1=VALUE_1,HEADER_2=VALUE_2,...
更改下列內容:
PRIORITY
:您要建立規則的優先順序SECURITY_POLICY
:安全性政策的名稱EXPRESSION
:與您要強制執行 reCAPTCHA 評估的流量相符的自訂規則語言運算式SRC_IP_RANGE
:IP 位址範圍。使用這個值,可針對這個範圍內的所有要求強制執行 reCAPTCHA 評估。HEADER_#
:您要用來修飾要求的請求標頭名稱VALUE_#
:您要用來修飾要求的請求標頭值
以下範例會建立規則,允許流量指定 /login.html
,前提是要求的 reCAPTCHA 動作權杖分數也低於 0.2
。
gcloud compute security-policies rules create 1000 \ --security-policy SECURITY_POLICY \ --expression "request.path.matches("/login.html") && token.recaptcha_action.score < 0.2" \ --action allow \ --request-headers-to-add "reCAPTCHA-Warning=high"