啟用即時通訊通知

總覽

Security Command Center 會在Google Cloud 控制台中即時通知您發現項目。本指南說明如何使用 Google Cloud 服務和第三方 API 擴充功能,並在 Slack 中接收近乎即時的通知。完成指南後,您不必登入 Google Cloud 控制台,就能收到已設定第三方服務的新發現項目快訊,進而更快地分類安全漏洞和威脅。進一步瞭解 Security Command Center 中的各種安全漏洞和威脅。

拓撲

在本指南中,您將建立下圖所示的設定。

近乎即時的通知工作流程 (按一下即可放大)
Security Command Center 近乎即時的通知工作流程 (按一下即可放大)

目標

在本指南中,您將執行下列操作:

  1. 設定 Pub/Sub 主題。
  2. 設定 Slack。
  3. 在 Cloud Run functions 中編寫程式碼。
  4. 設定 Pub/Sub 和 Cloud Run 函式,在有新的高嚴重性或重大嚴重性發現項目寫入 Security Command Center 時,傳送 Slack 通知。
  5. 排解通知問題。

費用

本教學課程使用 Google Cloud的計費元件,包括:

  • Pub/Sub
  • Cloud Run functions
  • Cloud Build

使用 Pricing Calculator,根據您的預測用量來產生預估費用。

事前準備

如要完成本指南,您必須具備下列 Identity and Access Management (IAM) 角色:

  • 組織管理員 (roles/resourcemanager.organizationAdmin)
  • 安全中心管理員 (roles/securitycenter.admin)
  • 安全管理員 (roles/iam.securityAdmin)
  • 具備 serviceusage.services.use 權限的角色,例如「擁有者」角色 (roles/owner)、「編輯者」角色 (roles/editor) 或自訂角色
  • 建立服務帳戶 (roles/iam.serviceAccountCreator)
  • Pub/Sub 編輯者 (roles/pubsub.editor)
  • 帳單帳戶管理員 (roles/billing.admin)

您可以在組織、資料夾或專案層級授予 Security Command Center 的 IAM 角色。您能否查看、編輯、建立或更新發現項目、資產和安全來源,取決於獲准的存取層級。如要進一步瞭解 Security Command Center 角色,請參閱「存取控管」。

設定專案

請按照下列步驟建立或選取專案。

  1. 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自體驗產品的實際應用成效。新客戶還能獲得價值 $300 美元的免費抵免額,能用於執行、測試及部署工作負載。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Build API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  5. Install the Google Cloud CLI.

  6. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  7. To initialize the gcloud CLI, run the following command:

    gcloud init
  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  9. Verify that billing is enabled for your Google Cloud project.

  10. Enable the Cloud Build API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  11. Install the Google Cloud CLI.

  12. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  13. To initialize the gcloud CLI, run the following command:

    gcloud init

預估時間:為單一服務設定及測試通知約需一小時。

設定 Pub/Sub 主題

Pub/Sub 是一項即時訊息服務,可在不同應用程式之間收發訊息。進一步瞭解 Pub/Sub。

在本節中,您將設定 Security Command Center,將發現項目發布至 Pub/Sub 主題。

如要設定及訂閱 Pub/Sub 主題,請按照下列步驟操作:

  1. 在環境變數中指定 Google Cloud 專案。

    export PROJECT_ID=PROJECT_ID
    

    將 PROJECT_ID 替換為專案 ID。

  2. 在環境變數中指定 Google Cloud 機構。

    export ORG_ID=ORG_ID
    

    請將 ORG_ID 替換成組織 ID。

  3. 設定 gcloud 指令的專案 ID。

    gcloud config set project PROJECT_ID
    
  4. 建立發布通知的 Pub/Sub 主題。

    gcloud pubsub topics create scc-critical-and-high-severity-findings-topic
    
  5. 在環境變數中指定主題。

    export TOPIC=projects/$PROJECT_ID/topics/scc-critical-and-high-severity-findings-topic
    
  6. 建立訂閱項目,在訊息發布至主題時通知 Cloud Run 函式傳送即時通訊訊息。

    gcloud pubsub subscriptions create scc-critical-and-high-severity-findings-sub \
      --topic scc-critical-and-high-severity-findings-topic
    
  7. 設定 Security Command Center,將通知發布至該主題。您可以使用任何與 ListFindings API 相容的篩選器。

    以下篩選器會針對 global 位置中嚴重程度為高和緊急的有效發現項目發布通知。進一步瞭解如何篩選發現結果。

     gcloud scc notifications create scc-critical-high-severity \
         --pubsub-topic=$TOPIC \
         --organization=$ORG_ID \
         --location=global \
         --filter '(severity="HIGH" OR severity="CRITICAL") AND state="ACTIVE"' \
         --deletion-notifications-enabled
    

    新增 --deletion-notifications-enabled 旗標,即可接收因資料保留政策而刪除搜尋結果的事件通知。通知包含 deletedFinding: true 欄位集。請確認 Cloud Run 函式或應用程式會剖析 deletedFinding 屬性,以便警報正確反映發現項目刪除情形。如要停用刪除通知,請更新 NotificationConfig 並設定 --no-deletion-notifications-enabled 旗標。

