Microsoft365 External Document Sharing


Description

Document shared externally

Query · python

import re
from fnmatch import fnmatch

from panther_base_helpers import EMAIL_REGEX
from panther_msft_helpers import m365_alert_context

ALLOWED_DOMAINS = ["mycompany.com", "alloweddomain.com"]  # should be in lowercase

ALLOWED_USERS = ["exception@outsider.com"]  # should be in lowercase

ALLOWED_PATHS = ["*/External/*", "External/*"]


def allowed_path(relative_url):
    for path in ALLOWED_PATHS:
        if fnmatch(relative_url, path):
            return True
    return False


def rule(event):
    if event.get("Operation", "") == "AnonymousLinkCreated":
        return not allowed_path(event.get("SourceRelativeUrl"))
    if event.get("Operation", "") == "AddedToSecureLink":
        if allowed_path(event.get("SourceRelativeUrl")):
            return False
        target = event.get("TargetUserOrGroupName", "")
        if target.lower() in ALLOWED_USERS:
            return False
        if re.fullmatch(EMAIL_REGEX, target):
            if target.split("@")[1].lower() not in ALLOWED_DOMAINS:
                return True
    return False


def title(event):
    return (
        f"Microsoft365: [{event.get('SourceRelativeUrl')}] "
        "has been shared with external users by "
        f"[{event.get('UserId', '<user-not-found>')}]"
    )


def alert_context(event):
    return m365_alert_context(event)

Analyst notes

Check the document metadata to ensure it is not a sensitive document.

Raw source Microsoft365 External Document Sharing · Panther Python
Esc
Published by panther-labs/panther-analysis ↗, licensed under Apache 2.0 ↗. Reproduced here unmodified.
AnalysisType: rule
Description: Document shared externally
DisplayName: "Microsoft365 External Document Sharing"
Enabled: true
Filename: microsoft365_external_sharing.py
Reports:
  MITRE ATT&CK:
    - TA0009:T1039 # Collection - Data from Network Shared Drive
