套用 YARA-L 2.0 時間範圍邏輯

支援的服務:

本指南可協助資安工程師選擇正確的查詢視窗類型,避免發生「變數未繫結」編譯器錯誤。從滑動視窗轉換為滾動式時間區間,即可建立依賴事件缺少的邏輯,例如缺少活動訊號或記錄來源失敗。

事前準備

確認帳戶具備下列其中一個角色,可建立及修改 YARA-L 查詢:

  • 偵測引擎管理員 (roles/chronicle.detectionEngineAdmin)
  • SecOps 編輯者 (roles/chronicle.editor)

支援的視窗類型

YARA-L 2.0 會使用不同的時間範圍行為,決定時間切片和事件分組的方式。您可以使用下列支援的時間範圍,依指定的時間精細程度,將 match 區段中的事件欄位和預留位置分組:

支援的視窗類型 語法 說明 常見用途
Hop $key over <duration> 建立固定的重疊時間間隔。系統會定義重疊和對齊方式,以擷取邊界附近的事件。 多個事件的一般關聯,不考慮確切開始時間。
滾筒式烘衣 $key by <duration> 將資料分割成大小固定、連續且不重疊的區塊。評估時不考量事件到達時間。 量化固定時段 (例如 $user by 30m) 的活動,或偵測事件是否發生。
滑動 $key over <duration> [before|after] $pivot 將視窗錨定至特定 "pivot" 事件。必須存在基準點,才能觸發回溯或前瞻。 事件順序至關重要的嚴格排序 (例如 File Download after Login)。

瞭解 window_start 和 window_end 邏輯

Google Security Operations 提供兩個通用保留關鍵字,可直接解析為 GoogleSQL 時間戳記,以便在查詢中使用。這些關鍵字可讓您存取時間值區的邊界:

  • window_start:時間值區的起始邊界。
  • window_end:時間值區的結束邊界。

使用限制

  • 比對條件:您必須在 match 區段中定義視窗 (by 或 over),才能使用這些關鍵字。
  • 部分限制:在 condition、order 或 outcome 中使用這些關鍵字。在 events 區段中無效。
  • 命名空間:請勿使用這些做為自訂變數名稱 (例如 $window_start)。

決定視窗類型和語法

您可以快速參考這個表格,判斷偵測規則要使用的視窗類型和語法。

情境 建議期間 建議的關鍵字 主要優勢
高頻率偵測 (例如蠻力攻擊) 滑動式時間區間 (over) window_start、window_end 在滾動週期內達到門檻時,系統會立即觸發。
缺席/不存在 (例如心跳漏拍) 滾動式時間區間 (by) window_start、window_end 即使沒有事件發生,也會評估固定時間區塊,避免發生「Variable Not Bounded」錯誤。
按時間順序製作報表 滑動或翻滾 order: window_start asc 標準化警告輸出,方便在 Chronicle UI 中分析時間軸。
時間限制篩選 滑動或翻滾 condition: window_start > "2026-01-01 00:00:00Z" 使用 GoogleSQL 時間戳記強制轉型,將規則評估限制在特定日期。

比較時間戳記類型和時區

YARA-L 支援直接將 window_start 和 window_end 與 GoogleSQL 時間戳記格式的字串進行比較。這樣一來,您就能執行精確的篩選,不必手動轉換類型。

注意:請一律使用 Z 後置字串指定世界標準時間 (例如 "2026-03-17T17:35:00Z"),以便與 UDM 事件時間戳記保持一致。

使用時間戳記篩選器可提升效能。如果只需要確認事件發生的先後順序,在 condition 區段中比較時間戳記通常更有效率:$e1.metadata.event_timestamp.seconds < $e2.metadata.event_timestamp.seconds。

進一步瞭解 match 區段語法和 condition 區段語法。

例如:window_end 和 window_start

  • window_end < "2026-01-01T12:30:00Z" (日期、時間和世界標準時間後置字串)
  • window_start != "2026-01-01 15:00:00+00" (含世界標準時間時差)

如果比較失敗,請確認字串符合 GoogleSQL 標準時間戳記格式。

跳躍式時間區間

跳躍式時間區間會建立重疊的時間間隔,因此不會遺漏發生在時間區間邊界附近的事件。

  • 用途:最適合用於偵測特定情境,無論時間範圍間隔何時開始或結束。
  • 支援:支援在搜尋和資訊主頁中匯總。

含有 match 區段的 YARA-L 查詢會使用跳躍式時間區間,建立多個事件在一段時間內的關聯。系統會將查詢執行時間範圍劃分為一組固定的重疊躍進視窗。雖然您可以在 match 區段中指定這些時間範圍的時間長度,但系統會定義重疊間隔和時間範圍對齊方式。接著,系統會將這些預定時間範圍內的事件建立關聯。

