KQL - 7. Automate Response with KQL
.
Automation is not just a buzzword in the cybersecurity community - it's a necessity. Discover how to elevate your security operations with my upcoming post, "Automate Response with KQL." Learn how to harness the power of Kusto Query Language to create custom detection rules and automate incident responses, transforming your SOC into a proactive and highly efficient defense mechanism.
In the realm of cybersecurity, automation is a critical component for enhancing efficiency and ensuring timely responses to threats. KQL plays a pivotal role in this automation process, especially when integrated with Microsoft Sentinel. By utilizing custom detection rules in Defender XDR and Sentinel analytic rules, security operations centers can significantly amplify their threat detection and response capabilities. This blog post delves into the power of automating response with KQL, exploring its benefits, best practices, and practical applications.
Automating detections in Defender XDR and Sentinel using KQL can revolutionize the way security operations centers handle threats. By defining specific criteria through custom detection rules and analytic rules, SOCs can tailor KQL queries to match their unique threat landscape. These rules can automatically trigger alerts and initiate predefined responses when suspicious activities are identified, such as multiple failed login attempts indicative of a brute-force attack.
Sentinel analytic rules further enhance this capability by enabling continuous monitoring and analysis of log data. These rules can execute automated responses, from alerting and ticket creation to isolating compromised systems or blocking malicious IP addresses. Integrating these automated processes in Defender XDR and Sentinel allows for real-time detection and swift responses, significantly enhancing the efficiency and efficacy of security operations. Embracing KQL for automation ensures that digital assets are protected with precision and agility, ultimately strengthening the overall security posture.
Custom detection rules in Microsoft Defender XDR allow security operations centers to define specific criteria for identifying and responding to suspicious activities. These rules utilize Kusto Query Language (KQL) queries, which can be customized to suit the unique threat landscape of an organization. By automating the detection of threats such as multiple failed login attempts, these rules help in triggering alerts and initiating predefined responses, thereby enhancing the efficiency and effectiveness of security operations.
In the dynamic world of cybersecurity, the ability to respond swiftly and accurately to threats is paramount. While Microsoft Sentinel offers robust analytic capabilities, there might be instances where organizations have not yet integrated Sentinel into their security operations or prefer not to wait the approximately 10 minutes it can take for an incident to emerge. In such scenarios, Custom Detection Rules in Defender XDR present an invaluable alternative.
Custom Detection Rules in Defender XDR provide immediate advantages by enabling real-time detection and response. These rules empower security operations centers to define specific criteria tailored to their unique threat landscape. With this level of customization, organizations can ensure that their security measures are precise and highly effective.
When you have to create Custom Detection Rules in Defender XDR it offers a wide array of actions that can be automatically triggered upon detecting suspicious activities.
Create the rule:

With the query in the query editor, select Create detection rule and specify the following alert details:
Rule frequency
When you save a new rule, it runs and checks for matches from the past 30 days of data. The rule then runs again at fixed intervals, applying a lookback duration based on the frequency you choose:
Ensure that the time filters in your query align with the lookback duration. Results outside of this duration will be disregarded.
When a rule is edited, it will execute with the applied changes at the next scheduled run time, according to the set frequency. The frequency of the rule is determined by the event timestamp rather than the ingestion time.
Continuous (NRT) frequency
Running custom detection in Continuous (NRT) frequency helps your organization identify threats faster with minimal impact on resource usage. Consider this frequency for any qualified custom detection rule.
Specify the actions needed:

