Reader small image

You're reading from  Mastering Microsoft 365 Defender

Product typeBook
Published inJul 2023
PublisherPackt
ISBN-139781803241708
Edition1st Edition
Right arrow
Authors (2):
Ru Campbell
Ru Campbell
author image
Ru Campbell

Ruairidh (Ru) Campbell is a Microsoft Security MVP and leads Microsoft consultancy at Threatscape. At Threatscape, Ru develops, delivers, and manages offerings and professional services for cybersecurity, compliance, identity, and management. In the cybersecurity community, Ru runs the Microsoft 365 Security & Compliance user group and his blog (campbell.scot), regularly speaks at other user groups and conferences, and contributes to well-known industry publications such as Practical 365. Ru holds 14 Microsoft certifications and a B.Sc. (Distinction) in computer networking from the University of the West of Scotland. Away from cybersecurity, he is a petrolhead who enjoys heavy metal and hiking around Scotland with his wife.
Read more about Ru Campbell

Viktor Hedberg
Viktor Hedberg
author image
Viktor Hedberg

Viktor Hedberg is a Microsoft Security MVP and senior consultant at Truesec. At Truesec, Viktor works with proactive security measures within the Microsoft sphere of technologies, by delivering workshops on best practices and by his deep technical expertise in these areas. In the cybersecurity community, Viktor runs his blogs at Truesec (Experts – viktor-hedberg). Alongside this, he is one of the hosts of the Swedish Windows Security user group, as well as a co-host of the Swedish podcast The Nerd Herd. He is a frequent speaker at both conferences and user groups around the world, focusing on matters of Microsoft Security. Viktor holds numerous Microsoft certifications, as well as being a Microsoft Certified Trainer. Away from cybersecurity, Viktor is a family man, spending most of his time with his wife and three kids, as well as enjoying football, both as a practitioner and as a fan. Heavy metal has been part of his life since his early teens.
Read more about Viktor Hedberg

View More author details
Right arrow

Advanced Hunting with KQL

This chapter will cover the exciting topic of advanced hunting with Kusto Query Language (KQL). Advanced hunting is a powerful feature that enables security analysts to proactively search for and identify potential security threats within an organization’s environment. The chapter will cover key aspects of KQL, including syntax, data types, and functions, and will provide practical examples of how to use KQL to create custom queries for advanced hunting.

Additionally, the chapter provides insights into best practices for optimizing query performance and using the results to investigate and remediate security threats. By following the instructions in this chapter, security analysts can leverage KQL and advanced hunting to enhance their organization’s overall security posture.

This chapter will cover these main topics of advanced hunting with KQL:

  • Understanding advanced hunting
  • How to construct KQL queries to hunt
  • How to create...

Understanding advanced hunting

Advanced hunting is a powerful tool for proactively hunting threats by querying up to 30 days of raw data. It allows for a comprehensive examination of events in your Microsoft 365 Defender (M365D) environment to identify potential threats and entities. With flexible access to data, you can search for both known and unknown threats.

There are two modes of advanced hunting: guided and advanced. The guided mode is suitable for those who are not familiar with KQL or prefer the ease of using a query builder. On the other hand, the advanced mode is recommended for users comfortable with creating KQL queries from scratch.

