KQL分析查詢結果

KQL 是一種查詢語言,用於對資料進行分析,以建立分析、workbooks,並在 Microsoft Sentinel 和 Microsoft Defender XDR 中執行搜尋。了解如何使用 KQL 語句匯總和視覺化資料為建立高效的威脅偵測奠定了基礎。可使用此網站 LA Demo site練習本文將介紹的語句。

summarize operator

count operator及其變體使用指定column的計算結果建立一個new column。

下面的第一個語句傳回一個column,該column是「Activity」column value的唯一清單。

第二條語句傳回 SecurityEvent rows的計數,其中 EventID 等於 4688,並且該計數會按Process和電腦分組。由於使用了 by 子句,結果集包含三個column:Process、Computer、Count。

單獨執行每個查詢以查看結果。

SecurityEvent | summarize by Activity

SecurityEvent
| where EventID == "4688"
| summarize count() by Process, Computer

下面的範例是與summarize operator一起使用的最常見簡單聚合函數的部分清單。

count function example

可以透過在聚合函數之前包含“fieldname=”來明確命名聚合函數列。

KQL 語句傳回三列:「cnt」、「AccountType」和「Computer」。 “cnt”欄位名稱替換預設的“count_”名稱。

SecurityEvent
| where TimeGenerated > ago(1h)
| where EventID == 4624
| summarize cnt=count() by AccountType, Computer

<dcount> function example
以下範例傳回唯一 IP 位址的計數。

SecurityEvent
| summarize dcount(IpAddress)

以下語句是偵測相同帳號的多個應用程式中的無效密碼失敗的規則。

ResultDescription 的 where operator會過濾結果集以尋找包含「無效密碼」的結果。接下來,語句「summarize」產生應用程式名稱的不同計數以及按使用者和 IP 位址分組的計數。最後,檢查已建立的變數(閾值)以查看數量是否超過允許的數量。

let timeframe = 30d;
let threshold = 1;
SigninLogs
| where TimeGenerated >= ago(timeframe)
| where ResultDescription has "Invalid password"
| summarize applicationCount = dcount(AppDisplayName) by UserPrincipalName, IPAddress
| where applicationCount >= threshold

summarize operator to filter results

arg_max() 和 arg_min() 函數分別過濾掉最上面與最下面的一個row。

以下語句傳回電腦 SQL10.NA.contosohotels.com 的 SecurityEvent table中的most current row。 arg_max 函數中的 * 請求該row的所有欄位。

SecurityEvent 
| where Computer == "SQL10.na.contosohotels.com"
| summarize arg_max(TimeGenerated,*) by Computer

在此語句中,電腦 SQL10.NA.contosohotels.com 的最舊的 SecurityEvent 將作為結果集傳回。

SecurityEvent 
| where Computer == "SQL10.na.contosohotels.com"
| summarize arg_min(TimeGenerated,*) by Computer

Revisiting the result pipe

順序結果透過pipe character傳遞。查看以下兩個 KQL 語句。結果集之間有什麼區別?單獨執行每個查詢以查看結果。

// Statement 1

SecurityEvent
| summarize arg_max(TimeGenerated, *) by Account
| where EventID == "4624"

// Statement 2

SecurityEvent
| where EventID == "4624"
| summarize arg_max(TimeGenerated, *) by Account

Statement 1 包含最後一次活動為登入的帳號。

SecurityEvent table進行匯總並傳回每個帳號的最新row。然後僅傳回 EventID 等於 4624(登入)的row。

Statement 2 包含已登入帳號的最新登入資訊。

SecurityEvent table經過篩選,僅包含 EventID = 4624。

summarize operator to prepare data

make_ 函數根據特定函數的用途傳回Dynamic(JSON) array。

此函數傳回群組中所有 Expression 值的Dynamic(JSON) array。

此 KQL 查詢將首先使用 where 過濾 EventID。接下來,對於每台電腦,結果是帳號的 JSON array。產生的 JSON array將包含重複的帳號。

SecurityEvent
| where EventID == "4624"
| summarize make_list(Account) by Computer

傳回一個dynamic (JSON) array,其中包含expression在群組中採用的不同值。

此 KQL 查詢將首先使用 where過濾 EventID。接下來,對於每台電腦,結果是單一帳號的 JSON array。

SecurityEvent
| where EventID == "4624"
| summarize make_set(Account) by Computer

render operator to create visualizations

render operator產生查詢結果的視覺化。支援的視覺化有:

  • areachart
  • barchart
  • columnchart
  • piechart
  • scatterchart
  • timechart
SecurityEvent 
| summarize count() by Account
| render barchart

Use the summarize operator to create time series

bin() 函數將值向下捨去為特定 bin 大小的整數倍。經常與 summarize by … 結合使用。將產生的時間序列和render的管道與某種類型的時間表結合,提供時間序列視覺化。

SecurityEvent 
| summarize count() by bin(TimeGenerated, 1d)
| render timechart

--

--

運用"雲端服務"加速企業的數位轉型願景
運用"雲端服務"加速企業的數位轉型願景

Written by 運用"雲端服務"加速企業的數位轉型願景

我們協助您駕馭名為"雲端運算"的怪獸,馴服它為您所用。諮詢請來信jason.kao@suros.com.tw. https://facebook.com/jason.kao.for.cloud

No responses yet