azure-kusto-graph

azure-kusto-graph

热门

通过自然语言构建和查询 Kusto 图。包含瞬态图 (make-graph)、持久图模型/快照、模式匹配 (graph-match)、最短路径、连通分量以及图导出为表。生成边优先思维:定义边、定义节点查找、联合、make-graph。适用场景:make-graph、graph-match、graph-shortest-paths、graph-to-table、graph-mark-components、持久图、图模型、图快照、从数据构建图、查找节点间的路径、图中的模式匹配、连通分量、瞬态图、Kusto 图、KQL 图。

1531Star
251Fork
更新于 2026/10/2
SKILL.md
只读
名称
azure-kusto-graph
描述

通过自然语言构建和查询 Kusto 图。包含瞬态图 (make-graph)、持久图模型/快照、模式匹配 (graph-match)、最短路径、连通分量以及图导出为表。生成边优先思维:定义边、定义节点查找、联合、make-graph。适用场景:make-graph、graph-match、graph-shortest-paths、graph-to-table、graph-mark-components、持久图、图模型、图快照、从数据构建图、查找节点间的路径、图中的模式匹配、连通分量、瞬态图、Kusto 图、KQL 图。

Kusto 图语义

使用 KQL 图运算符从表格数据构建瞬态图和持久图。此技能可将自然语言转换为边优先的图构建模式和图查询运算符。

激活触发条件

当用户遇到以下需求时使用此技能:

  • 希望从表格数据构建图 (make-graph)
  • 要求查找数据中的模式、路径或关系
  • 提及 graph-match、graph-shortest-paths、graph-to-table、graph-mark-components
  • 希望创建持久图模型或快照
  • 提到“构建图”、“查找最短路径”、“查找连通分量”、“显示关系”
  • 询问有关瞬态图与持久图的区别

本技能不是自然语言到 KQL 的通用转换器。 输入通常应为一个可正常运行的 KQL 查询(用户希望将其结果转换为图),外加对所需图结构的自然语言描述。仅当基础 NL 提取请求可以直接映射到具有明确列的已知表时才支持。对于通用的 NL 到 KQL 转换,请使用专用的查询生成技能(单独提供)。

互补技能:

  • azure-kusto-irql -- 可组合的安全查询原语,用于生成图的表格输入
  • azure-kusto-irql-graph -- IRQL 的 Lift_To_Graph JSON 映射系统,用于在 Kusto Explorer 中展示丰富类型与图标装饰的图

边优先方法 (The Edges-First Approach)

在 Kusto 中构建图的基本模式:

1. 定义你的边 (EDGES)       -> src --> dest,包含关系类型/属性
2. 定义你的节点查找 (NODE LOOKUPS) -> 为每个节点 ID 提供显示名称、类型、属性
3. 联合边类型             -> 如果有多种关系类型
4. 联合节点查找           -> 如果有多种节点类型
5. 调用 make-graph          -> edges | make-graph Source --> Target with nodes on nodeId

这就是在 make-graph 中思考的方式。边是您关注的关系。节点是查找表,为这些 ID 赋予外观——显示名称、类型和属性。

图运算符参考

make-graph -- 从表构建图

Edges | make-graph SourceId --> TargetId with Nodes on NodeId
  • Edges:每行为一条边的表格数据源
  • SourceId --> TargetId:包含源节点 ID 和目标节点 ID 的列
  • with Nodes on NodeId:通过 ID 连接的可选节点属性表
  • 支持多个节点表:with Nodes1 on Id1, Nodes2 on Id2
  • 在边中出现但在节点表中缺失的节点将获得空属性

graph-match -- 查找模式

G | graph-match (a)-[e]->(b) where <constraints> project <output>

模式记号:

元素 具名 匿名
节点 (n) ()
边 左->右 -[e]-> -->
边 右->左 <-[e]- <--
任意方向 -[e]- --
变长 -[e*1..5]-> -[*1..5]->

