KQL Demo - Sentinel
Create queries for Microsoft Sentinel using Kusto Query Language (KQL)
Lab scenario
You are a Security Operations Analyst working at a company that is implementing Microsoft Sentinel. You are responsible for performing log data analysis to search for malicious activity, display visualizations, and perform threat hunting. To query log data, you use the Kusto Query Language (KQL).
Task 1: Access the KQL testing area.
In this task, you will access a Log Analytics environment where you can practice writing KQL statements.
Go to https://aka.ms/lademo in your browser. Login with the Administrator credentials.
Explore the available tables listed in the tab on the left side of the screen.
In the query editor, enter the following query and select the Run button. You should see the query results in the bottom window.
Notice that you have reached the maximum number of results (30,000).
Change the Time range to Last 30 minutes in the Query Window.
Next to the first record, select the > to expand the information for the row.
Task 2: Run Basic KQL Statements
In this task, you will build basic KQL statements.
Important: For each query, clear the previous statement from the Query Window or open a new Query Windows by selecting + after the last opened tab (up to 25).
The following statement demonstrates the search operator, which searches all columns in the table for the value. In the Query Window enter the following statement and select Run:
The following statement demonstrates searching across tables listed with the "in" clause. Enter the following statement and select Run:
Change back the Time range to Last 24 hours in the Query Window.
The following statements demonstrates the where operator. In the Query Window. Enter the following statement and select Run:
Important: You should "run" after entering the query from each code block below.
The following statement demonstrates the use of the let statement to declare variables. In the Query Window. Enter the following statement and select Run:
The following statement demonstrates the use of the let statement to declare a dynamic list. In the Query Window enter the following statement and select Run:
Tip: You can re-format the query easily by selecting the ellipsis (...) in the Query window and select Format query.
The following statement demonstrates the use of the "let" statement to declare a dynamic table. In the Query Window. Enter the following statement and select Run:
Change the Time range to Last hour in the Query Window. This will limit our results for the following statements.
The following statement demonstrates creating fields using the extend operator In the Query Window. Enter the following statement and select Run:
The following statement demonstrates sorting results using the order by operator. In the Query Window. Enter the following statement and select Run:
The following statements demonstrate specifying fields for the result set using the project operators.
Note: You should "Run" after entering the query from each code block below.
In the Query Window. Enter the following statement and select Run:
Task 3: Analyze Results in KQL with the Summarize Operator
In this task, you will build KQL statements to prepare data.
The following statement demonstrates the count() function. In the Query Window. Enter the following statement and select Run:
The following statement demonstrates the count() function. In the Query Window. Enter the following statement and select Run:
The following statement demonstrates the dcount() function. In the Query Window. Enter the following statement and select Run:
The following statement is a rule to detect Invalid password failures across multiple applications for the same account. In the Query Window enter the following statement and select Run:
The following statement demonstrates the arg_max() function.
The following statement will return the most current row from the SecurityEvent table for the computer SQL10.NA.contosohotels.com. The * in the arg_max function requests all columns for the row. In the Query Window. Enter the following statement and select Run:
The following statement demonstrates the arg_min() function.
In this statement, the oldest SecurityEvent for the computer SQL10.NA.contosohotels.com will be returned as the result set. In the Query Window. Enter the following statement and select Run:
The following statements demonstrate the importance of understanding results based on the order of the pipe "|". In the Query Window. Enter the following queries and run each separately:
Query 1 will have Accounts for which the last activity was a login. The SecurityEvent table will first be summarized and return the most current row for each Account. Then only rows with EventID equals 4624 (login) will be returned.
Query 2 will have the most recent login for Accounts that have logged in. The SecurityEvent table will be filtered to only include EventID = 4624. Then these results will be summarized for the most current login row by Account.
Note: You can also review the "Total CPU" and "Data used for processed query" by selecting the bar "Completed" and compare the data between both statements.
The following statement demonstrates the make_list() function.
The make_list function returns a dynamic (JSON) array of all the values of Expression in the group. This KQL query will first filter the EventID with the where operator. Next, for each Computer, the results are a JSON array of Accounts. The resulting JSON array will include duplicate accounts.
In the Query Window. Enter the following statement and select Run:
The following statement demonstrates the make_set() function.
The make_set function returns a dynamic (JSON) array containing distinct values that Expression takes in the group. This KQL query will first filter the EventID with the where operator. Next, for each Computer, the results are a JSON array of unique Accounts. In the Query Window. Enter the following statement and select Run:
Task 4: Create visualizations in KQL with the Render Operator
In this task, you will use generate visualizations with KQL statements.
The following statement demonstrates the render operator visualizing results with a barchart. In the Query Window. Enter the following statement and select Run:
The following statement demonstrates the render operator visualizing results with a time series.
The bin() function rounds values down to an integer multiple of the given bin size. Used frequently in combination with summarize by .... If you have a scattered set of values, the values are grouped into a smaller set of specific values. Combining the generated time series and pipe to a render operator with a type of timechart provides a time series visualization. In the Query Window. Enter the following statement and select Run:
Task 5: Build multi-table statements in KQL
In this task, you will build multi-table KQL statements.
The following statement demonstrates the union operator that takes two or more tables and returns the rows of all of them. Understanding how results are passed and impacted with the pipe character is essential. In the Query Window. Enter the following statements and select Run for each separately to see the results:
Query 1 will return all rows of SecurityEvent and all rows of SigninLogs.
Query 2 will return one row and column, which is the count of all rows of SecurityEvent and all rows of SigninLogs.
Query 3 will return all rows of SecurityEvent and one row for SigninLogs. The row for SigninLogs will have the count of the SigninLogs rows.
The following statement demonstrates the union operator support for wildcards to union multiple tables. In the Query Window. Enter the following statement and select Run:
The following statement demonstrates the join operator, which merges the rows of two tables to form a new table by matching the specified columns' values from each table. In the Query Window. Enter the following statement and select Run:
The first table specified in the join is considered the Left table. The table after the join keyword is the right table. When working with columns from the tables, the $left.Column name and $right.Column name is to distinguish which tables column are referenced.
Task 6: Work with string data in KQL
In this task, you will work with structured and unstructured string fields with KQL statements.
The following statement demonstrates the extract function(). Extract gets a match for a regular expression from a text string. You have the option to convert the extracted substring to the indicated type. In the Query Window. Enter the following statement and select Run:
The following statements use the extract() function to pull out the Account Name from the Account field of the SecurityEvent table. In the Query Window. Enter the following statement and select Run:
The following statement demonstrates the parse operator. Parse evaluates a string expression and parses its value into one or more calculated columns. The computed columns will have nulls for unsuccessfully parsed strings.
The following statement demonstrates working with dynamic fields, which are special since they can take on any value of other data types. In this example, The DeviceDetail field from the SigninLogs table is of type dynamic. In the Query Window enter the following statement and select Run:
The following example shows how to break out packed fields for SigninLogs. In the Query Window enter the following statement and select Run:
The following statement demonstrates operators to manipulate JSON stored in string fields. Many logs submit data in JSON format, which requires you to know how to transform JSON data to queryable fields.
In the Query Window. Enter the following statements individually and select Run:
The mv-expand operator expands multi-value dynamic arrays or property bags into multiple records.
The mv-apply operator applies a subquery to each record and returns the union of the results of all subqueries.
To create a function:
Note: You will not be able to do this in the lademo environment used for data in this lab, but it's an important concept to be used in your environment.
After running a query, select the Save button and then select Save As function from the drop-down. Enter the name your want, for example: PrivLogins in the Function name box and enter a Legacy category, like General and select Save.
The function will be available in KQL by using the function alias:
Last updated