Detection rules › YARA-L
API access to mailboxes for top N messages via the Microsoft Graph
Identify when mailboxes are being accessed with requests for N number of messages
Rule body
// Copyright 2025 Google LLC. Licensed under Apache-2.0.
rule ms_graph_mail_api_requests_top_n_messages {
meta:
author = "Google Cloud Security"
description = "Identify when mailboxes are being accessed with requests for N number of messages"
rule_id = "mr_4c2e1d84-5569-4bde-a9ad-d747b6fb84bf"
rule_name = "API access to mailboxes for top N messages via the Microsoft Graph"
type = "Alert"
data_source = "MS Graph Activity Logs"
platform = "Azure"
severity = "Medium"
priority = "Medium"
events:
$api.metadata.event_type = "NETWORK_HTTP"
$api.metadata.product_event_type = "Microsoft Graph Activity"
$api.network.http.method = "GET"
$api.network.http.response_code = 200
re.regex($api.target.url, `^https://graph.microsoft.com/v1.0/users/.*/mailFolders/.*\?\$top\=`) nocase
//Can be tuned for specific User Agent strings being used
//$api.network.http.user_agent = /PowerShell/ nocase
$api.principal.ip = $ip
$api.network.session_id = $session
match:
$ip, $session over 5m
outcome:
$risk_score = 65
$event_count = count_distinct($api.metadata.id)
$requesting_user_guid = array_distinct($api.principal.user.userid)
$requesting_ip = array_distinct($api.principal.ip)
$user_agent = array_distinct($api.network.http.user_agent)
$location = array_distinct($api.principal.location.name)
$target_application_id_guid = array_distinct($api.target.resource.product_object_id)
$session_id = array_distinct($api.network.session_id)
$target_url = array_distinct($api.target.url)
$victim_user = array_distinct(re.capture($api.target.url, `^https://graph.microsoft.com/v1.0/users/(.*)/mailFolders/.*`))
$victim_folder = array_distinct(re.capture($api.target.url, `^https://graph.microsoft.com/v1.0/users/.*/mailFolders/(.*)/messages`))
$messages_requested = max(cast.as_int((re.capture($api.target.url, `^https://graph.microsoft.com/v1.0/users/.*/mailFolders/.*/messages\?\$top\=(\d+)`))))
condition:
//Optional threshold to set on the number of emails requested via API
$api //and $messages_requested >= 100
}
YARA-L rule structure
Condition
Fires when at least one $api event in the 5m window.
Events
$api
metadata.product_event_type = "Microsoft Graph Activity"network.http.method = "GET"network.http.response_code = "200"target.url matches "^https://graph.microsoft.com/v1.0/users/.*/mailFolders/.*\?\$top\="
Match and correlation
Outcome
Outcome variables add context to a detection. When the rule generates an alert, $risk_score is displayed with it.
| Field | Expression |
|---|---|
risk_score | 65 |
event_count | count_distinct($api.metadata.id) |
requesting_user_guid | array_distinct($api.principal.user.userid) |
requesting_ip | array_distinct($api.principal.ip) |
user_agent | array_distinct($api.network.http.user_agent) |
location | array_distinct($api.principal.location.name) |
target_application_id_guid | array_distinct($api.target.resource.product_object_id) |
session_id | array_distinct($api.network.session_id) |
target_url | array_distinct($api.target.url) |
victim_user | array_distinct(re.capture($api.target.url, `^https: |
victim_folder | array_distinct(re.capture($api.target.url, `^https: |
messages_requested | max(cast.as_int((re.capture($api.target.url, `^https: |