安全性政策範例

本頁面說明不同類型負載平衡器和安全性政策的安全性政策設定範例。

設定外部應用程式負載平衡器的安全性政策

以下是設定 Google Cloud Armor 安全性政策的高階步驟,可啟用允許或拒絕流量傳入全域外部應用程式負載平衡器或傳統版應用程式負載平衡器的規則:

  1. 建立 Cloud Armor 安全性政策。
  2. 根據 IP 位址清單、自訂運算式或預先設定的運算式集,在安全性政策中新增規則。
  3. 將安全性政策附加至全域外部應用程式負載平衡器或傳統版應用程式負載平衡器的後端服務,以控管存取權。
  4. 視需要更新安全性政策。

在下列範例中,您會建立兩項 Cloud Armor 安全性政策,並套用至不同的後端服務。

範例:將兩項安全性政策套用至不同的後端服務。
範例:將兩項安全政策套用至不同的後端服務 (按一下可放大)。

以這個範例來說,這些是 Cloud Armor 安全性政策:

  • mobile-clients-policy適用於 games 服務的外部使用者。
  • internal-users-policy 適用於貴機構的「test-network」團隊。

您將 mobile-clients-policy 套用至 games 服務,該服務的後端服務名為 games,並將 internal-users-policy 套用至測試團隊的內部 test 服務,該服務的對應後端服務名為 test-network

如果後端服務的後端執行個體位於多個區域,與該服務相關聯的 Cloud Armor 安全性政策就會套用至所有區域的執行個體。在上述範例中,安全性政策 mobile-clients-policy 適用於 us-central 中的執行個體 1、2、3 和 4,以及 us-east 中的執行個體 5 和 6。

建立範例

請按照這些操作說明,建立上一節討論的設定範例。

主控台

為外部使用者設定安全性政策:

  1. 在 Google Cloud 控制台中,前往「Google Cloud Armor policies」(Google Cloud Armor 政策) 頁面。

    前往 Google Cloud Armor 政策

  2. 按一下「Create policies」(建立政策)

  3. 在「Name」(名稱) 欄位中輸入 mobile-clients-policy

  4. 在「Description」(說明) 欄位中輸入 Policy for external users

  5. 在「預設規則動作」中選取「拒絕」

  6. 如要設定「拒絕」狀態,請選取「404 (找不到)」。

  7. 點選「下一步」

新增更多規則:

  1. 按一下 [新增規則]
  2. 在「Description」(說明) 欄位中輸入 allow traffic from 192.0.2.0/24
  3. 在「模式」中,選取「基本模式 (僅限 IP 位址/範圍)」
  4. 在「Match」欄位中輸入 192.0.2.0/24
  5. 在「動作」部分選取「允許」
  6. 在「Priority」(優先順序) 欄位中輸入 1000
  7. 按一下 [完成]
  8. 點選「下一步」

對目標套用政策:

  1. 點選「新增目標」
  2. 在「目標」清單中選取目標。
  3. 按一下 [完成]
  4. 按一下「建立政策」

視需要啟用 Google Cloud Armor 自動調整式防護機制:

  1. 如要啟用 Adaptive Protection,請勾選「啟用」核取方塊。

為內部使用者設定安全性政策:

  1. 按一下「Create policies」(建立政策)
  2. 在「Name」(名稱) 欄位中輸入 internal-users-policy
  3. 在「Description」(說明) 欄位中輸入 Policy for internal test users
  4. 在「預設規則動作」中選取「拒絕」
  5. 在「拒絕狀態」中,選取「502 (閘道錯誤)」
  6. 點選「下一步」

新增更多規則:

  1. 按一下 [新增規則]
  2. 在「Description」(說明) 欄位中輸入 allow traffic from 198.51.100.0/24
  3. 在「模式」中,選取「基本模式 (僅限 IP 位址/範圍)」
  4. 在「Match」欄位中輸入 198.51.100.0/24
  5. 在「動作」部分選取「允許」
  6. 如要僅預覽,請勾選「啟用」核取方塊。
  7. 在「Priority」(優先順序) 欄位中輸入 1000
  8. 按一下 [完成]
  9. 點選「下一步」

對目標套用政策:

  1. 點選「新增目標」
  2. 在「目標」清單中選取目標。
  3. 按一下 [完成]
  4. 按一下「建立政策」