Advanced hunting enables the creation of custom detection rules by supporting queries that cover a wider range of datasets from different Microsoft Defender products, such as Microsoft Defender for Endpoint (MDE), Microsoft Defender for Office 365 (MDO), Microsoft Defender for Cloud Apps (MDA), and Microsoft Defender for Identity (MDI...

Constructing KQL queries to hunt

M365D advanced hunting has two ways of constructing queries, as mentioned in the Understanding advanced hunting section: the more basic Query in builder (guided) and the more advanced Query in editor. Let us start by looking at the builder option, as it might be just what you need to take that next step in advanced hunting:

  1. In the Microsoft 365 Defender portal, go to Advanced hunting | + Create new | Query in builder:
Figure 19.1 – Creating a new Query in builder

Figure 19.1 – Creating a new Query in builder

  1. In the builder, we can start by adding the information we want to look for in the different fields:

Image 19.2 – Showing the result of entering just a device name in the query builder

  1. From here, we can add more data that is relevant to our case simply by adding it to the corresponding text box.
  2. If we want to learn more about Kusto, in the upper right corner of the builder, there is a button...

Creating custom detections

In the KQL editor, we have a Create detection rule option, which allows us to create our detections tailored to our own environment:

Figure 19.4 – The Create detection rule option in the KQL editor

Figure 19.4 – The Create detection rule option in the KQL editor

Say we want to monitor changes to sensitive administrative groups in AD. We would start by constructing a query like this:

let MonitoredGroups = datatable(GroupName:string)
[
"Account Operators",
"Administrators",
"Backup Operators",
"Domain Admins",
"Domain Controllers",
"Enterprise Admins",
];
IdentityDirectoryEvents
| where ActionType == "Group Membership changed"
| extend AdditionalFields = parse_json(AdditionalFields)
| extend FromGroup = AdditionalFields.["FROM.GROUP"]
| extend ToGroup = AdditionalFields.["TO.GROUP"]
// Extract target user or device name
| extend TargetObject =  iff( isnull(AdditionalFields.["TARGET_OBJECT...

Summary

This chapter has been all about the wonderful world of KQL and how we should and can perform advanced hunting. Through practical examples, you have learned how to create custom queries to identify potential security threats within your organization’s environment.

In addition to technical guidance, the chapter provided lessons learned, including the importance of keeping queries simple and manageable and utilizing query sharing and collaboration features. The chapter also highlighted the need for continuous monitoring and analysis of query results to proactively identify and remediate security threats. By following the guidance and best practices outlined in this chapter, security analysts can effectively leverage KQL and advanced hunting to enhance their organization’s security posture and mitigate potential risks.

As you’ve probably already figured out, this chapter was not meant to be a KQL for dummies chapter, as that would take up the bulk of...

Questions

The best way to learn KQL is to practice it, hands-on, in the Microsoft 365 Defender portal. But, you can also test yourself with these questions:

  1. True or false? You should practice actively hunting in your environment, rather than relying on automation, to stay ahead of emerging threats that may not have their own detection yet:
    1. True
    2. False
  2. Which is this chapter’s recommended join flavor in advanced hunting?
    1. rightsemi
    2. leftsemi
    3. innerunique
    4. fullout
  3. How many days back in time can you perform advanced hunting queries?
    1. 90 days
    2. 30 days
    3. 120 days
    4. 180 days
  4. Which of the following two options can advanced hunting data be categorized into?
    1. Entity data
    2. Cloud/on-prem data
    3. Automatic/manual data
    4. Event/activity data
  5. Which of the following is not an option for how often custom queries can run?
    1. Continuous
    2. Every 24 hours
    3. Every 48 hours
    4. Every 12 hours
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Microsoft 365 Defender
Published in: Jul 2023Publisher: PacktISBN-13: 9781803241708
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Ru Campbell

Ruairidh (Ru) Campbell is a Microsoft Security MVP and leads Microsoft consultancy at Threatscape. At Threatscape, Ru develops, delivers, and manages offerings and professional services for cybersecurity, compliance, identity, and management. In the cybersecurity community, Ru runs the Microsoft 365 Security & Compliance user group and his blog (campbell.scot), regularly speaks at other user groups and conferences, and contributes to well-known industry publications such as Practical 365. Ru holds 14 Microsoft certifications and a B.Sc. (Distinction) in computer networking from the University of the West of Scotland. Away from cybersecurity, he is a petrolhead who enjoys heavy metal and hiking around Scotland with his wife.
Read more about Ru Campbell

author image
Viktor Hedberg

Viktor Hedberg is a Microsoft Security MVP and senior consultant at Truesec. At Truesec, Viktor works with proactive security measures within the Microsoft sphere of technologies, by delivering workshops on best practices and by his deep technical expertise in these areas. In the cybersecurity community, Viktor runs his blogs at Truesec (Experts – viktor-hedberg). Alongside this, he is one of the hosts of the Swedish Windows Security user group, as well as a co-host of the Swedish podcast The Nerd Herd. He is a frequent speaker at both conferences and user groups around the world, focusing on matters of Microsoft Security. Viktor holds numerous Microsoft certifications, as well as being a Microsoft Certified Trainer. Away from cybersecurity, Viktor is a family man, spending most of his time with his wife and three kids, as well as enjoying football, both as a practitioner and as a fan. Heavy metal has been part of his life since his early teens.
Read more about Viktor Hedberg