THREAT-BEC-OAuthDeviceCode Google Chronicle · YARA-L

Detect Business Email Compromise via OAuth Device Code Flow Phishing in Google Chronicle

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

YARA-L Detection Query

Google Chronicle (YARA-L)
yaral
rule bec_oauth_device_code_phishing {
  meta:
    author = "df00tech"
    description = "Detects OAuth Device Code Flow BEC phishing in Microsoft 365: device code authentication from suspicious clients or anomalous locations, and post-compromise inbox forwarding rule creation. Threat actors: Scattered Spider, Storm-2372, Midnight Blizzard."
    severity = "CRITICAL"
    priority = "HIGH"
    reference = "https://attack.mitre.org/techniques/T1078/004/"
    mitre_attack_tactic = "Initial Access, Persistence"
    mitre_attack_technique = "T1078.004, T1114.003"

  events:
    // Event 1: Device code flow sign-in with suspicious user agent or anomalous location
    (
      $e1.metadata.event_type = "USER_LOGIN"
      AND (
        re.regex($e1.network.http.user_agent, `(?i)(python-requests|curl|wget|Go-http-client|okhttp|axios)`)
        OR (
          $e1.extensions.auth.auth_details = "deviceCode"
          AND NOT re.regex($e1.principal.location.country_or_region, `(?i)^(United Kingdom|United States)$`)
        )
      )
      AND $e1.extensions.auth.auth_details = "deviceCode"
      AND $e1.target.application = /(?i)(Microsoft|Office|Teams|SharePoint|OneDrive|Exchange)/
      AND $user = $e1.principal.user.userid
    )
    OR
    // Event 2: Inbox forwarding or deletion rule created (post-BEC follow-on)
    (
      $e2.metadata.event_type = "EMAIL_TRANSACTION"
      AND re.regex($e2.metadata.description, `(?i)(New-InboxRule|Set-InboxRule|Set-Mailbox)`)
      AND re.regex($e2.about.labels["Parameters"], `(?i)(ForwardTo|DeleteMessage|RedirectTo|ForwardAsAttachmentTo|MoveToFolder)`)
      AND NOT re.regex($e2.about.labels["Parameters"], `(?i)Junk Email`)
      AND $user = $e2.principal.user.userid
    )

  match:
    $user over 4h

  condition:
    $e1 and $e2
}
critical severity high confidence

YARA-L 2.0 rule for Google Chronicle that correlates OAuth device code flow authentication events (with suspicious automation user agents or logins from unexpected countries) with subsequent inbox rule creation containing forwarding or deletion parameters. The 4-hour match window captures the typical BEC post-compromise sequence used by Scattered Spider, Storm-2372, and Midnight Blizzard.

Data Sources

Microsoft 365 Management Activity Logs via Chronicle ingestionAzure AD Sign-In Logs via Chronicle UDM

Required Tables

UDM events with metadata.event_type USER_LOGIN and EMAIL_TRANSACTION from M365/Azure AD log sources

False Positives & Tuning

  • IT automation scripts performing legitimate device code authentication for service account provisioning
  • Users on VPN or traveling who appear to sign in from unexpected countries via device code
  • Administrators creating inbox rules programmatically as part of email migration or onboarding workflows
  • Security tools or email hygiene platforms that create inbox rules on behalf of users

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.

  1. 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.

  2. 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.

Unlock Pro Content

Get the full detection package for THREAT-BEC-OAuthDeviceCode including response playbook, investigation guide, and atomic red team tests.

Response PlaybookInvestigation GuideHunting QueriesAtomic Red Team TestsTuning Guidance

Related Detections