設定頻率限制

本頁說明如何設定 Google Cloud Armor 規則,透過設定節流或以速率為準的禁止動作,強制執行每個用戶端的速率限制。設定速率限制前,請務必先詳閱速率限制總覽中的資訊。

事前準備

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

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

下列作業需要 Identity and Access Management (IAM) 的 Compute Security Admin 角色 (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 方法 getlistgetRule 查看 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

根據費率限制的規則

在 Google Cloud CLI 中,以速率為準的節流規則格式如下:

gcloud compute security-policies rules create PRIORITY \
    --security-policy=SECURITY_POLICY \
    {--expression=EXPRESSION | --src-ip-ranges=SRC_IP_RANGE} \
    --action "throttle" \
    --rate-limit-threshold-count=RATE_LIMIT_THRESHOLD_COUNT \
    --rate-limit-threshold-interval-sec=RATE_LIMIT_THRESHOLD_INTERVAL_SEC \
    --conform-action=[allow] \
    --exceed-action=[deny-403|deny-404|deny-429|deny-502|redirect] \
    --exceed-redirect-type=[google-recaptcha|external-302] \
    --exceed-redirect-target=REDIRECT_URL \
    --enforce-on-key=[IP | ALL | HTTP-HEADER | XFF-IP | HTTP-COOKIE | HTTP-PATH | SNI | REGION-CODE] \
    --enforce-on-key-name=[HTTP_HEADER_NAME|HTTP_COOKIE_NAME]

單一金鑰的頻率限制

舉例來說,下列 gcloud CLI 指令會在優先順序 105 建立 throttle 規則,並為 1.2.3.0/24 中的每個 IP 位址設定速率限制,每 60 秒 100 個要求。如果要求超出節流限制,系統會傳回 429 錯誤代碼。

gcloud compute security-policies rules create 105 \
    --security-policy SECURITY_POLICY \
    --src-ip-ranges="1.2.3.0/24" \
    --action=throttle \
    --rate-limit-threshold-count=100 \
    --rate-limit-threshold-interval-sec=60 \
    --conform-action=allow \
    --exceed-action=deny-429 \
    --enforce-on-key=IP

舉例來說,下列 gcloud CLI 指令會在優先順序 110 建立 throttle 規則,針對來自 1.2.3.0/24 中 IP 位址的所有要求,為每個 HTTP 標頭 User-Agent 的不重複值設定每 60 秒 10 次的要求傳送頻率上限。如果要求超出節流限制,系統會傳回 429 錯誤代碼。

gcloud compute security-policies rules create 110 \
    --security-policy SECURITY_POLICY \
    --src-ip-ranges="1.2.3.0/24" \
    --action=throttle \
    --rate-limit-threshold-count=10 \
    --rate-limit-threshold-interval-sec=60 \
    --conform-action=allow \
    --exceed-action=deny-429 \
    --enforce-on-key=HTTP-HEADER \
    --enforce-on-key-name='User-Agent'

最後,您可以針對擁有有效 reCAPTCHA 豁免 Cookie 的使用者發布以速率為準的封鎖。舉例來說,下列 gcloud CLI 指令會建立優先順序為 115throttle 規則,針對所有含有有效 reCAPTCHA 豁免 Cookie 的要求,為每個不重複的 reCAPTCHA 豁免 Cookie 設定每 5 分鐘 20 個要求的速率限制。如果要求超過節流上限,系統會重新導向要求以進行 reCAPTCHA 評估。如要進一步瞭解豁免 Cookie 和 reCAPTCHA 評估,請參閱機器人管理總覽

gcloud compute security-policies rules create 115 \
    --security-policy SECURITY_POLICY \
    --expression="token.recaptcha_exemption.valid" \
    --action=throttle \
    --rate-limit-threshold-count=20 \
    --rate-limit-threshold-interval-sec=300 \
    --conform-action=allow \
    --exceed-action=redirect \
    --exceed-redirect-type=google-recaptcha \
    --enforce-on-key=HTTP-COOKIE \
    --enforce-on-key-name="recaptcha-ca-e"

根據 JA4 和 JA3 指紋識別限制頻率

您可以將 JA4 和 JA3 指紋做為速率限制鍵。以下範例會建立優先順序為 1000throttle 規則,根據用戶端的 JA4 指紋,將 /login 路徑的要求比對為每 5 分鐘 20 個要求。超過節流限制的要求會遭到拒絕。

gcloud compute security-policies rules create 1000 \
    --security-policy SECURITY_POLICY \
    --expression "request.path.matches('/login')" \
    --action throttle \
    --rate-limit-threshold-count 20 \
    --rate-limit-threshold-interval-sec 300 \
    --conform-action allow \
    --exceed-action deny-429 \
    --enforce-on-key-configs tls-ja4-fingerprint

根據使用者 IP 位址設定速率限制

收到透過上游 Proxy 傳送的要求時,您可以根據原始用戶端的 IP 位址套用速率限制。下列範例會建立優先順序為 1000throttle 規則,根據來源用戶端的 IP 位址,將符合 /login 路徑的要求限制為每 5 分鐘 20 個。如果要求超過節流限制,系統會拒絕要求。

gcloud compute security-policies rules create 1000 \
    --security-policy SECURITY_POLICY
    --expression "request.path.matches('/login')"
    --action throttle
    --rate-limit-threshold-count 20
    --rate-limit-threshold-interval-sec 300
    --conform-action allow
    --exceed-action deny-429
    --enforce-on-key-configs user-ip

如要進一步瞭解使用者 IP 位址支援功能,請參閱規則語言參考資料

根據多個鍵設定速率限制

您也可以使用 enforce-on-key-configs 標記,根據多個速率限制鍵來節流流量。這個旗標會取代 enforce-on-key 旗標和 enforce-on-key-name 旗標。enforce-on-key-configs 旗標需要以半形逗號分隔的 KEY=NAME 配對清單,但部分鍵不需要提供名稱。

以下範例會為優先順序為 105 的政策 POLICY_NAME 建立 throttle 規則,針對來自 1.2.3.0/24 中 IP 位址的所有要求,為每個 HTTP-PATHsite_id 組合設定每 60 秒 100 項要求的速率限制。如果要求超出節流限制,系統會傳回 429 錯誤代碼。

gcloud compute security-policies rules create 105 \
    --security-policy=POLICY_NAME \
    --src-ip-ranges="1.2.3.0/24" \
    --action=throttle \
    --rate-limit-threshold-count=100 \
    --rate-limit-threshold-interval-sec=60 \
    --conform-action=allow \
    --exceed-action=deny-429 \
    --enforce-on-key-configs="HTTP-PATH,HTTP-COOKIE=site_id"

根據頻率封鎖的規則

gcloud CLI 中以速率為準的禁止規則格式如下:

gcloud compute security-policies rules create PRIORITY \
    --security-policy=SECURITY_POLICY \
    {--expression=EXPRESSION | --src-ip-ranges=SRC_IP_RANGE} \
    --action "rate-based-ban" \
    --rate-limit-threshold-count=RATE_LIMIT_THRESHOLD_COUNT \
    --rate-limit-threshold-interval-sec=RATE_LIMIT_THRESHOLD_INTERVAL_SEC \
    --ban-duration-sec=BAN_DURATION_SEC \
    --ban-threshold-count=BAN_THRESHOLD_COUNT \
    --ban-threshold-interval-sec=BAN_THRESHOLD_INTERVAL_SEC \
    --conform-action=[allow] \
    --exceed-action=[deny-403|deny-404|deny-429|deny-502|redirect] \
    --exceed-redirect-type=[google-recaptcha|external-302] \
    --exceed-redirect-target=REDIRECT_URL \
    --enforce-on-key=[IP | ALL | HTTP-HEADER | XFF-IP | HTTP-COOKIE | HTTP-PATH | SNI | REGION-CODE] \
    --enforce-on-key-name=[HTTP_HEADER_NAME|HTTP_COOKIE_NAME]

舉例來說,下列 gcloud CLI 指令會針對要求符合標頭 fish (值為 tuna) 的每個 IP 位址,在優先順序 100 建立以速率為準的禁止規則,並在速率超過限制 (每 120 秒 50 個要求) 時,禁止該 IP 位址 300 秒。遭禁止的要求會傳回 404 錯誤代碼。

gcloud compute security-policies rules create 100 \
    --security-policy=sec-policy \
    --expression="request.headers['fish'] == 'tuna'" \
    --action=rate-based-ban \
    --rate-limit-threshold-count=50 \
    --rate-limit-threshold-interval-sec=120 \
    --ban-duration-sec=300 \
    --conform-action=allow \
    --exceed-action=deny-404 \
    --enforce-on-key=IP

舉例來說,下列 gcloud CLI 指令會建立優先順序為 101 的速率型禁止規則,將區域代碼符合 US 的所有要求限制為每 60 秒 10 次。如果來自 US 地區的要求頻率超過上限 (每 600 秒 1000 個要求),系統也會禁止該地區的要求 300 秒。遭禁止的要求會傳回錯誤代碼 403

gcloud compute security-policies rules create 101 \
    --security-policy sec-policy \
    --expression "origin.region_code == 'US'" \
    --action rate-based-ban \
    --rate-limit-threshold-count 10 \
    --rate-limit-threshold-interval-sec 60 \
    --ban-duration-sec 300 \
    --ban-threshold-count 1000 \
    --ban-threshold-interval-sec 600 \
    --conform-action allow \
    --exceed-action deny-403 \
    --enforce-on-key ALL

舉例來說,下列 gcloud CLI 指令會在優先順序 102 建立以速率為準的禁止規則,將任何來源 IP 位址範圍的所有要求限制為每 60 秒 20 個要求。如果要求頻率超過上限 (每 400 秒 500 個要求),這項規則也會禁止任何來源 IP 位址範圍的要求 600 秒。遭禁止的要求會傳回錯誤代碼 429

gcloud compute security-policies rules create 102 \
    --security-policy sec-policy \
    --src-ip-ranges="*" \
    --action rate-based-ban \
    --rate-limit-threshold-count 20 \
    --rate-limit-threshold-interval-sec 60 \
    --ban-duration-sec 600 \
    --ban-threshold-count 500 \
    --ban-threshold-interval-sec 400 \
    --conform-action allow \
    --exceed-action deny-429 \
    --enforce-on-key ALL

將流量限制規則變更為以頻率為準的停權規則

您可以使用下列指令,將現有規則的動作從節流動作變更為以速率為準的禁止動作。

gcloud compute security-policies rules update 105 \
--action=rate-based-ban \
--security-policy=sec-policy \
--ban-duration-sec=600

您無法將現有規則的動作從以頻率為準的停權動作變更為流量限制動作。

後續步驟