惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
博客园 - 叶小钗
爱范儿
爱范儿
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
阮一峰的网络日志
阮一峰的网络日志
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
美团技术团队
T
Tailwind CSS Blog
博客园 - 司徒正美
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
宝玉的分享
宝玉的分享
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Cloudflare Blog
人人都是产品经理
人人都是产品经理
小众软件
小众软件
博客园 - 【当耐特】
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell

Black Hills Information Security, Inc.

Bad Habits: An ANTISOC Operation Same Problem, Different Angles: When Red Team and Blue Team Actually Talk to Each Other How to Identify and Exploit New Vulnerabilities Swapper – A Pure Regex Match/Replace Burp Extension A Practical Guide to BloodHound Data Collection Network Engineering Basics Signed, Trusted, and Abused: Proxy Execution via WebView2 Getting Started In Pentesting – Advice From The BHIS Pentest Lead Cloud Security: Tips and Resources for Securing the Cloud Lessons From A Chatbot Incident How to Lead Effective Tabletops Understanding GRC: How to Navigate Risks and Compliance Standards The “P” in PAM is for Persistence: Linux Persistence Technique Malware Analysis: How to Analyze and Understand Malware OSINT: How to Find, Use, and Control Open-Source Intelligence What to Do with Your First Home Lab When the SOC Goes to Deadwood: A Night to Remember Social Engineering and Microsoft SSPR: The Road to Pwnage is Paved with Good Intentions Common Cyber Threats Finding the Right Penetration Testing Company Deceptive-Auditing: An Active Directory Honeypots Tool The Curious Case of the Comburglar How to Set Smart Goals (That Actually Work For You) Inside the BHIS SOC: A Conversation with Hayden Covington Abusing Delegation with Impacket (Part 3): Resource-Based Constrained Delegation Why You Got Hacked – 2025 Super Edition Abusing Delegation with Impacket (Part 2): Constrained Delegation Abusing Delegation with Impacket (Part 1): Unconstrained Delegation GoSpoof – Turning Attacks into Intel Model Context Protocol (MCP)
Dynamic Device Code Phishing
BHIS · 2023-05-17 · via Black Hills Information Security, Inc.

rvrsh3ll // 

Introduction 

This blog post is intended to give a light overview of device codes, access tokens, and refresh tokens. Here, I focus on the technical how-to for standing up and operating a Dynamic Device Code phishing campaign. I’ll cover some key points during the attack flow so operators and defenders will better understand areas to key-in on to help avoid or detect, depending on your intent. 

Codes  

To better understand what a “device code” is, take into consideration when purchasing a new smart television. That television may come with apps installed such as Netflix or Hulu. Well, in order to use those applications, you need to sign in to your account. Now, we don’t want to be mashing TV remote buttons all day trying to type out our 24-character password, do we?  

No, we do not. Enter device codes. By navigating to https://netflix.com/tv8, we’re prompted to enter a code from the TV as seen below. 

Once the correct code is entered on the computer, the TV is signed in. The same premise applies to device code phishing. We, as the attacker, generate the code to give the user. That code is for https://microsoft.com/devicelogin as seen below. 

When the user signs in with the code, we receive the credentials package which includes access and refresh tokens. 

Tokens 

Access tokens or “access_tokens” are exactly what they sound like. They are JSON web tokens that allow users to securely access Microsoft endpoints such as MSGraph. More detailed documentation on access_tokens may be found at https://learn.microsoft.com/en-us/azure/active-directory/develop/access-tokens. Access tokens are typically good for 1 hour. 

Refresh tokens or “refresh_tokens” allow users to refresh their access_token for up to 90 days. A great way to stay entrenched in a target environment. 

TokenTactics 

TokenTactics is a PowerShell module for generating device codes and refreshing refresh tokens created by Bobby Cooke @0xBoku and myself. Token tactics’ main feature is the ability to refresh tokens to different audiences. Say, for example, you phished a user and received an MSGraph token. If you wanted to use that access to read the user’s email, you can refresh that token to the Outlook audience endpoint, which, gives you an access_token for Outlook. The token can be played in a POST request via BurpSuite to gain access to Outlook in the browser. That’s just one example. TokenTactics can be used to generate the device codes that you would send to the target user. 

OG Device Code Phishing 

Device code phishing started with offensive operators having to generate the code in PowerShell and send it to the target user via email or other means. One major downside to this is that device codes have a timeout window of 15 minutes. That’s a very restrictive time frame to work with on a phishing engagement. What if the target user doesn’t see your phish within that timeframe? Well, as the phisher, you’re out of luck. We need a better option. 

Dynamic Device Code Phishing 

In order to enhance our chances for phishing success, we need to extend that 15-minute window of opportunity during the phishing campaign. To do so, I use Azure Web Apps to deploy a static HTML page to an “.azurewebsite.net” site. This site is merely a front while JavaScript performs a GET request to the device code API, presents the user with the code, and sends a “capturecode” to us, the attacker, so that when the target user signs in, we receive the token package on a virtual private server running TokenTactics.  

