将 N OF 语法与事件变量搭配使用
本文档介绍了如何使用 N OF 语法在查询的 condition 部分中编写灵活的条件逻辑。
借助 N OF 语法,您可以组合多个条件,从而降低查询语法的总体复杂性:
- 提供了一种指定给定列表中的布尔值表达式必须至少有多少个计算结果为 true 的方法。
- 避免了指定每种可能组合的需求,从而减少了错误和工作量。
N OF 语法
N OF 语句的语法是以英文逗号分隔的布尔值表达式列表,如下所示:
[expr1, expr2, … , exprN]([expr1, expr2, … , exprN])
以下部分展示了 N OF 语法的一些变体。
ANY OF
以下示例用于检查列表中的表达式是否至少有一个为 true。
这与使用 or 连接所有表达式相同。
condition:
ANY OF [$e1, #e2 > 5, $outcome1 = "FAILED"]
ALL OF
如果以下列表中的所有表达式均为 true,则相应示例为 true。这与使用 and 连接所有表达式相同。
condition:
ALL OF [$e1, $e2, $outcome1 = "SUCCESS"]
X 的 N
如果列表中的表达式至少有 N 个为 true,则此函数的计算结果为 true。
condition:
2 of [$e1, $e2 > 0, $e3, arrays.contains($outcome_ips, "1.2.3.4")]
规则示例:标记敏感文件访问
以下规则会在用户成功登录 ($e1) 时标记该用户。然后,该规则会检查会话是否还满足以下两个条件之一:访问了敏感文件 ($e2) 或访问的唯一敏感文件总数超过 3。
rule MultiEventNOf {
meta:
author = "google-secops"
description = "Detects user login followed by sensitive file access or multiple sensitive files."
events:
$e1.principal.user.userid = $user
$e1.metadata.event_type = "USER_LOGIN"
$e2.principal.user.userid = $user
$e2.metadata.event_type = "FILE_OPEN"
re.regex($e2.target.file.full_path, `^/cns/sensitive/`)
match:
$user over 1h
outcome:
$sensitive_file_count = count_distinct($e2.target.file.full_path)
condition:
$e1 and ANY OF [$e2, $sensitive_file_count > 3]
}
搜索示例:标记敏感文件访问
以下示例展示了如何将示例规则重新格式化为搜索查询。
$e1.principal.user.userid = $user
$e1.metadata.event_type = "USER_LOGIN"
$e2.principal.user.userid = $user
$e2.metadata.event_type = "FILE_OPEN"
re.regex($e2.target.file.full_path, `^/cns/sensitive/`)
match:
$user over 1h
outcome:
$sensitive_file_count = count_distinct($e2.target.file.full_path)
condition:
$e1 and ANY OF [$e2, $sensitive_file_count > 3]
已知限制
必需的事件存在:N OF 语法的表达式不能是不受限的。事件的存在必须是子句为 true 的必要条件(例如
$e1或#e1 > 0)。变量类型混合:您无法在同一 N OF 列表中将非 UDM 变量与 UDM 事件变量组合使用。
窗口限制:您无法将滚动窗口与 N OF 语法搭配使用。
后续步骤
需要更多帮助?获得社区成员和 Google SecOps 专业人士的解答。