Home lab · detection engineering
Detecting Device Code Phishing in Microsoft Sentinel
Attack run in an isolated tenant · allowlist-tuned analytics rule fired an incident · contained and blocked with Conditional Access
Why I built it
I wanted hands-on detection work in a Microsoft cloud environment, not just on-prem telemetry, and device code phishing was the right thing to build around because it is current. It is the Entra ID technique behind the Storm-2372 campaigns, and it is interesting precisely because the sign-in it produces looks legitimate. I ran the whole loop myself in an isolated tenant, from the attack through the detection to the response, so I would understand where each control actually sits.
Walkthrough
Attack to detection to response, one step at a time. Click any screenshot to view it full size.
- AttackStep 1 / 8
Starting the device code flow
Playing the attacker, I generated a device code with the Microsoft Graph CLI from a Kali VM. In a real campaign this is the lure: the attacker starts the flow and sends the victim a short code with a pitch to enter it at microsoft.com/devicelogin. I ran it from a VPN exit in another state so the sign-in would land on an IP far from my normal Michigan activity.
Fig 1The device code flow kicked off from Kali. The code and URL are what a victim would be social-engineered into using. What this showsNothing malicious touches the victim’s machine here. The attack rides a legitimate Microsoft authentication flow, which is exactly why attachment scanning and URL filtering miss it, and why the detection has to live in the sign-in logs.
- AttackStep 2 / 8
The attacker inherits an authenticated session
I redeemed the code as a standard user to stand in for a victim completing the sign-in, MFA included. The token came back to the session that started the flow, which in a real attack belongs to the attacker. The context dump confirms a live delegated session for the victim account, obtained through the device code flow.
Fig 2A valid delegated session for the victim account, with the device-code credential type, held by the machine that started the flow. What this showsThis is the part people get wrong about device code phishing: it does not break MFA, it sidesteps it. The victim satisfies MFA and the attacker inherits the resulting token, so adding another factor does not help. Blocking the flow is the control that does.
- DetectStep 3 / 8
Hunting for the sign-in
Switching to the defender seat, I pulled every device code sign-in in the tenant. The benign admin account using the CLI from my home IP sits right next to the attacker account signing in from New York. One field I expected, AuthenticationProtocol, came back empty in this tenant, so I keyed on the signals that were actually present: the Microsoft Graph Command Line Tools application and the mobile and desktop client type.
Fig 3Every device code sign-in in the window: a legitimate admin from home and the attacker from a different state, same auth method. What this showsDevice code auth is not malicious by itself, which is the trap. A rule that alerts on every device code sign-in would bury the SOC. The real signal is an account using it that has no business doing so, from somewhere it never signs in.
- DetectStep 4 / 8
Tuning out the false positives
The detection work was the allowlist. I carved out the admin account that legitimately uses CLI auth so the rule stays quiet on it, and fires only on an account that should never authenticate this way. After that, the attacker sign-in from New York is the only thing left, with a successful token issuance.
let allowlist = dynamic(["itops@<tenant>.onmicrosoft.com"]); SigninLogs | where AppDisplayName == "Microsoft Graph Command Line Tools" | where ClientAppUsed == "Mobile Apps and Desktop clients" | where UserPrincipalName !in~ (allowlist) | project TimeGenerated, UserPrincipalName, IPAddress, Location, ResultType, ResultDescription | order by TimeGenerated descFig 4With the allowlist applied, the legitimate admin drops out and only the anomalous account from New York remains. What this showsThis is the difference between a rule a SOC keeps and one they mute on day one. A detection’s value is in its false-positive rate as much as its catch rate, and the allowlist is where that judgment lives.
- DetectStep 5 / 8
From query to analytics rule
I promoted the query to a scheduled Sentinel analytics rule, mapped it to the techniques it actually catches, T1528 (Steal Application Access Token) and T1566 (Phishing), and mapped the user and source IP as entities so an analyst can pivot straight from the alert.
Fig 5The rule tied to MITRE ATT&CK, with the account and IP mapped as entities for investigation. What this showsMapping to ATT&CK by the behavior, not just a label, is what lets a detection slot into coverage tracking. It turns a one-off query into something a detection program can measure and defend.
- DetectStep 6 / 8
The incident fires on its own
With the rule live, the attacker sign-in generated a Sentinel incident without me running anything. It arrives with the entities attached and the response steps written into it, so the next analyst opens a case that is ready to work.
Fig 6The detection firing automatically and producing an investigable incident, not just a query result. What this showsA detection that only works when someone runs the query is a hunt, not a control. The jump from a good query to an automated, enriched incident is the part that actually reduces time to detect on a live queue.
- RespondStep 7 / 8
Containing the account
I worked the response in order. First I revoked the account’s sessions, which invalidates the refresh token the attacker is holding, then I reset the password. Doing it the other way around leaves the stolen token alive.
Fig 7Revoking every session for the compromised account to kill the attacker’s refresh token. What this showsRevoke before reset. A password reset alone does not touch an already-issued refresh token, so the attacker keeps access until the session is explicitly revoked. That ordering is the detail that separates real containment from a checkbox.
- PreventStep 8 / 8
Blocking it for good
Finally I added a Conditional Access policy that blocks the device code flow, excluded the break-glass account so I could not lock myself out, tested it in report-only first, then enforced it and confirmed the next attempt was blocked. Newer tenants get this from Security Defaults by default, but rebuilding it as an explicit, audited policy is how an organization that has moved past Security Defaults would actually run it.
Fig 8The Conditional Access policy blocking the device code flow, running in report-only before enforcement. What this showsReport-only first is deliberate: it proves the policy would not break legitimate access before it starts blocking. Shipping a blocking control straight to enforced is how you cause an outage and lose the business’s trust in the security team.
MITRE ATT&CK coverage
In a real SOC
In a real tenant this rule would need to cover more than the Graph CLI, since device code phishing targets other first-party clients too, and the allowlist would come from asking which service accounts and admins actually use CLI auth rather than guessing. The response matters more than the detection: the window between the token being issued and the session being revoked is the whole incident, so the revoke-first ordering is the part I would least want a tired analyst to get wrong. Everything here ran against my own accounts in an isolated tenant, and the one sign-in I show as successful required me to turn off a default protection to produce it.
What I learned
- Device code phishing does not break MFA, it sidesteps it. The victim completes MFA and the resulting token flows to the attacker, so the control that actually stops it is blocking the flow, not adding another factor.
- Security Defaults already blocks the device code flow in new tenants, a change Microsoft made in July 2026, which is why my first attempts came back blocked with a 530035. I documented the blocked attempts, then turned Security Defaults off to show the attack succeeding, then rebuilt the block as an explicit Conditional Access policy, which is closer to how an organization that has outgrown Security Defaults would handle it.
- The detection is only as good as its allowlist. The useful version knows which accounts are expected to use CLI auth and stays quiet on them, which is the difference between a rule a SOC keeps and one they mute on day one.
- Revoking the refresh token versus resetting the password is the detail that separates real containment from a checkbox. A reset alone leaves the stolen token valid, so the revoke has to come first.