This method extends our window of opportunity as the user’s actions generate the device code by browsing to the site. Then, our 15-minute timeout window begins. Since the user has visited the site, there’s a chance they’ll continue to login during that window. 

To better understand this flow, I’ve created a diagram. 

Here are some key points to the diagram: 

  • Source IP is always where the device code was generated. 
  • 15 minutes start when visiting the azurewebsite. 
  • CORS-Anywhere is used to proxy headers back to the user’s browser and render the code generation in-browser. https://github.com/Rob–W/cors-anywhere 
  • Tokens are received on the captureserver.  

Here’s a quick demo video of it in action. 

Defenses 

I won’t be diving into implementation of defenses in this blog post. However, here are some good starting points in Microsoft documentation and a tip that Conditional Access and Sign-on Protections should be implemented to alert or block users from signing in from unknown locations. Don’t blindly allow iPhone or Android either because TokenTactics can spoof those devices. 

  1. Initial Defenses 

Deployment 

You will first need to be signed in using the How to install the Azure CLI | Microsoft Learn

You may sign in with az login –use-device-code 

From a PowerShell terminal, clone TokenTactics. 

git clone https://github.com/rvrsh3ll/TokenTactics 

Clone Azure-App-Tools. 

git clone https://github.com/rvrsh3ll/Azure-App-Tools  

Deploy a capture server. 

cd TokenTactics/capturetokenphish 

import-module .\deploycaptureserver.ps1 

Invoke-DeployCaptureServer -ResourceGroup YOURRESOURCEGROUP -location eastus -vmName codecapture -vmPublicDNSName msftdevicecodes -pubKey ./mykey.pub 

This will deploy an Azure virtual machine, set the FQDN name, grab a LetsEncrypt certificate, and clone TokenTactics. 

SSH to your server. 

ssh -i mykey azureuser@<vmPublicDNSName> 

cd TokenTactics 

*Keep this window open and start a new PowerShell terminal. 

Deploy a landing page. 

cd Azure-App-Tools/DynamicDeviceCodes 

Open index.html with VSCode. 

code index.html 

Change the “MyCaptureServer” to your Azure FQDN. Save. 

Deploy the site using a unique to the Internet “YOURNEWSUBDOMAIN”.  

az webapp up –location eastus –resource-group YOURRESOURCEGROUP –name YOURNEWSUBDOMAIN –html –sku FREE 

Your website will deploy to https://YOURNEWSUBDOMAIN.azurewebsites.net

Back in your other PowerShell prompt you left open in TokenTactics in your SSH session, run your capture server.  

python3 capturetokenphish.py -i 0.0.0.0 -p 8443 

Browse to your site to view the device code generation. 

The script sends a device code that is generated with the user_code and is sent via “GET /id?TOKEN” request to the capture server. Once the server receives that string, PowerShell is loaded to invoke TokenTactics. 

After the user enters the device code, we should receive the access and refresh tokens and they will be saved to TokenLog.log. 

Post-Capture 

You may parse your access_token received client-side at https://jwt.io or with Token Tactics’ “Parse-JWTtoken” cmdlet. This should give you important information such as the username for the token. 

Tokens may be used with some of your favorite cloud enumeration and hacking tools listed below. For a quick win, dump all user email addresses with BARK. If your access is killed, at least you’ll have a full list of users to spray or phish again. 

git clone https://github.com/BloodHoundAD/BARK 

cd BARK 

Import-Module .\Bark.ps1 

$AllUsers = Get-AllAzureADUsers -Token <Insert token eyJ..> -ShowProgress 

Also, check out this great blog by TrustedSec on what to do with tokens. 

Closing 

It’s important to note that wherever the device code is generated, that IP address will show in the logs. Keep that in mind when avoiding or implementing conditional access policies. 

Also, the authentication package brings MFA along with it. So, future code refreshes do not require MFA (currently). 

Microsoft tokens are a powerful way to authenticate and access Azure and Microsoft 365 resources. Use the Microsoft tools available to prevent, detect, and respond to token abuse. I hope this blog post has helped defenders to better protect their network while providing offensive operators with another technique to test and enhance enterprise defenses. 

Black Hills Information Security (BHIS) uses dynamic device code phishing, along with other techniques, against its continuous testing clients to assess phishing prevention, detection, and response capabilities tied to Azure Active Directory, Microsoft 365, and their associated security boundaries.

BHIS has had mixed success with dynamic device code phishing. Some clients have detected the phish as “credential phishing”. Some have had automated solutions detect the email vector and remove the email from inboxes. Others have strict conditional access policies that help mitigate this attack. There are key detection and prevention points in the attack chain as outlined in the blog. Still, we test the assumption of security.

Trust and verify.

Available live/virtual and on-demand