gcloud

  1. 建立 Cloud Armor 安全性政策:

    gcloud compute security-policies create mobile-clients-policy \
        --description "policy for external users"
    
    gcloud compute security-policies create internal-users-policy \
        --description "policy for internal test users"
    
  2. 更新安全性政策的預設規則,拒絕流量:

    gcloud compute security-policies rules update 2147483647 \
        --security-policy mobile-clients-policy \
        --action "deny-404"
    
    gcloud compute security-policies rules update 2147483647 \
        --security-policy internal-users-policy \
        --action "deny-502"
    
  3. 在安全性政策中新增規則:

    gcloud compute security-policies rules create 1000 \
        --security-policy mobile-clients-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 create 1000 \
        --security-policy internal-users-policy \
        --description "allow traffic from 198.51.100.0/24" \
        --src-ip-ranges "198.51.100.0/24" \
        --action "allow"
    
  4. 將安全性政策附加至後端服務:

    gcloud compute backend-services update games \
        --security-policy mobile-clients-policy
    
    gcloud compute backend-services update test-network \
        --security-policy internal-users-policy
    
  5. (選用) 啟用 Adaptive Protection:

    gcloud compute security-policies update mobile-clients-policy \
        --enable-layer7-ddos-defense
    
    gcloud compute security-policies update internal-users-policy \
        --enable-layer7-ddos-defense
    

建立安全性政策

您可以使用 Google Cloud 控制台或 gcloud CLI 建立安全性政策。本節中的操作說明假設您要設定安全性政策,套用至現有的全域外部應用程式負載平衡器或傳統版應用程式負載平衡器和後端服務。如需填寫欄位的範例,請參閱「建立範例」。

主控台

建立 Cloud Armor 安全性政策和規則,並將安全性政策附加至後端服務:

  1. 在 Google Cloud 控制台中,前往「Google Cloud Armor policies」(Google Cloud Armor 政策) 頁面。

    前往 Google Cloud Armor 政策

  2. 按一下「Create policies」(建立政策)

  3. 在「Name」(名稱) 欄位中,輸入政策名稱。

  4. 選用:輸入政策說明。

  5. 在「政策類型」中,選擇「後端安全政策」或「邊緣安全政策」

  6. 在「預設規則動作」中,選取「允許」,建立允許存取的預設規則;或選取「拒絕」,建立禁止存取特定 IP 位址或 IP 位址範圍的預設規則。

    預設規則的優先順序最低,只有在沒有其他適用規則時才會生效。

  7. 如果您要設定「拒絕」規則,請選取「拒絕狀態」訊息。 如果沒有存取權的使用者嘗試存取,Cloud Armor 就會顯示這則錯誤訊息。

  8. 無論設定的規則類型為何,請點選「下一步」

新增更多規則:

  1. 按一下 [新增規則]
  2. 選用:輸入規則說明。
  3. 選取模式:

    • 基本模式:根據 IP 位址或 IP 範圍允許或拒絕流量。
    • 進階模式:根據規則運算式允許或拒絕流量。
  4. 在「比對」欄位中,指定規則的適用條件:

    • 基本模式:輸入要與規則比對的 IP 位址或 IP 範圍。
    • 進階模式:輸入運算式或子運算式,根據傳入的要求進行評估。如要瞭解如何編寫運算式,請參閱「設定自訂規則語言屬性」。
  5. 在「動作」中選取「允許」或「拒絕」,允許或拒絕符合規則的流量。

  6. 如要啟用預覽模式,請選取「啟用」核取方塊。在預覽模式中,您可以查看規則的運作方式,但規則不會啟用。

  7. 輸入規則的「Priority」(優先順序)。可以是介於 0 到 2,147,483,646 之間的任何正整數 (含首尾)。如要進一步瞭解評估順序,請參閱「規則評估順序」。

  8. 按一下 [完成]

  9. 如要新增更多規則,請按一下「新增規則」,然後重複上述步驟。 否則,請點選「下一步」

對目標套用政策:

  1. 點選「新增目標」
  2. 在「目標」清單中選取目標。
  3. 如要新增更多目標,請按一下「新增目標」
  4. 按一下 [完成]
  5. 按一下「建立政策」

