Query Details
// Hunt : M365 - SharePoint / OneDrive External Sharing Full Audit (30d)
// Purpose : Enumerate all external sharing events from SharePoint and OneDrive
// over 30 days, with domain classification, file types, and anonymous
// link identification. Supports exfiltration path reconstruction.
// Tables : OfficeActivity
// Period : P30D
//==========================================================================================
let LookbackDays = 30d;
OfficeActivity
| where TimeGenerated > ago(LookbackDays)
| where RecordType in ("SharePoint", "OneDrive", "SharePointSharingOperation", "SharePointFileOperation")
| where Operation in (
"SharingInvitationCreated", "AnonymousLinkCreated",
"SharingSet", "AddedToSecureLink",
"SecureLinkUsed", "AnonymousLinkUsed",
"SharingInvitationAccepted")
| extend
TargetUser = tostring(TargetUserOrGroupName),
TargetDomain = tostring(split(TargetUserOrGroupName, "@")[1]),
FileExt = tostring(extract(@"(\.[a-zA-Z0-9]+)$", 1, SourceFileName)),
SharingType = tostring(parse_json(Event_Data).SharingType),
LinkScope = tostring(parse_json(Event_Data).LinkScope)
| extend
IsAnonymous = (Operation has "Anonymous" or LinkScope == "Anyone"),
IsExternal = isnotempty(TargetDomain)
and TargetDomain !has "onmicrosoft.com"
and TargetUser has "@"
| summarize
TotalShares = count(),
AnonymousShares = countif(IsAnonymous),
ExternalShares = countif(IsExternal),
UniqueRecipients = dcount(TargetUser),
FileTypes = make_set(FileExt, 10),
SampleFiles = make_set(SourceFileName, 10),
SiteURLs = make_set(Site_Url, 5),
RecipientDomains = make_set(TargetDomain, 10)
by UserId
| sort by AnonymousShares desc
| project
UserId,
TotalShares,
AnonymousShares,
ExternalShares,
UniqueRecipients,
RecipientDomains,
FileTypes,
SampleFiles,
SiteURLs
This query is designed to analyze external sharing activities in SharePoint and OneDrive over the past 30 days. Here's a simplified breakdown of what it does:
Data Source: It examines records from the OfficeActivity table, focusing on events related to SharePoint and OneDrive.
Time Frame: It looks at activities that occurred in the last 30 days.
Event Types: The query filters for specific sharing operations, such as creating sharing invitations, anonymous links, and secure links, as well as when these links are used or accepted.
Data Extraction:
Classification:
Aggregation:
Sorting and Output:
Overall, this query helps identify and analyze patterns in external sharing activities, which can be useful for security audits and understanding data exfiltration risks.

David Alonso
Released: March 18, 2026
Tables
Keywords
Operators