多跳模式:(a)-[e1]->(b)-[e2]->(c)
星形模式:(a)--(center)--(b), (c)--(center)--(d)
环路控制:cycles = all | none | unique_edges(默认:unique_edges)

graph-shortest-paths -- 查找最短路径

G | graph-shortest-paths (start)-[e*1..20]->(end)
      where start.name == "Alice" and end.name == "Server01"
      project Path = e, Length = array_length(e)
  • 必须包含至少一条变长边
  • output = any(默认,每对节点一条路径)或 output = all(所有等长的最短路径)
  • 变长边的属性作为动态数组返回

graph-to-table -- 将图导出为表

G | graph-to-table nodes                                     // 导出节点
G | graph-to-table edges                                     // 导出边
G | graph-to-table nodes as N, edges as E                    // 同时导出两者
G | graph-to-table nodes with_node_id=Id                     // 包含节点哈希 ID
G | graph-to-table edges with_source_id=Src with_target_id=Tgt  // 包含边端点 ID

graph-mark-components -- 查找连通分量

G | graph-mark-components with_component_id=ComponentId
  | graph-to-table nodes
  | summarize Members = make_list(name) by ComponentId

为每个节点分配一个 ComponentId。位于同一连通分量中的节点共享相同的 ID。

graph() 函数 -- 查询持久图

graph("MyGraphModel")                              // 最新快照
graph("MyGraphModel", "Snapshot_2025_01")           // 指定快照
graph("MyGraphModel", true)                         // 从模型定义生成的瞬态图

瞬态图 (Transient Graphs)

在查询执行期间动态创建。无需设置。非常适合即时分析、探索和原型设计。

模板:基础双实体图

// 1. 定义边
let edges = <SourceTable>
    | summarize <aggregations> by SourceCol, TargetCol;
// 2. 定义节点查找
let source_nodes = edges
    | distinct SourceCol
    | project nodeId = SourceCol, label = SourceCol, nodeType = "<SourceType>";
let target_nodes = edges
    | distinct TargetCol
    | project nodeId = TargetCol, label = TargetCol, nodeType = "<TargetType>";
let all_nodes = union source_nodes, target_nodes;
// 3. 构建并查询图
edges
| make-graph SourceCol --> TargetCol with all_nodes on nodeId
| graph-match (s)-[e]->(t)
    where <constraints>
    project Source = s.label, Target = t.label, <edge properties>

模板:多关系图

// 多种边类型 -> 将它们合并为统一模式
let auth_edges = AuthEvents
    | project Source = username, Target = hostname, edgeType = "authenticates", ts = timestamp;
let net_edges = NetworkEvents
    | project Source = src_ip, Target = url, edgeType = "connects", ts = timestamp;
let all_edges = union auth_edges, net_edges;
// 来自所有数据源的节点查找
let user_nodes = Employees | project nodeId = username, label = name, nodeType = "User";
let host_nodes = AuthEvents | distinct hostname | project nodeId = hostname, label = hostname, nodeType = "Host";
let all_nodes = union user_nodes, host_nodes;
all_edges
| make-graph Source --> Target with all_nodes on nodeId

持久图 (Persistent Graphs)

适用于大规模、可重用的图。存储在数据库元数据中。支持快照以进行历史对比。

安全提示: 创建或修改图模型及快照会更改数据库。在执行 .create-or-alter graph_model 或 .make graph_snapshot 之前,请务必向用户展示准确的命令并获得确认。

步骤 1:创建图模型