gcloud

  1. 如要建立新的 Cloud Armor 安全性政策,請使用 gcloud compute security-policies create 指令。

    type 欄位中,使用 CLOUD_ARMOR 建立後端安全政策,或使用 CLOUD_ARMOR_EDGE 建立邊緣安全政策。type 旗標為選用項目;如未指定類型,系統會預設建立後端安全政策:

    gcloud compute security-policies create NAME \
       [--type=CLOUD_ARMOR|CLOUD_ARMOR_EDGE] \
       [--file-format=FILE_FORMAT | --description=DESCRIPTION] \
       [--file-name=FILE_NAME]
    
    

    更改下列內容:

    • NAME:安全性政策的名稱
    • DESCRIPTION:安全性政策的說明

    以下指令會更新您先前建立的政策、開啟 JSON 剖析功能,並將記錄層級變更為 VERBOSE

    gcloud compute security-policies update my-policy \
        --json-parsing=STANDARD \
        --log-level=VERBOSE
    
  2. 如要將規則新增至安全性政策,請使用 gcloud compute security-policies rules create PRIORITY 指令。

    gcloud compute security-policies rules create PRIORITY  \
        [--security-policy POLICY_NAME] \
        [--description DESCRIPTION] \
        --src-ip-ranges IP_RANGE,... | --expression EXPRESSION \
        --action=[ allow | deny-403 | deny-404 | deny-502 ] \
        [--preview]
    

    PRIORITY 換成政策中指派給規則的優先順序。如要瞭解規則優先順序的運作方式,請參閱「規則評估順序」。

    舉例來說,下列指令會新增規則,封鎖來自 IP 位址範圍 192.0.2.0/24198.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"
    

    加入 --preview 標記後,系統會將規則新增至政策,但不會強制執行,且只會記錄觸發規則的流量。

    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" \
        --preview
    

    使用 --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('cookie_name=cookie_value')" \
        --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-403" \
        --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"
    

    下列指令會新增一項規則,使用預先設定的運算式,允許來自已命名 IP 位址清單中所有 IP 位址的存取要求:

    gcloud compute security-policies rules create 1000 \
        --security-policy my-policy \
        --expression "evaluatePreconfiguredWaf('sourceiplist-fastly')" \
        --action "allow"
    

設定區域性外部應用程式負載平衡器的安全性政策

本節說明如何為區域性外部應用程式負載平衡器設定區域範圍的 Cloud Armor 安全性政策。

保護區域性負載平衡工作負載

請按照下列步驟設定安全政策,保護區域範圍的後端服務:

  1. 建立區域範圍的安全性政策。

    gcloud compute security-policies create POLICY_NAME \
       --type=CLOUD_ARMOR \
       --region=REGION
    
  2. 將區域範圍的安全性政策附加至區域範圍的後端服務。將 BACKEND_NAME 替換為現有區域範圍後端服務的名稱。

    gcloud compute backend-services update BACKEND_NAME \
       --security-policy=POLICY_NAME \
       --region=REGION
    

套用區域範圍的 Cloud Armor 安全性政策

舉例來說,假設您是安全管理員,希望滿足所有後端工作負載和 WAF 規則都部署在特定區域的駐留需求。假設您已事先完成下列事項:

  1. 您已在該區域中建立區域範圍的負載平衡後端服務。
  2. 您已在部署作業中停用任何現有的全域範圍安全性政策。
  3. 您已在相同區域中建立並附加區域範圍的安全性政策 (如上一節所述)。

您可以使用下列範例指令,在滿足需求的情況下,將 WAF 規則和其他進階規則新增至政策:

  • 將 WAF 規則新增至政策:

    gcloud compute security-policies rules create 1000 --action=deny-404 \
      --expression="evaluatePreconfiguredWaf('xss-v33-stable', ['owasp-crs-v030301-id941100-xss', 'owasp-crs-v030301-id941160-xss'])" \
      --security-policy=POLICY_NAME \
      --region=REGION
    
  • 在政策中新增進階規則:

    gcloud compute security-policies rules create 1000 --action=allow \
      --expression="has(request.headers['cookie']) && request.headers['cookie'].contains('80=EXAMPLE')" \
      --security-policy=POLICY_NAME \
      --region=REGION
    
  • 在政策中新增速率限制規則:

    gcloud compute security-policies rules create 1000 --action=throttle \
      --src-ip-ranges="1.1.1.1/32" \
      --rate-limit-threshold-count=1000 \
      --rate-limit-threshold-interval-sec=120 \
      --conform-action="allow" \
      --exceed-action="deny-429" \
      --enforce-on-key=IP \
      --ban-duration-sec=999 \
      --ban-threshold-count=5000 \
      --ban-threshold-interval-sec=60 \
      --security-policy=POLICY_NAME \
      --region=REGION
    

後續步驟