Published on February 27th, 2023 📆 | 3982 Views ⚑
0Microsoft 365 OAuth Device Code Flow and Phishing
During a recent red team engagement, we found that the target organization used a well-known identity access management (IAM) product for their multi-factor authentication (MFA) solution. Most of their Internet-facing login portals, such as Office365 and Citrix portals, were behind this IAM platform; however, their users were allowed to enable multiple MFA options, including Email OTP (one-time passcode). Given these situations, we thought a phishing campaign abusing Microsoft 365 OAuth device code flow would be a great attack vector for potentially bypassing their MFA and gaining access to the applications from the Internet.
Â
Â
Â
Â
Microsoft OAuth Authentication Flow
According to Microsoft, it uses OAuth device authorization grant to allow users to sign in to input-constrained devices such as smart TVs, IoT devices or printers. To enable this authorization flow, the device will have the user visit a webpage from another deviceâs browser to sign in. Once the user signs in, the device can request access tokens as needed.
Â
Â
Â
Â
To explain what this is doing:
Â
- A user initiates an application on a device, which supports this device authorization grant flow.
- The application connects the /devicecode endpoint with the client_id of the application and the permission scopes, such as Files.ReadWrite.
- The /devicecode endpoint sends back user_code, device_code and verification_uri.
- The user now visits the verification_uri and submits the user_code.
- Azure will prompt the user to complete the authentication process and accept the presented permission scopes.
- The device continuously connects to the /token endpoint with client_id and device_code. After the userâs successful login, it gets access_token, refresh_token (if offline_access scope was consent) and id_token.
Â
Â
How Can We Abuse This for Phishing?
Â
- An attacker creates a fake Azure App leveraging Azure Active Directory.
- The attacker requests user_code, device_code and verification_uri by connecting to /devicecode endpoint with client_id of the created Azure App.
- After receiving necessary items, the attacker sends a phishing email to a victim soliciting them to visit the verification_uri and enter the user_code.
- The victim submits the user_code and completes the normal Microsoft sign-in, and accepts the permission consent.
- The attacker requests access_token and refresh_token from /token endpoint to impersonate the victim.
- The access_token can then be used to access the victimâs Office365 products, including Outlook mail leveraging Microsoft Graph API.
- By default, the access_tokens are valid for 60 days and refresh_tokens are valid for a year Thus, with refresh_token, one can continuously re-request for the victimâs access_token for persistence purposes.
Â
Â
Practical Example of Microsoft OAuth Device Code Phishing
One perk about this phishing technique is that we do not need to build custom phishing infrastructure (e.g., creating a phishing server and landing page). We can use pretty much everything provided by Microsoft since we are technically leveraging their legitimate OAuth authentication flow.
Â
Creating Azure Application
Â
First, we need to create an Azure application. This application will serve as an endpoint where we will generate client_id and device_code, which are later used for obtaining the victimâs access_token, refresh_token and id_token.
Â
Steps: Azure Portal â Azure Active Directory â App registrations â New registration
Â
Â
Â
Â
Next, enter a name for the application. This can be anything such as âOptiv Remote User Managementâ, etc. For Supported account types, select âAccounts in any organizational directory (Any Azure AD directory â Multitenant) and personal Microsoft accounts (e.g., Skype, Xbox)â so that target users could use their business Microsoft accounts to allow the application consents.
Â
Â
Â
Â
This will create our Azure application and make a note for the Application (client) ID value. This will serve as client_id value.
Â
Â
Â
Â
Finally, change Allow public client flows to âYesâ under the Authentication section and select âSave.â
Â
Â
Â
Â
Launching Phishing Campaign
Â
To conduct the device code phishing, we first need to make a POST request to /devicecode endpoint to obtain user_code and device_code. Postman, an API testing and development platform, can be very useful for this.
Â
1) HTTP POST Request to /devicecode | |
---|---|
Endpoint | https://login.microsoftonline.com/organizations/oauth2/v2.0/devicecode |
client_id | 54c9f794-489c-4473-8407-XXXXX (*Application ID from the Azure App) |
scope | Contacts.Read Files.ReadWrite Mail.Read Notes.Read Mail.ReadWrite openid profile User.Read email offline_access |
Â
*Note: A full list of the scope can be found at Microsoft.com. Make sure to carefully check permissions required for each scope since some require admin consent.
Â
Â
Â
Â
Next, upon obtaining the user_code, we need to send a phishing email to the victim. This is the only tricky part of this phishing attack since you need to lure the victim to visit the verification_uri and enter the user_code. Another caveat is that obtained user_code and device_code will expire 15 minutes after they have been generated. Thus, if they expire, you will be required to regenerate them and resend the phishing email.
Â
For our engagement, we came up with the following phishing email template:
Â
Â
Â
Â
We registered our phishing domain to send emails from Microsoft Outlook. Also, the reason why we did not embed the hyperlink for the verification_uri (https://microsoft.com/devicelogin) was that our phishing email kept landing in the spam folder. Though we are not 100% sure, we think Microsoft may have done some subtle filtering for hyperlinked verification_uri and user_code combinations not originating from a Microsoft email domain. We observed this behavior from our dynamic testing at that time, so we decided to make our phishing email like the above.
Â
If the phishing attempt was successful and the victim visited the verification_uri, entered the user_code, and completed authentication, the victim would have seen the following devicelogin and permission consent pages:
Â
Â
Â
Â
Â
Â
Â
Â
Â
Â
As for the attacker, they will need to keep checking whether the victim completed the authorization process by sending a POST request to /token API endpoint after the phishing email is sent. Once the authorization process is completed, it will present a bearer token formatted access_token and refresh_token, which you can use to mimic the victimâs access.
Â
2) HTTP POST Request to /token | |
---|---|
Endpoint | https://login.microsoftonline.com/organizations/oauth2/v2.0/token |
client_id | 54c9f794-489c-4473-8407-XXXXX (*Application ID from the Azure App) |
code | W5WkvHGOjuQwlAOFB7Qo4ni8PjfAYRKx6tGqv39c9_sIgAA) |
grant_type | urn:ietf:params:oauth:grant-type:device_code |
Â
Â
Â
Â
Â
Â
For example, with the access_token, the attacker can now access the victimâs email inbox using Microsoft Graph API.
Â
Â
Â
Â
Automation of Device Code Phishing
Â
Going back to our red team scenario, our goals were to:
Â
- Launch a Microsoft 365 device code phishing attack
- Gain access to the target userâs inbox (*The targets were ones whom we had already compromised passwords for via password spraying against their MDM solution. We used airCross created by Matt Burch to perform a password guessing attack.)
- Trigger the Email OTP for MFA options
- Read the created Email OTP code from the victimâs inbox
- Bypass MFA
Â
We originally found a great open-source framework, TokenTactics created by revrsh3ll to conduct our phishing attack. The tool was designed to contain all the necessary functions and tactics to abuse Azure JSON Web Token (JWT). We highly recommend trying it out. Leveraging his tool, however, we decided to create a simple Python script to automate some of the device code phishing flow to solely meet our goals. The first function getDeviceCode() was created to request for user_code and device_code.
Â
Â
Â
Â
Â
Â
Next, using the newly obtained device_code, the getAccessToken() function continuously loops through the authorization process. If the phishing attack is successful, it will yield an access_token and refresh_token; otherwise, the loop will end after 15 minutes due to the token expiration.
Â
Â
Â
Â
Â
Â
Â
Â
With access_token, the getMail() function keeps looking for an email containing specific text. For our case, it was looking for a string âMFA â One Time Codeâ within the email text, so if we were to trigger the Email OTP, it would have sent the temporary code to the victimâs inbox. If it finds the right email, it prints out the email context with the OTP code and saves the email_id value.
Â
Â
Â
Â
Â
Â
Â
Â
Finally, with the stored email_id value, the deleteMail() function will delete the OTP email from the victimâs inbox.
Â
Â
Â
Â
Â
The script used in this demo can be found here: Microsoft365-devicePhish
Â
Â
Mitigation of Microsoft OAuth Device Code Phishing
This device code phishing attack can be mitigated by making configuration changes for Azure AD. Under the Enterprise applications settings, prevent users from giving consent to all applications.
Â
Â
Â
If the above solution is too restrictive, then please check out the following best practices provided by Microsoft:
Â
Â
Â
References
Â
Gloss