.create-or-alter graph_model SecurityGraph
{
  "Schema": {
    "Nodes": {
      "User": {"name": "string", "role": "string"},
      "Host": {"hostname": "string"},
      "IP":   {"ip": "string"}
    },
    "Edges": {
      "AuthenticatesTo": {"timestamp": "datetime", "result": "string"},
      "ConnectsFrom":    {"timestamp": "datetime"}
    }
  },
  "Definition": {
    "Steps": [
      {
        "Kind": "AddNodes",
        "Query": "Employees | project name, role",
        "NodeIdColumn": "name",
        "Labels": ["User"]
      },
      {
        "Kind": "AddNodes",
        "Query": "AuthenticationEvents | distinct hostname | project hostname",
        "NodeIdColumn": "hostname",
        "Labels": ["Host"]
      },
      {
        "Kind": "AddEdges",
        "Query": "AuthenticationEvents | project username, hostname, timestamp, result",
        "SourceColumn": "username",
        "TargetColumn": "hostname",
        "Labels": ["AuthenticatesTo"]
      }
    ]
  }
}

步骤 2:创建快照

.make graph_snapshot SecurityGraph Snapshot_2025_07

步骤 3:查询快照

graph("SecurityGraph")
| graph-match (user)-[auth]->(host)
    where user.role == "Admin" and auth.result == "Failed Login"
    project User = user.name, Host = host.hostname, Time = auth.timestamp

管理命令

安全提示: 以下所有控制命令都会修改或删除数据库对象。切勿自动执行 .drop、.create-or-alter graph_model 或 .make graph_snapshot。请始终展示确切的命令、集群、数据库和受影响的对象,并在执行前要求用户显式确认。

.show graph_models                        // 列出所有模型
.show graph_model SecurityGraph           // 显示模型详情
.show graph_snapshots SecurityGraph       // 列出快照
.drop graph_snapshot SecurityGraph Snapshot_2025_07  // 删除快照(请先确认)
.drop graph_model SecurityGraph           // 删除模型及其所有快照(请先确认)

瞬态图与持久图:选型指南

维度 瞬态图 (make-graph) 持久图 (graph())
配置需求 无 -- 在查询中内联定义 创建模型 + 快照
生命周期 仅在查询执行期间 存储在数据库元数据中
数据新鲜度 始终为最新 创建快照时的静态数据
规模限制 受查询内存限制 企业级规模
重用性 每次查询重新生成 跨用户/查询共享
最佳适用场景 即时排查、原型验证 生产工作流、仪表板

安全与威胁猎捕示例

身份验证图:谁在何地登录了什么

let auth_edges = AuthenticationEvents
    | summarize
        logins = count(),
        fails = countif(result == "Failed Login")
      by src_ip, username, hostname;
let ip_nodes = auth_edges | distinct src_ip
    | project nodeId = src_ip, label = src_ip, nodeType = "IP";
let user_nodes = auth_edges | distinct username
    | project nodeId = username, label = username, nodeType = "User";
let host_nodes = auth_edges | distinct hostname
    | project nodeId = hostname, label = hostname, nodeType = "Host";
let all_nodes = union ip_nodes, user_nodes, host_nodes;
// IP -> User 边
let ip_user = auth_edges
    | project Source = src_ip, Target = username, logins, fails;
// User -> Host 边
let user_host = auth_edges
    | project Source = username, Target = hostname, logins, fails;
union ip_user, user_host
| make-graph Source --> Target with all_nodes on nodeId
| graph-match (ip)-[e1]->(user)-[e2]->(host)
    where e2.fails > 20
    project
        IP = ip.label,
        User = user.label,
        Host = host.label,
        Failures = e2.fails
| order by Failures desc

横向移动检测:共享受受害主机的用户

// 模式:(user1)-[auth1]->(host)<-[auth2]-(user2)
// 两个用户在同一台主机上均出现登录失败 = 可能的凭据喷射
let edges = AuthenticationEvents
    | summarize fails = countif(result == "Failed Login"), logins = count()
      by username, hostname;
let nodes = union
    (edges | distinct username | project nodeId = username, nodeType = "User"),
    (edges | distinct hostname | project nodeId = hostname, nodeType = "Host");
