Detect Business Email Compromise via OAuth Device Code Flow Phishing in Sumo Logic CSE
OAuth Device Code Flow phishing is a prevalent Business Email Compromise (BEC) technique actively used by Scattered Spider, Storm-2372, and nation-state actors including Midnight Blizzard. The attacker sends a phishing message containing a Microsoft device code (a short alphanumeric code from https://microsoft.com/devicelogin), social-engineered to appear as an IT helpdesk request, MFA enrollment notification, or remote support session. When the victim enters the code, the attacker receives a valid OAuth access token and refresh token for the victim's Microsoft 365 account — with no password required. The attacker then has full access to email, Teams, SharePoint, OneDrive, and any M365 service the victim is licensed for. Refresh tokens may persist for 90 days, providing long-term access even after password reset. This technique bypasses MFA entirely because the device code flow is a legitimate Microsoft authentication mechanism.
MITRE ATT&CK
- Tactic
- Credential Access Collection
Sumo Detection Query
(_sourceCategory="o365" OR _sourceCategory="azure/signin")
| json field=_raw "Operation", "UserId", "ClientIP", "AuthenticationDetails", "Parameters", "UserAgent", "Location", "AppDisplayName", "ResourceDisplayName" nodrop
| where Operation in ("UserLoggedIn", "New-InboxRule", "Set-InboxRule", "Set-Mailbox")
| eval device_code = if(Operation = "UserLoggedIn" AND matches(AuthenticationDetails, "(?i)deviceCode"), "true", "false")
| eval suspicious_agent = if(matches(UserAgent, "(?i)(python-requests|curl|wget|Go-http-client|okhttp|axios)"), "true", "false")
| eval inbox_rule_fwd = if(
Operation in ("New-InboxRule", "Set-InboxRule", "Set-Mailbox") AND
matches(Parameters, "(?i)(ForwardTo|DeleteMessage|RedirectTo|ForwardAsAttachmentTo|MoveToFolder)") AND
!matches(Parameters, "(?i)Junk Email"),
"true", "false"
)
| where device_code = "true" OR inbox_rule_fwd = "true"
| where suspicious_agent = "true" OR inbox_rule_fwd = "true" OR (device_code = "true" AND Location not in ("GB", "US"))
| timeslice 4h
| stats
count as Events,
values(Operation) as Operations,
values(ClientIP) as IPs,
values(AuthenticationDetails) as AuthDetails,
values(Parameters) as RuleParams,
values(UserAgent) as UserAgents,
values(Location) as Locations
by UserId, _timeslice
| where Events > 1 OR inbox_rule_fwd = "true"
| fields UserId, _timeslice, Events, Operations, IPs, AuthDetails, RuleParams, UserAgents, Locations
| sort by Events desc Detects OAuth Device Code Flow BEC phishing in Microsoft 365 by identifying device code authentication events from suspicious user agents or anomalous locations, followed by inbox rule creation with forwarding or deletion actions — a hallmark post-compromise BEC pattern used by Scattered Spider, Storm-2372, and Midnight Blizzard.
Data Sources
Required Tables
False Positives & Tuning
- Legitimate IT administrators using scripting tools (Python, curl) to automate M365 management tasks via device code flow
- Users traveling internationally who authenticate via device code from unfamiliar locations
- Inbox rules created by users for legitimate email management such as auto-forwarding to a secondary personal account
- Helpdesk or IT onboarding workflows that provision inbox rules as part of standard user setup
Other platforms for THREAT-BEC-OAuthDeviceCode
Testing Methodology
Validate this detection against 2 adversary techniques from Atomic Red Team. Each test below lists the behaviour to exercise and the telemetry you should expect to see. Executable commands and cleanup steps are available with Pro.
- Test 1OAuth Device Code Flow Token Request (Simulated Phishing)
Expected signal: Azure AD Sign-in logs record a DeviceCode flow initiation. When user enters the code, AADSignInLogs records a successful authentication with AuthenticationProtocol=deviceCode.
- Test 2Create BEC Inbox Forwarding Rule via Exchange Online PowerShell
Expected signal: O365 Unified Audit Log records New-InboxRule operation with ForwardTo parameter for the authenticated user.
Response Playbook
Triage
- Confirm whether the device code sign-in was legitimate by contacting the affected user directly (NOT via email or Teams if account may be compromised). Ask if they recently used a code at microsoft.com/devicelogin.
- Check the sign-in location and IP address against the user's typical locations (Azure AD Sign-in logs, last 30 days). A sign-in from a country the user has never visited is high confidence.
- Review the OAuth token issued: which application (AppId) received the token? Legitimate apps include Microsoft Office, Outlook, Teams. Suspicious: generic app IDs, unknown application names, or 'first seen' apps.
- Check for inbox rules created within 24 hours of the device code sign-in. Look specifically for: ForwardTo (exfiltration), DeleteMessage (hiding attacker activity), MoveToFolder where the target folder is not Junk.
- Review email access (MailItemsAccessed, MessageRead operations in O365 audit) following the device code sign-in for signs of mail reading or forwarding.
- Determine if the user received a phishing email before the device code sign-in: search incoming mail for messages containing 'microsoft.com/devicelogin' or 8-character alphanumeric codes sent from external senders.
Containment
- Revoke all refresh tokens for the affected user immediately: Azure AD > Users > [User] > Revoke sessions (or via PowerShell: Revoke-AzureADUserAllRefreshToken -ObjectId <userID>).
- Delete or disable any suspicious inbox rules identified during triage.
- Block the attacker IP address(es) in Conditional Access or Named Locations policy.
- Reset the user's password and require re-enrollment of MFA devices.
- Enable Conditional Access policy to block Device Code flow for all users where possible (CA policy: Grant > Require compliant device, block legacy authentication).
- Review and revoke any OAuth application consents granted during the attacker's session (Azure AD > Enterprise Applications > review third-party apps with delegated permissions).
Evidence Collection
- Azure AD Sign-in logs: device code events, token issuances, subsequent application activity
- O365 Unified Audit Log: MailItemsAccessed, MessageRead, SendAs, SendOnBehalf operations in the incident window
- O365 inbox rules export: all rules for the affected user
- Outbound email logs: emails sent by or forwarded from the compromised account
- Conditional Access evaluation logs for the device code sign-in event
- The phishing email itself (if recoverable) for threat actor infrastructure IOCs
Escalation Criteria
- !Evidence of financial transactions initiated from compromised email (wire transfer requests, invoice changes)
- !Email access to finance team, HR, or executive mailboxes from compromised account (lateral BEC targeting)
- !OAuth consent grant to third-party application with mail.read, files.read, or contacts.read permissions
- !Evidence of email exfiltration (MailItemsAccessed showing bulk message access from attacker IP)
- !Compromised account used to send internal phishing emails to other employees
Investigation Guide
Related Techniques
Forensic Artifacts
- >
Azure AD Sign-In logs: AppId, IPAddress, UserAgent, AuthenticationDetails fields for device code events - >
O365 Unified Audit Log: all operations by the user in the 24 hours post-compromise - >
O365 MailItemsAccessed events: which messages were read and by which client IP - >
O365 message trace: all sent/forwarded messages from the compromised account - >
Azure AD OAuth token activity: which refresh tokens were issued and when they were used - >
Browser or email client forensics on victim device: the phishing message that contained the device code
Tuning Guidance
Device code flow is legitimately used for IoT device enrollment, smart TVs, and printer registration — these are usually one-time events. Tune by creating a baseline of users who have historically used device code authentication (e.g., IT staff enrolling conference room devices) and apply enrichment to distinguish these. The highest-fidelity signal is the sequence: device code auth + inbox rule with ForwardTo. Consider disabling device code flow entirely via Conditional Access policies for users who do not need it (most corporate users). Set up a Conditional Access policy to block the 'Azure AD Device Registration' flow or require compliant devices for this authentication method.
Hunting Queries
Hunt for users with multiple device code sign-ins across multiple locations — indicates ongoing credential theft campaign or compromised user repeatedly being targeted.
AADSignInLogs
| where TimeGenerated > ago(30d)
| where AuthenticationProtocol =~ "deviceCode"
| where Status.errorCode == 0
| summarize
DeviceCodeLogins=count(),
Locations=make_set(Location),
IPs=make_set(IPAddress),
Apps=make_set(AppDisplayName)
by UserPrincipalName
| where DeviceCodeLogins > 3 or array_length(Locations) > 2
| sort by DeviceCodeLogins desc index=azure sourcetype="azure:aad:signin" properties.authentication_protocol="deviceCode" properties.status.error_code=0
| stats count AS LoginCount, dc(properties.ip_address) AS UniqueIPs, values(properties.location) AS Locations
BY properties.user_principal_name
| where LoginCount > 3 OR UniqueIPs > 2
| sort - LoginCount Hunt for inbox forwarding rule creation within 4 hours of a device code sign-in — the high-confidence combined BEC indicator that storm-2372 and Scattered Spider are documented using.
OfficeActivity
| where TimeGenerated > ago(30d)
| where Operation in ("New-InboxRule", "Set-InboxRule")
| where Parameters has_any ("ForwardTo", "DeleteMessage", "RedirectTo", "ForwardAsAttachmentTo")
| join kind=leftouter (
AADSignInLogs
| where TimeGenerated > ago(30d)
| where AuthenticationProtocol =~ "deviceCode"
| project UserId=UserPrincipalName, DeviceCodeTime=TimeGenerated
) on $left.UserId == $right.UserId
| where isnotnull(DeviceCodeTime)
| extend TimeDiff=datetime_diff('minute', TimeGenerated, DeviceCodeTime)
| where TimeDiff >= 0 and TimeDiff <= 240
| project TimeGenerated, UserId, Operation, Parameters, DeviceCodeTime, TimeDiff index=o365 sourcetype="o365:management:activity" Operation IN ("New-InboxRule", "Set-InboxRule") Parameters="*ForwardTo*"
| rename UserId AS user
| join type=inner user [
search index=azure sourcetype="azure:aad:signin" properties.authentication_protocol="deviceCode"
| rename properties.user_principal_name AS user
| eval dc_time=_time
]
| where (_time - dc_time) >= 0 AND (_time - dc_time) <= 14400
| table _time, user, Operation, Parameters, dc_time, ClientIP Atomic Red Team Tests
Simulates an OAuth device code phishing attack by initiating the device code flow against a Microsoft 365 tenant and waiting for a user to authenticate at microsoft.com/devicelogin. This tests detection of device code token acquisition in AADSignInLogs.
Command
curl -s -X POST 'https://login.microsoftonline.com/<TENANT_ID>/oauth2/v2.0/devicecode' -d 'client_id=d3590ed6-52b3-4102-aeff-aad2292ab01c&scope=openid+profile+email+https://graph.microsoft.com/Mail.Read' Expected Telemetry
Azure AD Sign-in logs record a DeviceCode flow initiation. When user enters the code, AADSignInLogs records a successful authentication with AuthenticationProtocol=deviceCode.
Expected Detection
Alert fires on device code authentication event from suspicious user agent or unexpected geographic location.
Simulates the post-compromise BEC inbox rule creation that attackers use to forward victim email to external addresses after OAuth token theft.
Command
Connect-ExchangeOnline; New-InboxRule -Name 'Test_Rule' -ForwardTo '[email protected]' -Enabled $true Cleanup
Remove-InboxRule -Identity 'Test_Rule' -Force Expected Telemetry
O365 Unified Audit Log records New-InboxRule operation with ForwardTo parameter for the authenticated user.
Expected Detection
Alert fires on New-InboxRule with ForwardTo parameter. If preceded by device code sign-in within 4 hours, high confidence BEC alert.