KQLとは?
Kusto Query Languageと呼ばれ、Microsoftのサービス内で動作する読み取り専用のリクエストとなり、データを処理して結果を返すものです。Microsoft Defender やMicrosoft Sentinelのログの詳細分析やAzure Data Explorer、Azure Resourcce Graph、Application Insightでも利用されているため、覚えておくと色々なサービスで活用できます。
基本構文
ソースとなるテーブルに対して 演算子を| (パイプ) で並べていきます。
データは、演算子から次の演算子へと流れていき、その中で処理(フィルター処理、並べ替え、集計)が行われます。
この処理の順番はパフォーマンスにも影響してくるため、より効率的な演算を考える必要があります。
![](https://www.satorifactory.jp/wp-content/uploads/image-310-1200x578.png)
よく使うオペレーター
今回は以下のようなサンプルレコードを想定して、よく使うオペレーターを見ていきます。
サンプルテーブル
Username | Device | Location | Status |
---|---|---|---|
John | Windows11 | Tokyo, JP | Success |
Emily | Windows 10 | Yamanashi, JP | Fail |
Michael | Mac OS X | Chiba, JP | Success |
Sarah | iPhone | Melbourne, AU | Success |
David | iPhone | Singapore, SG | Fail |
文字列検索(Search)
特定のキーワードを含むレコードを検索します。
SinginLogs
| search "Tokyo, JP"
Username | Device | Location | Status |
---|---|---|---|
John | Windows11 | Tokyo, JP | Success |
テーブルのフィルター処理
テーブルの特定の列の値に対してフィルター処理をします。
SigninLogs
| where Device == “iPhone” and Location ==”Singapore, SG”
Username | Device | Location | Status |
---|---|---|---|
David | iPhone | Singapore, SG | Fail |
指定した行数のデータを取得(Take)
対象のレコードから指定した最新のレコードを取得します。
SigninLogs
| take 2
Username | Device | Location | Status |
---|---|---|---|
John | Windows11 | Tokyo, JP | Success |
Emily | Windows 10 | Yamanashi, JP | Fail |
入力したテーブルのレコード数を取得(Count)
対象テーブルのレコード数をカウントします。
SigninLogs
| count
Count |
---|
5 |
入力テーブルの内容を集計(Summarize)
Status の値ごとに該当するレコードをカウントします。
SigninLogs
| summarize Total = count() by Status
Status | Total |
---|---|
Success | 3 |
Fail | 2 |
新しいフィールドを追加(Extend)
ソーステーブルのLocation を split でカンマで区切りにし、配列に代入します。
Location を split でカンマで区切りにし、Extendで追加したCountryに設定します。
SigninLogs
| extend Country = split(Location, “,”)[1]
Username | Device | Location | Status | Country |
---|---|---|---|---|
John | Windows11 | Tokyo, JP | Success | JP |
Emily | Windows 10 | Yamanashi, JP | Fail | JP |
Michael | Mac OS X | Chiba, JP | Success | JP |
Sarah | iPhone | Melbourne, AU | Success | AU |
David | iPhone | Singapore, SG | Fail | SG |
出力するフィールドを指定(Project)
指定した列を出力します。
SigninLogs
| project Location
Location |
---|
Tokyo, JP |
Yamanashi, JP |
Chiba, JP |
Melbourne, AP |
Singapore, SG |
まとめ
今回ご紹介したもの以外にもたくさんの演算子がありますので公式リファレンスをご参照ください。
こちらの記事で実際にMicrosoft DefenderのAdvanced Hunting機能でKQLを使っている様子をご紹介します。
公式リファレンス:Kusto 照会言語 (KQL) の概要 – Azure Data Explorer | Microsoft Learn
コメント