Skip to content

Threat Analysis

Device code phishing: how it works and how to detect it

How attackers abuse the OAuth device code flow to steal M365 tokens without a fake login page, and how to catch and block it with KQL and Conditional Access.

Published 6 min readReviewed by The Ledger EditorIntermediate

Most phishing training teaches people to check the address bar: if the sign-in page is not really Microsoft's, do not type your password. Device code phishing turns that advice upside down. The victim signs in on the genuine Microsoft page, with their real password and their real MFA, and still hands the attacker access to their mailbox, Teams and OneDrive.

This post explains how the attack works, why it gets past controls that stop ordinary credential phishing, how to find it in Entra ID sign-in logs, and how to shut it down with Conditional Access.

The legitimate feature behind it

The OAuth 2.0 device authorization grant, usually called device code flow, exists for devices that cannot show a full browser or have no keyboard: smart TVs, meeting room screens, command-line tools, printers. The flow works like this:

  1. The device asks Microsoft's identity platform for a code. It gets back a device code, which it keeps, and a short user code for the human.
  2. The device shows a message such as "go to the sign-in page and enter code ABCD-EFGH".
  3. The user opens the page on their phone or laptop, enters the code, and signs in normally, including MFA.
  4. Meanwhile, the device keeps polling the token endpoint. As soon as the user finishes, the device receives an access token and, usually, a refresh token.

According to Microsoft's documentation, the user has 15 minutes by default to enter the code before it expires. Nothing in the flow ties the device that requested the code to the person who enters it. That is the gap attackers use.

How the attack works

In device code phishing, the attacker plays the part of the "device":

  1. The attacker requests a device code from Microsoft, and can use the client ID of a legitimate Microsoft application, such as an Office or Teams client, so everything the victim sees looks familiar.
  2. They send the user code to the victim with a pretext: a Teams meeting invite where the "meeting ID" is the code, a document share, a message saying "to join, go to the Microsoft page and enter this code".
  3. The victim visits the real Microsoft page, enters the code, signs in and completes MFA. Everything they see is genuine.
  4. The attacker's script, still polling, receives the tokens. With them it can read mail, search files and send messages as the victim, often through Microsoft Graph, and keep going with the refresh token after the access token expires.

Because the code expires in 15 minutes, attackers try to deliver it while the target is online, which is why live chat and instant messaging lures are common.

A real campaign: Storm-2372

In February 2025, Microsoft Threat Intelligence described a campaign it tracked as Storm-2372, active since August 2024 against government, NGOs, IT services, defense, telecommunications, health, higher education and energy organizations in Europe, North America, Africa and the Middle East. Microsoft later assessed the group as a sub-cluster of Midnight Blizzard focused on initial access.

The operators first built rapport over WhatsApp, Signal or Microsoft Teams, posing as a prominent person relevant to the target, and then sent a fake Teams meeting invitation with the device code as the meeting ID. After a successful compromise, they searched the victim's mailbox through Graph for words such as password, admin and credentials, exfiltrated what they found, and sent new device code lures from the victim's account to colleagues. A later variant used the client ID of the Microsoft Authentication Broker, which let them register a device in Entra ID and obtain a Primary Refresh Token for longer-lasting access.

Why ordinary defenses miss it

  • There is no fake domain. URL filtering, lookalike-domain detection and user training about the address bar do not help: the page is login.microsoftonline.com.
  • MFA succeeds, by design. The victim completes MFA themselves. Even phishing-resistant MFA such as passkeys does not stop it, because the victim is authenticating to the real Microsoft site. This is different from adversary-in-the-middle phishing, where the proxy domain is what passkeys defeat.
  • No consent prompt for a malicious app. Unlike consent phishing, the attacker often uses a first-party Microsoft client ID, so there is no unknown application to review.
  • The tokens are real. Once issued, they look like a normal session from a supported client.

How to detect it

Find device code sign-ins

Entra ID records the protocol used for each sign-in. In the Entra admin center, the sign-in logs have an Authentication protocol filter with a Device code option. In Microsoft Sentinel, the same information is in the AuthenticationProtocol column of SigninLogs:

device-code-signins.kql
SigninLogs
| where TimeGenerated > ago(30d)
| where AuthenticationProtocol == "deviceCode"
| summarize SignIns = count(), Apps = make_set(AppDisplayName, 5),
    IPs = make_set(IPAddress, 5), Countries = make_set(Location, 5)
    by UserPrincipalName
| sort by SignIns desc

In most organizations this list is short and predictable: a few admins using command-line tools, meeting room devices, perhaps a service desk. That baseline is what makes the attack visible. A device code sign-in for a user who has never used one, from a hosting provider or an unusual country, deserves an immediate look.

Check where the token was used