Actions on devices
These actions are applied to devices in the DeviceId column of the query results:
Actions on files
Actions on users
For more details on user actions, read Remediation actions in Microsoft Defender for Identity.
Actions on emails
Set the rule scope
Set the scope to specify which devices are covered by the rule. The scope influences rules that check devices and doesn't affect rules that check only mailboxes and user accounts or identities.
When setting the scope, you can select:
Only data from devices in the scope will be queried. Also, actions are taken only on those devices.
By leveraging these automated actions, Custom Detection Rules in Defender XDR not only enhance the responsiveness and effectiveness of security operations but also contribute to a more resilient and proactive security posture. This proactive approach ensures that organizations can stay ahead of potential threats, safeguarding their digital assets with agility and precision.
Sentinel analytic rules leverage KQL to perform continuous monitoring and analysis of log data. These rules are designed to automatically detect and respond to threats by executing predefined actions when certain conditions are met. By using analytic rules, SOCs can automate routine tasks such as alerting, ticket creation, and even initiating automated responses like isolating compromised systems or blocking malicious IP addresses.
Get started creating a scheduled query rule
To get started, go to the Analytics page in Microsoft Sentinel to create a scheduled analytics rule.

Name the rule and define general information
Name: A unique name for your rule.
Description: A free-text description for your rule.
Severity: Match the impact the activity triggering the rule might have on the target environment, should the rule be a true positive.
Informational: No impact on your system, but the information might be indicative of future steps planned by a threat actor.
Low: The immediate impact would be minimal. A threat actor would likely need to conduct multiple steps before achieving an impact on an environment.
Medium: The threat actor could have some impact on the environment with this activity, but it would be limited in scope or require additional activity.
High: The activity identified provides the threat actor with wide ranging access to conduct actions on the environment or is triggered by impact on the environment.
MITRE ATT&CK: Choose those threat activities which apply to your rule. Select from among the MITRE ATT&CK tactics and techniques presented in the drop-down list. You can make multiple selections.
Status: Enabled: The rule runs immediately upon creation, or at the specific date and time you choose to schedule it (currently in PREVIEW).
Disabled: The rule is created but doesn't run. Enable it later from your Active rules tab when you need it.

Define the rule logic
The next step is to set the rule logic which includes adding the Kusto query that you created.
Rule query: Paste the query you designed, built, and tested into the Rule query window. Every change you make in this window is instantly validated, so if there are any mistakes, you’ll see an indication right below the window.
Map entities: Expand Entity mapping and define up to 10 entity types recognized by Microsoft Sentinel onto fields in your query results. This mapping integrates the identified entities into the Entities field in your alert schema.
Surface custom details in your alerts: Expand Custom details and define any fields in your query results you wish to be surfaced in your alerts as custom details. These fields appear in any incidents that result as well.
Customize alert details: Expand Alert details and customize otherwise-standard alert properties according to the content of various fields in each individual alert. For example, customize the alert name or description to include a username or IP address featured in the alert.
Set the following parameters in the Query scheduling section:
Run query every: Controls the query interval: how often the query is run.
Allowed range: 5 minutes to 14 days.
Lookup data from the last: Determines the lookback period: the time period covered by the query.
Allowed range: 5 minutes to 14 days.
Must be longer than or equal to the query interval.
Start running Automatically: The rule will run for the first time immediately upon being created, and after that at the query interval.
At specific time (Preview): Set a date and time for the rule to first run, after which it will run at the query interval.
Allowed range: 10 minutes to 30 days after the rule creation (or enablement) time.
Use the Alert threshold section to define the sensitivity level of the rule. For example, set a minimum threshold of 100:
Generate alert when number of query results: Is greater than
Number of events: 100
If you don’t want to set a threshold, enter 0 in the number field.
Under Event grouping, choose one of two ways to handle the grouping of events into alerts:
Group all events into a single alert
(default): The rule generates a single alert every time it runs, as long as the query returns more results than the specified alert threshold above. This single alert summarizes all the events returned in the query results.
Trigger an alert for each event: The rule generates a unique alert for each event returned by the query. This is useful if you want events to be displayed individually, or if you want to group them by certain parameters—by user, hostname, or something else. You can define these parameters in the query.
To suppress a rule beyond its next run time if an alert is generated, turn the Stop running query after alert is generated setting On. If you turn this on, set Stop running query for to the amount of time the query should stop running, up to 24 hours.
In the Results simulation area, select Test with current data to see what your rule results would look like if it had been running on your current data. Microsoft Sentinel simulates running the rule 50 times on the current data, using the defined schedule, and shows you a graph of the results (log events). If you modify the query, select Test with current data again to update the graph. The graph shows the number of results over the time period defined by the settings in the Query scheduling section.