接著,請建立或設定即時通訊應用程式,接收 Pub/Sub 通知。

設定訊息應用程式

本節說明如何使用 Pub/Sub 和 Cloud Run functions 或 Cloud Run functions (第 1 代),為 Slack 啟用近乎即時的通知。

如要將通知傳送至 Slack 頻道,請按照下列步驟操作:

  • 建立新的 Slack 應用程式,並取得足夠的權限,以便在公開 Slack 頻道中發布訊息。
  • 建立及部署 Cloud Run 函式,在收到 Pub/Sub 通知時,將即時通訊訊息發布至 Slack。

建立新的 Slack 應用程式

在本節中,您會建立新的 Slack 應用程式來接收通知。

  1. 前往 Slack API 應用程式。 頁面會在新的分頁中開啟。
  2. 登入或建立帳戶。

  3. 選取「建立應用程式」。

  4. 將「應用程式名稱」設為「SCC Finding Notifier」。

  5. 選取要讓 Slack 機器人發布訊息的開發 Slack 工作區,然後按一下「建立應用程式」。

  6. 選取導覽面板中的「OAuth & Permissions」(OAuth 和權限)。

  7. 前往「範圍」部分。範圍分為兩類:

    • 機器人權杖範圍
    • 使用者權杖範圍
  8. 在本練習中,您不需要新增使用者權杖範圍。在「機器人權杖範圍」下方,按一下「新增 OAuth 範圍」,然後輸入:

    • chat:write
    • chat:write.public
  9. 前往「OAuth 和權限」頁面頂端,然後按一下「將應用程式安裝到 Workspace」。

  10. 在確認對話方塊中,按一下「允許」。

  11. 複製 Bot User OAuth 存取權杖,以便在 Cloud Function 中使用。

接著,您會部署 Cloud Run 函式,將通知傳送至 Slack 群組。

建立 Slack Cloud Run 函式

在本節中,您將部署函式,以便將通知傳送至 Slack 帳戶。本節的程式碼範例是以 Python 編寫,但您可以使用 Cloud Run functions 支援的任何語言。

  1. 前往 Google Cloud 控制台的 Cloud Run functions「Services」(服務) 頁面。

    前往「服務」分頁

  2. 選取含有 Pub/Sub 主題的專案 ID。

  3. 點選「編寫函式」,然後填寫下列欄位:

    1. 在「服務名稱」部分,輸入 slack-chat-high-and-critical-findings。
    2. 在「Region」(區域),選取要儲存服務的區域。
    3. 在「執行階段」部分,選取要用來執行函式的 Python 版本。
  4. 按一下「新增觸發條件」,然後選取「Pub/Sub 觸發條件」。

  5. 輸入您在「設定 Pub/Sub 主題」中建立的 Pub/Sub 主題詳細資料,然後按一下「儲存觸發條件」。

  6. 點選「建立」。系統會顯示服務詳細資料。

  7. 在「來源」分頁的「來源」窗格中,按一下「requirements.txt」。

  8. 在程式碼編輯器中,新增 requests 和 functions-framework==3.*:

    requests
    functions-framework==3.*
    
  9. 在「來源」窗格中,點選「main.py」。

  10. 在程式碼編輯器中,將程式碼替換為下列程式碼:

    import base64
    import json
    import requests
    import functions_framework
    
    TOKEN = "BOT_ACCESS_TOKEN"
    
    @functions_framework.cloud_event
    def send_slack_chat_notification(cloud_event):
    
        pubsub_message = base64.b64decode(cloud_event.data["message"]["data"]).decode('utf-8').strip()
        message_json = json.loads(pubsub_message)
    
        finding = message_json['finding']
    
        requests.post("https://slack.com/api/chat.postMessage", data={
            "token": TOKEN,
            "channel": "#SLACK_CHANNEL_NAME",
            "text": f"A high severity finding {finding['category']} was detected."
        })
    

    更改下列內容:

    • BOT_ACCESS_TOKEN:您使用 Slack 應用程式建立的機器人使用者 OAuth 存取權杖。
    • SLACK_CHANNEL_NAME:要傳送通知的 Slack 頻道。
  11. 在「Function entry point」(函式進入點) 欄位中,輸入程式碼範例中的函式名稱:send_slack_chat_notification。

  12. 按一下「儲存並重新部署」。如果函式名稱旁顯示綠色勾號,表示函式已成功部署。這項程序可能需要幾分鐘才能完成。

