在 YARA-L 查詢中使用匯總

支援的國家/地區:

本指南可協助安全工程師在 YARA-L 查詢中計算平均值或總數。並說明如何使用必要語法,避免編譯器發生錯誤。

常見用途

下列情境代表在 YARA-L 中使用匯總的主要目標。

偵測磁碟區異常狀況

  • 目標:找出特定指標 (例如資料外洩或登入嘗試) 超出歷來基準的時間。
  • 價值:自動設定門檻可減少手動調整,並偵測靜態限制可能錯過的隱匿式低速攻擊。

優先處理案件

  • 目標:根據與計算平均值的偏差,計算「outcome」部分中的風險分數。
  • 價值:分析師可專注於統計顯著程度最高的偵測結果,縮短回應時間並減少警報疲勞。

重要術語

如要編寫有效的 YARA-L 2.0 語法,請務必瞭解下列平台專屬術語:

  • window. prefix:規則引擎中特定匯總函式的語法規定。這會指示平台對整個 match 視窗執行計算,而非單一事件。
  • outcome 區段:YARA-L 規則的一部分,用於計算變數,並為產生的偵測結果提供額外背景資訊。進一步瞭解outcome 區段語法。
  • match 視窗:引擎用來將事件分組的特定時間長度 (例如 7d),定義於 match 區段中。進一步瞭解match 區段語法。

事前準備

建立時間範圍聚合前,請先確認您具備下列存取權,並瞭解相關規定:

  • 權限:您必須具備「偵測作者」 (roles/chronicle.detectionAuthor) IAM 角色,才能存取規則編輯器。

在 outcome 區段中實作匯總作業

如要正確彙整資料以觸發以門檻為準的快訊,請按照下列步驟操作:

  1. 定義結果變數。在 outcome 區段中建立變數,用來保存計算值。
  2. 套用 window. 前置字串。下列函式需要 window. 前置字元,才能將計算作業明確繫結至 match 視窗。

    類別 支援的視窗函式
    統計 window.avg、window.variance、window.variance_pop、window.percentile、window.mode、window.stddev
    訂購 window.first、window.last、window.last_non_null

    必要語法:$avg_bytes = window.avg($e.network.sent_bytes)

  3. 設定門檻條件。在「condition」部分,將即時事件與 outcome 變數進行比較。

範例:偵測網路流量大幅增加

請使用下列範例導入「平均值 3 倍」的偵測邏輯 (3 * $avg_bytes)。

規則

rule NetworkBytesSpikeDetection {
meta:
  author = ""
  description = "Detects when network sent bytes significantly spike above the average for a host."
  severity = "Medium"

events:
  $e.metadata.event_type = "NETWORK_CONNECTION"
  $e.principal.hostname = $hostname
  $e.network.sent_bytes > 0

match:
  // Define the window over which aggregations in the outcome section are calculated.
  $hostname over 1h

outcome:
  // Calculate the maximum value of network.sent_bytes within the 1h window.
  $max_sent_bytes = max($e.network.sent_bytes)
  $avg_sent_bytes = window.avg($e.network.sent_bytes)
  // Calculate the threshold: 3 times the average sent bytes.
  $threshold = 3 * $avg_sent_bytes

condition:
  // The rule triggers if there is at least one matching event ($e)
  // AND the maximum sent bytes ($max_sent_bytes) exceeds the calculated threshold.
  $e and $max_sent_bytes > $threshold
}
metadata.event_type = "NETWORK_CONNECTION"
principal.hostname = $hostname
network.sent_bytes > 0

match:
  // Define the window over which aggregations in the outcome section are calculated.
  $hostname over 1h

outcome:
  // Calculate the maximum value of network.sent_bytes within the 1h window.
  $max_sent_bytes = max(network.sent_bytes)
  // window.avg is not required in search and dashboard
  $avg_sent_bytes = avg(network.sent_bytes)
  // Calculate the threshold: 3 times the average sent bytes.
  $threshold = 3 * $avg_sent_bytes

資訊主頁

metadata.event_type = "NETWORK_CONNECTION"
principal.hostname = $hostname
network.sent_bytes > 0

match:
  // Define the window over which aggregations in the outcome section are calculated.
  $hostname over 1h

outcome:
  // Calculate the maximum value of network.sent_bytes within the 1h window.
  $max_sent_bytes = max(network.sent_bytes)
  // window.avg is not required in search and dashboard
  $avg_sent_bytes = avg(network.sent_bytes)
  // Calculate the threshold: 3 times the average sent bytes.
  $threshold = 3 * $avg_sent_bytes

疑難排解

本節會管理效能預期,並提供常見匯總問題的自助式修正方式。

驗證和測試

使用「測試規則」工具驗證匯總邏輯。由於視窗函式依附於 match 視窗,因此您應檢查測試結果中的「偵測」詳細資料。

  • 確認變數填入:確認 $threshold、$avg_bytes 和其他 outcome 變數不是 null。null 值通常表示所選時間範圍內的事件資料缺少匯總中參照的 UDM 欄位。
  • 檢查數字準確度:確認計算出的平均值符合預期。如果值不一致,請檢查 match 視窗是否擷取正確的遙測量。

錯誤修正

建立 YARA-L 規則時,請參考下表解決常見的彙整和編譯器問題。

錯誤 說明 修正
規則無法觸發 「condition」部分的門檻過高,或 outcome 變數傳回 null 值。 使用「測試規則」工具檢查偵測詳細資料。如果結果為 null,請確認該視窗的遙測資料中是否含有 UDM 欄位。
偵測延遲時間較長 計算範圍涵蓋大型match視窗或大量來源。 縮短 match 視窗或在 events 區段中新增特定篩選器。

後續步驟

還有其他問題嗎?向社群成員和 Google SecOps 專業人員尋求解答。