Configure the incident creation settings
In the Incident settings tab, choose whether Microsoft Sentinel turns alerts into actionable incidents, and whether and how alerts are grouped together in incidents.
In the Incident settings section, Create incidents from alerts triggered by this analytics rule is set by default to Enabled, meaning that Microsoft Sentinel will create a single, separate incident from each and every alert triggered by the rule.
Set alert grouping settings.
In the Alert grouping section, if you want a single incident to be generated from a group of up to 150 similar or recurring alerts (see note), set Group related alerts, triggered by this analytics rule, into incidents to Enabled, and set the following parameters.
b. Limit the group to alerts created within the selected time frame: Set the time frame within which the similar or recurring alerts are grouped together. Alerts outside this time frame generate a separate incident or set of incidents.
c. Group alerts triggered by this analytics rule into a single incident by: Choose how alerts are grouped together:
Group alerts into a single incident if all the entities match: Alerts are grouped together if they share identical values for each of the mapped entities (defined in the Set rule logic tab above). This is the recommended setting.
Group all alerts triggered by this rule into a single incident: All the alerts generated by this rule are grouped together even if they share no identical values.
Group alerts into a single incident if the selected entities and details match: Alerts are grouped together if they share identical values for all of the mapped entities, alert details, and custom details selected from the respective drop-down lists.
d. Re-open closed matching incidents: If an incident has been resolved and closed, and later on another alert is generated that should belong to that incident, set this setting to Enabled if you want the closed incident re-opened, and leave as Disabled if you want the alert to create a new incident.
Select Next: Automated response.

Review or add automated responses

Validate configuration and create the rule

Automating response with KQL offers a multitude of benefits for security operations centers. Some of the key advantages include:
By automating routine tasks and responses, SOCs can free up valuable time and resources, allowing security analysts to focus on more complex and strategic activities. This leads to a more efficient and productive security operation.
Automation enables real-time detection and response to security incidents, significantly reducing the time it takes to mitigate threats. This rapid response is crucial in minimizing the impact of cyberattacks and preventing further damage.
Automated responses ensure that predefined actions are executed consistently and accurately, reducing the risk of human error. This consistency is vital in maintaining a robust security posture and ensuring that threats are handled appropriately.
As organizations grow and their IT environments become more complex, manual threat detection and response become increasingly challenging. Automation with KQL allows SOCs to scale their operations seamlessly, handling larger volumes of data and more sophisticated threats with ease.
When utilizing KQL for automating response in your SOC, consider the following best practices to maximize its effectiveness:
Create comprehensive and well-defined detection rules that cover a wide range of potential threats. Regularly review and update these rules to ensure they remain relevant and effective in detecting the latest attack techniques.
Integrate machine learning models with your KQL queries to enhance detection capabilities. Machine learning can help identify anomalous behavior that may not be easily detectable through traditional rule-based methods.
Set up automated responses for common security incidents to ensure timely and consistent action. For example, automate the isolation of a compromised system or the blocking of malicious IP addresses to quickly contain threats.
Encourage collaboration within your SOC by sharing effective queries and insights. This collective knowledge can significantly enhance your team’s overall threat detection and response proficiency.
Automating response with KQL is a game-changer for modern SOCs, providing the speed, accuracy, and scalability needed to stay ahead of sophisticated cyber threats. By developing robust detection rules, leveraging advanced features like machine learning, and fostering collaboration within your team, you can maximize the effectiveness of KQL in your security operations. Embrace automation to enhance your incident response capabilities and ensure a robust defense against ever-evolving cyber threats.