如果上述步驟順利完成,設定程序即告完成,你將開始收到通知。注意事項:

  • 您會收到每項重大或高度嚴重性發現的個別即時通訊訊息。通知頻率或數量取決於貴機構的資源。
  • 系統會近乎即時地發布及傳送通知。不過,訊息的即時性無法保證,且多種因素都可能導致延遲,包括 Slack 發生問題。

如要變更通知工作流程,請按照下列步驟操作:

  • 如要變更收件者,請更新 Cloud Run 函式。
  • 更新 Pub/Sub 主題的篩選器,即可變更會觸發通知的發現項目。

測試通知

如要測試通知是否已正確設定,請按照這些操作說明,在高嚴重程度的發現項目中切換啟用和停用狀態。

  1. 前往 Security Command Center 的「發現項目」頁面。
    前往「發現項目」頁面
  2. 如果系統提示,請選取貴機構。
  3. 在「快速篩選器」面板中,前往「嚴重程度」部分,然後選取「高」或「重大」。「發現項目查詢結果」面板會更新,只顯示所選嚴重程度的發現項目。
  4. 在「發現項目查詢結果」面板中,勾選發現項目名稱旁的方塊,選取要查看的項目。
  5. 在「發現項目查詢結果」動作列的「變更有效狀態」選單中,選取「無效」。如果目前的發現項目查詢只顯示有效發現項目,該發現項目就會從查詢結果中移除。
  6. 在「快速篩選器」面板中,前往「狀態」部分,然後變更選取項目,只選取「無效」。「發現項目查詢結果」面板會更新,只顯示已停用的發現項目。
  7. 在「發現項目查詢結果」面板中,選取您標示為不活躍的發現項目。
  8. 在「發現項目查詢結果」動作列的「變更有效狀態」選單中,選取「有效」。
  9. 查看 Slack 訊息。畫面會顯示類似下圖的訊息:

Slack 通知

疑難排解

如果無法傳送或接收即時通訊訊息,請按照下列步驟找出並解決潛在問題。

  1. 查看 Cloud Run 函式的 Stackdriver 記錄,判斷函式是否遭到叫用。如果沒有,請確認通知設定正確無誤。

  2. 如果正在叫用 Cloud Run 函式,則函式可能會當機。使用 Google Cloud Error Reporting 檢查 Cloud Run 函式是否有錯誤。

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本教學課程所用資源的費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。

刪除專案

  1. 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。

    前往「Manage resources」(管理資源)

  2. 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
  3. 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。

刪除個別資源

  1. 刪除通知設定:

    gcloud scc notifications delete scc-critical-high-severity \
        --organization=ORG_ID \
        --location=global
    

    請將 ORG_ID 替換成組織 ID。

    如要保留設定並暫時停止通知,請在 Cloud Run 函式中註解掉 send 或 post 呼叫。

  2. 刪除 Cloud Run 函式:

    1. 前往 Cloud Run functions。
      前往 Cloud Run functions
    2. 勾選要刪除的函式旁邊的核取方塊。
    3. 按一下「刪除」圖示 。
  3. 刪除服務帳戶:

    1. 前往「Service accounts」(服務帳戶) 頁面。
      前往「服務帳戶」頁面
    2. 選取專案。
    3. 選取要刪除的服務帳戶,然後按一下「刪除」圖示 。

後續步驟