設定機器人管理

本頁說明如何設定 Cloud Armor 安全性政策規則,以管理機器人。設定機器人管理功能前,請務必先瞭解機器人管理總覽中的資訊。

事前準備

以下各節說明設定 Cloud Armor 安全性政策時,所需的所有 Identity and Access Management (IAM) 角色和權限。如要使用本文中的用途,您只需要 compute.securityPolicies.createcompute.securityPolicies.update 權限。

設定 Cloud Armor 安全性政策的 IAM 權限

下列作業需要 Identity and Access Management (IAM) 的 Compute Security Admin 角色 (roles/compute.securityAdmin)

  • 設定、修改、更新及刪除 Cloud Armor 安全性政策
  • 使用下列 API 方法:
    • SecurityPolicies insert
    • SecurityPolicies delete
    • SecurityPolicies patch
    • SecurityPolicies addRule
    • SecurityPolicies patchRule
    • SecurityPolicies removeRule

具備 Compute 網路管理員角色 (roles/compute.networkAdmin) 的使用者可以執行下列作業:

  • 為後端服務設定 Cloud Armor 安全性政策
  • 使用下列 API 方法:
    • BackendServices setSecurityPolicy
    • BackendServices list (僅限 gcloud)

具備安全管理員角色 (roles/iam.securityAdmin) 和 Compute 網路管理員角色的使用者,可以透過 SecurityPolicies API 方法 getlistgetRule 查看 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 手動驗證問題,區分真人或自動化用戶端

如要在 Cloud Armor 中使用 reCAPTCHA,您必須將 CHALLENGEPAGE 類型的 reCAPTCHA WAF 網站金鑰 (reCAPTCHA 金鑰) 與安全性政策建立關聯。如要進一步瞭解 reCAPTCHA 金鑰,請參閱 reCAPTCHA 金鑰總覽

如要將自己的 reCAPTCHA 金鑰與安全性政策建立或取消關聯,請使用下列指令:

gcloud compute security-policies update SECURITY_POLICY \
    --recaptcha-redirect-site-key SITE_KEY

更改下列內容:

  • SECURITY_POLICY:安全性政策的名稱
  • SITE_KEY:您的 reCAPTCHA CHALLENGEPAGE 金鑰

關聯 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:安全性政策的名稱
  • :自訂規則語言運算式,可比對要強制執行 reCAPTCHA 評估的流量EXPRESSION
  • 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。請將 ATTRIBUTE 改成 Cloud Armor 規則語言中的有效權杖屬性。同樣地,您也可以使用 token.recaptcha_session.ATTRIBUTE 從 reCAPTCHA 工作階段符記擷取屬性。如要進一步瞭解可用 reCAPTCHA 權杖屬性的語法,請參閱規則語言參考資料

動作權杖可來自網頁應用程式、iOS 應用程式或 Android 應用程式,但工作階段權杖只能來自網頁應用程式。每個平台都需要個別的 reCAPTCHA 金鑰。token.recaptcha_action.ATTRIBUTEtoken.recaptcha_session.ATTRIBUTE 運算式適用於來自上述任一平台的權杖。為區分不同平台的權杖並防止權杖遭竊,建議您在設定使用這些運算式的規則時,一併建立 reCAPTCHA 金鑰關聯。

範例

第一個範例會建立規則,允許以 /login.html 為目標的流量,但前提是 reCAPTCHA 動作權杖的分數不低於 0.8

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-1example-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 為目標的流量,但必須具備以 example-site-key-3 reCAPTCHA 金鑰核發的 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:安全性政策的名稱
  • :自訂規則語言運算式,可比對要強制執行 reCAPTCHA 評估的流量EXPRESSION
  • 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:安全性政策的名稱
  • :自訂規則語言運算式,可比對要強制執行 reCAPTCHA 評估的流量EXPRESSION
  • SRC_IP_RANGE:IP 位址範圍。使用這項功能,對這個範圍內的所有要求強制執行 reCAPTCHA 評估。
  • HEADER_#:要裝飾要求的請求標頭名稱
  • VALUE_#:要用來裝飾要求的要求標頭值

以下範例會建立規則,允許以 /login.html 為目標的流量,前提是要求也必須具有低於 0.2 的 reCAPTCHA 動作權杖分數。

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"

後續步驟