Automating with PowerShell: Sending MFA push messages to users

I’ve been trying to explore how Microsoft generates MFA pushes for some time now. I wanted to offer the same functionality for tools I am building. One of them being a AAD RADIUS replacement tool that’s almost done for public use, the other is a method of verifying the identity of a specific user. Unfortunately Microsoft doesn’t really publish any documentation about how they generate these tokens and send them to users.

After some reverge engineering and checking how the tokens are both received and sent on devices I’ve been able to create a PowerShell script that allows you to send MFA requests to any user that you administer either in your own tenant, or in client tenants using the secure application model. As an example I’ve included a user validation portal Azure Function, but of course you can use this in any process where you need to confirm that the user is really themselves.

Example Azure Function

Click the deploy button below to create the Azure Function that allows you send MFA requests to any user in your tenant.

After clicking the button and entering your Secure Application Model IDs and deploying the application, visiting the application will present you with this screen, enter a username there and press send and it’ll send the MFA request to a user, allowing you to validate their identity. This only works with push-based MFA types, such the PhoneApp, passwordless, or a phone call.

![](../uploads/2021/05/image.png)If you don’t feel comfortable pressing the easy deploy button, or want to check out the source you can do that over [here.](https://github.com/KelvinTegelaar/AzValidate)

Example Script

The GenPass should be a generated password. The first time you execute this for a tenant it might fail as it takes some time for the password to apply to the application in your M365 environment.

######### Secrets #########
$ApplicationId = 'ApplicationID'
$ApplicationSecret = 'ApplicationSecret'
$RefreshToken = 'RefreshToken'
$EmailToPush = "Youruser@receivepush.com"
$Genpass = 'RandomGeneratedPasswordToSetOnMFAApplicationPleaseKeepThisSecureAsItCanBeUsedToGenerateMFARequests'
######### Secrets #########
write-host "Creating credentials and tokens." -ForegroundColor Green
$credential = New-Object System.Management.Automation.PSCredential($ApplicationId, ($ApplicationSecret | Convertto-SecureString -AsPlainText -Force))
$aadGraphToken = New-PartnerAccessToken -ApplicationId $ApplicationId -Credential $credential -RefreshToken $refreshToken -Scopes 'https://graph.windows.net/.default' -ServicePrincipal
$graphToken = New-PartnerAccessToken -ApplicationId $ApplicationId -Credential $credential -RefreshToken $refreshToken -Scopes 'https://graph.microsoft.com/.default' -ServicePrincipal
write-host "Connecting to MSOL" -ForegroundColor Green

Connect-MsolService -AdGraphAccessToken $aadGraphToken.AccessToken -MsGraphAccessToken $graphToken.AccessToken
$UserTenantName = $EmailToPush -split '@' | Select-Object -last 1
$UserTenantGUID = (Invoke-WebRequest "https://login.windows.net/$UserTenantName/.well-known/openid-configuration" | ConvertFrom-Json).token_endpoint.Split('/')[3]
$MFAAppID = '981f26a1-7f43-403b-a875-f8b09b8cd720'
write-host "Setting temporary password" -ForegroundColor Green
New-MsolServicePrincipalCredential -TenantId $UserTenantGUID -AppPrincipalId $MFAAppID -Type password -Usage verify -Value $GenPass -Verbose
write-host "Generating XML" -ForegroundColor Green

$XML = @"
<BeginTwoWayAuthenticationRequest>
<Version>1.0</Version>
<UserPrincipalName>$EmailToPush</UserPrincipalName>
<Lcid>en-us</Lcid><AuthenticationMethodProperties xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><a:KeyValueOfstringstring><a:Key>OverrideVoiceOtp</a:Key><a:Value>false</a:Value></a:KeyValueOfstringstring></AuthenticationMethodProperties><ContextId>69ff05bf-eb61-47f7-a70e-e7d77b6d47d0</ContextId>
<SyncCall>true</SyncCall><RequireUserMatch>true</RequireUserMatch><CallerName>radius</CallerName><CallerIP>UNKNOWN:</CallerIP></BeginTwoWayAuthenticationRequest>
"@

$body = @{
'resource' = 'https://adnotifications.windowsazure.com/StrongAuthenticationService.svc/Connector'
'client_id' = $MFAAppID
'client_secret' = $Genpass
'grant_type' = "client_credentials"
'scope' = "openid"
}

$ClientToken = Invoke-RestMethod -Method post -Uri "https://login.microsoftonline.com/$UserTenantGUID/oauth2/token" -Body $body
$headers = @{ "Authorization" = "Bearer $($ClientToken.access_token)" }
write-host "Generating MFA Request" -ForegroundColor Green

$obj = Invoke-RestMethod -uri 'https://adnotifications.windowsazure.com/StrongAuthenticationService.svc/Connector//BeginTwoWayAuthentication' -Method POST -Headers $Headers -Body $XML -ContentType 'application/xml'

if($obj.BeginTwoWayAuthenticationResponse.AuthenticationResult -ne $true){
write-host "Authentication failed, does the user have Push/Phone call MFA configured?"
}

With this you could implement Azure AD based MFA in your own applications, I have some other blogs incoming that will show you how to combine this with an Azure Function to create a RADIUS alternative that supports AAD, and some other cool gizmos. As always, Happy PowerShelling.

Recent Articles

The return of CyberDrain CTF

CyberDrain CTF returns! (and so do I!)

It’s been since september that I actually picked up a digital pen equivalent and wrote anything down. This was due to me being busy with life but also my side projects like CIPP. I’m trying to get back into the game of scripting and blogging about these scripts. There’s still so much to automate and so little time, right? ;)

Monitoring with PowerShell: Monitoring Acronis Backups

Intro

This is a monitoring script requested via Reddit, One of the reddit r/msp users wondered how they can monitor Acronis a little bit easier. I jumped on this because it happened pretty much at the same time that I was asked to speak at the Acronis CyberSummit so it kinda made sense to script this so I have something to demonstrate at my session there.

Monitoring with PowerShell: Monitoring VSS Snapshots

Intro

Wow! It’s been a while since I’ve blogged. I’ve just been so swamped with CIPP that I’ve just let the blogging go entirely. It’s a shame because I think out of all my hobbies it’s one I enjoy the most. It’s always nice helping others achieve their scripting target. I even got a couple of LinkedIn questions asking if I was done with blogging but I’m not. Writing always gives me some more piece of mind so I’ll try to catch up again. I know I’ve said that before but this time I’ll follow through. I’m sitting down right now and scheduling the release of 5 blogs in one go. No more whining and no more waiting.