在 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.avgwindow.variancewindow.variance_popwindow.percentilewindow.modewindow.stddev
    訂購 window.firstwindow.lastwindow.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 變數不是 nullnull 值通常表示所選時間範圍內的事件資料缺少匯總中參照的 UDM 欄位。
  • 檢查數值準確度:確認計算出的平均值符合預期。如果值不一致,請檢查 match 視窗是否擷取正確的遙測量。

錯誤修正

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

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

後續步驟

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