catalinacarlen/splunk-spl-cheatset

GitHub: catalinacarlen/splunk-spl-cheatset

Splunk SPL实用参考,简化SIEM分析和威胁狩猎。

Stars: 0 | Forks: 0

# Splunk SPL 快速参考 Splunk 搜索处理语言 (SPL) 的实用、无废话参考——用于在 SIEM 中搜索、关联和可视化机器数据的查询语言。作为学习辅助和快速参考,用于安全分析和威胁狩猎。 ## 1. 搜索结构 搜索从左到右读取;数据通过由 `|` 分隔的命令 `pipeline` 流动。 ``` index=web sourcetype=access_combined status=500 ← search terms (filter early!) | stats count by clientip ← transform | sort -count ← order | head 10 ← limit ``` **黄金法则:** 在第一个管道之前尽可能多地过滤。你越早丢弃事件(通过 `index`、`sourcetype`、时间范围),搜索就越快、成本就越低。 ## 2. 时间 | 任务 | SPL | |------|-----| | 上 15 分钟 / 24 小时 / 7 天 | 使用时间选择器,或 `earliest=-15m` / `-24h` / `-7d` | | 相对范围 | `earliest=-7d@d latest=now` | | 精确到天开始 | `@d`(也适用于 `@h`、`@w`、`@mon`) | | 绝对时间 | `earliest="10/01/2024:00:00:00"` | | 将事件分入时间区间 | `bin _time span=1h` | ## 3. 过滤与搜索词 | 目标 | SPL | |------|-----| | 字段等于 | `status=404` | | 通配符 | `uri_path="/admin*"` | | 布尔 | `status=500 OR status=503`,`NOT status=200` | | 比较 | `bytes>10000`,`duration<=2` | | 字段存在 | `field=*` | | 在集合中 | `status IN (500, 502, 503)` | | 自由文本 | `"failed password"` | ## 4. 核心命令 | 命令 | 功能 | 示例 | |---------|--------------|---------| | `fields` | 保留/删除字段(加快搜索) | `\| fields clientip, status` | | `table` | 以表格形式显示结果 | `\| table _time, user, action` | | `rename` | 重命名字段 | `\| rename clientip AS src_ip` | | `dedup` | 删除重复事件 | `\| dedup user` | | `sort` | 排序结果 | `\| sort -count`(降序) | | `head` / `tail` | 前 / 后 N 个 | `\| head 20` | | `where` | 使用表达式过滤 | `\| where count > 100` | | `eval` | 创建/修改字段 | `\| eval mb=bytes/1024/1024` | | `search` | 在管道中间过滤 | `\| search status=500` | ## 5. 统计与聚合 | 命令 | 用途 | 示例 | |---------|-----|---------| | `stats` | 在所有事件上聚合 | `\| stats count, avg(bytes) by host` | | `tstats` | 在索引/加速数据上快速统计 | `\| tstats count where index=* by sourcetype` | | `chart` | 聚合到表格以进行图表化 | `\| chart count over status by host` | | `timechart` | 时间聚合 | `\| timechart span=1h count by status` | | `top` | 最常见的值(+ %) | `\| top limit=10 src_ip` | | `rare` | 最少见的值 | `\| rare user` | | `eventstats` | 将聚合作为字段添加到每个事件 | `\| eventstats avg(bytes) as avg_b` | | `streamstats` | 运行/累积统计 | `\| streamstats count by user` | **常见的 `stats` 函数:** `count`、`dc`(不同计数)、`sum`、`avg`、`min`、`max`、`values`、`list`、`latest`、`earliest`、`range`、`stdev`、`perc95`。 ``` index=auth action=failure | stats count AS failures, dc(user) AS users_tried by src_ip | where failures > 20 | sort -failures ``` ## 6. eval — 万能工具 ``` | eval status_class = case( status>=500, "server_error", status>=400, "client_error", status>=200, "ok", true(), "other") ``` | 函数 | 示例 | |----------|---------| | `if(cond, a, b)` | `eval flag=if(bytes>1e6,"big","small")` | | `case(...)` | 多分支(见上面) | | `coalesce(a,b)` | 第一个非空值 | | `round(x, n)` | `eval mb=round(bytes/1048576, 2)` | | `len(x)` | 字符串长度 | | `lower()/upper()` | 大小写转换 | | `strftime/strptime` | 时间戳 ↔ 文本时间 | | `mvcount()/mvindex()` | 多值字段 | ## 7. 关联与查找 | 命令 | 用途 | |---------|-----| | `lookup` | 从 CSV/KV 存储中丰富事件(例如,将 IP 映射到资产所有者) | | `inputlookup` | 读取查找表作为事件 | | `join` | 两个搜索的 SQL 风格连接(谨慎使用——成本高昂) | | `append` / `appendcols` | 合并结果集 | | `transaction` | 将相关事件组合成一个(通过会话、用户等) | ``` index=firewall | lookup threat_intel ip AS src_ip OUTPUT category, score | where score > 80 ``` ## 8. 安全/威胁狩猎模式 **暴力破解(许多失败,一个来源):** ``` index=auth action=failure | bin _time span=5m | stats count by _time, src_ip | where count > 10 ``` **可能的数据泄露(大量出站):** ``` index=firewall direction=outbound | stats sum(bytes_out) AS total by src_ip | where total > 1000000000 | sort -total ``` **每个主机上的罕见进程(异常):** ``` index=endpoint sourcetype=process | rare limit=20 process by host ``` **首次出现(之前未见过的值):** ``` index=auth | stats earliest(_time) AS first_seen by user, src_ip | where first_seen > relative_time(now(), "-24h") ``` ## 9. 输出与可视化 | 命令 | 用途 | |---------|-----| | `timechart` | 时间序列图表 | | `chart` | 柱状图/饼图按字段 | | `stats` + table | 表格仪表板 | | `eval` + `gauge` | 单值 KPI | | `outputcsv` / `outputlookup` | 保存结果 | ## 10. 性能提示 - 总是指定 `index=` 和 `sourcetype=`——在生产环境中永远不要搜索 `index=*`。 - 在第一个 `|` 之前过滤;在转换之后。 - 早期使用 `fields` 来删除未使用的字段。 - 在大型/加速数据集上,优先使用 `tstats` 而不是 `stats`。 - 当 `stats by` 可以完成任务时,避免使用 `join` 和 `transaction`。 - 缩小时间范围——这是最大的性能杠杆。 由 [Catalina Carlen](https://github.com/catalinacarlen) 制作 · 信息安全 — Palermo 大学
标签:Cheatsheet, Core Commands, Data Analysis, Data Visualization, Dedup, Fields, Filtering, Head, Limit, Lookups, Machine Data, Order, Pipeline, Query Language, Reference Guide, Rename, Search Processing Language, Search Syntax, Search Terms, Security Analysis, Security Information and Event Management, Security Operations, Sort, SPL, Stats/Eval, Table, Threat Hunting, Time Filtering