edges
| make-graph username --> hostname with nodes on nodeId
| graph-match (u1)-[e1]->(h)<-[e2]-(u2)
    where u1.nodeId != u2.nodeId and e1.fails > 10 and e2.fails > 10
    project
        User1 = u1.nodeId, User2 = u2.nodeId,
        SharedHost = h.nodeId,
        User1Fails = e1.fails, User2Fails = e2.fails
| distinct User1, SharedHost, User2, User1Fails, User2Fails
| order by User1Fails + User2Fails desc

最短攻击路径

let edges = SecurityEvents
    | project Source = source_entity, Target = target_entity, action, timestamp;
let nodes = union
    (edges | distinct Source | project nodeId = Source),
    (edges | distinct Target | project nodeId = Target);
edges
| make-graph Source --> Target with nodes on nodeId
| graph-shortest-paths (start)-[e*1..10]->(end)
    where start.nodeId == "ExternalIP_1.2.3.4" and end.nodeId == "DatabaseServer"
    project
        PathLength = array_length(e),
        Actions = e.action,
        Hops = e.Target

连通分量:查找孤立集群

let edges = NetworkFlows
    | project Source = src_ip, Target = dst_ip;
let nodes = union
    (edges | distinct Source | project nodeId = Source),
    (edges | distinct Target | project nodeId = Target);
edges
| make-graph Source --> Target with nodes on nodeId
| graph-mark-components with_component_id = ComponentId
| graph-to-table nodes
| summarize Members = make_list(nodeId), Size = count() by ComponentId
| order by Size desc

在 Kusto Explorer 中进行可视化

在 make-graph 处结束查询(不通过管道传输到 graph-match)以触发 Kusto Explorer 的交互式图可视化窗口:

edges
| make-graph Source --> Target with all_nodes on nodeId
// <- 在此处停止。Kusto Explorer 将以可视化图表形式渲染图。

若要平铺回表格以用于仪表板或导出,请通过管道传输到 graph-match | project 或 graph-to-table。

与 IRQL 配合使用

处理安全数据时,考虑使用 azure-kusto-irql 技能中的 IRQL 选择器 (Get_*) 作为数据源。IRQL 为您提供统一的模式,无需记忆原始表名或列映射。要实现包含图标和节点折叠的丰富可视化,azure-kusto-irql-graph 技能中的 Lift_To_Graph 是更快捷的途径。

方案 最佳适用场景
原生 make-graph(本技能) 完全控制、持久模型、最短路径、连通分量、自定义模式
Lift_To_Graph (azure-kusto-irql-graph) 在 Kusto Explorer 中快速生成带图标装饰的可视化、节点折叠
IRQL Get_* -> make-graph 使用 IRQL 的统一模式作为输入,然后使用原生图运算符进行分析
IRQL Get_* -> Lift_To_Graph -> Graph_Render_View 从提出问题到生成可视化图表的最快途径

注意: Lift_To_Graph、Graph_Render_View 和 Graph_Fold_By_Property 是存储函数,而非内置运算符。它们已预先部署在 kc7001 示例集群上,但在其他集群上可能需要单独部署。有关函数定义和部署说明,请参阅 azure-kusto-irql-graph/references/DEPLOY_IRQL_FUNCTIONS.md。

示例:IRQL 选择器 -> make-graph -> 最短路径

IRQL 负责数据检索;make-graph 负责图分析。此示例通过认证事件查找从外部 IP 到邮件服务器的最短路径:

// IRQL 提供统一列 (ClientIp, Hostname, Username, Result)
let auth = Get_Event_Authentication_All
    | where Result == "Failed Login";
let edges = auth
    | summarize Failures = count() by ClientIp, Hostname;
let nodes = union
    (edges | distinct ClientIp | project nodeId = ClientIp, nodeType = "IP"),
    (edges | distinct Hostname | project nodeId = Hostname, nodeType = "Host");
edges
| make-graph ClientIp --> Hostname with nodes on nodeId
| graph-shortest-paths (src)-[e*1..5]->(dest)
    where src.nodeType == "IP" and dest.nodeId == "MAIL-SERVER01"
    project
        SourceIP = src.nodeId,
        PathLength = array_length(e),
        Hops = e.Hostname

