在 YARA-L 查询中使用聚合

支持的平台:

本指南可帮助安全工程师在 YARA-L 查询中计算平均值或总和。它介绍了如何使用必需的语法来防止编译器错误。

常见使用场景

以下场景代表了在 YARA-L 中使用聚合的主要目标。

检测基于数量的异常

  • 目标:确定特定指标(例如数据渗漏或登录尝试)何时超出历史基准。
  • 价值:自动设置阈值可减少手动调整,并让您发现静态限制可能遗漏的隐秘、缓慢的低强度攻击。

对支持请求进行分类和确定优先级

  • 目标:根据与计算出的平均值的偏差,计算 outcome 部分的风险得分。
  • 价值:分析师可以专注于具有最高统计显著性的检测结果,从而缩短响应时间并减少提醒疲劳。

主要术语

了解以下特定于平台的术语对于编写有效的 YARA-L 2.0 语法至关重要:

  • window. prefix:规则引擎中特定聚合函数的语法要求。它会指示平台对整个 match 窗口执行计算,而不是对单个事件执行计算。
  • outcome 部分:YARA-L 规则中用于计算变量并为检测结果提供额外上下文信息的部分。详细了解 outcome 部分语法
  • match 时间段:match 部分中定义的特定时间段(例如 7d),引擎使用该时间段来对事件进行分组。详细了解 match 部分语法

准备工作

在创建窗口聚合之前,请验证您是否具备以下访问权限并了解相关要求:

  • 权限:您必须拥有 Detection Author (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 专业人士的解答。