Runbook: Check the document metadata to ensure it is not a sensitive document.
Reference: https://support.microsoft.com/en-us/topic/manage-sharing-with-external-users-in-microsoft-365-small-business-2951a85f-c970-4375-aa4f-6b0d7035fe35#:~:text=Top%20of%20Page-,Turn%20external%20sharing%20on%20or%20off,-The%20ability%20to
Severity: Low
Tests:
  - ExpectedResult: false
    Log:
      AppAccessContext:
        AADSessionId: aa-bb-cc
        CorrelationId: dd-ee-ff
      ClientIP: 1.2.3.4
      CreationTime: "2022-12-12 19:31:41"
      EventData: <Type>Edit</Type><MembersCanShareApplied>False</MembersCanShareApplied>
      EventSource: SharePoint
      Id: 111-aa-234
      ItemType: File
      ObjectId: https://yourorg.sharepoint.com/personal/user_yourorg/Documents/importantsecrets.docx
      Operation: AddedToSecureLink
      OrganizationId: 11-22-abc
      RecordType: 14
      Site: aa-bb-dd-ee-ff
      SiteUrl: https://yourorg.sharepoint.com/personal/user_yourorg
      SourceFileExtension: docx
      SourceFileName: importantsecrets.docx
      SourceRelativeUrl: Documents/importantsecrets.docx
      TargetUserOrGroupName: OUTSIDER@ALLOWEDDOMAIN.COM
      TargetUserOrGroupType: Guest
      UserAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
      UserId: my.user@yourorg.onmicrosoft.com
      UserKey: i:0h.f|membership|12345@live.com
      UserType: 0
      Workload: OneDrive
    Name: Allowed Domain
  - ExpectedResult: false
    Log:
      AppAccessContext:
        AADSessionId: aa-bb-cc
        CorrelationId: dd-ee-ff
      ClientIP: 1.2.3.4
      CreationTime: "2022-12-12 19:31:41"
      EventData: <Type>Edit</Type><MembersCanShareApplied>False</MembersCanShareApplied>
      EventSource: SharePoint
      Id: 111-aa-234
      ItemType: File
      ObjectId: https://yourorg.sharepoint.com/personal/user_yourorg/Documents/External/public.docx
      Operation: AddedToSecureLink
      OrganizationId: 11-22-abc
      RecordType: 14
      Site: aa-bb-dd-ee-ff
      SiteUrl: https://yourorg.sharepoint.com/personal/user_yourorg
      SourceFileExtension: docx
      SourceFileName: public.docx
      SourceRelativeUrl: Documents/External/public.docx
      TargetUserOrGroupName: MARKETING@SAAS.COM
      TargetUserOrGroupType: Guest
      UserAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
      UserId: my.user@yourorg.onmicrosoft.com
      UserKey: i:0h.f|membership|12345@live.com
      UserType: 0
      Workload: OneDrive
    Name: Allowed Folder
  - ExpectedResult: false
    Log:
      AppAccessContext:
        AADSessionId: aa-bb-cc
        CorrelationId: dd-ee-ff
      ClientIP: 1.2.3.4
      CreationTime: "2022-12-12 19:31:41"
      EventData: <Type>Edit</Type><MembersCanShareApplied>False</MembersCanShareApplied>
      EventSource: SharePoint
      Id: 111-aa-234
      ItemType: File
      ObjectId: https://yourorg.sharepoint.com/personal/user_yourorg/Documents/importantsecrets.docx
      Operation: AddedToSecureLink
      OrganizationId: 11-22-abc
      RecordType: 14
      Site: aa-bb-dd-ee-ff
      SiteUrl: https://yourorg.sharepoint.com/personal/user_yourorg
      SourceFileExtension: docx
      SourceFileName: importantsecrets.docx
      SourceRelativeUrl: Documents/importantsecrets.docx
      TargetUserOrGroupName: EXCEPTION@OUTSIDER.COM
      TargetUserOrGroupType: Guest
      UserAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
      UserId: my.user@yourorg.onmicrosoft.com
      UserKey: i:0h.f|membership|12345@live.com
      UserType: 0
      Workload: OneDrive
    Name: Allowed User
  - ExpectedResult: true
    Log:
      AppAccessContext:
        AADSessionId: aa-bb-cc
        CorrelationId: dd-ee-ff
      ClientIP: 1.2.3.4
      CreationTime: "2022-12-12 19:31:41"
      EventData: <Type>Edit</Type><MembersCanShareApplied>False</MembersCanShareApplied>
      EventSource: SharePoint
      Id: 111-aa-234
      ItemType: File
      ObjectId: https://yourorg.sharepoint.com/personal/user_yourorg/Documents/importantsecrets.docx
      Operation: AddedToSecureLink
      OrganizationId: 11-22-abc
      RecordType: 14
      Site: aa-bb-dd-ee-ff
      SiteUrl: https://yourorg.sharepoint.com/personal/user_yourorg
      SourceFileExtension: docx
      SourceFileName: importantsecrets.docx
      SourceRelativeUrl: Documents/importantsecrets.docx
      TargetUserOrGroupName: OUTSIDER@EXTERNAL.IO
      TargetUserOrGroupType: Guest
      UserAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
      UserId: my.user@yourorg.onmicrosoft.com
      UserKey: i:0h.f|membership|12345@live.com
      UserType: 0
      Workload: OneDrive
    Name: External user
  - ExpectedResult: false
    Log:
      AppAccessContext:
        AADSessionId: aa-bb-cc
        CorrelationId: dd-ee-ff
      ClientIP: 1.2.3.4
      CreationTime: "2022-12-12 19:31:41"
      EventData: <Type>Edit</Type><MembersCanShareApplied>False</MembersCanShareApplied>
      EventSource: SharePoint
      Id: 111-aa-234
      ItemType: File
      ObjectId: https://yourorg.sharepoint.com/personal/user_yourorg/Documents/importantsecrets.docx
      Operation: AddedToSecureLink
      OrganizationId: 11-22-abc
      RecordType: 14
      Site: aa-bb-dd-ee-ff
      SiteUrl: https://yourorg.sharepoint.com/personal/user_yourorg
      SourceFileExtension: docx
      SourceFileName: importantsecrets.docx
      SourceRelativeUrl: Documents/importantsecrets.docx
      TargetUserOrGroupName: ANOTHERUSER@MYCOMPANY.COM
      TargetUserOrGroupType: NotGuest
      UserAgent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
      UserId: my.user@yourorg.onmicrosoft.com
      UserKey: i:0h.f|membership|12345@live.com
      UserType: 0
      Workload: OneDrive
    Name: Internal Share
DedupPeriodMinutes: 60
LogTypes:
  - Microsoft365.Audit.SharePoint
RuleID: "Microsoft365.External.Document.Sharing"
Threshold: 1


# ------ paired body: microsoft365_external_sharing.py ------

import re
from fnmatch import fnmatch

from panther_base_helpers import EMAIL_REGEX
from panther_msft_helpers import m365_alert_context

ALLOWED_DOMAINS = ["mycompany.com", "alloweddomain.com"]  # should be in lowercase

ALLOWED_USERS = ["exception@outsider.com"]  # should be in lowercase

ALLOWED_PATHS = ["*/External/*", "External/*"]


def allowed_path(relative_url):
    for path in ALLOWED_PATHS:
        if fnmatch(relative_url, path):
            return True
    return False


def rule(event):
    if event.get("Operation", "") == "AnonymousLinkCreated":
        return not allowed_path(event.get("SourceRelativeUrl"))
    if event.get("Operation", "") == "AddedToSecureLink":
        if allowed_path(event.get("SourceRelativeUrl")):
            return False
        target = event.get("TargetUserOrGroupName", "")
        if target.lower() in ALLOWED_USERS:
            return False
        if re.fullmatch(EMAIL_REGEX, target):
            if target.split("@")[1].lower() not in ALLOWED_DOMAINS:
                return True
    return False


def title(event):
    return (
        f"Microsoft365: [{event.get('SourceRelativeUrl')}] "
        "has been shared with external users by "
        f"[{event.get('UserId', '<user-not-found>')}]"
    )


def alert_context(event):
    return m365_alert_context(event)

Detection rules belong to the projects that publish them and remain under their own licenses. This site indexes and links to them; it claims no rights in them.