示例:IRQL 选择器 -> make-graph -> 连通分量

查找互连的 IP 和域名集群——潜在的 C2 基础设施:

let dns = Get_Dns_All;
let edges = dns | project Source = ClientIp, Target = Domain;
let nodes = union
    (edges | distinct Source | project nodeId = Source, nodeType = "IP"),
    (edges | distinct Target | project nodeId = Target, nodeType = "Domain");
edges
| make-graph Source --> Target with nodes on nodeId
| graph-mark-components with_component_id = ComponentId
| graph-to-table nodes
| summarize
    IPs = make_set_if(nodeId, nodeType == "IP"),
    Domains = make_set_if(nodeId, nodeType == "Domain"),
    Size = count()
  by ComponentId
| where Size > 3
| order by Size desc

示例:IRQL + make-graph 集成

有关结合 IRQL 选择器与 make-graph 的多源调查图以及 Lift_To_Graph 可视化图示例,请参阅 references/EXAMPLES.md。

实用场景示例

有关完整的完整示例,请参阅 references/SCENARIOS.md,包含:

  • 可达性分析(到达关键资产的最短路径)
  • 网络隔离验证(连通分量)
  • 受受害账户的爆炸半径/影响范围(变长路径匹配)
  • 面向 SOC 团队的持久图模型(graph_model + 快照)

使用的 MCP 工具

工具 用途
kusto_query 执行 KQL 查询,包括 make-graph、graph-match 和管理命令
kusto_table_schema_get 在构建边/节点投影前探测表列结构
kusto_cluster_list 列出可用的 ADX 集群
kusto_database_list 列出集群中的数据库

在 Kusto Explorer 中打开查询(仅限 Windows)

可选的便捷功能。 默认工作流程是在聊天中输出 KQL,让用户手动将其复制到 Kusto Explorer 或 VS Code Kusto 扩展中。自动启动仅为选择性加入 (opt-in)。

默认:在聊天中输出 KQL

始终输出完整的 KQL,并明确标记步骤 1(连接)和步骤 2(查询):

// 步骤 1:连接到您的集群(如果已连接可跳过)
// 示例:取消注释以连接到 KC7 训练集群
// #connect cluster('kc7001.eastus.kusto.windows.net').database('ValdyTimes')
// 或替换为您自己的集群:
// #connect cluster('<YOUR_CLUSTER>').database('<YOUR_DATABASE>')

// 步骤 2:运行下方查询
<以 make-graph 结尾的 KQL_QUERY>

然后在紧下方输出 ADX Web Explorer 版本,追加 | graph-to-table nodes as N, edges as E,因为 ADX Web Explorer 无法直接渲染 make-graph:

// ADX Web Explorer 版本(表格输出):
<相同的查询>
| graph-to-table nodes as N, edges as E

这确保了输出无需用户修改即可在 Kusto Explorer(图可视化)和 ADX Web Explorer(表格结果)中正常运行。

可选:保存并启动

如果用户要求保存或在 Kusto Explorer 中打开查询,请遵循 references/KUSTO_EXPLORER_LAUNCH.md 中的流程。核心规则:

  • 在写入文件或启动可执行文件之前,务必使用 ask_user 进行确认
  • 务必在聊天中展示文件内容,以便用户在打开前审核
  • 切勿使用 Shell 插值或 here-strings —— 统一通过 Set-Content/Add-Content 写入文件
  • 切勿将查询编码到浏览器 URL 中
  • 在 macOS/Linux 上,保存 .kql 文件并建议使用 VS Code Kusto 扩展或 ADX Web Explorer

对于 make-graph 可视化(图窗口),查询必须以 make-graph 结尾 —— 不要通过管道传输到 graph-match。Kusto Explorer 仅在输出为图对象(而非表格)时才会打开图可视化窗口。