進一步瞭解 match 區段語法。

躍點時間範圍對複合規則偵測有特定影響。詳情請參閱「複合規則:時間間隔和跳躍式時間區間」。

範例:重疊跳躍式時間區間,用於連續關聯

以下範例顯示在 [1:00, 2:00] 日期範圍內執行的查詢,其中包含 match 區段 $user over 30m。系統可能產生的重疊跳躍式時間區間組合為 [1:00, 1:30]、[1:03, 1:33] 和 [1:06, 1:36]。

規則

rule hop_window_brute_force_example {
meta:
  description = "Detects multiple failed logins within a shifting 30-minute window."
  severity = "Medium"

events:
  $login.metadata.event_type = "USER_LOGIN"
  $login.extensions.auth.auth_status = "FAILURE"
  $login.principal.user.userid = $user

match:
  // This creates the overlapping windows (e.g., 1:00-1:30, 1:03-1:33)
  $user over 30m

condition:
  // This will trigger if 10 or more failures fall into any single 30m hop
  #login >= 10
}
metadata.event_type = "USER_LOGIN"
security_result.action = "FAIL"
principal.user.userid = $user

match:
  // This creates the overlapping windows (e.g., 1:00-1:30, 1:03-1:33)
  $user over 30m
  ```

資訊主頁

metadata.event_type = "USER_LOGIN"
security_result.action = "FAIL"
principal.user.userid = $user

match:
  // This creates the overlapping windows (e.g., 1:00-1:30, 1:03-1:33)
  $user over 30m

範例:使用跳躍式時間區間建立多事件關聯

以下範例會擷取在相同一般時間範圍內發生的事件:

規則

rule hop_window_example {
meta:
  description = "Detect a user with a failed login followed by a success within 30m"

events:
  // Event 1: Capture failed login attempts
  $fail.metadata.event_type = "USER_LOGIN"
  $fail.security_result.action = "FAIL"
  $fail.principal.user.userid = $user

  // Event 2: Capture successful login attempts
  $success.metadata.event_type = "USER_LOGIN"
  $success.security_result.action = "ALLOW"
  $success.principal.user.userid = $user

match:
  // Correlate events for the SAME $user within a rolling 30-minute window.
  $user over 30m

condition:
  // Ensure both a failed ($fail) and a successful ($success) login event occurred for the same user within the 30m window.
  $fail and $success
}

搜尋

// Event 1: Capture failed login attempts
metadata.event_type = "USER_LOGIN"
security_result.action = "FAIL"
principal.user.userid = $user // Assign user ID to a placeholder

// Event 2: Capture successful login attempts
metadata.event_type = "USER_LOGIN"
security_result.action = "ALLOW"
principal.user.userid = $user // Link to the same user placeholder

match:
  // Correlate events for the SAME $user within a rolling 30-minute window.
  $user over 30m

資訊主頁

// Event 1: Capture failed login attempts
metadata.event_type = "USER_LOGIN"
security_result.action = "FAIL"
principal.user.userid = $user // Assign user ID to a placeholder

// Event 2: Capture successful login attempts
metadata.event_type = "USER_LOGIN"
security_result.action = "ALLOW"
principal.user.userid = $user // Link to the same user placeholder

match:
  // Correlate events for the SAME $user within a rolling 30-minute window.
  $user over 30m

範例:躍點視窗比較

為找出暴力破解攻擊,10m 視窗會將所有 USER_LOGIN 失敗事件分組。接著,條件會評估該特定 10 分鐘區間內的計數 (#e) 是否超過門檻。

規則

rule failed_logins
{
meta:
  author = "Security Team"
  description = "Detects multiple failed user logins within 10-minute windows."
  severity = "HIGH"

events:
  $e.metadata.event_type = "USER_LOGIN"
  $e.security_result.action = "FAIL"
  $user = $e.target.user.userid

match:
  $user over 10m

condition:
  #e >= 5
}

搜尋

metadata.event_type = "USER_LOGIN"
security_result.action = "FAIL"
$user = target.user.userid

match:
  $user over 10m

資訊主頁

metadata.event_type = "USER_LOGIN"
security_result.action = "FAIL"
$user = target.user.userid

match:
  $user over 10m

滾動式時間區間

注意:這項功能僅適用於部分地區的客戶。

「輪轉時間區間」會將資料分割成固定大小、不重疊且連續的時間間隔。每個事件的時間戳記都只會落在一個視窗中。各個輪轉視窗之間不會重疊。這與跳躍式時間區間或滑動視窗不同,後兩者的時間間隔可以重疊。

如要實作滾動式時間區間,請在 match 區段中使用 by 運算子。滾動式時間區間會將時間劃分為連續、緊接的時間區塊,例如:

  • by 1h:為每小時建立時間範圍 (例如 [00:00:00-00:59:59]、[01:00:00-01:59:59])。
  • by 10m:為每 10 分鐘的間隔建立視窗 (例如 [00:00:00-00:09:59]、[00:10:00-00:19:59])。

常見用途

滾動式時間區間的適用情況:

  • 在不重疊的不同時段分析事件。
  • 在每個固定時間間隔內,無論事件發生多少次,都只針對特定實體 (由比對變數定義) 產生一項偵測結果。
  • 計算固定期間內的不重複實體數量。

簡化行為

滾動式時間區間的主要特徵,在於引擎如何處理每個時間區間內,同一組比對變數的偵測結果:

  • 每個滾動式時間區間偵測一次:對於比對變數的一組特定值 (例如特定 $userid),引擎最多會為任何單一滾動式時間區間產生一次偵測。
  • 先到先贏:在特定時間範圍內,最先擷取且符合該組比對變數規則條件的事件會觸發偵測。
  • 簡化:在同一個時間範圍例項中,任何符合條件的後續事件都不會產生額外的偵測結果。

語法

match 區段的語法如下:match: $variable by <duration>

  • $variable 是您要比對的預留位置變數。
  • duration 是數字,後面加上時間單位:m (分)、h (小時)、d (天)。
  • 最短為一分鐘,最長為 72 小時或三天。

範例:固定間隔分組

下列規則會依 $userid 將登入活動分組,時間範圍為 1 小時,且不重疊。

規則

rule TumblingWindowExample {
meta:
  description = "Example using a 1-hour tumbling window"

events:
  $e.metadata.event_type = "USER_LOGIN"
  $e.principal.user.userid = $userid

match:
  $userid by 1h

condition:
  $e
}

搜尋

metadata.event_type = "USER_LOGIN"
principal.user.userid = $userid

match:
  $userid by 1h

資訊主頁

metadata.event_type = "USER_LOGIN"
principal.user.userid = $userid

match:
  $userid by 1h

範例:偵測行為 (使用者 =「Alex」)

  • 事件 1:Alex 在 00:30 登入。這屬於[00:00:00-00:59:59]視窗。引擎會在這個時間範圍內產生 Alex 的偵測結果。
  • 事件 2:另一位使用者「Taylor」在 00:45 登入。這也屬於 [00:00:00-00:59:59],但由於 $userid 不同,引擎會為 Taylor 產生獨立偵測結果。
  • 事件 3:Alex 在 00:40 再次登入。這仍在 [00:00:00-00:59:59] 期間內。由於 Alex 的偵測結果已存在,因此系統會簡化這項事件。不會產生新的偵測結果。
  • 事件 4:Alex 於 01:20 登入。這會落在下一個時間範圍 [01:00:00-01:59:59]。引擎會為 Alex 產生新的偵測結果。

雖然 Alex 的事件 1 和事件 4 發生時間相隔不到一小時,但由於事件 4 跨越固定時間範圍邊界,因此會歸入不同的偵測結果。

設定缺席偵測的滾動式時間區間

如要在計數為零時發出警告,並避免編譯器失敗,請完成下列步驟:

  1. 確立偵測目標。判斷您要搜尋的是高頻率活動,還是缺乏活動。
  2. 選取 window 關鍵字。over 代表頻率,by 代表缺席。
  3. 參考視窗邊界。使用 window_start 和 window_end 關鍵字,參照時間 bucket 的特定邊界。

背景資訊:使用 outcome: $ext_window_end = window_end,在警告中繼資料中加入心跳訊號遺漏的確切結束時間。

範例:使用中繼資料偵測缺少的活動訊號

您可以透過滾動式時間區間,在計數為零時發出警告,避免在搜尋缺少活動時發生編譯器失敗。

規則

rule missing_heartbeat_detection {
meta:
  description = "Alert when a system fails to check in within a 24h block"

events:
  $e.metadata.event_type = "STATUS_UPDATE"
  $host = $e.principal.hostname

match:
  $host by 24h  // Tumbling window lets you detect zero counts

outcome:
  $event_count = count($e.metadata.id)
  $missing_window_start = window_start
  $missing_window_end = window_end

condition:
  $event_count = 0 and window_start > "2026-01-01T12:30:00Z"
}

搜尋

metadata.event_type = "STATUS_UPDATE"
$host = principal.hostname

match:
  $host by 24h  // Tumbling window lets you detect zero counts

outcome:
  $event_count = count(metadata.id)
  $missing_window_start = window_start
  $missing_window_end = window_end

資訊主頁

metadata.event_type = "STATUS_UPDATE"
$host = principal.hostname

match:
  $host by 24h  // Tumbling window lets you detect zero counts

outcome:
  $event_count = count(metadata.id)
  $missing_window_start = window_start
  $missing_window_end = window_end

滑動式時間區間

滑動時間範圍會錨定在特定樞紐事件,並向前 (after) 或向後 (before) 查看。

常見用途

如果事件順序至關重要,請使用滑動視窗:

  • 嚴格排序:偵測攻擊鏈 (例如 e1 在 e2 之後最多兩分鐘內發生)。
  • 相對時間:找出在觸發條件特定時間差內發生的事件 (例如在 Process Start 發生後 30 秒內發生的 Network Connection)。
  • 偵測缺漏:在啟動事件後,找出未發生的必要「清除」或「心跳」事件。

語法

match: <grouping_keys> over <duration> [before|after] <$pivot_event>

元件 說明 範例
分組索引鍵 用於連結事件的常見欄位。 $host、$user
時間長度 與基準事件的時間偏移。 5m、1h、30s
方向 視窗要向前或向後延伸。 after、before
透視事件 用於錨定回溯期的事件變數。 $proc、$alert

以下是有效的滑動視窗範例:

  • $var1, $var2 over 5m after $e1
  • $user over 1h before $e2
  • $host, $ip over 1h before $e2

需求條件和限制

  • 效能:滑動視窗需要比跳躍式時間區間更多的處理能力。只有在事件順序有嚴格要求時 (例如某個事件必須在另一個事件發生後五分鐘內發生),才使用這類條件。
  • 單一事件查詢:請勿對單一事件邏輯使用滑動視窗。請改為在 condition 區段中使用多個事件變數。

範例:前瞻性關聯 (after)

規則

rule sliding_window_after_example {
meta:
  description = "Detect a network connection occurring within 1 minute after a suspicious process launch."
  severity = "High"

events:
  $proc.metadata.event_type = "PROCESS_LAUNCH"
  $proc.principal.hostname = $host

  $net.metadata.event_type = "NETWORK_HTTP"
  $net.principal.hostname = $host

match:
  // $proc is the pivot; the 1-minute window starts at the $proc timestamp
  $host over 1m after $proc

condition:
  $proc and $net
}

搜尋

搜尋功能不支援 after。

資訊主頁

資訊主頁不支援after。

範例:回溯相關性 (before)

使用 "before" 滑動視窗,調查特定警告發生前的活動。這通常用於根本原因分析,找出重大偵測事件發生前的情況。

規則

rule sliding_window_before_example {
meta:
  description = "Identify file modifications occurring in the 5 minutes before a ransomware alert."
  severity = "Critical"

events:
  $file.metadata.event_type = "FILE_MODIFICATION"
  $file.principal.hostname = $host

  $alert.metadata.event_type = "ANTIVIRUS_DETECTION"
  $alert.metadata.product_name = "Premium_AV"
  $alert.principal.hostname = $host

match:
  // $alert is the pivot; the 5-minute window ends at the $alert timestamp
  $host over 5m before $alert

condition:
  $file and $alert
}

搜尋

搜尋功能不支援 before。

資訊主頁

資訊主頁不支援before。

疑難排解

本節說明 YARA-L 中常見時間範圍問題的延遲時間、限制和修正方式。

延遲時間和限制

  • 使用滾動式時間區間 (by) 的規則只會在固定時間區塊結束時觸發。舉例來說,如果規則含有 match: $user by 24h,則只會在 24 小時的時間範圍完全結束後進行評估。

  • 快訊延遲時間:使用 by (輪轉) 的規則只會在固定時間區塊結束後觸發。by 24h 規則不會在時間範圍內發出快訊。

  • UI 不一致:在「搜尋」和「資訊主頁」中,window_start 關鍵字會直接參照 TIME_BUCKET 欄位。依「window_start」篩選或分組時,使用者介面中的欄標題會顯示為「TIME_BUCKET」。這是已知的使用者介面顯示限制,您的邏輯仍有效。

錯誤修正

錯誤代碼 說明 修正
變數未繫結 規則使用 over (滑動時間範圍) 和$count = 0條件。 將視窗化關鍵字變更為 by (滾動式時間區間)。滑動視窗需要事件才能錨定視窗,而滾動式時間區間則不需要。
UI 標頭不符 使用者會在 UI 中看到 TIME_BUCKET,而非 window_start。 不需修正。使用關鍵字時,篩選和排序邏輯仍可正常運作。
不明 ID:window_start 已使用關鍵字,但 match 中未定義視窗。 在 match 區段中新增視窗陳述式 (例如 $hostname by 1h)。
事件中的 window_start 無效 events 區段會參照這個關鍵字。 將邏輯移至 condition 或 outcome 部分。視窗邊界只會在比對階段的事件分組後計算。

後續步驟

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