Blog · Deliverability
The OAuth Device Code Attack That Slips Past Every Email Filter
The attack starts with a link you would click
An employee receives a convincing email. It might look like a SharePoint document sharing request or a Teams meeting invite. The link leads to a page that says "sign in with Microsoft" and asks for a short code. The employee enters the code, approves the sign-in, and the page closes.
Nothing seems wrong. The employee's credentials were not stolen. There was no fake login page to inspect. MFA fired as expected. But behind the scenes, the attacker now holds a valid access token and refresh token with the same permissions as the compromised application. The session can persist for weeks or months without triggering a password reset or suspicious activity alert.
This is device-code phishing. It exploits a legitimate part of Microsoft's OAuth infrastructure to sidestep every email security control that relies on detecting malicious URLs, credential theft, or fake login pages.
How the OAuth device authorization flow works in normal use
The OAuth device authorization grant (RFC 8628) exists because some applications cannot open a web browser directly. Think of the Teams desktop app on Windows or the Intune Company Portal on a managed device. These apps use a two-step flow.
First, the app contacts Microsoft's authorization endpoint and receives a short code plus a URL. The user visits the URL, enters the code, and completes authentication in a browser window. The app polls the token endpoint using the short code as proof. When the user finishes authenticating, the app receives access and refresh tokens.
The flow is convenient. It lets users sign in to rich clients without copying credentials into a terminal. It works with MFA because the authentication happens in a real browser session with all the user's registered authentication methods available.
How attackers turned a convenience feature into an attack vector
The attack misuses this flow by controlling both sides of the exchange. The attacker runs a malicious application in the cloud and controls the phishing page the user sees. The flow looks like this:
The user clicks a link in a phishing email. The phishing page instructs the user to visit microsoft.com/devicelogin and enter a short code. The user complies, authenticates with their real Microsoft credentials, and approves the sign-in. Meanwhile, the attacker's application polls Microsoft's token endpoint and receives a valid access token the moment the user completes authentication.
The access token typically includes scopes for email, calendar, Teams, and SharePoint. The refresh token allows session renewal without re-authentication. Unless Conditional Access policies intervene, the attacker now has persistent access to the victim's Microsoft 365 data that works through most of the victim's existing security controls.
Microsoft's own threat intelligence team published details on this attack pattern in 2024, and it has since become a known technique in real-world intrusions tracked by multiple security vendors.
tune Company Portal on a managed device. These apps use a two-step flow.First, the app contacts Microsoft's authorization endpoint and receives a short code plus a URL. The user visits the URL, enters the code, and completes authentication in a browser window. The app polls the token endpoint using the short code as proof. When the user finishes authenticating, the app receives access and refresh tokens.
The flow is convenient. It lets users sign in to rich clients without copying credentials into a terminal. It works with MFA because the authentication happens in a real browser session with all the user's registered authentication methods available.
How attackers turned a convenience feature into an attack vector
The attack misuses this flow by controlling both sides of the exchange. The attacker runs a malicious application in the cloud and controls the phishing page the user sees. The flow looks like this:
The user clicks a link in a phishing email. The phishing page instructs the user to visit microsoft.com/devicelogin and enter a short code. The user complies, authenticates with their real Microsoft credentials, and approves the sign-in. Meanwhile, the attacker's application polls Microsoft's token endpoint and receives a valid access token the moment the user completes authentication.
The access token typically includes scopes for email, calendar, Teams, and SharePoint. The refresh token allows session renewal without re-authentication. Unless Conditional Access policies intervene, the attacker now has persistent access to the victim's Microsoft 365 data that works through most of the victim's existing security controls.
Microsoft's own threat intelligence team published details on this attack pattern in 2024, and it has since become a known technique in real-world intrusions tracked by multiple security vendors.
Why email filters miss this entirely
Standard email security operates before the dangerous action occurs. Link sandboxing detonates URLs in a browser emulator. Reputation services check the URL against blocklists. Anti-phishing models look for fake login pages or typosquatted domains.
None of this applies to device-code phishing. The URL the user visits is genuinely microsoft.com. The login page is legitimately hosted by Microsoft. The authentication completes normally. There is no credential theft to flag, no forged password page to detect, and no suspicious domain to block.
URL-based controls cannot distinguish between a legitimate device code flow and an attacker-controlled one. The user is authentically logging into Microsoft's infrastructure. The attacker's advantage is that the entire authentication happens on infrastructure that every email security system trusts by default.
Conditional Access policies that stop device-code phishing
The device code flow can be restricted without breaking legitimate device enrollment. The controls live in Microsoft Entra Conditional Access.
Block the device code flow for unapproved applications. In Conditional Access, create a policy targeting All cloud apps with a grant control that requires approved client apps. This blocks the OAuth device authorization grant for applications that are not explicitly approved, while allowing legitimate device enrollment flows used by managed apps like Teams or Company Portal.
Require compliant devices. A policy that requires Hybrid Azure AD joined or Intune compliant devices will block device-code phishing attempts from unmanaged personal devices. The attacker receives tokens, but those tokens cannot access resources that require a managed device context.
Restrict user consent to verified publishers. In Entra app consent settings, limit user consent to applications published by verified publishers only. This prevents the attacker from registering a malicious OAuth application that users can inadvertently consent to.
Enable Continuous Access Evaluation. CAE revokes tokens in real time when a user's risk level changes, when Conditional Access policy changes, or when the user is offboarded. Without CAE enabled, a stolen refresh token can survive policy changes and remain active until expiry.
Require authentication strength for device code grants. If your tenant supports it, configure a policy that requires phishing-resistant MFA (FIDO2 or Windows Hello for Business) specifically for device code flows, rather than allowing any MFA method.
What DMARC, SPF, and DKIM have to do with this
Device-code phishing is not a DMARC problem. The attack does not spoof a sending domain or forge email headers. But email authentication standards contribute to the overall threat landscape in ways that matter for defense.
Most device-code phishing campaigns begin with a convincing phishing email. DMARC with a reject policy makes it harder for attackers to send forged emails from your domain. If your domain publishes p=reject, attackers cannot send messages that appear to come from your organization without triggering alignment failures in receiving mail systems.
SPF and DKIM alignment reported through DMARC aggregate reports can surface unusual sending patterns that may indicate a compromised tenant being used for further phishing distribution. If your Microsoft 365 tenant is compromised through device-code phishing, the attacker may use it to relay phishing emails to other organizations. DMARC reports from your receiving partners can catch when your domain is being used as a sender in a campaign you did not authorize.
DMARCFlow monitors aggregate reports for exactly this kind of anomaly. Unusual spikes in authentication failures, sudden changes in sending volume, or DKIM alignment failures from sources you do not control are early signals that something is wrong. These signals do not prevent device-code phishing, but they can catch the follow-on activity that makes a compromised tenant valuable to attackers.
The mitigation stack that actually works
Device-code phishing exploits the gap between email security and identity security. Email filters operate before delivery. Identity controls operate during authentication. The attack succeeds because it moves the action into a space where email security controls have no visibility.
The effective stack combines email authentication, identity controls, and monitoring.
At the email layer, DMARC with p=reject prevents spoofing and provides visibility into who is sending mail on behalf of your domain. SPF and DKIM do the authentication work; DMARC provides the reporting.
At the identity layer, Conditional Access policies block the device code flow for untrusted applications, require compliant devices, and enforce phishing-resistant MFA for high-privilege accounts.
At the monitoring layer, watch for unusual OAuth consent events in Entra, unexpected device code flow initiations from unfamiliar applications, and spikes in sending volume or authentication failures reported through DMARC.
No single control stops this attack. Email authentication prevents the delivery vector. Identity controls prevent the exploitation. Monitoring catches what the other layers miss.
Frequently Asked Questions
Can device-code phishing be detected by traditional phishing email filters?
No. The attack uses a legitimate Microsoft URL (microsoft.com/devicelogin) and completes a genuine authentication flow. Email filters have no signal to distinguish a legitimate device code grant from an attacker-controlled one. The attack is invisible to link sandboxing, URL reputation services, and anti-phishing heuristics.
Does MFA stop device-code phishing?
No. MFA completes as normal during the device code flow. The user enters their password and approves the MFA challenge on a legitimate Microsoft authentication page. The attack does not bypass MFA in the traditional sense; it simply uses a flow where MFA is expected and therefore does not raise an alert.
Can Conditional Access fully prevent device-code phishing?
Conditional Access policies are the most effective control. Policies that block the device code flow for unapproved applications, require compliant devices, or restrict consent to verified publishers significantly reduce the attack surface. However, no single policy is bulletproof. A defense-in-depth approach that combines Conditional Access with monitoring and email authentication is more effective than any individual control.
What should I monitor to catch device-code phishing early?
Monitor Entra sign-in logs for device code flow usage from unusual applications or IP addresses. Watch for OAuth app consent events to unverified publishers. Track DMARC aggregate reports for unexpected sending patterns from your domain. Sudden spikes in authentication failures or unusual sending volume can indicate that a compromised tenant is being used to relay phishing.
Does DMARC prevent device-code phishing?
No. DMARC prevents email spoofing, not OAuth session theft. However, DMARC with a reject policy makes it harder for attackers to send phishing emails from your domain, and DMARC aggregate reports provide visibility into unusual sending patterns that may indicate a compromised tenant being used for further attacks.