The victim enters the code on their own device, but the tokens are redeemed by the attacker. Compare the location of the device code sign-in with the user's normal sign-ins in the same hour:

device-code-new-location.kql
let deviceCode = SigninLogs
    | where TimeGenerated > ago(7d)
    | where AuthenticationProtocol == "deviceCode" and ResultType == "0"
    | project DeviceCodeTime = TimeGenerated, UserPrincipalName,
        DeviceCodeIP = IPAddress, DeviceCodeCountry = tostring(LocationDetails.countryOrRegion);
SigninLogs
| where TimeGenerated > ago(7d)
| where AuthenticationProtocol != "deviceCode" and ResultType == "0"
| project UserPrincipalName, UsualIP = IPAddress, UsualCountry = tostring(LocationDetails.countryOrRegion), TimeGenerated
| join kind=inner deviceCode on UserPrincipalName
| where abs(datetime_diff("minute", TimeGenerated, DeviceCodeTime)) < 60
| where UsualCountry != DeviceCodeCountry
| distinct UserPrincipalName, DeviceCodeTime, DeviceCodeIP, DeviceCodeCountry, UsualCountry

A user who signs in normally from one country and, within the same hour, completes a device code sign-in from another is a strong lead.

Look for the lure and what came after

  • The message. Microsoft's own hunting guidance for this campaign looks in Defender for Office 365 click data (UrlClickEvents) for URLs containing microsoft.com/devicelogin or login.microsoftonline.com/common/oauth2/deviceauth. Device code pages in email or Teams messages from outside the organization are rare and suspicious.
  • The follow-up. After a hit, check the mailbox for searches and mass reads through Graph, new inbox rules, messages sent to colleagues with the same lure, and new devices registered in Entra ID for the user.

How to respond

  1. Revoke the user's sessions and refresh tokens

    Resetting the password alone is not enough, because the attacker holds tokens, not the password. Revoke the user's sessions in Entra ID (or with the revokeSignInSessions Graph action) so refresh tokens stop working, and confirm the user as compromised in Identity Protection if you use it.

  2. Remove anything the attacker added

    Delete inbox rules and forwarding the user did not create, remove devices the attacker registered, and review app consents and MFA methods on the account.

  3. Scope the campaign

    Search for the same lure sent to other users, and run the device code query for everyone, not just the first victim. Campaigns rarely stop at one mailbox.

  4. Block the flow

    If device code flow is not needed, block it before the next attempt, as described below.

How to prevent it

Microsoft's recommendation is direct: block device code flow wherever possible.

  • Use the Conditional Access Authentication flows condition to block device code flow for all users, and exclude only the accounts or devices that genuinely need it, such as specific meeting room devices, limited by location where possible. Start in report-only mode and check the sign-in logs filtered by authentication protocol, as covered in Conditional Access policies every tenant needs.
  • Check for a Microsoft-managed policy. Eligible tenants may already have a Microsoft-managed policy that blocks device code flow in report-only mode. Turn it on rather than creating a duplicate.
  • Limit device registration to the users who need it, which blocks the Primary Refresh Token variant.
  • Add a sign-in risk policy if you have Entra ID P2, so risky sessions are challenged or blocked.
  • Tell users the new rule: a legitimate meeting never asks you to enter a code on a Microsoft sign-in page. If someone sends you a code to type in, stop and report it.

Why it matters for a SOC analyst

Device code phishing is a good example of why a sign-in that "passed MFA" is not automatically safe. When you investigate an identity alert, look at how the sign-in happened, not just whether it succeeded: the authentication protocol, the client app, the location compared with the user's other sign-ins in the same hour. A successful device code sign-in from an unusual place, followed by mailbox activity, is an incident until proven otherwise. The suspicious sign-in investigation walks through the rest of that process.

Terms in this article

Device code phishing
A phishing technique that tricks a user into entering an attacker's device code on the genuine Microsoft sign-in page, handing the attacker valid tokens.
Phishing
A message that impersonates someone trustworthy to trick the recipient into clicking a link, opening a file, sharing credentials or sending money.
Adversary-in-the-middle phishing
Phishing that relays the victim's sign-in to the real site through an attacker's proxy, stealing the session cookie after MFA succeeds.
Consent phishing
An attack that tricks a user into granting a malicious cloud app permission to their data, giving the attacker access without stealing a password.
Phishing-resistant MFA
Authentication methods, such as passkeys, FIDO2 keys and Windows Hello for Business, that cannot be relayed or approved by a phished user.
Conditional Access
Microsoft Entra ID's policy engine that decides, at each sign-in, whether to allow access, require MFA or another control, or block.
KQL
The read-only query language used by Microsoft Sentinel, Defender XDR advanced hunting, Azure Monitor and Azure Data Explorer.

Last reviewed on by The Ledger Editor, Security Engineer